Skip to content

Commit

Permalink
bugfix: handling of ANDed join predicates (vitessio#15551)
Browse files Browse the repository at this point in the history
Signed-off-by: Andres Taylor <[email protected]>
  • Loading branch information
systay authored Mar 23, 2024
1 parent a06cf7d commit 8a6fe42
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 4 deletions.
11 changes: 8 additions & 3 deletions go/vt/vtgate/planbuilder/operators/apply_join.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,9 +156,14 @@ func (aj *ApplyJoin) AddJoinPredicate(ctx *plancontext.PlanningContext, expr sql
if expr == nil {
return
}
col := breakExpressionInLHSandRHSForApplyJoin(ctx, expr, TableID(aj.LHS))
aj.JoinPredicates.add(col)
rhs := aj.RHS.AddPredicate(ctx, col.RHSExpr)
rhs := aj.RHS
predicates := sqlparser.SplitAndExpression(nil, expr)
for _, pred := range predicates {
col := breakExpressionInLHSandRHSForApplyJoin(ctx, pred, TableID(aj.LHS))
aj.JoinPredicates.add(col)
ctx.AddJoinPredicates(pred, col.RHSExpr)
rhs = rhs.AddPredicate(ctx, col.RHSExpr)
}
aj.RHS = rhs
}

Expand Down
1 change: 0 additions & 1 deletion go/vt/vtgate/planbuilder/operators/expressions.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ func breakExpressionInLHSandRHSForApplyJoin(
cursor.Replace(arg)
}, nil).(sqlparser.Expr)

ctx.AddJoinPredicates(expr, rewrittenExpr)
col.RHSExpr = rewrittenExpr
col.Original = expr
return
Expand Down
24 changes: 24 additions & 0 deletions go/vt/vtgate/planbuilder/testdata/select_cases.json
Original file line number Diff line number Diff line change
Expand Up @@ -2658,6 +2658,30 @@
]
}
},
{
"comment": "Complex join with multiple conditions merged into single route",
"query": "select 0 from user as u join user_extra as s on u.id = s.user_id join music as m on m.user_id = u.id and (s.foo or m.bar)",
"plan": {
"QueryType": "SELECT",
"Original": "select 0 from user as u join user_extra as s on u.id = s.user_id join music as m on m.user_id = u.id and (s.foo or m.bar)",
"Instructions": {
"OperatorType": "Route",
"Variant": "Scatter",
"Keyspace": {
"Name": "user",
"Sharded": true
},
"FieldQuery": "select 0 from `user` as u, user_extra as s, music as m where 1 != 1",
"Query": "select 0 from `user` as u, user_extra as s, music as m where u.id = s.user_id and m.user_id = u.id and (s.foo or m.bar)",
"Table": "`user`, music, user_extra"
},
"TablesUsed": [
"user.music",
"user.user",
"user.user_extra"
]
}
},
{
"comment": "union as a derived table",
"query": "select found from (select id as found from user union all (select id from unsharded)) as t",
Expand Down

0 comments on commit 8a6fe42

Please sign in to comment.