Skip to content

Commit

Permalink
Handle dual queries with LIMIT on the vtgate (#16400)
Browse files Browse the repository at this point in the history
Signed-off-by: Andres Taylor <[email protected]>
  • Loading branch information
systay authored Jul 18, 2024
1 parent b162048 commit 96974ef
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
19 changes: 18 additions & 1 deletion go/vt/vtgate/planbuilder/select.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,11 +242,28 @@ func createSelectOperator(ctx *plancontext.PlanningContext, selStmt sqlparser.Se
}

func isOnlyDual(sel *sqlparser.Select) bool {
if sel.Where != nil || sel.GroupBy != nil || sel.Having != nil || sel.Limit != nil || sel.OrderBy != nil {
if sel.Where != nil || sel.GroupBy != nil || sel.Having != nil || sel.OrderBy != nil {
// we can only deal with queries without any other subclauses - just SELECT and FROM, nothing else is allowed
return false
}

if sel.Limit != nil {
if sel.Limit.Offset != nil {
return false
}
limit := sel.Limit.Rowcount
switch limit := limit.(type) {
case nil:
case *sqlparser.Literal:
if limit.Val == "0" {
// A limit with any value other than zero can still return a row
return false
}
default:
return false
}
}

if len(sel.From) > 1 {
return false
}
Expand Down
22 changes: 22 additions & 0 deletions go/vt/vtgate/planbuilder/testdata/select_cases.json
Original file line number Diff line number Diff line change
Expand Up @@ -2744,6 +2744,28 @@
]
}
},
{
"comment": "Dual query should be handled on the vtgate even with a LIMIT",
"query": "select last_insert_id() limit 1",
"plan": {
"QueryType": "SELECT",
"Original": "select last_insert_id() limit 1",
"Instructions": {
"OperatorType": "Projection",
"Expressions": [
":__lastInsertId as last_insert_id()"
],
"Inputs": [
{
"OperatorType": "SingleRow"
}
]
},
"TablesUsed": [
"main.dual"
]
}
},
{
"comment": "PullOut subquery with an aggregation that should be typed in the final output",
"query": "select (select sum(col) from user) from user_extra",
Expand Down

0 comments on commit 96974ef

Please sign in to comment.