nebojsa-db commented on code in PR #47331:
URL: https://github.com/apache/spark/pull/47331#discussion_r1696785824


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/InsertMapSortInGroupingExpressions.scala:
##########
@@ -34,12 +35,46 @@ object InsertMapSortInGroupingExpressions extends 
Rule[LogicalPlan] {
     _.containsPattern(AGGREGATE), ruleId) {
     case a @ Aggregate(groupingExpr, _, _) =>
       val newGrouping = groupingExpr.map { expr =>
-        if (!expr.isInstanceOf[MapSort] && 
expr.dataType.isInstanceOf[MapType]) {
-          MapSort(expr)
+        if (!expr.exists(_.isInstanceOf[MapSort])
+          && expr.dataType.existsRecursively(_.isInstanceOf[MapType])) {
+          insertMapSortRecursively(expr)
         } else {
           expr
         }
       }
       a.copy(groupingExpressions = newGrouping)
   }
+
+  /*
+  Inserts MapSort recursively taking into account when
+  it is nested inside a struct or array.
+   */
+  private def insertMapSortRecursively(e: Expression): Expression = {
+    e.dataType match {
+      case _: MapType => MapSort(e)

Review Comment:
   So MapType as key type should be forbidden, but when MapType is type of 
value, this approach with MapSort won't work. We can create separate ticket for 
that one or try to address it as part of this PR? 
   Most likely we would need to modify MapSort with that recursive approach, 
similar with what we have done for initial version of this PR. 



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