cloud-fan commented on code in PR #47331:
URL: https://github.com/apache/spark/pull/47331#discussion_r1698745030
##########
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:
map is basically two arrays for keys and values. We can use `MapKeys` and
`MapValues` to get the two arrays, and `MapFromArrays` to rebuild the map. For
arrays, we already know how to add `MapSort`
--
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]