cloud-fan commented on a change in pull request #29626:
URL: https://github.com/apache/spark/pull/29626#discussion_r484971289



##########
File path: 
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/RewriteDistinctAggregates.scala
##########
@@ -293,12 +295,16 @@ object RewriteDistinctAggregates extends 
Rule[LogicalPlan] {
           val operators = expressions.map { e =>
             val af = e.aggregateFunction
             val naf = patchAggregateFunctionChildren(af) { x =>
-              val condition = if (e.filter.isDefined) {
-                e.filter.map(distinctAggFilterAttrLookup.get(_)).get
+              val condition = 
e.filter.map(distinctAggFilterAttrLookup.get(_)).flatten
+              if (distinctAggGroupLookup(e).contains(x)) {

Review comment:
       I'm wondering if we can simplify the logic a bit. The goal is to only do 
the replacement for the first child if all the children are foldable. How about
   ```
   val af = e.aggregateFunction
   val condition = e.filter.map(distinctAggFilterAttrLookup.get(_)).flatten
   val naf = if (af.children.forall(_.foldable)) {
     val firstChild = evalWithinGroup(id, af.children.head, condition)
     af.withNewChildren(firstChild +: 
af.children.drop(1)).asInstanceOf[AggregateFunction]
   } else {
     patchAggregateFunctionChildren(af) { x =>
       distinctAggChildAttrLookup.get(x).map(evalWithinGroup(id, _, condition))
     }
   }
   ```
   
   Then we don't need to change a lot of code to create `distinctAggGroupLookup`




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