Skip to content

Commit

Permalink
dual queries with LIMIT can still be handled on the vtgate
Browse files Browse the repository at this point in the history
Signed-off-by: Andres Taylor <andres@planetscale.com>
  • Loading branch information
systay committed Jul 16, 2024
1 parent 37b10ad commit b2b3249
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
18 changes: 17 additions & 1 deletion go/vt/vtgate/planbuilder/select.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,11 +242,27 @@ 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:
return true
case *sqlparser.Literal:
if limit.Val == "0" {
// A limit with any value other than zero can still return a row
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 @@ -2717,6 +2717,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 b2b3249

Please sign in to comment.