Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions go/vt/sqlparser/ast_funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -2145,6 +2145,18 @@ func RemoveKeyspace(in SQLNode) {
}, in)
}

// RemoveKeyspaceInTables removes the database qualifier for all table names in the AST
func RemoveKeyspaceInTables(in SQLNode) {
Rewrite(in, nil, func(cursor *Cursor) bool {
if tbl, ok := cursor.Node().(TableName); ok && !tbl.Qualifier.IsEmpty() {
tbl.Qualifier = NewIdentifierCS("")
cursor.Replace(tbl)
}

return true
})
}

func convertStringToInt(integer string) int {
val, _ := strconv.Atoi(integer)
return val
Expand Down
2 changes: 2 additions & 0 deletions go/vt/vtgate/planbuilder/operators/merging.go
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,8 @@ func (s *subQueryMerger) markPredicateInOuterRouting(outer *ShardedRouting, inne

func (s *subQueryMerger) mergeTables(outer, inner *ShardedRouting, op1, op2 *Route) (*Route, error) {
s.subq.ExtractedSubquery.Merged = true
// we need to make sure that table qualifiers are removed before sending the query to MySQL
sqlparser.RemoveKeyspaceInTables(s.subq.ExtractedSubquery)

routing, err := s.markPredicateInOuterRouting(outer, inner)
if err != nil {
Expand Down
85 changes: 85 additions & 0 deletions go/vt/vtgate/planbuilder/testdata/expected/onecase.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
[
{
"comment": "Add your test case here for debugging and run go test -run=One.",
"query": "select (select count(*) from user_extra) x from user order by x",
"plan": {
"QueryType": "SELECT",
"Original": "select (select count(*) from user_extra) x from user order by x",
"Instructions": {
"OperatorType": "UncorrelatedSubquery",
"Variant": "PulloutValue",
"PulloutVars": [
"__sq1"
],
"Inputs": [
{
"InputName": "SubQuery",
"OperatorType": "Aggregate",
"Variant": "Scalar",
"Aggregates": "sum_count_star(0) AS count(*)",
"Inputs": [
{
"OperatorType": "Route",
"Variant": "Scatter",
"Keyspace": {
"Name": "user",
"Sharded": true
},
"FieldQuery": "select count(*) from user_extra where 1 != 1",
"Query": "select count(*) from user_extra",
"Table": "user_extra"
}
]
},
{
"InputName": "Outer",
"OperatorType": "UncorrelatedSubquery",
"Variant": "PulloutValue",
"PulloutVars": [
"__sq1"
],
"Inputs": [
{
"InputName": "SubQuery",
"OperatorType": "Aggregate",
"Variant": "Scalar",
"Aggregates": "sum_count_star(0) AS count(*)",
"Inputs": [
{
"OperatorType": "Route",
"Variant": "Scatter",
"Keyspace": {
"Name": "user",
"Sharded": true
},
"FieldQuery": "select count(*) from user_extra where 1 != 1",
"Query": "select count(*) from user_extra",
"Table": "user_extra"
}
]
},
{
"InputName": "Outer",
"OperatorType": "Route",
"Variant": "Scatter",
"Keyspace": {
"Name": "user",
"Sharded": true
},
"FieldQuery": "select :__sq1 as x, __sq1, weight_string(__sq1) from `user` where 1 != 1",
"OrderBy": "(1|2) ASC",
"Query": "select :__sq1 as x, __sq1, weight_string(__sq1) from `user` order by __sq1 asc",
"ResultColumns": 1,
"Table": "`user`"
}
]
}
]
},
"TablesUsed": [
"user.user",
"user.user_extra"
]
}
}
]
58 changes: 58 additions & 0 deletions go/vt/vtgate/planbuilder/testdata/filter_cases.json
Original file line number Diff line number Diff line change
Expand Up @@ -846,6 +846,64 @@
]
}
},
{
"comment": "Merging subqueries should remove keyspace from query",
"query": "select u.id from user.user as u where not exists (select 1 from user.user_extra as ue where u.id = ue.user_id)",
"v3-plan": {
"QueryType": "SELECT",
"Original": "select u.id from user.user as u where not exists (select 1 from user.user_extra as ue where u.id = ue.user_id)",
"Instructions": {
"OperatorType": "Route",
"Variant": "Scatter",
"Keyspace": {
"Name": "user",
"Sharded": true
Comment on lines +849 to +860
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add a test case when the subquery has a system table in it. That database qualifier should not be removed.

},
"FieldQuery": "select u.id from `user` as u where 1 != 1",
"Query": "select u.id from `user` as u where not exists (select 1 from user_extra as ue where u.id = ue.user_id limit 1)",
"Table": "`user`"
}
},
"gen4-plan": {
"QueryType": "SELECT",
"Original": "select u.id from user.user as u where not exists (select 1 from user.user_extra as ue where u.id = ue.user_id)",
"Instructions": {
"OperatorType": "Route",
"Variant": "Scatter",
"Keyspace": {
"Name": "user",
"Sharded": true
},
"FieldQuery": "select u.id from `user` as u where 1 != 1",
"Query": "select u.id from `user` as u where not exists (select 1 from user_extra as ue where u.id = ue.user_id limit 1)",
"Table": "`user`"
},
"TablesUsed": [
"user.user",
"user.user_extra"
]
}
},
{
"comment": "Check that we don't remove information_schema qualifier",
"query": "select 1 from information_schema.tables where exists(select 1 from information_schema.tables where table_name = 'tables')",
"plan": {
"QueryType": "SELECT",
"Original": "select 1 from information_schema.tables where exists(select 1 from information_schema.tables where table_name = 'tables')",
"Instructions": {
"OperatorType": "Route",
"Variant": "DBA",
"Keyspace": {
"Name": "main",
"Sharded": false
},
"FieldQuery": "select 1 from information_schema.`tables` where 1 != 1",
"Query": "select 1 from information_schema.`tables` where exists (select 1 from information_schema.`tables` where table_name = :table_name /* VARCHAR */ limit 1)",
"SysTableTableName": "[table_name:VARCHAR(\"tables\")]",
"Table": "information_schema.`tables`"
}
}},

{
"comment": "Composite IN: tuple inside tuple",
"query": "select id from user where ((col1, name), col2) in ((('aa', 'bb'), 'cc'), (('dd', 'ee'), 'ff'))",
Expand Down