beliefer commented on a change in pull request #27058: [SPARK-30395][SQL] When one or more DISTINCT aggregate expressions operate on the same field, the DISTINCT aggregate expression allows the use of the FILTER clause URL: https://github.com/apache/spark/pull/27058#discussion_r364545405
########## File path: sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/RewriteDistinctAggregates.scala ########## @@ -317,7 +363,74 @@ object RewriteDistinctAggregates extends Rule[LogicalPlan] { } Aggregate(groupByAttrs, patchedAggExpressions, firstAggregate) } else { - a + val (distinctAggExpressions, regularAggExpressions) = aggExpressions.partition(_.isDistinct) + if (distinctAggExpressions.exists(_.filter.isDefined)) { + val regularAggExprs = regularAggExpressions.filter(e => e.children.exists(!_.foldable)) + val regularFunChildren = regularAggExprs + .flatMap(_.aggregateFunction.children.filter(!_.foldable)) + val regularFilterAttrs = regularAggExprs.flatMap(_.filterAttributes) + val regularAggChildren = (regularFunChildren ++ regularFilterAttrs).distinct + val regularAggChildAttrMap = regularAggChildren.map(expressionAttributePair) + val regularAggChildAttrLookup = regularAggChildAttrMap.toMap + val regularOperatorMap = regularAggExprs.map { + case ae @ AggregateExpression(af, _, _, filter, _) => + val newChildren = af.children.map(c => regularAggChildAttrLookup.getOrElse(c, c)) + val raf = af.withNewChildren(newChildren).asInstanceOf[AggregateFunction] + val filterOpt = filter.map(_.transform { + case a: Attribute => regularAggChildAttrLookup.getOrElse(a, a) + }) + val aggExpr = ae.copy(aggregateFunction = raf, filter = filterOpt) + (ae, aggExpr) + } + val distinctAggExprs = distinctAggExpressions.filter(e => e.children.exists(!_.foldable)) + val rewriteDistinctOperatorMap = distinctAggExprs.zipWithIndex.map { + case (ae @ AggregateExpression(af, _, _, filter, _), i) => + val phantomId = i + 1 + val unfoldableChildren = af.children.filter(!_.foldable) + val exprAttrs = unfoldableChildren.map { e => + (e, AttributeReference(s"phantom$phantomId-${e.sql}", e.dataType, nullable = true)()) + } + val exprAttrLookup = exprAttrs.toMap + val newChildren = af.children.map(c => exprAttrLookup.getOrElse(c, c)) + val raf = af.withNewChildren(newChildren).asInstanceOf[AggregateFunction] + val aggExpr = ae.copy(aggregateFunction = raf, filter = None) + // Expand projection + val projection = unfoldableChildren.map { + case e if filter.isDefined => If(filter.get, e, nullify(e)) + case e => e + } + (projection, exprAttrs, (ae, aggExpr)) + } + val rewriteDistinctAttrMap = rewriteDistinctOperatorMap.map(_._2).flatten + val distinctAggChildAttrs = rewriteDistinctAttrMap.map(_._2) + val allAggAttrs = (regularAggChildAttrMap.map(_._2) ++ distinctAggChildAttrs) + // Construct the aggregate input projection. + val rewriteDistinctProjections = rewriteDistinctOperatorMap.map(_._1).flatten Review comment: OK ---------------------------------------------------------------- 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: us...@infra.apache.org With regards, Apache Git Services --------------------------------------------------------------------- To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org For additional commands, e-mail: reviews-h...@spark.apache.org