uros-b commented on code in PR #57629: URL: https://github.com/apache/spark/pull/57629#discussion_r3713919242
########## sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/optimizer/InsertMapSortInAggregateSuite.scala: ########## @@ -0,0 +1,95 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.spark.sql.catalyst.optimizer + +import org.apache.spark.sql.catalyst.dsl.expressions._ +import org.apache.spark.sql.catalyst.expressions.{Alias, MapSort} +import org.apache.spark.sql.catalyst.expressions.aggregate.AggregateExpression +import org.apache.spark.sql.catalyst.plans.PlanTest +import org.apache.spark.sql.catalyst.plans.logical.{Aggregate, LocalRelation, LogicalPlan, Project} +import org.apache.spark.sql.internal.SQLConf +import org.apache.spark.sql.types.{IntegerType, StringType} + +class InsertMapSortInAggregateSuite extends PlanTest { + private val input = LocalRelation(Symbol("m").map(StringType, IntegerType)) + private val mapAttribute = input.output.head + + private def aliasesNamed(plan: LogicalPlan, name: String): Seq[Alias] = { + plan.flatMap { node => + node.expressions.flatMap(_.collect { + case alias @ Alias(_, aliasName) if aliasName == name => alias + }) + } + } + + test("reuse map sort when a grouping key is also a distinct argument") { + val plan = Aggregate( + Seq(mapAttribute), + Seq(mapAttribute, countDistinct(mapAttribute).as("count")), + input) + val rewritten = InsertMapSortInAggregate(plan) + val groupingAliases = aliasesNamed(rewritten, "_groupingmapsort") + + assert(groupingAliases.size == 1) + assert(groupingAliases.head.child.isInstanceOf[MapSort]) + rewritten match { + case Aggregate(Seq(groupingExpression), aggregateExpressions, _: Project, _) => + assert(groupingExpression.semanticEquals(groupingAliases.head.toAttribute)) + val distinctChildren = aggregateExpressions.flatMap(_.collect { + case expression: AggregateExpression if expression.isDistinct => + expression.aggregateFunction.children + }).flatten + assert(distinctChildren.size == 1) + assert(distinctChildren.head.semanticEquals(groupingAliases.head.toAttribute)) + case other => + fail(s"Unexpected plan:\n$other") + } + } + + test("project complex distinct arguments only when needed") { + val attributePlan = Aggregate( + Nil, + Seq(countDistinct(mapAttribute).as("count")), + input) + val complexPlan = Aggregate( + Nil, + Seq(countDistinct(namedStruct("m", mapAttribute)).as("count")), + input) + + val rewrittenAttributePlan = InsertMapSortInAggregate(attributePlan) + assert(rewrittenAttributePlan.collect { case _: Project => 1 }.size == 1) + assert(aliasesNamed(rewrittenAttributePlan, "_distinctaggregateexpression").isEmpty) + assert(aliasesNamed(rewrittenAttributePlan, "_distinctmapsort").size == 1) + + val rewrittenComplexPlan = InsertMapSortInAggregate(complexPlan) + assert(rewrittenComplexPlan.collect { case _: Project => 1 }.size == 2) + assert(aliasesNamed(rewrittenComplexPlan, "_distinctaggregateexpression").size == 1) + assert(aliasesNamed(rewrittenComplexPlan, "_distinctmapsort").size == 1) + } + + test("skip distinct argument normalization when disabled") { + val plan = Aggregate( + Nil, + Seq(countDistinct(mapAttribute).as("count")), + input) + + withSQLConf(SQLConf.INSERT_MAP_SORT_IN_DISTINCT_AGGREGATES_ENABLED.key -> "false") { + comparePlans(InsertMapSortInAggregate(plan), plan) + } + } +} Review Comment: One coverage gap worth closing while this suite is new. The rule no longer wraps every Aggregate in the plan once the plan-wide shouldRewrite check passes; the early return at InsertMapSortExpression.scala:78 skips aggregates whose own expressions carry no maps, where the old code would still have added a Project(child.output, child). That's a deliberate improvement over the previous behavior and nothing currently pins it down. A plan with a map-free Aggregate alongside a map-carrying one, asserting the map-free one comes back untouched, would cover it cheaply. -- 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]
