uros-db commented on code in PR #46640:
URL: https://github.com/apache/spark/pull/46640#discussion_r1622380248


##########
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:
   If I just try to use `supportsHashAggregate` here, I might find that the 
aggregate does not support hash aggregation _before_ the rewrite, but will 
support it _after_ the rewrite (as a result of this, the rewrite rule will 
never actually execute)
   
   However, we perform this check before doing the plan rewrite, so the point 
of this check is to verify that the current Aggregate is only a grouping 
aggregate with respect to StringType (i.e. StringType is not found in 
aggregateExpressions).
   
   Any ideas on how to make this better?



-- 
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]

Reply via email to