Skip to content

Commit

Permalink
[release-18.0] fix: remove keyspace when merging subqueries (vitessio…
Browse files Browse the repository at this point in the history
…#16019) (vitessio#16026)

Signed-off-by: Andres Taylor <[email protected]>
Signed-off-by: Florent Poinsard <[email protected]>
Co-authored-by: vitess-bot[bot] <108069721+vitess-bot[bot]@users.noreply.github.com>
Co-authored-by: Andres Taylor <[email protected]>
  • Loading branch information
vitess-bot[bot] and systay authored Jun 11, 2024
1 parent 516f58a commit 3ed3ba1
Show file tree
Hide file tree
Showing 3 changed files with 36 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 @@ -2139,6 +2139,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
1 change: 1 addition & 0 deletions go/vt/vtgate/planbuilder/operators/subquery_planning.go
Original file line number Diff line number Diff line change
Expand Up @@ -526,6 +526,7 @@ func tryMergeSubqueryWithOuter(ctx *plancontext.PlanningContext, subQuery *SubQu
return outer, rewrite.SameTree, nil
}
exprs := subQuery.GetMergePredicates()
sqlparser.RemoveKeyspaceInTables(subQuery.Original)
merger := &subqueryRouteMerger{
outer: outer,
original: subQuery.Original,
Expand Down
23 changes: 23 additions & 0 deletions go/vt/vtgate/planbuilder/testdata/filter_cases.json
Original file line number Diff line number Diff line change
Expand Up @@ -870,6 +870,29 @@
]
}
},
{
"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)",
"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)",
"Table": "`user`"
},
"TablesUsed": [
"user.user",
"user.user_extra"
]
}
},
{
"comment": "Single table equality route with unsigned value",
"query": "select id from user where name = 18446744073709551615",
Expand Down

0 comments on commit 3ed3ba1

Please sign in to comment.