Skip to content

Commit 151301a

Browse files
authored
implement EXPLAIN and EXPLAIN PLAN (#2784)
1 parent c4c1d5f commit 151301a

File tree

8 files changed

+66
-27
lines changed

8 files changed

+66
-27
lines changed

enginetest/memory_engine_test.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -200,9 +200,16 @@ func TestSingleScript(t *testing.T) {
200200
t.Skip()
201201
var scripts = []queries.ScriptTest{
202202
{
203-
Name: "test script",
204-
SetUpScript: []string{},
205-
Assertions: []queries.ScriptTestAssertion{},
203+
Name: "test script",
204+
SetUpScript: []string{
205+
"create table t (i int);",
206+
},
207+
Assertions: []queries.ScriptTestAssertion{
208+
{
209+
Query: "select 1 into @a",
210+
Expected: []sql.Row{},
211+
},
212+
},
206213
},
207214
}
208215

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ require (
66
github.com/dolthub/go-icu-regex v0.0.0-20240916130659-0118adc6b662
77
github.com/dolthub/jsonpath v0.0.2-0.20240227200619-19675ab05c71
88
github.com/dolthub/sqllogictest/go v0.0.0-20201107003712-816f3ae12d81
9-
github.com/dolthub/vitess v0.0.0-20241126223332-cd8f828f26ac
9+
github.com/dolthub/vitess v0.0.0-20241206185536-814752c66695
1010
github.com/go-kit/kit v0.10.0
1111
github.com/go-sql-driver/mysql v1.7.2-0.20231213112541-0004702b931d
1212
github.com/gocraft/dbr/v2 v2.7.2
@@ -43,4 +43,4 @@ require (
4343
gopkg.in/check.v1 v1.0.0-20200902074654-038fdea0a05b // indirect
4444
)
4545

46-
go 1.22.2
46+
go 1.23.3

go.sum

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -58,20 +58,8 @@ github.com/dolthub/jsonpath v0.0.2-0.20240227200619-19675ab05c71 h1:bMGS25NWAGTE
5858
github.com/dolthub/jsonpath v0.0.2-0.20240227200619-19675ab05c71/go.mod h1:2/2zjLQ/JOOSbbSboojeg+cAwcRV0fDLzIiWch/lhqI=
5959
github.com/dolthub/sqllogictest/go v0.0.0-20201107003712-816f3ae12d81 h1:7/v8q9XGFa6q5Ap4Z/OhNkAMBaK5YeuEzwJt+NZdhiE=
6060
github.com/dolthub/sqllogictest/go v0.0.0-20201107003712-816f3ae12d81/go.mod h1:siLfyv2c92W1eN/R4QqG/+RjjX5W2+gCTRjZxBjI3TY=
61-
github.com/dolthub/vitess v0.0.0-20241119005402-6a198321d993 h1:MhD6jHjshx2djyUq/uZxtCyHBYAnE3WshhJDUaO9fD8=
62-
github.com/dolthub/vitess v0.0.0-20241119005402-6a198321d993/go.mod h1:uBvlRluuL+SbEWTCZ68o0xvsdYZER3CEG/35INdzfJM=
63-
github.com/dolthub/vitess v0.0.0-20241120000209-5ff664bddfc4 h1:C3RSQjvv2T5TdQzRYpLLIbFxfyznzZi25XpOqdu89ng=
64-
github.com/dolthub/vitess v0.0.0-20241120000209-5ff664bddfc4/go.mod h1:uBvlRluuL+SbEWTCZ68o0xvsdYZER3CEG/35INdzfJM=
65-
github.com/dolthub/vitess v0.0.0-20241121221517-3e7b5ffc22b0 h1:C8X4RkkWKcrJG6rG+MsdFINX2PhB7ObpbBvFcWsI8K8=
66-
github.com/dolthub/vitess v0.0.0-20241121221517-3e7b5ffc22b0/go.mod h1:alcJgfdyIhFaAiYyEmuDCFSLCzedz3KCaIclLoCUtJg=
67-
github.com/dolthub/vitess v0.0.0-20241125233820-b4036b797518 h1:aXx3d3vpAg9/E7oB2AsadnRbBkwQn6Kza7l8JrBjHLE=
68-
github.com/dolthub/vitess v0.0.0-20241125233820-b4036b797518/go.mod h1:alcJgfdyIhFaAiYyEmuDCFSLCzedz3KCaIclLoCUtJg=
69-
github.com/dolthub/vitess v0.0.0-20241126003322-7d7ad4a18141 h1:W1jhfE3dnfJGO6spd5BVts4C1iW/g7pb1ml7vm4Swa4=
70-
github.com/dolthub/vitess v0.0.0-20241126003322-7d7ad4a18141/go.mod h1:alcJgfdyIhFaAiYyEmuDCFSLCzedz3KCaIclLoCUtJg=
71-
github.com/dolthub/vitess v0.0.0-20241126205153-88972ec5fe52 h1:RH0eygj4DLPQ6fvJCBBk4ZOq3PpeIQU/1ot6Fye9WCU=
72-
github.com/dolthub/vitess v0.0.0-20241126205153-88972ec5fe52/go.mod h1:alcJgfdyIhFaAiYyEmuDCFSLCzedz3KCaIclLoCUtJg=
73-
github.com/dolthub/vitess v0.0.0-20241126223332-cd8f828f26ac h1:A0U/OdIqdCkAV0by7MVBbnSyZBsa94ZjIZxx7PhjBW4=
74-
github.com/dolthub/vitess v0.0.0-20241126223332-cd8f828f26ac/go.mod h1:alcJgfdyIhFaAiYyEmuDCFSLCzedz3KCaIclLoCUtJg=
61+
github.com/dolthub/vitess v0.0.0-20241206185536-814752c66695 h1:jqTM+/S/LKYdsdretQMalu7dhHupuweSPiPNC2Jpoiw=
62+
github.com/dolthub/vitess v0.0.0-20241206185536-814752c66695/go.mod h1:1gQZs/byeHLMSul3Lvl3MzioMtOW1je79QYGyi2fd70=
7563
github.com/dustin/go-humanize v0.0.0-20171111073723-bb3d318650d4/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk=
7664
github.com/eapache/go-resiliency v1.1.0/go.mod h1:kFI+JgMyC7bLPUVY133qvEBtVayf5mFgVsvEsIPBvNs=
7765
github.com/eapache/go-xerial-snappy v0.0.0-20180814174437-776d5712da21/go.mod h1:+020luEh2TKB4/GOp8oxxtq0Daoen/Cii55CzbTV6DU=

sql/describe.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ type DescribeOptions struct {
108108
Analyze bool
109109
Estimates bool
110110
Debug bool
111+
Plan bool
111112
}
112113

113114
func (d DescribeOptions) String() string {

sql/plan/describe.go

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ package plan
1616

1717
import (
1818
"github.com/dolthub/go-mysql-server/sql"
19+
"github.com/dolthub/go-mysql-server/sql/types"
20+
21+
"github.com/dolthub/vitess/go/sqltypes"
1922
)
2023

2124
// Describe is a node that describes its children.
@@ -102,6 +105,22 @@ func (*DescribeQuery) CollationCoercibility(ctx *sql.Context) (collation sql.Col
102105

103106
// DescribeSchema is the schema returned by a DescribeQuery node.
104107
var DescribeSchema = sql.Schema{
108+
{Name: "id", Type: types.Uint64},
109+
{Name: "select_type", Type: types.MustCreateStringWithDefaults(sqltypes.VarChar, 57)},
110+
{Name: "table", Type: types.MustCreateStringWithDefaults(sqltypes.VarChar, 192)},
111+
{Name: "partitions", Type: types.Text},
112+
{Name: "type", Type: types.MustCreateStringWithDefaults(sqltypes.VarChar, 30)},
113+
{Name: "possible_keys", Type: types.MustCreateStringWithDefaults(sqltypes.VarChar, 12288)},
114+
{Name: "key", Type: types.MustCreateStringWithDefaults(sqltypes.VarChar, 192)},
115+
{Name: "key_len", Type: types.MustCreateStringWithDefaults(sqltypes.VarChar, 12288)},
116+
{Name: "ref", Type: types.MustCreateStringWithDefaults(sqltypes.VarChar, 3072)},
117+
{Name: "rows", Type: types.Uint64},
118+
{Name: "filtered", Type: types.Float64},
119+
{Name: "Extra", Type: types.MustCreateStringWithDefaults(sqltypes.VarChar, 765)},
120+
}
121+
122+
// DescribePlanSchema is the schema returned by a DescribeQuery node.
123+
var DescribePlanSchema = sql.Schema{
105124
{Name: "plan", Type: VarChar25000},
106125
}
107126

@@ -112,7 +131,11 @@ func NewDescribeQuery(format sql.DescribeOptions, child sql.Node) *DescribeQuery
112131

113132
// Schema implements the Node interface.
114133
func (d *DescribeQuery) Schema() sql.Schema {
115-
return DescribeSchema
134+
if d.Format.Plan {
135+
return DescribePlanSchema
136+
} else {
137+
return DescribeSchema
138+
}
116139
}
117140

118141
func (d *DescribeQuery) Describe(options sql.DescribeOptions) string {

sql/planbuilder/explain.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ func (b *Builder) buildExplain(inScope *scope, n *sqlparser.Explain) (outScope *
2929

3030
describeOptions := sql.DescribeOptions{
3131
Analyze: n.Analyze,
32+
Plan: n.Plan,
3233
}
3334

3435
formatFlags := strings.Split(n.ExplainFormat, "_")

sql/rowexec/describe_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ func TestDescribeQuery(t *testing.T) {
8383
Analyze: false,
8484
Estimates: false,
8585
Debug: false,
86+
Plan: true,
8687
}
8788
node := plan.NewDescribeQuery(format, plan.NewProject(
8889
[]sql.Expression{

sql/rowexec/show.go

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -74,14 +74,32 @@ func (b *BaseBuilder) buildDescribeQuery(ctx *sql.Context, n *plan.DescribeQuery
7474
}
7575

7676
var rows []sql.Row
77-
formatString := sql.Describe(n.Child, n.Format)
78-
79-
for _, l := range strings.Split(formatString, "\n") {
80-
if strings.TrimSpace(l) != "" {
81-
rows = append(rows, sql.NewRow(l))
77+
if n.Format.Plan {
78+
formatString := sql.Describe(n.Child, n.Format)
79+
for _, l := range strings.Split(formatString, "\n") {
80+
if strings.TrimSpace(l) != "" {
81+
rows = append(rows, sql.NewRow(l))
82+
}
8283
}
83-
}
84-
return sql.RowsToRowIter(rows...), nil
84+
return sql.RowsToRowIter(rows...), nil
85+
}
86+
87+
ctx.Warn(0, "EXPLAIN Output is currently a placeholder; use EXPLAIN PLAN for old behavior")
88+
dummyRow := sql.Row{
89+
1, // id
90+
"SELECT", // select_type
91+
"NULL", // table
92+
"NULL", // partitions
93+
"NULL", // type
94+
"NULL", // possible_keys
95+
"NULL", // key
96+
"NULL", // key_len
97+
"NULL", // ref
98+
"NULL", // rows
99+
"NULL", // filtered
100+
"", // Extra
101+
}
102+
return sql.RowsToRowIter(dummyRow), nil
85103
}
86104

87105
func (b *BaseBuilder) buildShowWarnings(ctx *sql.Context, n plan.ShowWarnings, row sql.Row) (sql.RowIter, error) {

0 commit comments

Comments
 (0)