From b9cbb26600f93cf50bed96bf9a59816c782f8370 Mon Sep 17 00:00:00 2001 From: Steven McCanne Date: Sun, 22 Nov 2026 15:24:39 -0800 Subject: [PATCH 1/2] fix join bug MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fix filters after outer joins being pushed into the side the join preserves (or lifted into the join condition), which turned filtered-out values into unmatched values and returned extra rows, e.g., LEFT JOIN … WHERE p.color='red' returned every left row. Inner joins are unchanged. Adds compiler/ztests/join-filter-outer.yaml. --- compiler/optimizer/join.go | 10 ++++++++-- compiler/optimizer/optimizer.go | 35 +++++++++++++++++++++++++-------- 2 files changed, 35 insertions(+), 10 deletions(-) diff --git a/compiler/optimizer/join.go b/compiler/optimizer/join.go index 9873d8daaa..f8ac613bb3 100644 --- a/compiler/optimizer/join.go +++ b/compiler/optimizer/join.go @@ -52,18 +52,24 @@ func liftFilterIntoJoin(seq dag.Seq, lhs, rhs dag.Expr) bool { if !lok || !rok { return false } + pushLeft, pushRight := pushableSides(join.Style) if lhsFirst == rhsFirst { lhs, rhs = dag.CopyExpr(lhs), dag.CopyExpr(rhs) stripFirstThisPathComponent(lhs) stripFirstThisPathComponent(rhs) - if lhsFirst == join.LeftAlias { + if lhsFirst == join.LeftAlias && pushLeft { return liftFilterIntoJoin(fork.Paths[0], lhs, rhs) } - if lhsFirst == join.RightAlias { + if lhsFirst == join.RightAlias && pushRight { return liftFilterIntoJoin(fork.Paths[1], lhs, rhs) } return false } + if !pushLeft || !pushRight { + // Moving the predicate into the condition of an outer or anti join + // would turn the values it removes into unmatched values. + return false + } if lhsFirst != join.LeftAlias { lhsFirst, rhsFirst = rhsFirst, lhsFirst lhs, rhs = rhs, lhs diff --git a/compiler/optimizer/optimizer.go b/compiler/optimizer/optimizer.go index ff84da8ce3..e94fc803ca 100644 --- a/compiler/optimizer/optimizer.go +++ b/compiler/optimizer/optimizer.go @@ -528,7 +528,7 @@ func joinFilterPullup(seq dag.Seq) dag.Seq { seq = mergeFilters(seq) for i := 0; i <= len(seq)-3; i++ { fork, isfork := seq[i].(*dag.ForkOp) - leftAlias, rightAlias, isjoin := isJoin(seq[i+1]) + style, leftAlias, rightAlias, isjoin := isJoin(seq[i+1]) filter, isfilter := seq[i+2].(*dag.FilterOp) if !isfork || !isjoin || !isfilter { continue @@ -536,13 +536,14 @@ func joinFilterPullup(seq dag.Seq) dag.Seq { if len(fork.Paths) != 2 { panic(seq[i]) } + pushLeft, pushRight := pushableSides(style) var remaining []dag.Expr for _, e := range splitPredicate(filter.Expr) { - if pullup, ok := pullupExpr(leftAlias, e); ok { + if pullup, ok := pullupExpr(leftAlias, e); ok && pushLeft { fork.Paths[0] = append(fork.Paths[0], dag.NewFilterOp(pullup)) continue } - if pullup, ok := pullupExpr(rightAlias, e); ok { + if pullup, ok := pullupExpr(rightAlias, e); ok && pushRight { fork.Paths[1] = append(fork.Paths[1], dag.NewFilterOp(pullup)) continue } @@ -560,15 +561,33 @@ func joinFilterPullup(seq dag.Seq) dag.Seq { return seq } -func isJoin(op dag.Op) (string, string, bool) { +func isJoin(op dag.Op) (string, string, string, bool) { switch op := op.(type) { case *dag.HashJoinOp: - return op.LeftAlias, op.RightAlias, true + return op.Style, op.LeftAlias, op.RightAlias, true case *dag.JoinOp: - return op.LeftAlias, op.RightAlias, true + return op.Style, op.LeftAlias, op.RightAlias, true default: - return "", "", false - } + return "", "", "", false + } +} + +// pushableSides reports whether a filter on the left or right side of a join +// of the given style may be applied to that side's input before the join. +// It may not for the side that an outer join preserves when unmatched, i.e., +// the right side of a left join, the left side of a right join, and the right +// side of an anti join, since filtering that input would turn values that +// the filter removes into unmatched values instead. +func pushableSides(style string) (left, right bool) { + switch style { + case "inner", "cross", "": + return true, true + case "left", "anti": + return true, false + case "right": + return false, true + } + return false, false } func buildConjunction(exprs []dag.Expr) dag.Expr { From 02dd6f799e93ec7787703b69be8360e2cb573c9f Mon Sep 17 00:00:00 2001 From: Steven McCanne Date: Sun, 22 Nov 2026 16:30:46 -0800 Subject: [PATCH 2/2] deep review - add test --- compiler/ztests/join-filter-outer.yaml | 51 ++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 compiler/ztests/join-filter-outer.yaml diff --git a/compiler/ztests/join-filter-outer.yaml b/compiler/ztests/join-filter-outer.yaml new file mode 100644 index 0000000000..2c90c4c75e --- /dev/null +++ b/compiler/ztests/join-filter-outer.yaml @@ -0,0 +1,51 @@ +# A filter after an outer join can't be pushed into the input that the join +# preserves when unmatched, nor lifted into the join condition, since either +# would turn the values the filter removes into unmatched values. A filter on +# the other side is still pushed down. +script: | + super -s -c "SELECT s.id FROM 's.sup' s LEFT JOIN 'p.sup' p ON s.id=p.id WHERE p.color='red'" + echo === + super -s -c "SELECT s.id FROM 's.sup' s LEFT JOIN 'p.sup' p ON s.id=p.id WHERE s.k=p.k" + echo === + super -s -c "SELECT p.id FROM 's.sup' s RIGHT JOIN 'p.sup' p ON s.id=p.id WHERE s.k=1" + echo === + super compile -C -O -c "SELECT s.id FROM 's.sup' s LEFT JOIN 'p.sup' p ON s.id=p.id WHERE p.color='red' AND s.k=1" + +inputs: + - name: s.sup + data: | + {id:1,k:1} + {id:2,k:2} + {id:3,k:3} + - name: p.sup + data: | + {id:1,k:1,color:"red"} + {id:2,k:9,color:"blue"} + +outputs: + - name: stdout + data: | + {id:1} + === + {id:1} + === + {id:1} + === + fork + ( + file s.sup format sup type {id:int64,k:int64} unordered project [id,k] + meta-filter project [k.max,k.min] ( + compare(1, this!.k!.min, true)>=0 and compare(1, this!.k!.max, true)<=0 + ) + data-filter project [k] ( + this!.k==1 + ) + | where this!.k==1 + ) + ( + file p.sup format sup type {id:int64,k:int64,color:string} unordered project [color,id] + ) + | left hashjoin as {left,right} on this!.id==this!.id + | where this!.right!.color=="red" + | values {id:this!.left!.id} + | output main