Skip to content

Commit

Permalink
feat: handle info_schema routing for DESCRIBE
Browse files Browse the repository at this point in the history
Signed-off-by: Andres Taylor <andres@planetscale.com>
  • Loading branch information
systay committed May 9, 2024
1 parent cbf89bd commit 85dd84f
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 33 deletions.
27 changes: 5 additions & 22 deletions go/vt/vtgate/planbuilder/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,31 +173,14 @@ func buildShowTblPlan(show *sqlparser.ShowBasic, vschema plancontext.VSchema) (e
show.DbName = sqlparser.NewIdentifierCS("")
}

dest := key.Destination(key.DestinationAnyShard{})
var ks *vindexes.Keyspace
var err error

if show.Tbl.Qualifier.NotEmpty() && sqlparser.SystemSchema(show.Tbl.Qualifier.String()) {
ks, err = vschema.AnyKeyspace()
if err != nil {
return nil, err
}
} else {
table, _, _, _, destination, err := vschema.FindTableOrVindex(show.Tbl)
if err != nil {
return nil, err
}
if table == nil {
return nil, vterrors.VT05004(show.Tbl.Name.String())
}
ks, dest, table, err := findDestinationToTable(vschema, show.Tbl)
if err != nil {
return nil, err
}
if table != nil /*table will be empty for info_schema tables*/ {
// Update the table.
show.Tbl.Qualifier = sqlparser.NewIdentifierCS("")
show.Tbl.Name = table.Name

if destination != nil {
dest = destination
}
ks = table.Keyspace
}

return &engine.Send{
Expand Down
21 changes: 21 additions & 0 deletions go/vt/vtgate/planbuilder/testdata/other_read_cases.json
Original file line number Diff line number Diff line change
Expand Up @@ -146,5 +146,26 @@
"user.music"
]
}
},
{
"comment": "describe info_schema table",
"query": "describe information_schema.administrable_role_authorizations",
"plan": {
"QueryType": "EXPLAIN",
"Original": "describe information_schema.administrable_role_authorizations",
"Instructions": {
"OperatorType": "Send",
"Keyspace": {
"Name": "main",
"Sharded": false
},
"TargetDestination": "AnyShard()",
"Query": "explain information_schema.administrable_role_authorizations",
"SingleShardOnly": true
},
"TablesUsed": [
"main.administrable_role_authorizations"
]
}
}
]
48 changes: 37 additions & 11 deletions go/vt/vtgate/planbuilder/vexplain.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import (
"context"
"encoding/json"

"vitess.io/vitess/go/vt/vtgate/vindexes"

"vitess.io/vitess/go/sqltypes"
"vitess.io/vitess/go/vt/key"
querypb "vitess.io/vitess/go/vt/proto/query"
Expand All @@ -41,31 +43,55 @@ func buildVExplainPlan(ctx context.Context, vexplainStmt *sqlparser.VExplainStmt
return nil, vterrors.Errorf(vtrpcpb.Code_INTERNAL, "[BUG] unexpected vtexplain type: %s", vexplainStmt.Type.ToString())
}

func explainTabPlan(explain *sqlparser.ExplainTab, vschema plancontext.VSchema) (*planResult, error) {
_, _, ks, _, destination, err := vschema.FindTableOrVindex(explain.Table)
if err != nil {
return nil, err
}
explain.Table.Qualifier = sqlparser.NewIdentifierCS("")
func findDestinationToTable(vschema plancontext.VSchema, tbl sqlparser.TableName) (
ks *vindexes.Keyspace,
destination key.Destination,
table *vindexes.Table,
err error,
) {
if tbl.Qualifier.NotEmpty() && sqlparser.SystemSchema(tbl.Qualifier.String()) {
ks, err = vschema.AnyKeyspace()
if err != nil {
return
}
} else {
table, _, _, _, destination, err = vschema.FindTableOrVindex(tbl)
if err != nil {
return
}
if table == nil {
err = vterrors.VT05004(tbl.Name.String())
return
}

ks = table.Keyspace
}
if destination == nil {
destination = key.DestinationAnyShard{}
}

keyspace, err := vschema.FindKeyspace(ks)
if ks == nil {
err = vterrors.VT14004(ks)
}

return
}

func explainTabPlan(explain *sqlparser.ExplainTab, vschema plancontext.VSchema) (*planResult, error) {
ks, destination, table, err := findDestinationToTable(vschema, explain.Table)
if err != nil {
return nil, err
}
if keyspace == nil {
return nil, vterrors.VT14004(ks)
if table != nil /*table will be empty for info_schema tables*/ {
explain.Table.Qualifier = sqlparser.NewIdentifierCS("")
}

return newPlanResult(&engine.Send{
Keyspace: keyspace,
Keyspace: ks,
TargetDestination: destination,
Query: sqlparser.String(explain),
SingleShardOnly: true,
}, singleTable(keyspace.Name, explain.Table.Name.String())), nil
}, singleTable(ks.Name, explain.Table.Name.String())), nil
}

func buildVExplainVtgatePlan(ctx context.Context, explainStatement sqlparser.Statement, reservedVars *sqlparser.ReservedVars, vschema plancontext.VSchema, enableOnlineDDL, enableDirectDDL bool) (*planResult, error) {
Expand Down

0 comments on commit 85dd84f

Please sign in to comment.