Skip to content

Commit

Permalink
added more show plans and fixed the show planbuilder
Browse files Browse the repository at this point in the history
Signed-off-by: Harshit Gangal <harshit@planetscale.com>
  • Loading branch information
harshit-gangal committed Feb 17, 2021
1 parent 42d62b9 commit 742c5f9
Show file tree
Hide file tree
Showing 2 changed files with 227 additions and 26 deletions.
67 changes: 41 additions & 26 deletions go/vt/vtgate/planbuilder/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,17 +163,25 @@ func buildDBPlan(show *sqlparser.ShowBasic, vschema ContextVSchema) (engine.Prim
}

func buildPlanWithDB(show *sqlparser.ShowBasic, vschema ContextVSchema) (engine.Primitive, error) {
destination, keyspace, _, err := vschema.TargetDestination(show.DbName)
dbName := show.DbName
if sqlparser.SystemSchema(dbName) {
ks, err := vschema.AnyKeyspace()
if err != nil {
return nil, err
}
dbName = ks.Name
} else {
// Remove Database Name from the query.
show.DbName = ""
}
destination, keyspace, _, err := vschema.TargetDestination(dbName)
if err != nil {
return nil, err
}
if destination == nil {
destination = key.DestinationAnyShard{}
}

// Remove Database Name from the query.
show.DbName = ""

query := sqlparser.String(show)
return &engine.Send{
Keyspace: keyspace,
Expand Down Expand Up @@ -311,19 +319,24 @@ func buildShowCreatePlan(show *sqlparser.ShowCreate, vschema ContextVSchema) (en
}

func buildCreateDbPlan(show *sqlparser.ShowCreate, vschema ContextVSchema) (engine.Primitive, error) {
dest := key.Destination(key.DestinationAnyShard{})
var ks *vindexes.Keyspace
var err error
dbName := show.Op.Name.String()
if sqlparser.SystemSchema(dbName) {
ks, err = vschema.AnyKeyspace()
} else {
dest, ks, _, err = vschema.TargetDestination(dbName)
ks, err := vschema.AnyKeyspace()
if err != nil {
return nil, err
}
dbName = ks.Name
}

dest, ks, _, err := vschema.TargetDestination(dbName)
if err != nil {
return nil, err
}

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

return &engine.Send{
Keyspace: ks,
TargetDestination: dest,
Expand Down Expand Up @@ -370,27 +383,29 @@ func buildCreateTblPlan(show *sqlparser.ShowCreate, vschema ContextVSchema) (eng
}

func buildCreatePlan(show *sqlparser.ShowCreate, vschema ContextVSchema) (engine.Primitive, error) {
dest := key.Destination(key.DestinationAnyShard{})
var ks *vindexes.Keyspace
var err error
if show.Op.Qualifier.IsEmpty() {
ks, err = vschema.DefaultKeyspace()
if err != nil {
return nil, err
}
dbName := ""
if !show.Op.Qualifier.IsEmpty() {
dbName = show.Op.Qualifier.String()
}
if ks == nil {
dbName := show.Op.Qualifier.String()
if sqlparser.SystemSchema(dbName) {
ks, err = vschema.AnyKeyspace()
} else {
dest, ks, _, err = vschema.TargetDestination(dbName)
show.Op.Qualifier = sqlparser.NewTableIdent("")
}

if sqlparser.SystemSchema(dbName) {
ks, err := vschema.AnyKeyspace()
if err != nil {
return nil, err
}
dbName = ks.Name
} else {
show.Op.Qualifier = sqlparser.NewTableIdent("")
}

dest, ks, _, err := vschema.TargetDestination(dbName)
if err != nil {
return nil, err
}
if dest == nil {
dest = key.DestinationAnyShard{}
}

return &engine.Send{
Keyspace: ks,
TargetDestination: dest,
Expand Down
186 changes: 186 additions & 0 deletions go/vt/vtgate/planbuilder/testdata/show_cases.txt
Original file line number Diff line number Diff line change
Expand Up @@ -255,8 +255,194 @@
"Name": "user",
"Sharded": true
},
"TargetDestination": "AnyShard()",
"IsDML": false,
"Query": "show create database `user`",
"SingleShardOnly": true
}
}

# show create database system_schema
"show create database mysql"
{
"QueryType": "SHOW",
"Original": "show create database mysql",
"Instructions": {
"OperatorType": "Send",
"Keyspace": {
"Name": "main",
"Sharded": false
},
"TargetDestination": "AnyShard()",
"IsDML": false,
"Query": "show create database mysql",
"SingleShardOnly": true
}
}

# show create procedure
"show create procedure proc"
{
"QueryType": "SHOW",
"Original": "show create procedure proc",
"Instructions": {
"OperatorType": "Send",
"Keyspace": {
"Name": "main",
"Sharded": false
},
"TargetDestination": "AnyShard()",
"IsDML": false,
"Query": "show create procedure proc",
"SingleShardOnly": true
}
}

# show create procedure from system_schema
"show create procedure information_schema.proc"
{
"QueryType": "SHOW",
"Original": "show create procedure information_schema.proc",
"Instructions": {
"OperatorType": "Send",
"Keyspace": {
"Name": "main",
"Sharded": false
},
"TargetDestination": "AnyShard()",
"IsDML": false,
"Query": "show create procedure information_schema.proc",
"SingleShardOnly": true
}
}

# show create table on table present in sharded but as unsharded is selected it goes to unsharded keyspace
"show create table user_extra"
{
"QueryType": "SHOW",
"Original": "show create table user_extra",
"Instructions": {
"OperatorType": "Send",
"Keyspace": {
"Name": "main",
"Sharded": false
},
"TargetDestination": "AnyShard()",
"IsDML": false,
"Query": "show create table user_extra",
"SingleShardOnly": true
}
}

# show create table with qualifier
"show create table user.user_extra"
{
"QueryType": "SHOW",
"Original": "show create table user.user_extra",
"Instructions": {
"OperatorType": "Send",
"Keyspace": {
"Name": "user",
"Sharded": true
},
"TargetDestination": "AnyShard()",
"IsDML": false,
"Query": "show create table user_extra",
"SingleShardOnly": true
}
}

# show create table with unsharded as default keyspace
"show create table unknown"
{
"QueryType": "SHOW",
"Original": "show create table unknown",
"Instructions": {
"OperatorType": "Send",
"Keyspace": {
"Name": "main",
"Sharded": false
},
"TargetDestination": "AnyShard()",
"IsDML": false,
"Query": "show create table unknown",
"SingleShardOnly": true
}
}

# show create table with table not present with qualifier
"show create table user.unknown"
"table unknown not found"

# show create table from system_schema
"show create table information_schema.tables"
{
"QueryType": "SHOW",
"Original": "show create table information_schema.tables",
"Instructions": {
"OperatorType": "Send",
"Keyspace": {
"Name": "main",
"Sharded": false
},
"TargetDestination": "AnyShard()",
"IsDML": false,
"Query": "show create table information_schema.`tables`",
"SingleShardOnly": true
}
}

# show tables
"show tables"
{
"QueryType": "SHOW",
"Original": "show tables",
"Instructions": {
"OperatorType": "Send",
"Keyspace": {
"Name": "main",
"Sharded": false
},
"TargetDestination": "AnyShard()",
"IsDML": false,
"Query": "show tables",
"SingleShardOnly": true
}
}

# show tables from db
"show tables from user"
{
"QueryType": "SHOW",
"Original": "show tables from user",
"Instructions": {
"OperatorType": "Send",
"Keyspace": {
"Name": "user",
"Sharded": true
},
"TargetDestination": "AnyShard()",
"IsDML": false,
"Query": "show tables",
"SingleShardOnly": true
}
}

# show tables from system schema
"show tables from performance_schema"
{
"QueryType": "SHOW",
"Original": "show tables from performance_schema",
"Instructions": {
"OperatorType": "Send",
"Keyspace": {
"Name": "main",
"Sharded": false
},
"TargetDestination": "AnyShard()",
"IsDML": false,
"Query": "show tables from performance_schema",
"SingleShardOnly": true
}
}

0 comments on commit 742c5f9

Please sign in to comment.