Skip to content

Commit

Permalink
Cherry-pick da594ae with conflicts
Browse files Browse the repository at this point in the history
  • Loading branch information
vitess-bot[bot] committed May 9, 2024
1 parent e1d799d commit efb745f
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 12 deletions.
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 @@ -87,5 +87,26 @@
"SingleShardOnly": true
}
}
},
{
"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"
]
}
}
]
90 changes: 78 additions & 12 deletions go/vt/vtgate/planbuilder/vexplain.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import (

"vitess.io/vitess/go/vt/vtgate/planbuilder/plancontext"

"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 Down Expand Up @@ -63,24 +65,37 @@ func buildVExplainPlan(ctx context.Context, vexplainStmt *sqlparser.VExplainStmt
}

func explainTabPlan(explain *sqlparser.ExplainTab, vschema plancontext.VSchema) (*planResult, error) {
_, _, ks, _, destination, err := vschema.FindTableOrVindex(explain.Table)
if err != nil {
return nil, err
var keyspace *vindexes.Keyspace
var destination key.Destination

if sqlparser.SystemSchema(explain.Table.Qualifier.String()) {
var err error
keyspace, err = vschema.AnyKeyspace()
if err != nil {
return nil, err
}
} else {
var err error
var ks string
_, _, ks, _, destination, err = vschema.FindTableOrVindex(explain.Table)
if err != nil {
return nil, err
}
explain.Table.Qualifier = sqlparser.NewIdentifierCS("")

keyspace, err = vschema.FindKeyspace(ks)
if err != nil {
return nil, err
}
if keyspace == nil {
return nil, vterrors.VT14004(ks)
}
}
explain.Table.Qualifier = sqlparser.NewIdentifierCS("")

if destination == nil {
destination = key.DestinationAnyShard{}
}

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

return newPlanResult(&engine.Send{
Keyspace: keyspace,
TargetDestination: destination,
Expand Down Expand Up @@ -126,3 +141,54 @@ func buildVExplainLoggingPlan(ctx context.Context, explain *sqlparser.VExplainSt

return &planResult{primitive: &engine.VExplain{Input: input.primitive, Type: explain.Type}, tables: input.tables}, nil
}
<<<<<<< HEAD
=======

// buildExplainStmtPlan takes an EXPLAIN query and if possible sends the whole query to a single shard
func buildExplainStmtPlan(stmt sqlparser.Statement, reservedVars *sqlparser.ReservedVars, vschema plancontext.VSchema) (*planResult, error) {
explain := stmt.(*sqlparser.ExplainStmt)
switch explain.Statement.(type) {
case sqlparser.SelectStatement, *sqlparser.Update, *sqlparser.Delete, *sqlparser.Insert:
return explainPlan(explain, reservedVars, vschema)
default:
return buildOtherReadAndAdmin(sqlparser.String(explain), vschema)
}
}

func explainPlan(explain *sqlparser.ExplainStmt, reservedVars *sqlparser.ReservedVars, vschema plancontext.VSchema) (*planResult, error) {
ctx, err := plancontext.CreatePlanningContext(explain.Statement, reservedVars, vschema, Gen4)
if err != nil {
return nil, err
}

ks := ctx.SemTable.SingleKeyspace()
if ks == nil {
return nil, vterrors.VT03031()
}

if err = queryRewrite(ctx, explain.Statement); err != nil {
return nil, err
}

// Remove keyspace qualifier from columns and tables.
sqlparser.RemoveKeyspace(explain.Statement)

var tables []string
for _, table := range ctx.SemTable.Tables {
name, err := table.Name()
if err != nil {
// this is just for reporting which tables we are touching
// it's OK to ignore errors here
continue
}
tables = append(tables, operators.QualifiedString(ks, name.Name.String()))
}

return newPlanResult(&engine.Send{
Keyspace: ks,
TargetDestination: key.DestinationAnyShard{},
Query: sqlparser.String(explain),
SingleShardOnly: true,
}, tables...), nil
}
>>>>>>> da594ae738 (fix: handle info_schema routing (#15899))

0 comments on commit efb745f

Please sign in to comment.