Skip to content
9 changes: 8 additions & 1 deletion go/vt/sqlparser/analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ const (
StmtUnknown
StmtComment
StmtPriv
StmtExplain
)

//ASTToStatementType returns a StatementType from an AST stmt
Expand All @@ -75,6 +76,8 @@ func ASTToStatementType(stmt Statement) StatementType {
return StmtUse
case *OtherRead, *OtherAdmin:
return StmtOther
case *Explain:
return StmtExplain
case *Begin:
return StmtBegin
case *Commit:
Expand Down Expand Up @@ -158,7 +161,9 @@ func Preview(sql string) StatementType {
return StmtShow
case "use":
return StmtUse
case "analyze", "describe", "desc", "explain", "repair", "optimize":
case "describe", "desc", "explain":
return StmtExplain
case "analyze", "repair", "optimize":
return StmtOther
case "grant", "revoke":
return StmtPriv
Expand Down Expand Up @@ -198,6 +203,8 @@ func (s StatementType) String() string {
return "OTHER"
case StmtPriv:
return "PRIV"
case StmtExplain:
return "EXPLAIN"
default:
return "UNKNOWN"
}
Expand Down
6 changes: 3 additions & 3 deletions go/vt/sqlparser/analyzer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,9 @@ func TestPreview(t *testing.T) {
{"show", StmtShow},
{"use", StmtUse},
{"analyze", StmtOther},
{"describe", StmtOther},
{"desc", StmtOther},
{"explain", StmtOther},
{"describe", StmtExplain},
{"desc", StmtExplain},
{"explain", StmtExplain},
{"repair", StmtOther},
{"optimize", StmtOther},
{"grant", StmtPriv},
Expand Down
20 changes: 20 additions & 0 deletions go/vt/sqlparser/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,12 @@ type (
// Rollback represents a Rollback statement.
Rollback struct{}

// Explain represents an EXPLAIN statement
Explain struct {
Type string
Statement Statement
}

// OtherRead represents a DESCRIBE, or EXPLAIN statement.
// It should be used only as an indicator. It does not contain
// the full AST for the statement.
Expand Down Expand Up @@ -254,6 +260,7 @@ func (*Use) iStatement() {}
func (*Begin) iStatement() {}
func (*Commit) iStatement() {}
func (*Rollback) iStatement() {}
func (*Explain) iStatement() {}
func (*OtherRead) iStatement() {}
func (*OtherAdmin) iStatement() {}
func (*Select) iSelectStatement() {}
Expand Down Expand Up @@ -1295,6 +1302,19 @@ func (node *Rollback) Format(buf *TrackedBuffer) {
buf.WriteString("rollback")
}

// Format formats the node.
func (node *Explain) Format(buf *TrackedBuffer) {
format := ""
switch node.Type {
case "": // do nothing
case AnalyzeStr:
format = AnalyzeStr + " "
default:
format = "format = " + node.Type + " "
}
buf.astPrintf(node, "explain %s%v", format, node.Statement)
}

// Format formats the node.
func (node *OtherRead) Format(buf *TrackedBuffer) {
buf.WriteString("otherread")
Expand Down
7 changes: 7 additions & 0 deletions go/vt/sqlparser/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,4 +163,11 @@ const (

TxReadOnly = "read only"
TxReadWrite = "read write"

// Explain formats
TreeStr = "tree"
JSONStr = "json"
VitessStr = "vitess"
TraditionalStr = "traditional"
AnalyzeStr = "analyze"
)
35 changes: 31 additions & 4 deletions go/vt/sqlparser/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1373,14 +1373,41 @@ var (
input: "use ks@replica",
output: "use `ks@replica`",
}, {
input: "describe foobar",
output: "otherread",
input: "describe select * from t",
output: "explain select * from t",
}, {
input: "desc select * from t",
output: "explain select * from t",
}, {
input: "desc foobar",
output: "otherread",
}, {
input: "explain foobar",
input: "explain t1",
output: "otherread",
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add these test back.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

}, {
input: "explain t1 col",
output: "otherread",
}, {
input: "explain select * from t",
}, {
input: "explain format = traditional select * from t",
}, {
input: "explain analyze select * from t",
}, {
input: "explain format = tree select * from t",
}, {
input: "explain format = json select * from t",
}, {
input: "explain format = vitess select * from t",
}, {
input: "describe format = vitess select * from t",
output: "explain format = vitess select * from t",
}, {
input: "explain delete from t",
}, {
input: "explain insert into t(col1, col2) values (1, 2)",
}, {
input: "explain update t set col = 2",
}, {
input: "truncate table foo",
output: "truncate table foo",
Expand Down Expand Up @@ -1607,7 +1634,7 @@ func TestValid(t *testing.T) {
tcase.output = tcase.input
}
tree, err := Parse(tcase.input)
require.NoError(t, err)
require.NoError(t, err, tcase.input)
out := String(tree)
if diff := cmp.Diff(tcase.output, out); diff != "" {
t.Errorf("Parse(%q):\n%s", tcase.input, diff)
Expand Down
7 changes: 7 additions & 0 deletions go/vt/sqlparser/rewriter.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading