Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions compiler/optimizer/optimizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
82 changes: 82 additions & 0 deletions compiler/optimizer/outerjoin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
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 other than none
// 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 {
lit, ok := other.(*dag.PrimitiveExpr)
return op == "==" && ok && lit.Value != "none"
}
}
return true
}
6 changes: 3 additions & 3 deletions compiler/ztests/join-filter-outer.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
59 changes: 59 additions & 0 deletions compiler/ztests/join-outer-to-inner.yaml
Original file line number Diff line number Diff line change
@@ -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
Loading