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
10 changes: 8 additions & 2 deletions compiler/optimizer/join.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
35 changes: 27 additions & 8 deletions compiler/optimizer/optimizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -528,21 +528,22 @@ 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
}
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
}
Expand All @@ -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 {
Expand Down
51 changes: 51 additions & 0 deletions compiler/ztests/join-filter-outer.yaml
Original file line number Diff line number Diff line change
@@ -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
Loading