From 7cd901bfa897e213ed6483bc3f658c4f2a874f8d Mon Sep 17 00:00:00 2001 From: Steven McCanne Date: Mon, 23 Nov 2026 04:32:38 -0800 Subject: [PATCH 1/2] optimize outer to inner join to allow filter pushdown MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Turn an outer join into an inner join when the filter after it can never be true for an unmatched value (e.g., LEFT JOIN p … WHERE p.color='red'), so the filter is pushed down again as it was before #7703. Filters that can be true for unmatched values, like IS NULL or OR, keep the outer join, and paths read with ?. convert only for == since none compares unequal to other values. Adds compiler/ztests/join-outer-to-inner.yaml. --- compiler/optimizer/optimizer.go | 1 + compiler/optimizer/outerjoin.go | 81 ++++++++++++++++++++++++ compiler/ztests/join-filter-outer.yaml | 6 +- compiler/ztests/join-outer-to-inner.yaml | 59 +++++++++++++++++ 4 files changed, 144 insertions(+), 3 deletions(-) create mode 100644 compiler/optimizer/outerjoin.go create mode 100644 compiler/ztests/join-outer-to-inner.yaml diff --git a/compiler/optimizer/optimizer.go b/compiler/optimizer/optimizer.go index e94fc803c..adcea1f88 100644 --- a/compiler/optimizer/optimizer.go +++ b/compiler/optimizer/optimizer.go @@ -141,6 +141,7 @@ func (o *Optimizer) optimizeSeq(seq dag.Seq) (dag.Seq, error) { seq = mergeFilters(seq) seq = mergeValuesOps(seq) inlineRecordExprSpreads(seq) + seq = convertOuterJoins(seq) seq = liftFiltersIntoJoins(seq) replaceJoinWithHashJoin(seq) seq = joinFilterPullup(seq) diff --git a/compiler/optimizer/outerjoin.go b/compiler/optimizer/outerjoin.go new file mode 100644 index 000000000..a8e6dd020 --- /dev/null +++ b/compiler/optimizer/outerjoin.go @@ -0,0 +1,81 @@ +package optimizer + +import "github.com/superdb/super/compiler/dag" + +// convertOuterJoins turns an outer join into an inner join when the filter +// that follows it can never be true for a value the join leaves unmatched. +// Such a filter removes every unmatched value anyway, so the result is the +// same, and the filter can then be pushed into the join's inputs or lifted +// into its condition like any filter after an inner join. +func convertOuterJoins(seq dag.Seq) dag.Seq { + for i, op := range seq { + fork, ok := op.(*dag.ForkOp) + if !ok { + continue + } + for k := range fork.Paths { + fork.Paths[k] = convertOuterJoins(fork.Paths[k]) + } + if i+2 >= len(seq) || len(fork.Paths) != 2 { + continue + } + join, ok := seq[i+1].(*dag.JoinOp) + if !ok { + continue + } + filter, ok := seq[i+2].(*dag.FilterOp) + if !ok { + continue + } + switch join.Style { + case "left": + // An unmatched value of a left join has no right side. + if rejectsMissing(filter.Expr, join.RightAlias) { + join.Style = "inner" + } + case "right": + if rejectsMissing(filter.Expr, join.LeftAlias) { + join.Style = "inner" + } + } + } + return seq +} + +// rejectsMissing reports whether predicate e is never true when field alias +// is missing, i.e., e has a conjunct comparing a path under alias. When an +// operand of a comparison is null (as for a missing column in SQL) or an error +// (as for a missing field), the comparison is null or an error, which a filter +// treats as false. A path read with ?. is none instead, and none is unequal +// to every other value, so only equality with a literal rejects it. +func rejectsMissing(e dag.Expr, alias string) bool { + for _, conjunct := range splitPredicate(e) { + b, ok := conjunct.(*dag.BinaryExpr) + if !ok { + continue + } + switch b.Op { + case "==", "!=", "<", "<=", ">", ">=": + if comparesMissing(b.Op, b.LHS, b.RHS, alias) || comparesMissing(b.Op, b.RHS, b.LHS, alias) { + return true + } + } + } + return false +} + +// comparesMissing reports whether comparison "path op other" is never true +// when field alias is missing, where path is a path under alias. +func comparesMissing(op string, path, other dag.Expr, alias string) bool { + this, ok := path.(*dag.ThisExpr) + if !ok || len(this.Chain) == 0 || this.Chain[0].ID != alias { + return false + } + for _, elem := range this.Chain { + if elem.Noneish { + _, isLiteral := other.(*dag.PrimitiveExpr) + return op == "==" && isLiteral + } + } + return true +} diff --git a/compiler/ztests/join-filter-outer.yaml b/compiler/ztests/join-filter-outer.yaml index 2c90c4c75..97425a306 100644 --- a/compiler/ztests/join-filter-outer.yaml +++ b/compiler/ztests/join-filter-outer.yaml @@ -9,7 +9,7 @@ script: | 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" + 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 IS NULL OR p.k=9) AND s.k=1" inputs: - name: s.sup @@ -43,9 +43,9 @@ outputs: | where this!.k==1 ) ( - file p.sup format sup type {id:int64,k:int64,color:string} unordered project [color,id] + file p.sup format sup type {id:int64,k:int64,color:string} unordered project [color,id,k] ) | left hashjoin as {left,right} on this!.id==this!.id - | where this!.right!.color=="red" + | where this!.right!.color IS NULL or this!.right!.k==9 | values {id:this!.left!.id} | output main diff --git a/compiler/ztests/join-outer-to-inner.yaml b/compiler/ztests/join-outer-to-inner.yaml new file mode 100644 index 000000000..78982d07e --- /dev/null +++ b/compiler/ztests/join-outer-to-inner.yaml @@ -0,0 +1,59 @@ +# An outer join followed by a filter that is never true for an unmatched +# value, e.g., a comparison on the side that would be missing, becomes an +# inner join so the filter can be pushed down. Filters that can be true for +# an unmatched value, like IS NULL, leave the outer join alone. +script: | + for where in "p.color='red'" "p.color!='red'" "s.k=p.k" "p.color IS NULL" "p.color='red' OR s.k=3"; do + echo "// $where" + super -s -c "SELECT s.id FROM 's.sup' s LEFT JOIN 'p.sup' p ON s.id=p.id WHERE $where ORDER BY s.id" + done + echo "// right join" + 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'" + +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: | + // p.color='red' + {id:1} + // p.color!='red' + {id:2} + // s.k=p.k + {id:1} + // p.color IS NULL + {id:3} + // p.color='red' OR s.k=3 + {id:1} + {id:3} + // right join + {id:1} + === + fork + ( + file s.sup format sup type {id:int64,k:int64} unordered project [id] + ) + ( + file p.sup format sup type {id:int64,k:int64,color:string} unordered project [color,id] + meta-filter project [color.max,color.min] ( + compare("red", this!.color!.min, true)>=0 and compare("red", this!.color!.max, true)<=0 + ) + data-filter project [color] ( + this!.color=="red" + ) + | where this!.color=="red" + ) + | inner hashjoin as {left,right} on this!.id==this!.id + | values {id:this!.left!.id} + | output main From 80402164a3521d7b5d6d3411eb904a50561fb4f5 Mon Sep 17 00:00:00 2001 From: Steven McCanne Date: Mon, 23 Nov 2026 04:41:55 -0800 Subject: [PATCH 2/2] deep review --- compiler/optimizer/outerjoin.go | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/compiler/optimizer/outerjoin.go b/compiler/optimizer/outerjoin.go index a8e6dd020..56c7dbdac 100644 --- a/compiler/optimizer/outerjoin.go +++ b/compiler/optimizer/outerjoin.go @@ -47,7 +47,8 @@ func convertOuterJoins(seq dag.Seq) dag.Seq { // operand of a comparison is null (as for a missing column in SQL) or an error // (as for a missing field), the comparison is null or an error, which a filter // treats as false. A path read with ?. is none instead, and none is unequal -// to every other value, so only equality with a literal rejects it. +// to every other value, so only equality with a literal other than none +// rejects it. func rejectsMissing(e dag.Expr, alias string) bool { for _, conjunct := range splitPredicate(e) { b, ok := conjunct.(*dag.BinaryExpr) @@ -73,8 +74,8 @@ func comparesMissing(op string, path, other dag.Expr, alias string) bool { } for _, elem := range this.Chain { if elem.Noneish { - _, isLiteral := other.(*dag.PrimitiveExpr) - return op == "==" && isLiteral + lit, ok := other.(*dag.PrimitiveExpr) + return op == "==" && ok && lit.Value != "none" } } return true