beliefer commented on a change in pull request #26656: [SPARK-27986][SQL] 
Support ANSI SQL filter clause for aggregate expression
URL: https://github.com/apache/spark/pull/26656#discussion_r353636185
 
 

 ##########
 File path: 
sql/core/src/main/scala/org/apache/spark/sql/execution/aggregate/AggUtils.scala
 ##########
 @@ -135,19 +135,25 @@ object AggUtils {
     }
     val distinctAttributes = namedDistinctExpressions.map(_.toAttribute)
     val groupingAttributes = groupingExpressions.map(_.toAttribute)
+    val filterWithDistinctAttributes = 
functionsWithDistinct.flatMap(_.filterAttributes.toSeq)
 
     // 1. Create an Aggregate Operator for partial aggregations.
     val partialAggregate: SparkPlan = {
       val aggregateExpressions = functionsWithoutDistinct.map(_.copy(mode = 
Partial))
       val aggregateAttributes = aggregateExpressions.map(_.resultAttribute)
       // We will group by the original grouping expression, plus an additional 
expression for the
-      // DISTINCT column. For example, for AVG(DISTINCT value) GROUP BY key, 
the grouping
-      // expressions will be [key, value].
+      // DISTINCT column and the referred attributes in the FILTER clause 
associated with each
+      // aggregate function. For example:
+      // 1.for the AVG (DISTINCT value) GROUP BY key, the grouping expression 
will be [key, value];
+      // 2.for AVG (DISTINCT value) Filter (WHERE value2> 20) GROUP BY key, 
the grouping expression
+      // will be [key, value, value2].
 
 Review comment:
   I removed value2 from the grouping expressions and find PartialMerge-AGG-3 
cannot obtain the input value2. 
   I execute `select sum(distinct id) filter (where sex = 'man') from student;` 
will lead an error:
   ```
   Caused by: java.lang.RuntimeException: Couldn't find sex#8 in [cast(id#6 as 
bigint)#13L]
           at scala.sys.package$.error(package.scala:27)
           at 
org.apache.spark.sql.catalyst.expressions.BindReferences$$anonfun$bindReference$1$$anonfun$applyOrElse$1.apply(BoundAttribute.scala:85)
           at 
org.apache.spark.sql.catalyst.expressions.BindReferences$$anonfun$bindReference$1$$anonfun$applyOrElse$1.apply(BoundAttribute.scala:79)
           at 
org.apache.spark.sql.catalyst.errors.package$.attachTree(package.scala:52)
   ```
   I don't know why PartialMerge-AGG-3 cannot bind the value2 ?

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


With regards,
Apache Git Services

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

Reply via email to