Skip to content

Commit

Permalink
[release-19.0] Fix subquery planning having an aggregation that is us…
Browse files Browse the repository at this point in the history
…ed in order by as long as we can merge it all into a single route (#16402) (#16407)

Signed-off-by: Manan Gupta <manan@planetscale.com>
Co-authored-by: vitess-bot[bot] <108069721+vitess-bot[bot]@users.noreply.github.com>
  • Loading branch information
vitess-bot[bot] authored Jul 16, 2024
1 parent 98262aa commit fc48181
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 4 deletions.
13 changes: 9 additions & 4 deletions go/vt/vtgate/planbuilder/operators/horizon_expanding.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,13 +138,18 @@ func expandOrderBy(ctx *plancontext.PlanningContext, op Operator, qp *QueryProje
continue
}

// If the operator is not a projection, we cannot handle subqueries with aggregation
// If the operator is not a projection, we cannot handle subqueries with aggregation if we are unable to push everything into a single route.
if !ok {
panic(vterrors.VT12001("subquery with aggregation in order by"))
ctx.SemTable.NotSingleRouteErr = vterrors.VT12001("subquery with aggregation in order by")
return &Ordering{
Source: op,
Order: qp.OrderExprs,
}
} else {
// Add the new subquery expression to the projection
proj.addSubqueryExpr(ctx, aeWrap(newExpr), newExpr, subqs...)
}

// Add the new subquery expression to the projection
proj.addSubqueryExpr(ctx, aeWrap(newExpr), newExpr, subqs...)
// Replace the original order expression with the new expression containing subqueries
newOrder = append(newOrder, OrderBy{
Inner: &sqlparser.Order{
Expand Down
27 changes: 27 additions & 0 deletions go/vt/vtgate/planbuilder/testdata/select_cases.json
Original file line number Diff line number Diff line change
Expand Up @@ -736,6 +736,33 @@
]
}
},
{
"comment": "subquery with an aggregation in order by that can be merged into a single route",
"query": "select col, trim((select user_name from user where id = 3)) val from user_extra where user_id = 3 group by col order by val",
"plan": {
"QueryType": "SELECT",
"Original": "select col, trim((select user_name from user where id = 3)) val from user_extra where user_id = 3 group by col order by val",
"Instructions": {
"OperatorType": "Route",
"Variant": "EqualUnique",
"Keyspace": {
"Name": "user",
"Sharded": true
},
"FieldQuery": "select col, trim((select user_name from `user` where 1 != 1)) as val from user_extra where 1 != 1 group by col",
"Query": "select col, trim((select user_name from `user` where id = 3)) as val from user_extra where user_id = 3 group by col order by trim((select `user`.user_name from `user` where `user`.id = 3)) asc",
"Table": "user_extra",
"Values": [
"3"
],
"Vindex": "user_index"
},
"TablesUsed": [
"user.user",
"user.user_extra"
]
}
},
{
"comment": "Jumbled references",
"query": "select user.col, user_extra.id, user.col2 from user join user_extra",
Expand Down
5 changes: 5 additions & 0 deletions go/vt/vtgate/planbuilder/testdata/unsupported_cases.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@
"query": "update user set id = 1 where id = 1",
"plan": "VT12001: unsupported: you cannot UPDATE primary vindex columns; invalid update on vindex: user_index"
},
{
"comment": "subquery with an aggregation in order by that cannot be merged into a single route",
"query": "select col, trim((select user_name from user where col = 'a')) val from user_extra where user_id = 3 group by col order by val",
"plan": "VT12001: unsupported: subquery with aggregation in order by"
},
{
"comment": "update change in multicol vindex column",
"query": "update multicol_tbl set colc = 5, colb = 4 where cola = 1 and colb = 2",
Expand Down

0 comments on commit fc48181

Please sign in to comment.