dbatomic commented on code in PR #46640: URL: https://github.com/apache/spark/pull/46640#discussion_r1622360837
########## sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/RewriteGroupByCollation.scala: ########## @@ -0,0 +1,57 @@ +/* + * 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.analysis + +import org.apache.spark.sql.catalyst.expressions.{AttributeReference, CollationKey} +import org.apache.spark.sql.catalyst.plans.logical.{Aggregate, LogicalPlan} +import org.apache.spark.sql.catalyst.rules.Rule +import org.apache.spark.sql.catalyst.util.CollationFactory +import org.apache.spark.sql.types.StringType + +object RewriteGroupByCollation extends Rule[LogicalPlan] { + def apply(plan: LogicalPlan): LogicalPlan = plan transformUpWithNewOutput { + case a: Aggregate if canRewriteAggregateCollation(a) => + + val newGroupingExpressions = a.groupingExpressions.map { + case attr: AttributeReference if attr.dataType.isInstanceOf[StringType] && + !CollationFactory.fetchCollation( + attr.dataType.asInstanceOf[StringType].collationId).supportsBinaryEquality => + CollationKey(attr) + case other => other + } + + val newAggregate = a.copy( + groupingExpressions = newGroupingExpressions, + aggregateExpressions = a.aggregateExpressions + ) + + if (!newAggregate.fastEquals(a)) { + (newAggregate, a.output.zip(newAggregate.output)) + } else { + (a, a.output.zip(a.output)) + } + } + + private def canRewriteAggregateCollation(aggregate: Aggregate): Boolean = { + // This rewrite rule is used to enabled hash aggregation on collated string columns. However, + // hash aggregation is currently only supported for grouping aggregations - this means that no + // string type can be found in the aggregate expressions, so we avoid rewrite in this case. + !aggregate.aggregateExpressions.exists(e => e.dataType.isInstanceOf[StringType]) Review Comment: I think that you should check if hash aggregation is supported at all, regardless of `StringType`. If we are going to end up doing merge agg there is no need to insert collation_key. -- 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]
