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

    https://github.com/apache/spark/pull/17520#discussion_r109521143
  
    --- Diff: 
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/Optimizer.scala
 ---
    @@ -823,9 +890,61 @@ object PushDownPredicate extends Rule[LogicalPlan] 
with PredicateHelper {
             filter
           }
     
    +    // Similar to the above Filter over Aggregate
    +    // LeftSemi/LeftAnti over Aggregate
    +    case join @ Join(aggregate: Aggregate, rightOp, 
LeftSemiOrAnti(joinType), joinCond) =>
    +      if (joinCond.isEmpty) {
    +        // No join condition, just push down Join below Aggregate
    +        aggregate.copy(child = Join(aggregate.child, rightOp, joinType, 
joinCond))
    +      } else {
    +        // Find all the aliased expressions in the aggregate list that 
don't include any actual
    +        // AggregateExpression, and create a map from the alias to the 
expression
    +        lazy val aliasMap = 
AttributeMap(aggregate.aggregateExpressions.collect {
    +          case a: Alias if 
a.child.find(_.isInstanceOf[AggregateExpression]).isEmpty =>
    +            (a.toAttribute, a.child)
    +        })
    +
    +        // For each join condition, expand the alias and
    +        // check if the condition can be evaluated using
    +        // attributes produced by the aggregate operator's child operator.
    +        val (candidates, containingNonDeterministic) =
    +          splitConjunctivePredicates(joinCond.get).span(_.deterministic)
    +
    +        val (pushDown, rest) = candidates.partition { cond =>
    +          val replaced = replaceAlias(cond, aliasMap)
    +          cond.references.nonEmpty &&
    +            replaced.references.subsetOf(aggregate.child.outputSet ++ 
rightOp.outputSet) &&
    +            !SubqueryExpression.hasCorrelatedSubquery(cond) &&
    +            !SubExprUtils.containsOuter(cond)
    +        }
    +
    +        val stayUp = rest ++ containingNonDeterministic
    +
    +        if (pushDown.nonEmpty) {
    +          val pushDownPredicate = pushDown.reduce(And)
    +          val replaced = replaceAlias(pushDownPredicate, aliasMap)
    +          val newAggregate = aggregate.copy(child =
    +            Join(aggregate.child, rightOp, joinType, Option(replaced)))
    +          // If there is no more filter to stay up, just return the 
Aggregate over Join.
    +          // Otherwise, create "Filter(stayUp) <- Aggregate <- 
Join(pushDownPredicate)".
    +          if (stayUp.isEmpty) newAggregate else Filter(stayUp.reduce(And), 
newAggregate)
    +        } else {
    +          // The join condition is not a subset of the Aggregate's GROUP 
BY columns,
    +          // no push down.
    +          join
    +        }
    +      }
    +
    --- End diff --
    
    [To reviewers] Should we separate this into a new rule?
    
    This code is added to preserver the existing behaviour when subquery is in 
the form of a predicate. A point worth making here is pushing down EXISTS/IN 
subquery is not always a winner. If the processing in the subquery is expensive 
such as scanning a very large table and the filtering effect is marginal, 
performing the join below the aggregate may not save much of the aggregate. If 
the aggregate can reduce more because of the small number of distinct values on 
the group by columns, not pushing down the subquery is actually a better plan.


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