uros-b commented on code in PR #57629:
URL: https://github.com/apache/spark/pull/57629#discussion_r3689560277
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/InsertMapSortExpression.scala:
##########
@@ -52,32 +63,97 @@ object InsertMapSortInGroupingExpressions extends
Rule[LogicalPlan] {
plan transformUpWithNewOutput {
case agg @ Aggregate(groupingExprs, aggregateExpressions, child, hint) =>
- val exprToMapSort = new mutable.HashMap[Expression, NamedExpression]
- val newGroupingKeys = groupingExprs.map { expr =>
- val inserted = insertMapSortRecursively(expr)
- if (expr.ne(inserted)) {
- exprToMapSort.getOrElseUpdate(
- expr.canonicalized,
- Alias(inserted, "_groupingmapsort")()
- ).toAttribute
+ val distinctExpressions = if (normalizeDistinctAggregates) {
+ distinctAggregateChildren(aggregateExpressions)
+ } else {
+ Seq.empty
+ }
+ val expressionsToNormalize = groupingExprs ++ distinctExpressions
+ if (!expressionsToNormalize.exists(mapTypeExistsRecursively)) {
+ agg -> Nil
+ } else {
+ val groupingMapSortAliases =
insertMapSortInExpressions(groupingExprs)
+ val distinctExpressionAliases =
rawDistinctExpressionAliases(distinctExpressions)
+ val distinctMapSortAliases = insertMapSortInExpressions(
+ distinctExpressions,
+ groupingMapSortAliases,
+ distinctExpressionAliases)
+ val newGroupingKeys = groupingExprs.map { expr =>
+
groupingMapSortAliases.get(expr.canonicalized).map(_.toAttribute).getOrElse(expr)
+ }
+ val newAggregateExprs = aggregateExpressions.map {
+ case named if groupingMapSortAliases.contains(named.canonicalized)
=>
+ // If we replace the top-level named expr, then should add back
the original name
+
groupingMapSortAliases(named.canonicalized).toAttribute.withName(named.name)
+ case other =>
+ // This must be top-down so distinct arguments are normalized
before their children.
+ // Continuing downward also substitutes grouping aliases in
other arguments.
+ other.transformDown {
+ case ae: AggregateExpression if ae.isDistinct =>
+ ae.copy(aggregateFunction =
ae.aggregateFunction.withNewChildren(
+ ae.aggregateFunction.children.map { child =>
+ distinctMapSortAliases.get(child.canonicalized)
+ .map(_.toAttribute).getOrElse(child)
+ }).asInstanceOf[AggregateFunction])
+ case e =>
+
groupingMapSortAliases.get(e.canonicalized).map(_.toAttribute).getOrElse(e)
+ }.asInstanceOf[NamedExpression]
+ }
+ val distinctInput = if (distinctExpressionAliases.nonEmpty) {
+ Project(child.output ++ distinctExpressionAliases.values, child)
} else {
- expr
+ child
}
+ val newChild = Project(
+ child.output ++ (groupingMapSortAliases ++
distinctMapSortAliases).values,
+ distinctInput)
+ val newAgg = Aggregate(newGroupingKeys, newAggregateExprs, newChild,
hint)
+ newAgg -> agg.output.zip(newAgg.output)
}
- val newAggregateExprs = aggregateExpressions.map {
- case named if exprToMapSort.contains(named.canonicalized) =>
- // If we replace the top-level named expr, then should add back
the original name
- exprToMapSort(named.canonicalized).toAttribute.withName(named.name)
- case other =>
- other.transformUp {
- case e =>
exprToMapSort.get(e.canonicalized).map(_.toAttribute).getOrElse(e)
- }.asInstanceOf[NamedExpression]
- }
- val newChild = Project(child.output ++ exprToMapSort.values, child)
- val newAgg = Aggregate(newGroupingKeys, newAggregateExprs, newChild,
hint)
- newAgg -> agg.output.zip(newAgg.output)
}
}
+
+ private def distinctAggregateChildren(
Review Comment:
EliminateDistinct runs in a later batch than FinishAnalysis and strips
DISTINCT from Max, Min, First, Last, CollectSet, BitAndAgg and BitOrAgg. Of
those, First and Last accept map input (the others reject map input or fail as
unorderable), so FIRST(DISTINCT m) now gets a MapSort inserted, returns a
key-sorted map instead of the original one, and then has its DISTINCT deleted
as a no-op right afterwards. That's a changed return value and a wasted sort
for an aggregate where DISTINCT never meant anything.
--
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.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]