From f027bed31a3bf112723e84dab7fd9d7ee10dd47d Mon Sep 17 00:00:00 2001 From: Andres Taylor Date: Fri, 22 Mar 2024 14:36:24 +0100 Subject: [PATCH] bugfix: fix handling of complex join predicates Signed-off-by: Andres Taylor --- .../planbuilder/operators/apply_join.go | 12 ++++++---- .../planbuilder/testdata/select_cases.json | 24 +++++++++++++++++++ 2 files changed, 32 insertions(+), 4 deletions(-) diff --git a/go/vt/vtgate/planbuilder/operators/apply_join.go b/go/vt/vtgate/planbuilder/operators/apply_join.go index 0bf03a1eb26..043e76b5e15 100644 --- a/go/vt/vtgate/planbuilder/operators/apply_join.go +++ b/go/vt/vtgate/planbuilder/operators/apply_join.go @@ -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 } diff --git a/go/vt/vtgate/planbuilder/testdata/select_cases.json b/go/vt/vtgate/planbuilder/testdata/select_cases.json index 63fba06202c..da96cbec0ec 100644 --- a/go/vt/vtgate/planbuilder/testdata/select_cases.json +++ b/go/vt/vtgate/planbuilder/testdata/select_cases.json @@ -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",