diff --git a/go/vt/vtgate/planbuilder/operators/apply_join.go b/go/vt/vtgate/planbuilder/operators/apply_join.go index 23dc63eccc6..e18169c28b1 100644 --- a/go/vt/vtgate/planbuilder/operators/apply_join.go +++ b/go/vt/vtgate/planbuilder/operators/apply_join.go @@ -163,9 +163,7 @@ func (aj *ApplyJoin) AddJoinPredicate(ctx *plancontext.PlanningContext, expr sql col := breakExpressionInLHSandRHSForApplyJoin(ctx, pred, TableID(aj.LHS)) aj.JoinPredicates.add(col) ctx.AddJoinPredicates(pred, col.RHSExpr) - ctx.JoinPredInProgress = pred rhs = rhs.AddPredicate(ctx, col.RHSExpr) - ctx.JoinPredInProgress = nil } aj.RHS = rhs } diff --git a/go/vt/vtgate/planbuilder/plancontext/planning_context.go b/go/vt/vtgate/planbuilder/plancontext/planning_context.go index e8408de77c3..4874350d8ae 100644 --- a/go/vt/vtgate/planbuilder/plancontext/planning_context.go +++ b/go/vt/vtgate/planbuilder/plancontext/planning_context.go @@ -28,9 +28,6 @@ type PlanningContext struct { SemTable *semantics.SemTable VSchema VSchema - // JoinPredInProgress is used to track the current join predicate being processed. - JoinPredInProgress sqlparser.Expr - // joinPredicates maps each original join predicate (key) to a slice of // variations of the RHS predicates (value). This map is used to handle // different scenarios in join planning, where the RHS predicates are @@ -194,8 +191,12 @@ func (ctx *PlanningContext) execOnJoinPredicateEqual(joinPred sqlparser.Expr, fn func (ctx *PlanningContext) RewriteDerivedTableExpression(expr sqlparser.Expr, tableInfo semantics.TableInfo) sqlparser.Expr { modifiedExpr := semantics.RewriteDerivedTableExpression(expr, tableInfo) - if ctx.JoinPredInProgress != nil { - ctx.AddJoinPredicates(ctx.JoinPredInProgress, modifiedExpr) + for key, expr := range ctx.joinPredicates { + for _, rhsExpr := range expr { + if ctx.SemTable.EqualsExpr(rhsExpr, rhsExpr) { + ctx.joinPredicates[key] = append(ctx.joinPredicates[key], modifiedExpr) + } + } } return modifiedExpr }