Skip to content

Commit

Permalink
implement EXPLAIN and EXPLAIN PLAN (#2784)
Browse files Browse the repository at this point in the history
  • Loading branch information
jycor authored Dec 6, 2024
1 parent c4c1d5f commit 151301a
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 27 deletions.
13 changes: 10 additions & 3 deletions enginetest/memory_engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,9 +200,16 @@ func TestSingleScript(t *testing.T) {
t.Skip()
var scripts = []queries.ScriptTest{
{
Name: "test script",
SetUpScript: []string{},
Assertions: []queries.ScriptTestAssertion{},
Name: "test script",
SetUpScript: []string{
"create table t (i int);",
},
Assertions: []queries.ScriptTestAssertion{
{
Query: "select 1 into @a",
Expected: []sql.Row{},
},
},
},
}

Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ require (
github.com/dolthub/go-icu-regex v0.0.0-20240916130659-0118adc6b662
github.com/dolthub/jsonpath v0.0.2-0.20240227200619-19675ab05c71
github.com/dolthub/sqllogictest/go v0.0.0-20201107003712-816f3ae12d81
github.com/dolthub/vitess v0.0.0-20241126223332-cd8f828f26ac
github.com/dolthub/vitess v0.0.0-20241206185536-814752c66695
github.com/go-kit/kit v0.10.0
github.com/go-sql-driver/mysql v1.7.2-0.20231213112541-0004702b931d
github.com/gocraft/dbr/v2 v2.7.2
Expand Down Expand Up @@ -43,4 +43,4 @@ require (
gopkg.in/check.v1 v1.0.0-20200902074654-038fdea0a05b // indirect
)

go 1.22.2
go 1.23.3
16 changes: 2 additions & 14 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -58,20 +58,8 @@ github.com/dolthub/jsonpath v0.0.2-0.20240227200619-19675ab05c71 h1:bMGS25NWAGTE
github.com/dolthub/jsonpath v0.0.2-0.20240227200619-19675ab05c71/go.mod h1:2/2zjLQ/JOOSbbSboojeg+cAwcRV0fDLzIiWch/lhqI=
github.com/dolthub/sqllogictest/go v0.0.0-20201107003712-816f3ae12d81 h1:7/v8q9XGFa6q5Ap4Z/OhNkAMBaK5YeuEzwJt+NZdhiE=
github.com/dolthub/sqllogictest/go v0.0.0-20201107003712-816f3ae12d81/go.mod h1:siLfyv2c92W1eN/R4QqG/+RjjX5W2+gCTRjZxBjI3TY=
github.com/dolthub/vitess v0.0.0-20241119005402-6a198321d993 h1:MhD6jHjshx2djyUq/uZxtCyHBYAnE3WshhJDUaO9fD8=
github.com/dolthub/vitess v0.0.0-20241119005402-6a198321d993/go.mod h1:uBvlRluuL+SbEWTCZ68o0xvsdYZER3CEG/35INdzfJM=
github.com/dolthub/vitess v0.0.0-20241120000209-5ff664bddfc4 h1:C3RSQjvv2T5TdQzRYpLLIbFxfyznzZi25XpOqdu89ng=
github.com/dolthub/vitess v0.0.0-20241120000209-5ff664bddfc4/go.mod h1:uBvlRluuL+SbEWTCZ68o0xvsdYZER3CEG/35INdzfJM=
github.com/dolthub/vitess v0.0.0-20241121221517-3e7b5ffc22b0 h1:C8X4RkkWKcrJG6rG+MsdFINX2PhB7ObpbBvFcWsI8K8=
github.com/dolthub/vitess v0.0.0-20241121221517-3e7b5ffc22b0/go.mod h1:alcJgfdyIhFaAiYyEmuDCFSLCzedz3KCaIclLoCUtJg=
github.com/dolthub/vitess v0.0.0-20241125233820-b4036b797518 h1:aXx3d3vpAg9/E7oB2AsadnRbBkwQn6Kza7l8JrBjHLE=
github.com/dolthub/vitess v0.0.0-20241125233820-b4036b797518/go.mod h1:alcJgfdyIhFaAiYyEmuDCFSLCzedz3KCaIclLoCUtJg=
github.com/dolthub/vitess v0.0.0-20241126003322-7d7ad4a18141 h1:W1jhfE3dnfJGO6spd5BVts4C1iW/g7pb1ml7vm4Swa4=
github.com/dolthub/vitess v0.0.0-20241126003322-7d7ad4a18141/go.mod h1:alcJgfdyIhFaAiYyEmuDCFSLCzedz3KCaIclLoCUtJg=
github.com/dolthub/vitess v0.0.0-20241126205153-88972ec5fe52 h1:RH0eygj4DLPQ6fvJCBBk4ZOq3PpeIQU/1ot6Fye9WCU=
github.com/dolthub/vitess v0.0.0-20241126205153-88972ec5fe52/go.mod h1:alcJgfdyIhFaAiYyEmuDCFSLCzedz3KCaIclLoCUtJg=
github.com/dolthub/vitess v0.0.0-20241126223332-cd8f828f26ac h1:A0U/OdIqdCkAV0by7MVBbnSyZBsa94ZjIZxx7PhjBW4=
github.com/dolthub/vitess v0.0.0-20241126223332-cd8f828f26ac/go.mod h1:alcJgfdyIhFaAiYyEmuDCFSLCzedz3KCaIclLoCUtJg=
github.com/dolthub/vitess v0.0.0-20241206185536-814752c66695 h1:jqTM+/S/LKYdsdretQMalu7dhHupuweSPiPNC2Jpoiw=
github.com/dolthub/vitess v0.0.0-20241206185536-814752c66695/go.mod h1:1gQZs/byeHLMSul3Lvl3MzioMtOW1je79QYGyi2fd70=
github.com/dustin/go-humanize v0.0.0-20171111073723-bb3d318650d4/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk=
github.com/eapache/go-resiliency v1.1.0/go.mod h1:kFI+JgMyC7bLPUVY133qvEBtVayf5mFgVsvEsIPBvNs=
github.com/eapache/go-xerial-snappy v0.0.0-20180814174437-776d5712da21/go.mod h1:+020luEh2TKB4/GOp8oxxtq0Daoen/Cii55CzbTV6DU=
Expand Down
1 change: 1 addition & 0 deletions sql/describe.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ type DescribeOptions struct {
Analyze bool
Estimates bool
Debug bool
Plan bool
}

func (d DescribeOptions) String() string {
Expand Down
25 changes: 24 additions & 1 deletion sql/plan/describe.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ package plan

import (
"github.com/dolthub/go-mysql-server/sql"
"github.com/dolthub/go-mysql-server/sql/types"

"github.com/dolthub/vitess/go/sqltypes"
)

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

// DescribeSchema is the schema returned by a DescribeQuery node.
var DescribeSchema = sql.Schema{
{Name: "id", Type: types.Uint64},
{Name: "select_type", Type: types.MustCreateStringWithDefaults(sqltypes.VarChar, 57)},
{Name: "table", Type: types.MustCreateStringWithDefaults(sqltypes.VarChar, 192)},
{Name: "partitions", Type: types.Text},
{Name: "type", Type: types.MustCreateStringWithDefaults(sqltypes.VarChar, 30)},
{Name: "possible_keys", Type: types.MustCreateStringWithDefaults(sqltypes.VarChar, 12288)},
{Name: "key", Type: types.MustCreateStringWithDefaults(sqltypes.VarChar, 192)},
{Name: "key_len", Type: types.MustCreateStringWithDefaults(sqltypes.VarChar, 12288)},
{Name: "ref", Type: types.MustCreateStringWithDefaults(sqltypes.VarChar, 3072)},
{Name: "rows", Type: types.Uint64},
{Name: "filtered", Type: types.Float64},
{Name: "Extra", Type: types.MustCreateStringWithDefaults(sqltypes.VarChar, 765)},
}

// DescribePlanSchema is the schema returned by a DescribeQuery node.
var DescribePlanSchema = sql.Schema{
{Name: "plan", Type: VarChar25000},
}

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

// Schema implements the Node interface.
func (d *DescribeQuery) Schema() sql.Schema {
return DescribeSchema
if d.Format.Plan {
return DescribePlanSchema
} else {
return DescribeSchema
}
}

func (d *DescribeQuery) Describe(options sql.DescribeOptions) string {
Expand Down
1 change: 1 addition & 0 deletions sql/planbuilder/explain.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ func (b *Builder) buildExplain(inScope *scope, n *sqlparser.Explain) (outScope *

describeOptions := sql.DescribeOptions{
Analyze: n.Analyze,
Plan: n.Plan,
}

formatFlags := strings.Split(n.ExplainFormat, "_")
Expand Down
1 change: 1 addition & 0 deletions sql/rowexec/describe_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ func TestDescribeQuery(t *testing.T) {
Analyze: false,
Estimates: false,
Debug: false,
Plan: true,
}
node := plan.NewDescribeQuery(format, plan.NewProject(
[]sql.Expression{
Expand Down
32 changes: 25 additions & 7 deletions sql/rowexec/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,32 @@ func (b *BaseBuilder) buildDescribeQuery(ctx *sql.Context, n *plan.DescribeQuery
}

var rows []sql.Row
formatString := sql.Describe(n.Child, n.Format)

for _, l := range strings.Split(formatString, "\n") {
if strings.TrimSpace(l) != "" {
rows = append(rows, sql.NewRow(l))
if n.Format.Plan {
formatString := sql.Describe(n.Child, n.Format)
for _, l := range strings.Split(formatString, "\n") {
if strings.TrimSpace(l) != "" {
rows = append(rows, sql.NewRow(l))
}
}
}
return sql.RowsToRowIter(rows...), nil
return sql.RowsToRowIter(rows...), nil
}

ctx.Warn(0, "EXPLAIN Output is currently a placeholder; use EXPLAIN PLAN for old behavior")
dummyRow := sql.Row{
1, // id
"SELECT", // select_type
"NULL", // table
"NULL", // partitions
"NULL", // type
"NULL", // possible_keys
"NULL", // key
"NULL", // key_len
"NULL", // ref
"NULL", // rows
"NULL", // filtered
"", // Extra
}
return sql.RowsToRowIter(dummyRow), nil
}

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

0 comments on commit 151301a

Please sign in to comment.