Github user nsyca commented on a diff in the pull request:

    https://github.com/apache/spark/pull/17520#discussion_r109521220
  
    --- Diff: 
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/Optimizer.scala
 ---
    @@ -848,11 +967,84 @@ object PushDownPredicate extends Rule[LogicalPlan] 
with PredicateHelper {
             filter
           }
     
    +    // Similar to the above Filter over Union
    +    // LeftSemi/LeftAnti over Union
    +    case join @ Join(union: Union, rightOp, LeftSemiOrAnti(joinType), 
joinCond) =>
    +      if (joinCond.isEmpty) {
    +        // Push down the Join below Union
    +        val newGrandChildren = union.children.map { grandchild =>
    +          Join(grandchild, rightOp, joinType, joinCond)
    +        }
    +        union.withNewChildren(newGrandChildren)
    +      } else {
    +        // Union could change the rows, so non-deterministic predicate 
can't be pushed down
    +        val (candidates, containingNonDeterministic) =
    +          splitConjunctivePredicates(joinCond.get).span(_.deterministic)
    +
    +        val (pushDown, rest) = candidates.partition { cond =>
    +          !SubqueryExpression.hasCorrelatedSubquery(cond) &&
    +            !SubExprUtils.containsOuter(cond)
    +        }
    +        val stayUp = rest ++ containingNonDeterministic
    +
    +        if (pushDown.nonEmpty) {
    +          val pushDownCond = pushDown.reduceLeft(And)
    +          val output = union.output
    +          val newGrandChildren = union.children.map { grandchild =>
    +            val newCond = pushDownCond transform {
    +              case e if output.exists(_.semanticEquals(e)) =>
    +                grandchild.output(output.indexWhere(_.semanticEquals(e)))
    +            }
    +            assert(newCond.references.subsetOf(grandchild.outputSet ++ 
rightOp.outputSet))
    +            Join(grandchild, rightOp, joinType, Option(newCond))
    +          }
    +          val newUnion = union.withNewChildren(newGrandChildren)
    +          if (stayUp.isEmpty) newUnion else Filter(stayUp.reduceLeft(And), 
newUnion)
    +        } else {
    +          // Nothing to push down
    +          join
    +        }
    +      }
    +
    --- End diff --
    
    [To reviewers] Should we separate this into a new rule?


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org
For additional commands, e-mail: reviews-h...@spark.apache.org

Reply via email to