Skip to content

Commit

Permalink
feat: optimize subqueries used in both select and order by
Browse files Browse the repository at this point in the history
Signed-off-by: Andres Taylor <andres@planetscale.com>
  • Loading branch information
systay committed Jun 5, 2024
1 parent d353217 commit 59bad72
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 77 deletions.
5 changes: 3 additions & 2 deletions go/vt/vtgate/planbuilder/operators/horizon_expanding.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,8 @@ func expandOrderBy(ctx *plancontext.PlanningContext, op Operator, qp *QueryProje
if !ok {
panic(vterrors.VT12001("subquery with aggregation in order by"))
}
proj.addSubqueryExpr(aeWrap(newExpr), newExpr, subqs...)

proj.addSubqueryExpr(ctx, aeWrap(newExpr), newExpr, subqs...)
newOrder = append(newOrder, OrderBy{
Inner: &sqlparser.Order{
Expr: newExpr,
Expand Down Expand Up @@ -284,7 +285,7 @@ func createProjectionWithoutAggr(ctx *plancontext.PlanningContext, qp *QueryProj
// there was no subquery in this expression
proj.addUnexploredExpr(org, expr)
} else {
proj.addSubqueryExpr(org, newExpr, subqs...)
proj.addSubqueryExpr(ctx, org, newExpr, subqs...)
}
}
proj.Source = sqc.getRootOperator(src, nil)
Expand Down
13 changes: 12 additions & 1 deletion go/vt/vtgate/planbuilder/operators/projection.go
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,18 @@ func (p *Projection) addUnexploredExpr(ae *sqlparser.AliasedExpr, e sqlparser.Ex
return p.addProjExpr(newProjExprWithInner(ae, e))
}

func (p *Projection) addSubqueryExpr(ae *sqlparser.AliasedExpr, expr sqlparser.Expr, sqs ...*SubQuery) {
func (p *Projection) addSubqueryExpr(ctx *plancontext.PlanningContext, ae *sqlparser.AliasedExpr, expr sqlparser.Expr, sqs ...*SubQuery) {
ap, err := p.GetAliasedProjections()
if err != nil {
panic(err)
}
for _, projExpr := range ap {
if ctx.SemTable.EqualsExprWithDeps(projExpr.EvalExpr, expr) {
// if we already have this column, we can just return the offset
return
}
}

pe := newProjExprWithInner(ae, expr)
pe.Info = SubQueryExpression(sqs)

Expand Down
1 change: 1 addition & 0 deletions go/vt/vtgate/planbuilder/operators/query_planning.go
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,7 @@ func tryPushOrdering(ctx *plancontext.PlanningContext, in *Ordering) (Operator,
return in, NoRewrite
}
in.Order[idx].SimplifiedExpr = src.rewriteColNameToArgument(order.SimplifiedExpr)
in.Order[idx].Inner.Expr = src.rewriteColNameToArgument(order.Inner.Expr)
}
src.Outer, in.Source = in, src.Outer
return src, Rewrote("push ordering into outer side of subquery")
Expand Down
13 changes: 13 additions & 0 deletions go/vt/vtgate/planbuilder/operators/subquery.go
Original file line number Diff line number Diff line change
Expand Up @@ -309,3 +309,16 @@ func (sq *SubQuery) rewriteColNameToArgument(expr sqlparser.Expr) sqlparser.Expr

return sqlparser.Rewrite(expr, pre, nil).(sqlparser.Expr)
}

func (sq *SubQuery) Compact(*plancontext.PlanningContext) (Operator, *ApplyResult) {
other, ok := sq.Outer.(*SubQuery)
if !ok {
return sq, NoRewrite
}
if other.ArgName == sq.ArgName {
sq.Outer = other.Outer
return sq, Rewrote("removed duplicate subquery")
}

return sq, NoRewrite
}
111 changes: 37 additions & 74 deletions go/vt/vtgate/planbuilder/testdata/select_cases.json
Original file line number Diff line number Diff line change
Expand Up @@ -2278,98 +2278,61 @@
"QueryType": "SELECT",
"Original": "select (select col from user limit 1) as a from user join user_extra order by a",
"Instructions": {
"OperatorType": "SimpleProjection",
"ColumnNames": [
"0:a"
],
"Columns": "0",
"OperatorType": "Join",
"Variant": "Join",
"JoinColumnIndexes": "L:0",
"TableName": "`user`_user_extra",
"Inputs": [
{
"OperatorType": "Join",
"Variant": "Join",
"JoinColumnIndexes": "L:0,L:1",
"TableName": "`user`_user_extra",
"OperatorType": "UncorrelatedSubquery",
"Variant": "PulloutValue",
"PulloutVars": [
"__sq1"
],
"Inputs": [
{
"OperatorType": "UncorrelatedSubquery",
"Variant": "PulloutValue",
"PulloutVars": [
"__sq1"
],
"InputName": "SubQuery",
"OperatorType": "Limit",
"Count": "1",
"Inputs": [
{
"InputName": "SubQuery",
"OperatorType": "Limit",
"Count": "1",
"Inputs": [
{
"OperatorType": "Route",
"Variant": "Scatter",
"Keyspace": {
"Name": "user",
"Sharded": true
},
"FieldQuery": "select `user`.col from `user` where 1 != 1",
"Query": "select `user`.col from `user` limit 1",
"Table": "`user`"
}
]
},
{
"InputName": "Outer",
"OperatorType": "UncorrelatedSubquery",
"Variant": "PulloutValue",
"PulloutVars": [
"__sq1"
],
"Inputs": [
{
"InputName": "SubQuery",
"OperatorType": "Limit",
"Count": "1",
"Inputs": [
{
"OperatorType": "Route",
"Variant": "Scatter",
"Keyspace": {
"Name": "user",
"Sharded": true
},
"FieldQuery": "select col from `user` where 1 != 1",
"Query": "select col from `user` limit 1",
"Table": "`user`"
}
]
},
{
"InputName": "Outer",
"OperatorType": "Route",
"Variant": "Scatter",
"Keyspace": {
"Name": "user",
"Sharded": true
},
"FieldQuery": "select :__sq1 as a, :__sq1 as __sq1, :__sq1, weight_string(:__sq1) from `user` where 1 != 1",
"OrderBy": "(2|3) ASC",
"Query": "select :__sq1 as a, :__sq1 as __sq1, :__sq1, weight_string(:__sq1) from `user` order by __sq1 asc",
"Table": "`user`"
}
]
"OperatorType": "Route",
"Variant": "Scatter",
"Keyspace": {
"Name": "user",
"Sharded": true
},
"FieldQuery": "select `user`.col from `user` where 1 != 1",
"Query": "select `user`.col from `user` limit 1",
"Table": "`user`"
}
]
},
{
"InputName": "Outer",
"OperatorType": "Route",
"Variant": "Scatter",
"Keyspace": {
"Name": "user",
"Sharded": true
},
"FieldQuery": "select 1 from user_extra where 1 != 1",
"Query": "select 1 from user_extra",
"Table": "user_extra"
"FieldQuery": "select :__sq1 as a, :__sq1, weight_string(:__sq1) from `user` where 1 != 1",
"OrderBy": "(1|2) ASC",
"Query": "select :__sq1 as a, :__sq1, weight_string(:__sq1) from `user` order by :__sq1 asc",
"Table": "`user`"
}
]
},
{
"OperatorType": "Route",
"Variant": "Scatter",
"Keyspace": {
"Name": "user",
"Sharded": true
},
"FieldQuery": "select 1 from user_extra where 1 != 1",
"Query": "select 1 from user_extra",
"Table": "user_extra"
}
]
},
Expand Down

0 comments on commit 59bad72

Please sign in to comment.