cloud-fan commented on code in PR #46974:
URL: https://github.com/apache/spark/pull/46974#discussion_r1645297165


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/InsertMapSortInGroupingExpressions.scala:
##########
@@ -17,29 +17,41 @@
 
 package org.apache.spark.sql.catalyst.optimizer
 
-import org.apache.spark.sql.catalyst.expressions.MapSort
-import org.apache.spark.sql.catalyst.plans.logical.{Aggregate, LogicalPlan}
+import org.apache.spark.sql.catalyst.expressions.{Alias, AliasHelper, MapSort}
+import org.apache.spark.sql.catalyst.plans.logical.{Aggregate, LogicalPlan, 
Project}
 import org.apache.spark.sql.catalyst.rules.Rule
 import org.apache.spark.sql.catalyst.trees.TreePattern.AGGREGATE
 import org.apache.spark.sql.types.MapType
 
 /**
- * Adds MapSort to group expressions containing map columns, as the key/value 
paris need to be
- * in the correct order before grouping:
+ * Adds MapSort to group expressions containing map columns by pushing down 
the projection,
+ * as the key/value pairs need to be in the correct order before grouping:
  * SELECT COUNT(*) FROM TABLE GROUP BY map_column =>
- * SELECT COUNT(*) FROM TABLE GROUP BY map_sort(map_column)
+ * SELECT COUNT(*) FROM (SELECT map_sort(map_column) as map_sorted) GROUP BY 
map_sorted
+ *
+ * SELECT map_column FROM TABLE GROUP BY map_column =>
+ * SELECT map_sorted FROM (SELECT map_sort(map_column) as map_sorted) GROUP BY 
map_sorted

Review Comment:
   The problem with this rewrite is it changes the final output schema (column 
name changed from `map_column` to `map_sorted`). We should alias 
`map_sort(map_column)` to the original name of the grouping column.



##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/InsertMapSortInGroupingExpressions.scala:
##########
@@ -17,29 +17,41 @@
 
 package org.apache.spark.sql.catalyst.optimizer
 
-import org.apache.spark.sql.catalyst.expressions.MapSort
-import org.apache.spark.sql.catalyst.plans.logical.{Aggregate, LogicalPlan}
+import org.apache.spark.sql.catalyst.expressions.{Alias, AliasHelper, MapSort}
+import org.apache.spark.sql.catalyst.plans.logical.{Aggregate, LogicalPlan, 
Project}
 import org.apache.spark.sql.catalyst.rules.Rule
 import org.apache.spark.sql.catalyst.trees.TreePattern.AGGREGATE
 import org.apache.spark.sql.types.MapType
 
 /**
- * Adds MapSort to group expressions containing map columns, as the key/value 
paris need to be
- * in the correct order before grouping:
+ * Adds MapSort to group expressions containing map columns by pushing down 
the projection,
+ * as the key/value pairs need to be in the correct order before grouping:
  * SELECT COUNT(*) FROM TABLE GROUP BY map_column =>
- * SELECT COUNT(*) FROM TABLE GROUP BY map_sort(map_column)
+ * SELECT COUNT(*) FROM (SELECT map_sort(map_column) as map_sorted) GROUP BY 
map_sorted
+ *
+ * SELECT map_column FROM TABLE GROUP BY map_column =>
+ * SELECT map_sorted FROM (SELECT map_sort(map_column) as map_sorted) GROUP BY 
map_sorted

Review Comment:
   The problem with this rewrite is it changes the final output schema (column 
name changed from `map_column` to `map_sorted`). We should alias 
`map_sort(map_column)` to the original name of the grouping expression if it's 
a subclass of `NamedExpression`.



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

Reply via email to