Skip to content

Commit

Permalink
improve analyze statement parsing and plan using table information
Browse files Browse the repository at this point in the history
Signed-off-by: Harshit Gangal <[email protected]>
  • Loading branch information
harshit-gangal committed Oct 13, 2023
1 parent 6e3609e commit 0bceca7
Show file tree
Hide file tree
Showing 10 changed files with 5,488 additions and 5,511 deletions.
3 changes: 2 additions & 1 deletion go/vt/sqlparser/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -684,7 +684,8 @@ type (

// Analyze represents the Analyze statement.
Analyze struct {
Table TableName
IsLocal bool
Table TableName
}

// OtherAdmin represents a misc statement that relies on ADMIN privileges,
Expand Down
3 changes: 2 additions & 1 deletion go/vt/sqlparser/ast_equals.go

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

6 changes: 5 additions & 1 deletion go/vt/sqlparser/ast_format.go
Original file line number Diff line number Diff line change
Expand Up @@ -1069,7 +1069,11 @@ func (node *CallProc) Format(buf *TrackedBuffer) {

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

// Format formats the node.
Expand Down
6 changes: 5 additions & 1 deletion go/vt/sqlparser/ast_format_fast.go

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

2 changes: 1 addition & 1 deletion go/vt/sqlparser/cached_size.go

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

5 changes: 5 additions & 0 deletions go/vt/sqlparser/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2117,6 +2117,11 @@ var (
output: "alter table a drop primary key, lock none",
}, {
input: "analyze table a",
}, {
input: "analyze NO_WRITE_TO_BINLOG table a",
output: "analyze local table a",
}, {
input: "analyze local table a",
}, {
input: "flush tables",
}, {
Expand Down
10,943 changes: 5,441 additions & 5,502 deletions go/vt/sqlparser/sql.go

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions go/vt/sqlparser/sql.y
Original file line number Diff line number Diff line change
Expand Up @@ -3964,9 +3964,9 @@ truncate_statement:
}

analyze_statement:
ANALYZE TABLE table_name
ANALYZE local_opt TABLE table_name
{
$$ = &Analyze{Table: $3}
$$ = &Analyze{IsLocal: $2, Table: $4}
}

purge_statement:
Expand Down
26 changes: 25 additions & 1 deletion go/vt/vtgate/planbuilder/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,8 +195,10 @@ func createInstructionFor(ctx context.Context, query string, stmt sqlparser.Stat
return buildExplainPlan(ctx, stmt, reservedVars, vschema, enableOnlineDDL, enableDirectDDL)
case *sqlparser.VExplainStmt:
return buildVExplainPlan(ctx, stmt, reservedVars, vschema, enableOnlineDDL, enableDirectDDL)
case *sqlparser.Analyze, *sqlparser.OtherAdmin:
case *sqlparser.OtherAdmin:
return buildOtherReadAndAdmin(query, vschema)
case *sqlparser.Analyze:
return buildAnalyzePlan(stmt, vschema)
case *sqlparser.Set:
return buildSetPlan(stmt, vschema)
case *sqlparser.Load:
Expand Down Expand Up @@ -237,6 +239,28 @@ func createInstructionFor(ctx context.Context, query string, stmt sqlparser.Stat
return nil, vterrors.VT13001(fmt.Sprintf("unexpected statement type: %T", stmt))
}

func buildAnalyzePlan(stmt *sqlparser.Analyze, vschema plancontext.VSchema) (*planResult, error) {
tbl, _, _, _, dest, err := vschema.FindTableOrVindex(stmt.Table)
if err != nil {
return nil, err
}
if tbl == nil {
return nil, vterrors.VT05004(sqlparser.String(stmt.Table))
}
if dest == nil {
dest = key.DestinationAllShards{}
}
stmt.Table.Qualifier = sqlparser.NewIdentifierCS("")
stmt.Table.Name = tbl.Name

prim := &engine.Send{
Keyspace: tbl.Keyspace,
TargetDestination: dest,
Query: sqlparser.String(stmt),
}
return newPlanResult(prim, sqlparser.String(stmt.Table)), nil
}

func buildDBDDLPlan(stmt sqlparser.Statement, _ *sqlparser.ReservedVars, vschema plancontext.VSchema) (*planResult, error) {
dbDDLstmt := stmt.(sqlparser.DBDDLStatement)
ksName := dbDDLstmt.GetDatabaseName()
Expand Down
1 change: 0 additions & 1 deletion go/vt/vttablet/tabletserver/vstreamer/vstreamer.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,6 @@ func (vs *vstreamer) Cancel() {

// Stream streams binlog events.
func (vs *vstreamer) Stream() error {
// defer vs.cancel()
ctx := context.Background()
vs.vse.vstreamerCount.Add(1)
defer func() {
Expand Down

0 comments on commit 0bceca7

Please sign in to comment.