cloud-fan commented on code in PR #49144:
URL: https://github.com/apache/spark/pull/49144#discussion_r1905306113
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/InsertMapSortExpression.scala:
##########
@@ -77,15 +71,54 @@ object InsertMapSortInGroupingExpressions extends
Rule[LogicalPlan] {
}.asInstanceOf[NamedExpression]
}
val newChild = Project(child.output ++ exprToMapSort.values, child)
- val newAgg = Aggregate(newGroupingKeys, newAggregateExprs, newChild)
+ val newAgg = Aggregate(newGroupingKeys, newAggregateExprs, newChild,
hint)
newAgg -> agg.output.zip(newAgg.output)
}
}
+}
+
+/**
+ * Adds [[MapSort]] to [[RepartitionByExpression]] expressions containing map
columns,
+ * as the key/value pairs need to be in the correct order before
repartitioning:
+ *
+ * SELECT * FROM TABLE DISTRIBUTE BY map_column =>
+ * SELECT * FROM TABLE DISTRIBUTE BY map_sort(map_column)
+ */
+object InsertMapSortInRepartitionExpressions extends Rule[LogicalPlan] {
+ import InsertMapSortExpression._
+
+ override def apply(plan: LogicalPlan): LogicalPlan = {
+ plan.transformUpWithPruning(_.containsPattern(REPARTITION_OPERATION)) {
+ case rep @
+ RepartitionByExpression(partitionExprs, _, _, _)
+ if rep.partitionExpressions.exists(mapTypeExistsRecursively) =>
+ val exprToMapSort = new mutable.HashMap[Expression, Expression]
+ val newPartitionExprs = partitionExprs.map { expr =>
+ val inserted = insertMapSortRecursively(expr)
+ if (expr.ne(inserted)) {
+ exprToMapSort.getOrElseUpdate(expr.canonicalized, inserted)
+ } else {
+ expr
+ }
+ }
+ rep.copy(partitionExpressions = newPartitionExprs)
Review Comment:
```suggestion
case rep: RepartitionByExpression
if rep.partitionExpressions.exists(mapTypeExistsRecursively) =>
val exprToMapSort = new mutable.HashMap[Expression, Expression]
val newPartitionExprs = rep.partitionExpressions.map { expr =>
val inserted = insertMapSortRecursively(expr)
if (expr.ne(inserted)) {
exprToMapSort.getOrElseUpdate(expr.canonicalized, inserted)
} else {
expr
}
}
rep.copy(partitionExpressions = newPartitionExprs)
```
--
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]