Github user nsyca commented on a diff in the pull request:
https://github.com/apache/spark/pull/17520#discussion_r109519783
--- 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 --
[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 [email protected] or file a JIRA ticket
with INFRA.
---
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]