diff --git a/go/vt/vtgate/planbuilder/physical/subquery_planning.go b/go/vt/vtgate/planbuilder/physical/subquery_planning.go index 2f31f400138..4abcb3c4347 100644 --- a/go/vt/vtgate/planbuilder/physical/subquery_planning.go +++ b/go/vt/vtgate/planbuilder/physical/subquery_planning.go @@ -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" @@ -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 } diff --git a/go/vt/vtgate/planbuilder/route.go b/go/vt/vtgate/planbuilder/route.go index 2d74934b062..e54da457ba6 100644 --- a/go/vt/vtgate/planbuilder/route.go +++ b/go/vt/vtgate/planbuilder/route.go @@ -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 diff --git a/go/vt/vtgate/planbuilder/testdata/select_cases.txt b/go/vt/vtgate/planbuilder/testdata/select_cases.txt index c7d52f6e80b..1de98a26d55 100644 --- a/go/vt/vtgate/planbuilder/testdata/select_cases.txt +++ b/go/vt/vtgate/planbuilder/testdata/select_cases.txt @@ -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" + } +}