Skip to content

Commit

Permalink
bugfix: fix handling of complex join predicates
Browse files Browse the repository at this point in the history
Signed-off-by: Andres Taylor <[email protected]>
  • Loading branch information
systay committed Mar 22, 2024
1 parent f605f62 commit f027bed
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 4 deletions.
12 changes: 8 additions & 4 deletions go/vt/vtgate/planbuilder/operators/apply_join.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,10 +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)
ctx.AddJoinPredicates(expr, col.RHSExpr)
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
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 f027bed

Please sign in to comment.