Skip to content

Commit

Permalink
bugfix: When the planner is able to merge subqueries, it's important …
Browse files Browse the repository at this point in the history
…to remember to remove the keyspace from the query before adding the subquery to the outer query.

Signed-off-by: Andres Taylor <andres@planetscale.com>
  • Loading branch information
systay committed Jun 4, 2024
1 parent de4fefe commit b391a63
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 0 deletions.
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
38 changes: 38 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,44 @@
]
}
},
{
"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
},
"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": "Composite IN: tuple inside tuple",
"query": "select id from user where ((col1, name), col2) in ((('aa', 'bb'), 'cc'), (('dd', 'ee'), 'ff'))",
Expand Down

0 comments on commit b391a63

Please sign in to comment.