Skip to content

Commit

Permalink
fix: analyze statement parsing and planning (#14268)
Browse files Browse the repository at this point in the history
Signed-off-by: Harshit Gangal <[email protected]>
  • Loading branch information
vitess-bot[bot] committed Oct 13, 2023
1 parent fe31219 commit f034d0d
Show file tree
Hide file tree
Showing 26 changed files with 5,745 additions and 5,631 deletions.
15 changes: 15 additions & 0 deletions go/test/endtoend/vtgate/queries/misc/misc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,3 +252,18 @@ func TestLeftJoinUsingUnsharded(t *testing.T) {
utils.Exec(t, mcmp.VtConn, "insert into uks.unsharded(id1) values (1),(2),(3),(4),(5)")
utils.Exec(t, mcmp.VtConn, "select * from uks.unsharded as A left join uks.unsharded as B using(id1)")
}

// TestAnalyze executes different analyze statement and validates that they run successfully.
func TestAnalyze(t *testing.T) {
mcmp, closer := start(t)
defer closer()

for _, workload := range []string{"olap", "oltp"} {
t.Run(workload, func(t *testing.T) {
utils.Exec(t, mcmp.VtConn, fmt.Sprintf("set workload = %s", workload))
utils.Exec(t, mcmp.VtConn, "analyze table t1")
utils.Exec(t, mcmp.VtConn, "analyze table uks.unsharded")
utils.Exec(t, mcmp.VtConn, "analyze table mysql.user")
})
}
}
11 changes: 9 additions & 2 deletions go/vt/sqlparser/analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ const (
StmtShow
StmtUse
StmtOther
StmtAnalyze
StmtUnknown
StmtComment
StmtPriv
Expand Down Expand Up @@ -88,8 +89,10 @@ func ASTToStatementType(stmt Statement) StatementType {
return StmtShowMigrationLogs
case *Use:
return StmtUse
case *OtherRead, *OtherAdmin, *Load:
case *OtherAdmin, *Load:
return StmtOther
case *Analyze:
return StmtAnalyze
case Explain, *VExplainStmt:
return StmtExplain
case *Begin:
Expand Down Expand Up @@ -245,8 +248,10 @@ func Preview(sql string) StatementType {
return StmtUse
case "describe", "desc", "explain":
return StmtExplain
case "analyze", "repair", "optimize":
case "repair", "optimize":
return StmtOther
case "analyze":
return StmtAnalyze
case "grant", "revoke":
return StmtPriv
case "release":
Expand Down Expand Up @@ -293,6 +298,8 @@ func (s StatementType) String() string {
return "USE"
case StmtOther:
return "OTHER"
case StmtAnalyze:
return "ANALYZE"
case StmtPriv:
return "PRIV"
case StmtExplain:
Expand Down
2 changes: 1 addition & 1 deletion go/vt/sqlparser/analyzer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func TestPreview(t *testing.T) {
{"set", StmtSet},
{"show", StmtShow},
{"use", StmtUse},
{"analyze", StmtOther},
{"analyze", StmtAnalyze},
{"describe", StmtExplain},
{"desc", StmtExplain},
{"explain", StmtExplain},
Expand Down
11 changes: 6 additions & 5 deletions go/vt/sqlparser/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -682,10 +682,11 @@ type (
Name IdentifierCI
}

// 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.
OtherRead struct{}
// Analyze represents the Analyze statement.
Analyze struct {
IsLocal bool
Table TableName
}

// OtherAdmin represents a misc statement that relies on ADMIN privileges,
// such as REPAIR, OPTIMIZE, or TRUNCATE statement.
Expand Down Expand Up @@ -726,7 +727,7 @@ func (*Rollback) iStatement() {}
func (*SRollback) iStatement() {}
func (*Savepoint) iStatement() {}
func (*Release) iStatement() {}
func (*OtherRead) iStatement() {}
func (*Analyze) iStatement() {}
func (*OtherAdmin) iStatement() {}
func (*CommentOnly) iStatement() {}
func (*Select) iSelectStatement() {}
Expand Down
27 changes: 14 additions & 13 deletions go/vt/sqlparser/ast_clone.go

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

42 changes: 26 additions & 16 deletions go/vt/sqlparser/ast_copy_on_rewrite.go

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

47 changes: 24 additions & 23 deletions go/vt/sqlparser/ast_equals.go

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

8 changes: 6 additions & 2 deletions go/vt/sqlparser/ast_format.go
Original file line number Diff line number Diff line change
Expand Up @@ -1060,8 +1060,12 @@ func (node *CallProc) Format(buf *TrackedBuffer) {
}

// Format formats the node.
func (node *OtherRead) Format(buf *TrackedBuffer) {
buf.literal("otherread")
func (node *Analyze) Format(buf *TrackedBuffer) {
buf.literal("analyze ")
if node.IsLocal {
buf.literal("local ")
}
buf.astPrintf(node, "table %v", node.Table)
}

// Format formats the node.
Expand Down
9 changes: 7 additions & 2 deletions go/vt/sqlparser/ast_format_fast.go

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

Loading

0 comments on commit f034d0d

Please sign in to comment.