n-young-db commented on code in PR #46974:
URL: https://github.com/apache/spark/pull/46974#discussion_r1638310150


##########
sql/core/src/test/scala/org/apache/spark/sql/DataFrameAggregateSuite.scala:
##########
@@ -2202,6 +2202,36 @@ class DataFrameAggregateSuite extends QueryTest
     assertAggregateOnDataframe(dfDifferent, numRows, "m0")
   }
 
+  test("Fix reference error when grouping by MapType") {

Review Comment:
   Let's have some tests for
   - multiple group-by columns
   - multiple maptype group-by columns
   - maptype in aggregate expressions
   - maptype in non-aggregate expression, but in another expression
   - selecting other rows
   to confirm behavior and non breaking



##########
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
+ *
+ * We inject a new `Project` to ensure that we don't do recomputation, and so 
that we can reference
+ * the newly sorted map column in the aggregate expressions list.
  */
-object InsertMapSortInGroupingExpressions extends Rule[LogicalPlan] {
+object InsertMapSortInGroupingExpressions extends Rule[LogicalPlan] with 
AliasHelper {
   override def apply(plan: LogicalPlan): LogicalPlan = 
plan.transformWithPruning(
     _.containsPattern(AGGREGATE), ruleId) {
-    case a @ Aggregate(groupingExpr, _, _) =>
-      val newGrouping = groupingExpr.map { expr =>
-        if (!expr.isInstanceOf[MapSort] && 
expr.dataType.isInstanceOf[MapType]) {
-          MapSort(expr)
-        } else {
-          expr
-        }
-      }
-      a.copy(groupingExpressions = newGrouping)
+    case a @ Aggregate(groupingExpr, aggregateExpr, child) =>
+      val toSort = groupingExpr.filter( expr =>
+        !expr.isInstanceOf[MapSort] && expr.dataType.isInstanceOf[MapType]
+      )
+      val newChild = Project(toSort.map( expr =>
+        Alias(MapSort(expr), "map_sorted")()

Review Comment:
   Might consider adding the name of `expr` into the alias so it makes it 
easier to track up the plan



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