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

[release-18.0] Fix subquery cloning and dependencies (#15039) #15068

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
3 changes: 3 additions & 0 deletions go/vt/vtgate/planbuilder/operators/ast_to_op.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ func findTablesContained(ctx *plancontext.PlanningContext, node sqlparser.SQLNod
return
}

<<<<<<< HEAD
func rewriteRemainingColumns(
ctx *plancontext.PlanningContext,
stmt sqlparser.SelectStatement,
Expand All @@ -144,6 +145,8 @@ func rewriteRemainingColumns(
}, nil).(sqlparser.SelectStatement)
}

=======
>>>>>>> fd99639e40 (Fix subquery cloning and dependencies (#15039))
// joinPredicateCollector is used to inspect the predicates inside the subquery, looking for any
// comparisons between the inner and the outer side.
// They can be used for merging the two parts of the query together
Expand Down
46 changes: 41 additions & 5 deletions go/vt/vtgate/planbuilder/operators/horizon_expanding.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,7 @@ func expandSelectHorizon(ctx *plancontext.PlanningContext, horizon *Horizon, sel
}

if len(qp.OrderExprs) > 0 {
op = &Ordering{
Source: op,
Order: qp.OrderExprs,
}
op = expandOrderBy(ctx, op, qp)
extracted = append(extracted, "Ordering")
}

Expand All @@ -132,11 +129,50 @@ func expandSelectHorizon(ctx *plancontext.PlanningContext, horizon *Horizon, sel
return op, rewrite.NewTree(fmt.Sprintf("expand SELECT horizon into (%s)", strings.Join(extracted, ", ")), op), nil
}

<<<<<<< HEAD
func createProjectionFromSelect(ctx *plancontext.PlanningContext, horizon *Horizon) (out ops.Operator, err error) {
qp, err := horizon.getQP(ctx)
if err != nil {
return nil, err
}
=======
func expandOrderBy(ctx *plancontext.PlanningContext, op Operator, qp *QueryProjection) Operator {
proj := newAliasedProjection(op)
var newOrder []OrderBy
sqc := &SubQueryBuilder{}
for _, expr := range qp.OrderExprs {
newExpr, subqs := sqc.pullOutValueSubqueries(ctx, expr.SimplifiedExpr, TableID(op), false)
if newExpr == nil {
// no subqueries found, let's move on
newOrder = append(newOrder, expr)
continue
}
proj.addSubqueryExpr(aeWrap(newExpr), newExpr, subqs...)
newOrder = append(newOrder, OrderBy{
Inner: &sqlparser.Order{
Expr: newExpr,
Direction: expr.Inner.Direction,
},
SimplifiedExpr: newExpr,
})

}

if len(proj.Columns.GetColumns()) > 0 {
// if we had to project columns for the ordering,
// we need the projection as source
op = proj
}

return &Ordering{
Source: op,
Order: newOrder,
}
}

func createProjectionFromSelect(ctx *plancontext.PlanningContext, horizon *Horizon) Operator {
qp := horizon.getQP(ctx)
>>>>>>> fd99639e40 (Fix subquery cloning and dependencies (#15039))

var dt *DerivedTable
if horizon.TableId != nil {
Expand Down Expand Up @@ -271,7 +307,7 @@ func createProjectionWithoutAggr(ctx *plancontext.PlanningContext, qp *QueryProj
sqc := &SubQueryBuilder{}
outerID := TableID(src)
for _, ae := range aes {
org := sqlparser.CloneRefOfAliasedExpr(ae)
org := ctx.SemTable.Clone(ae).(*sqlparser.AliasedExpr)
expr := ae.Expr
newExpr, subqs, err := sqc.pullOutValueSubqueries(ctx, expr, outerID, false)
if err != nil {
Expand Down
5 changes: 5 additions & 0 deletions go/vt/vtgate/planbuilder/operators/queryprojection.go
Original file line number Diff line number Diff line change
Expand Up @@ -370,8 +370,13 @@ func (qp *QueryProjection) addOrderBy(ctx *plancontext.PlanningContext, orderBy
if !es.add(ctx, order.Expr) {
continue
}
<<<<<<< HEAD
qp.OrderExprs = append(qp.OrderExprs, ops.OrderBy{
Inner: sqlparser.CloneRefOfOrder(order),
=======
qp.OrderExprs = append(qp.OrderExprs, OrderBy{
Inner: ctx.SemTable.Clone(order).(*sqlparser.Order),
>>>>>>> fd99639e40 (Fix subquery cloning and dependencies (#15039))
SimplifiedExpr: order.Expr,
})
canPushSorting = canPushSorting && !containsAggr(order.Expr)
Expand Down
7 changes: 7 additions & 0 deletions go/vt/vtgate/planbuilder/operators/subquery.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,14 @@ func (sq *SubQuery) GetMergePredicates() []sqlparser.Expr {

func (sq *SubQuery) settle(ctx *plancontext.PlanningContext, outer ops.Operator) (ops.Operator, error) {
if !sq.TopLevel {
<<<<<<< HEAD
return nil, subqueryNotAtTopErr
=======
panic(subqueryNotAtTopErr)
}
if sq.correlated && sq.FilterType != opcode.PulloutExists {
panic(correlatedSubqueryErr)
>>>>>>> fd99639e40 (Fix subquery cloning and dependencies (#15039))
}
if sq.IsProjection {
if len(sq.GetMergePredicates()) > 0 {
Expand Down
5 changes: 5 additions & 0 deletions go/vt/vtgate/planbuilder/operators/subquery_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,10 +183,15 @@ func createSubquery(
totalID := subqID.Merge(outerID)
sqc := &SubQueryBuilder{totalID: totalID, subqID: subqID, outerID: outerID}

<<<<<<< HEAD
predicates, joinCols, err := sqc.inspectStatement(ctx, subq.Select)
if err != nil {
return nil, err
}
=======
predicates, joinCols := sqc.inspectStatement(ctx, subq.Select)
correlated := !ctx.SemTable.RecursiveDeps(subq).IsEmpty()
>>>>>>> fd99639e40 (Fix subquery cloning and dependencies (#15039))

stmt := rewriteRemainingColumns(ctx, subq.Select, subqID)

Expand Down
2 changes: 1 addition & 1 deletion go/vt/vtgate/planbuilder/testdata/aggr_cases.json
Original file line number Diff line number Diff line change
Expand Up @@ -6386,7 +6386,7 @@
"Sharded": true
},
"FieldQuery": "select id, from_unixtime(min(col)) as col, min(col) from `user` where 1 != 1 group by id",
"OrderBy": "2 ASC",
"OrderBy": "2 ASC COLLATE utf8mb4_0900_ai_ci",
"Query": "select id, from_unixtime(min(col)) as col, min(col) from `user` group by id order by min(col) asc",
"ResultColumns": 2,
"Table": "`user`"
Expand Down
74 changes: 74 additions & 0 deletions go/vt/vtgate/planbuilder/testdata/select_cases.json
Original file line number Diff line number Diff line change
Expand Up @@ -2021,6 +2021,80 @@
}
},
{
<<<<<<< HEAD
=======
"comment": "select (select col from user limit 1) as a from user join user_extra order by a",
"query": "select (select col from user limit 1) as a from user join user_extra order by a",
"plan": {
"QueryType": "SELECT",
"Original": "select (select col from user limit 1) as a from user join user_extra order by a",
"Instructions": {
"OperatorType": "Join",
"Variant": "Join",
"JoinColumnIndexes": "L:0",
"TableName": "`user`_user_extra",
"Inputs": [
{
"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 :__upper_limit",
"Table": "`user`"
}
]
},
{
"InputName": "Outer",
"OperatorType": "Route",
"Variant": "Scatter",
"Keyspace": {
"Name": "user",
"Sharded": true
},
"FieldQuery": "select :__sq1 as __sq1, weight_string(:__sq1) from `user` where 1 != 1",
"OrderBy": "(0|1) ASC",
"Query": "select :__sq1 as __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"
}
]
},
"TablesUsed": [
"user.user",
"user.user_extra"
]
}
},
{
>>>>>>> fd99639e40 (Fix subquery cloning and dependencies (#15039))
"comment": "select t.a from (select (select col from user limit 1) as a from user join user_extra) t",
"query": "select t.a from (select (select col from user limit 1) as a from user join user_extra) t",
"plan": {
Expand Down
10 changes: 10 additions & 0 deletions go/vt/vtgate/semantics/semantic_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -592,3 +592,13 @@ func (st *SemTable) ASTEquals() *sqlparser.Comparator {
}
return st.comparator
}

func (st *SemTable) Clone(n sqlparser.SQLNode) sqlparser.SQLNode {
return sqlparser.CopyOnRewrite(n, nil, func(cursor *sqlparser.CopyOnWriteCursor) {
expr, isExpr := cursor.Node().(sqlparser.Expr)
if !isExpr {
return
}
cursor.Replace(sqlparser.CloneExpr(expr))
}, st.CopySemanticInfo)
}
Loading