cloud-fan commented on code in PR #47545:
URL: https://github.com/apache/spark/pull/47545#discussion_r1709472048
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/InsertMapSortInGroupingExpressions.scala:
##########
@@ -17,56 +17,85 @@
package org.apache.spark.sql.catalyst.optimizer
-import org.apache.spark.sql.catalyst.expressions.{ArrayTransform,
CreateNamedStruct, Expression, GetStructField, If, IsNull, LambdaFunction,
Literal, MapFromArrays, MapKeys, MapSort, MapValues, NamedLambdaVariable}
-import org.apache.spark.sql.catalyst.plans.logical.{Aggregate, LogicalPlan}
+import scala.collection.mutable
+
+import org.apache.spark.sql.catalyst.expressions.{Alias, ArrayTransform,
CreateNamedStruct, Expression, GetStructField, If, IsNull, LambdaFunction,
Literal, MapFromArrays, MapKeys, MapSort, MapValues, NamedExpression,
NamedLambdaVariable}
+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.catalyst.trees.TreePattern
import org.apache.spark.sql.types.{ArrayType, MapType, StructType}
import org.apache.spark.util.ArrayImplicits.SparkArrayOps
/**
- * Adds MapSort to group expressions containing map columns, as the key/value
paris need to be
+ * Adds [[MapSort]] to group expressions containing map columns, as the
key/value paris 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 map_column, COUNT(*) FROM TABLE GROUP BY map_column =>
+ * SELECT _groupingexpression as map_column, COUNT(*) FROM (
+ * SELECT map_sort(map_column) as _groupingexpression FROM TABLE
+ * ) GROUP BY _groupingexpression
*/
object InsertMapSortInGroupingExpressions extends Rule[LogicalPlan] {
- override def apply(plan: LogicalPlan): LogicalPlan =
plan.transformWithPruning(
- _.containsPattern(AGGREGATE), ruleId) {
- case a @ Aggregate(groupingExpr, _, _) =>
- val newGrouping = groupingExpr.map { expr =>
- if (!expr.exists(_.isInstanceOf[MapSort])
- && expr.dataType.existsRecursively(_.isInstanceOf[MapType])) {
- insertMapSortRecursively(expr)
- } else {
- expr
+ private def shouldAddMapSort(expr: Expression): Boolean = {
+ expr.dataType.existsRecursively(_.isInstanceOf[MapType])
+ }
+
+ override def apply(plan: LogicalPlan): LogicalPlan = {
+ if (!plan.containsPattern(TreePattern.AGGREGATE)) {
+ return plan
+ }
+ val shouldRewrite = plan.exists {
+ case agg: Aggregate if agg.groupingExpressions.exists(shouldAddMapSort)
=> true
+ case _ => false
+ }
+ if (!shouldRewrite) {
+ return plan
+ }
+
+ plan transformUpWithNewOutput {
+ case agg @ Aggregate(groupingExpr, aggregateExpressions, child)
+ if agg.groupingExpressions.exists(shouldAddMapSort) =>
+ val exprToMapSort = new mutable.HashMap[Expression, NamedExpression]
+ val newGroupingKeys =
groupingExpr.map(replaceWithMapSortRecursively(_, exprToMapSort))
+ val newAggregateExprs = aggregateExpressions.map {
+ case named if exprToMapSort.contains(named.canonicalized) =>
+ // If we replace the top-level named expr, then should add back
the original name
+ exprToMapSort(named.canonicalized).toAttribute.withName(named.name)
+ case other =>
+ other.transformUp {
+ case e =>
exprToMapSort.get(e.canonicalized).map(_.toAttribute).getOrElse(e)
+ }.asInstanceOf[NamedExpression]
}
- }
- a.copy(groupingExpressions = newGrouping)
+ val newChild = Project(child.output ++ exprToMapSort.values, child)
+ val newAgg = Aggregate(newGroupingKeys, newAggregateExprs, newChild)
+ newAgg -> agg.output.zip(newAgg.output)
+ }
}
- /*
- Inserts MapSort recursively taking into account when
- it is nested inside a struct or array.
+ /**
+ * Inserts MapSort recursively taking into account when it is nested inside
a struct or array.
*/
- private def insertMapSortRecursively(e: Expression): Expression = {
+ private def replaceWithMapSortRecursively(
Review Comment:
why rename? It's indeed inserting 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]