Skip to content

Commit

Permalink
Add support for global routing in describe statement (#17510)
Browse files Browse the repository at this point in the history
Signed-off-by: Harshit Gangal <[email protected]>
  • Loading branch information
harshit-gangal authored Jan 15, 2025
1 parent 0bcbe4c commit b0a5d66
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 38 deletions.
4 changes: 2 additions & 2 deletions go/test/endtoend/vtgate/misc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -753,9 +753,9 @@ func TestDescribeVindex(t *testing.T) {
_, err := conn.ExecuteFetch("describe hash", 1000, false)
require.Error(t, err)
mysqlErr := err.(*sqlerror.SQLError)
assert.Equal(t, sqlerror.ERNoSuchTable, mysqlErr.Num)
assert.Equal(t, sqlerror.ERUnknownTable, mysqlErr.Num)
assert.Equal(t, "42S02", mysqlErr.State)
assert.ErrorContains(t, mysqlErr, "NotFound desc")
assert.ErrorContains(t, mysqlErr, "VT05004: table 'hash' does not exist")
}

func TestEmptyQuery(t *testing.T) {
Expand Down
8 changes: 5 additions & 3 deletions go/vt/vtgate/executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2672,10 +2672,12 @@ func TestExecutorShowVitessMigrations(t *testing.T) {

func TestExecutorDescHash(t *testing.T) {
executor, _, _, _, ctx := createExecutorEnv(t)

showQuery := "desc hash_index"
session := econtext.NewSafeSession(&vtgatepb.Session{TargetString: "TestExecutor"})
_, err := executor.Execute(ctx, nil, "", session, showQuery, nil)

_, err := executor.Execute(ctx, nil, "", session, "desc hash_index", nil)
require.EqualError(t, err, "VT05004: table 'hash_index' does not exist")

_, err = executor.Execute(ctx, nil, "", session, "desc music", nil)
require.NoError(t, err)
}

Expand Down
4 changes: 2 additions & 2 deletions go/vt/vtgate/planbuilder/plan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -577,10 +577,10 @@ func (s *planTestSuite) TestOtherPlanningFromFile() {
vw, err := vschemawrapper.NewVschemaWrapper(env, vschema, TestBuilder)
require.NoError(s.T(), err)

s.testFile("other_read_cases.json", vw, false)

vw.Vcursor.SetTarget("main")
vw.Keyspace = &vindexes.Keyspace{Name: "main"}

s.testFile("other_read_cases.json", vw, false)
s.testFile("other_admin_cases.json", vw, false)
}

Expand Down
60 changes: 43 additions & 17 deletions go/vt/vtgate/planbuilder/testdata/other_read_cases.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,24 @@
"Instructions": {
"OperatorType": "Send",
"Keyspace": {
"Name": "main",
"Sharded": false
"Name": "user",
"Sharded": true
},
"TargetDestination": "AnyShard()",
"Query": "explain select * from `user`",
"SingleShardOnly": true
},
"TablesUsed": [
"main.user"
"user.user"
]
}
},
{
"comment": "Analyze statement",
"query": "analyze table t1",
"query": "analyze table main.t1",
"plan": {
"QueryType": "ANALYZE",
"Original": "analyze table t1",
"Original": "analyze table main.t1",
"Instructions": {
"OperatorType": "Send",
"Keyspace": {
Expand All @@ -42,43 +42,43 @@
},
{
"comment": "Describe statement",
"query": "describe select * from t",
"query": "describe select * from user",
"plan": {
"QueryType": "EXPLAIN",
"Original": "describe select * from t",
"Original": "describe select * from user",
"Instructions": {
"OperatorType": "Send",
"Keyspace": {
"Name": "main",
"Sharded": false
"Name": "user",
"Sharded": true
},
"TargetDestination": "AnyShard()",
"Query": "explain select * from t",
"Query": "explain select * from `user`",
"SingleShardOnly": true
},
"TablesUsed": [
"main.t"
"user.user"
]
}
},
{
"comment": "Desc statement",
"query": "desc select * from t",
"query": "desc select * from user",
"plan": {
"QueryType": "EXPLAIN",
"Original": "desc select * from t",
"Original": "desc select * from user",
"Instructions": {
"OperatorType": "Send",
"Keyspace": {
"Name": "main",
"Sharded": false
"Name": "user",
"Sharded": true
},
"TargetDestination": "AnyShard()",
"Query": "explain select * from t",
"Query": "explain select * from `user`",
"SingleShardOnly": true
},
"TablesUsed": [
"main.t"
"user.user"
]
}
},
Expand Down Expand Up @@ -167,5 +167,31 @@
"main.administrable_role_authorizations"
]
}
},
{
"comment": "describe table without qualifier",
"query": "describe user",
"plan": {
"QueryType": "EXPLAIN",
"Original": "describe user",
"Instructions": {
"OperatorType": "Send",
"Keyspace": {
"Name": "user",
"Sharded": true
},
"TargetDestination": "AnyShard()",
"Query": "explain `user`",
"SingleShardOnly": true
},
"TablesUsed": [
"user.user"
]
}
},
{
"comment": "describe table does not exists",
"query": "describe unknown_table",
"plan": "table unknown_table not found"
}
]
24 changes: 10 additions & 14 deletions go/vt/vtgate/planbuilder/vexplain.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func buildVExplainPlan(

func explainTabPlan(explain *sqlparser.ExplainTab, vschema plancontext.VSchema) (*planResult, error) {
var keyspace *vindexes.Keyspace
var destination key.Destination
var dest key.Destination

if sqlparser.SystemSchema(explain.Table.Qualifier.String()) {
var err error
Expand All @@ -64,30 +64,26 @@ func explainTabPlan(explain *sqlparser.ExplainTab, vschema plancontext.VSchema)
return nil, err
}
} else {
var tbl *vindexes.Table
var err error
var ks string
_, _, ks, _, destination, err = vschema.FindTableOrVindex(explain.Table)
tbl, _, _, _, dest, 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)
if tbl == nil {
return nil, vterrors.VT05004(explain.Table.Name.String())
}
keyspace = tbl.Keyspace
explain.Table = sqlparser.NewTableName(tbl.Name.String())
}

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

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

0 comments on commit b0a5d66

Please sign in to comment.