Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle dual queries with LIMIT on the vtgate #16400

Merged
merged 2 commits into from
Jul 18, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
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
systay marked this conversation as resolved.
Show resolved Hide resolved
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
Loading