This is an automated email from the ASF dual-hosted git repository.

zhangzc pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/gluten.git


The following commit(s) were added to refs/heads/main by this push:
     new 71b18036ac [GLUTEN-11838] Enable 'Eliminate two aggregate joins with 
attribute reordered' suite (#11884)
71b18036ac is described below

commit 71b18036ac33caf7bc698947d30e8357748684a5
Author: Zhichao Zhang <[email protected]>
AuthorDate: Tue Apr 7 18:22:48 2026 +0800

    [GLUTEN-11838] Enable 'Eliminate two aggregate joins with attribute 
reordered' suite (#11884)
    
    After #9473 , there is an issue when executing suite 'Eliminate two 
aggregate joins with attribute reordered'.
---
 .../gluten/backendsapi/clickhouse/CHRuleApi.scala  |  2 +-
 .../extension/columnar/CHEliminateLocalSort.scala  | 75 ++++++++++++++++++++++
 .../execution/GlutenEliminateJoinSuite.scala       |  2 +-
 3 files changed, 77 insertions(+), 2 deletions(-)

diff --git 
a/backends-clickhouse/src/main/scala/org/apache/gluten/backendsapi/clickhouse/CHRuleApi.scala
 
b/backends-clickhouse/src/main/scala/org/apache/gluten/backendsapi/clickhouse/CHRuleApi.scala
index d3b06b92bf..f6b9e2e317 100644
--- 
a/backends-clickhouse/src/main/scala/org/apache/gluten/backendsapi/clickhouse/CHRuleApi.scala
+++ 
b/backends-clickhouse/src/main/scala/org/apache/gluten/backendsapi/clickhouse/CHRuleApi.scala
@@ -120,7 +120,7 @@ object CHRuleApi {
     injector.injectPostTransform(_ => PushDownFilterToScan)
     injector.injectPostTransform(_ => PushDownInputFileExpression.PostOffload)
     injector.injectPostTransform(_ => EnsureLocalSortRequirements)
-    injector.injectPostTransform(_ => EliminateLocalSort)
+    injector.injectPostTransform(_ => CHEliminateLocalSort)
     injector.injectPostTransform(_ => CollapseProjectExecTransformer)
     injector.injectPostTransform(c => 
RewriteSortMergeJoinToHashJoinRule(c.session))
     injector.injectPostTransform(c => 
PushdownAggregatePreProjectionAheadExpand(c.session))
diff --git 
a/backends-clickhouse/src/main/scala/org/apache/gluten/extension/columnar/CHEliminateLocalSort.scala
 
b/backends-clickhouse/src/main/scala/org/apache/gluten/extension/columnar/CHEliminateLocalSort.scala
new file mode 100644
index 0000000000..77658a78eb
--- /dev/null
+++ 
b/backends-clickhouse/src/main/scala/org/apache/gluten/extension/columnar/CHEliminateLocalSort.scala
@@ -0,0 +1,75 @@
+/*
+ * 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.gluten.extension.columnar
+
+import org.apache.gluten.execution._
+
+import org.apache.spark.sql.catalyst.expressions.SortOrder
+import org.apache.spark.sql.catalyst.rules.Rule
+import org.apache.spark.sql.execution.{ProjectExec, SortExec, SparkPlan, 
UnaryExecNode}
+
+/**
+ * This rule is used to eliminate unnecessary local sort.
+ *
+ * This could happen if:
+ *   - Convert sort merge join to shuffled hash join
+ *   - Offload SortAggregate to native hash aggregate
+ *   - Offload WindowGroupLimit to native TopNRowNumber
+ *   - Offload Window which has date type range frame
+ */
+object CHEliminateLocalSort extends Rule[SparkPlan] {
+  private def canEliminateLocalSort(p: SparkPlan): Boolean = p match {
+    case _: HashAggregateExecBaseTransformer => true
+    case _: ShuffledHashJoinExecTransformerBase => true
+    case _: WindowGroupLimitExecTransformer => true
+    case s: SortExec if s.global == false => true
+    case s: SortExecTransformer if s.global == false => true
+    case _ => false
+  }
+
+  private def canThrough(p: SparkPlan): Boolean = p match {
+    case _: ProjectExec => true
+    case _: ProjectExecTransformer => true
+    case _ => false
+  }
+
+  private def orderingSatisfies(gChild: SparkPlan, requiredOrdering: 
Seq[SortOrder]): Boolean = {
+    SortOrder.orderingSatisfies(gChild.outputOrdering, requiredOrdering)
+  }
+
+  override def apply(plan: SparkPlan): SparkPlan = {
+    plan.transformDown {
+      case p if canEliminateLocalSort(p) =>
+        val requiredChildOrdering = p.requiredChildOrdering
+        assert(requiredChildOrdering.size == p.children.size)
+        val newChildren = p.children.zipWithIndex.map {
+          case (SortWithChild(gChild), i) if orderingSatisfies(gChild, 
requiredChildOrdering(i)) =>
+            gChild
+          case (p: UnaryExecNode, i) if canThrough(p) =>
+            // There may be more than one project between target operator and 
sort,
+            // e.g., both hash aggregate and sort pull out project
+            p.child match {
+              case SortWithChild(gChild) if orderingSatisfies(gChild, 
requiredChildOrdering(i)) =>
+                p.withNewChildren(gChild :: Nil)
+              case _ => p
+            }
+          case p => p._1
+        }
+        p.withNewChildren(newChildren)
+    }
+  }
+}
diff --git 
a/backends-clickhouse/src/test/scala/org/apache/gluten/execution/GlutenEliminateJoinSuite.scala
 
b/backends-clickhouse/src/test/scala/org/apache/gluten/execution/GlutenEliminateJoinSuite.scala
index 510fe0264c..923d6583a5 100644
--- 
a/backends-clickhouse/src/test/scala/org/apache/gluten/execution/GlutenEliminateJoinSuite.scala
+++ 
b/backends-clickhouse/src/test/scala/org/apache/gluten/execution/GlutenEliminateJoinSuite.scala
@@ -133,7 +133,7 @@ class GlutenEliminateJoinSuite extends 
GlutenClickHouseWholeStageTransformerSuit
       })
   }
 
-  ignore("Eliminate two aggregate joins with attribute reordered") {
+  test("Eliminate two aggregate joins with attribute reordered") {
     val sql = """
         select t1.k1, t1.k3, t2.k1, t2.k3, s1, s2 from (
           select k1, k3, sum(v1) s1 from (


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to