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

    https://github.com/apache/spark/pull/17520#discussion_r109522050
  
    --- Diff: 
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/Optimizer.scala
 ---
    @@ -792,6 +824,39 @@ object PushDownPredicate extends Rule[LogicalPlan] 
with PredicateHelper {
             filter
           }
     
    +    // Similar to the above Filter over Window
    +    // LeftSemi/LeftAnti over Window
    +    case join @ Join(w: Window, rightOp, LeftSemiOrAnti(joinType), 
joinCond)
    +      if w.partitionSpec.forall(_.isInstanceOf[AttributeReference]) =>
    +      if (joinCond.isEmpty) {
    +        // No join condition, just push down Join below Window
    +        w.copy(child = Join(w.child, rightOp, joinType, joinCond))
    +      } else {
    +        val partitionAttrs = 
AttributeSet(w.partitionSpec.flatMap(_.references)) ++
    +          rightOp.outputSet
    +
    +        val (candidates, containingNonDeterministic) =
    +          splitConjunctivePredicates(joinCond.get).span(_.deterministic)
    +
    +        val (pushDown, rest) = candidates.partition { cond =>
    +          cond.references.subsetOf(partitionAttrs) &&
    +            !SubqueryExpression.hasCorrelatedSubquery(cond) &&
    +            !SubExprUtils.containsOuter(cond)
    +        }
    +
    +        val stayUp = rest ++ containingNonDeterministic
    +
    +        if (pushDown.nonEmpty) {
    +          val pushDownPredicate = pushDown.reduce(And)
    +          val newPlan = w.copy(child = Join(w.child, rightOp, joinType, 
Option(pushDownPredicate)))
    +          if (stayUp.isEmpty) newPlan else Filter(stayUp.reduce(And), 
newPlan)
    +        } else {
    +          // The join condition is not a subset of the Window's PARTITION 
BY clause,
    +          // no push down.
    +          join
    +        }
    +      }
    +
    --- End diff --
    
    Unlike the cases of Aggregate and Union (see the comments below), Window 
never reduces the cardinality of the input stream, hence pushing down the 
LeftSemi/LeftAnti join (which guarantees by their semantics that it never 
expands the cardinality of their parent table) is safer.


---
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