Skip to content

Commit

Permalink
Merge pull request #9969 from planetscale/fix-dual-panic
Browse files Browse the repository at this point in the history
fix: dual query with exists clause having system table query in it
  • Loading branch information
harshit-gangal authored Mar 27, 2022
2 parents 1836b8d + 16b737c commit f10217f
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 0 deletions.
4 changes: 4 additions & 0 deletions go/vt/vtgate/planbuilder/physical/subquery_planning.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"vitess.io/vitess/go/vt/sqlparser"
"vitess.io/vitess/go/vt/vterrors"
"vitess.io/vitess/go/vt/vtgate/engine"
"vitess.io/vitess/go/vt/vtgate/evalengine"
"vitess.io/vitess/go/vt/vtgate/planbuilder/abstract"
"vitess.io/vitess/go/vt/vtgate/planbuilder/plancontext"

Expand Down Expand Up @@ -100,6 +101,9 @@ func mergeSubQueryOp(ctx *plancontext.PlanningContext, outer *Route, inner *Rout
}
outer.SysTableTableSchema = append(outer.SysTableTableSchema, inner.SysTableTableSchema...)
for k, v := range inner.SysTableTableName {
if outer.SysTableTableName == nil {
outer.SysTableTableName = map[string]evalengine.Expr{}
}
outer.SysTableTableName[k] = v
}

Expand Down
3 changes: 3 additions & 0 deletions go/vt/vtgate/planbuilder/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,9 @@ func (rb *route) MergeSubquery(pb *primitiveBuilder, inner *route) bool {
case engine.DBA, engine.Reference:
rb.eroute.SysTableTableSchema = append(rb.eroute.SysTableTableSchema, inner.eroute.SysTableTableSchema...)
for k, v := range inner.eroute.SysTableTableName {
if rb.eroute.SysTableTableName == nil {
rb.eroute.SysTableTableName = map[string]evalengine.Expr{}
}
rb.eroute.SysTableTableName[k] = v
}
rb.eroute.Opcode = engine.DBA
Expand Down
37 changes: 37 additions & 0 deletions go/vt/vtgate/planbuilder/testdata/select_cases.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2965,3 +2965,40 @@ Gen4 plan same as above
}
}
Gen4 plan same as above

# dual query with exists clause
"select 1 from dual where exists (select 1 from information_schema.TABLES where information_schema.TABLES.TABLE_NAME = 'proc' and information_schema.TABLES.TABLE_SCHEMA = 'mysql')"
{
"QueryType": "SELECT",
"Original": "select 1 from dual where exists (select 1 from information_schema.TABLES where information_schema.TABLES.TABLE_NAME = 'proc' and information_schema.TABLES.TABLE_SCHEMA = 'mysql')",
"Instructions": {
"OperatorType": "Route",
"Variant": "DBA",
"Keyspace": {
"Name": "main",
"Sharded": false
},
"FieldQuery": "select 1 from dual where 1 != 1",
"Query": "select 1 from dual where exists (select 1 from information_schema.`TABLES` where information_schema.`TABLES`.TABLE_NAME = :TABLES_TABLE_NAME and information_schema.`TABLES`.TABLE_SCHEMA = :__vtschemaname)",
"SysTableTableName": "[TABLES_TABLE_NAME:VARCHAR(\"proc\")]",
"SysTableTableSchema": "[VARCHAR(\"mysql\")]",
"Table": "dual"
}
}
{
"QueryType": "SELECT",
"Original": "select 1 from dual where exists (select 1 from information_schema.TABLES where information_schema.TABLES.TABLE_NAME = 'proc' and information_schema.TABLES.TABLE_SCHEMA = 'mysql')",
"Instructions": {
"OperatorType": "Route",
"Variant": "DBA",
"Keyspace": {
"Name": "main",
"Sharded": false
},
"FieldQuery": "select 1 from dual where 1 != 1",
"Query": "select 1 from dual where exists (select 1 from information_schema.`TABLES` where `TABLES`.TABLE_NAME = :TABLES_TABLE_NAME and `TABLES`.TABLE_SCHEMA = :__vtschemaname)",
"SysTableTableName": "[TABLES_TABLE_NAME:VARCHAR(\"proc\")]",
"SysTableTableSchema": "[VARCHAR(\"mysql\")]",
"Table": "dual"
}
}

0 comments on commit f10217f

Please sign in to comment.