maropu commented on a change in pull request #30018:
URL: https://github.com/apache/spark/pull/30018#discussion_r509152188



##########
File path: 
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/Optimizer.scala
##########
@@ -475,6 +476,58 @@ object RemoveRedundantAliases extends Rule[LogicalPlan] {
   def apply(plan: LogicalPlan): LogicalPlan = removeRedundantAliases(plan, 
AttributeSet.empty)
 }
 
+/**
+ * Remove redundant aggregates from a query plan. A redundant aggregate is an 
aggregate whose
+ * only goal is to keep distinct values, while its parent aggregate would 
ignore duplicate values.
+ */
+object RemoveRedundantAggregates extends Rule[LogicalPlan] with AliasHelper {
+  def apply(plan: LogicalPlan): LogicalPlan = plan transformUp {
+    case upper @ Aggregate(_, _, lower: Aggregate) if lowerIsRedundant(upper, 
lower) =>
+      val aliasMap = getAliasMap(lower)
+      upper.copy(
+        child = lower.child,
+        groupingExpressions = upper.groupingExpressions.map(replaceAlias(_, 
aliasMap)),
+        aggregateExpressions = upper.aggregateExpressions.map(
+          replaceAliasButKeepName(_, aliasMap))
+      )
+  }
+
+  private def lowerIsRedundant(upper: Aggregate, lower: Aggregate): Boolean = {
+    val isDeterministic = upper.aggregateExpressions.forall(_.deterministic) &&
+      lower.aggregateExpressions.forall(_.deterministic)
+
+    val upperReferencesOnlyGrouping = upper.references.subsetOf(AttributeSet(
+      lower.aggregateExpressions.filter(!isAggregate(_)).map(_.toAttribute)))
+
+    val upperHasNoAggregateExpressions = upper.aggregateExpressions
+      .forall(_.find(isAggregate).isEmpty)
+
+    isDeterministic && upperReferencesOnlyGrouping && 
upperHasNoAggregateExpressions
+  }
+
+  private def isAggregate(expr: Expression): Boolean = {
+    expr.find(e => e.isInstanceOf[AggregateExpression] ||
+      PythonUDF.isGroupedAggPandasUDF(e)).isDefined
+  }
+
+  /**
+   * Replace all attributes, that reference an alias, with the aliased 
expression,
+   * but keep the name of the outmost attribute.
+   */
+  private def replaceAliasButKeepName(

Review comment:
       Since this function is only used in the single place, could you inline 
it?
   ```
       case upper @ Aggregate(_, _, lower: Aggregate) if 
lowerIsRedundant(upper, lower) =>
         val aliasMap = getAliasMap(lower)
   
          // Replace all attributes, that reference an alias, with the aliased 
expression,
          // but keep the name of the outmost attribute.
         val newAggExprs = upper.aggregateExpressions.map {
           case a: Attribute if aliasMap.contains(a) =>
             Alias(replaceAlias(a, aliasMap), a.name)(a.exprId, a.qualifier)
           case expr =>
             replaceAlias(expr, aliasMap).asInstanceOf[NamedExpression]
         }
   
         upper.copy(
           child = lower.child,
           groupingExpressions = upper.groupingExpressions.map(replaceAlias(_, 
aliasMap)),
           aggregateExpressions = newAggExprs)
   ```
   




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
[email protected]



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to