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

yiguolei pushed a commit to branch branch-4.1
in repository https://gitbox.apache.org/repos/asf/doris.git


The following commit(s) were added to refs/heads/branch-4.1 by this push:
     new a8c7c355250 branch-4.1:  [fix](agg) Preserve weighted average when 
eliminating group by #67638 (#67832)
a8c7c355250 is described below

commit a8c7c355250ef5fc0685bdaaa4979b89e2c82296
Author: github-actions[bot] 
<41898282+github-actions[bot]@users.noreply.github.com>
AuthorDate: Sat Sep 12 00:54:24 2026 +0800

    branch-4.1:  [fix](agg) Preserve weighted average when eliminating group by 
#67638 (#67832)
    
    Cherry-picked from #67638
    
    Co-authored-by: feiniaofeiafei <[email protected]>
---
 .../nereids/rules/rewrite/EliminateGroupBy.java    |  4 +-
 .../rewrite/EliminateGroupByAvgWeightedTest.java   | 57 ++++++++++++++++++++++
 .../eliminate_gby_key/eliminate_group_by.out       |  6 ++-
 3 files changed, 63 insertions(+), 4 deletions(-)

diff --git 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/EliminateGroupBy.java
 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/EliminateGroupBy.java
index 628bd3cd823..24692b58ee8 100644
--- 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/EliminateGroupBy.java
+++ 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/EliminateGroupBy.java
@@ -27,7 +27,6 @@ import org.apache.doris.nereids.trees.expressions.Slot;
 import 
org.apache.doris.nereids.trees.expressions.functions.agg.AggregateFunction;
 import org.apache.doris.nereids.trees.expressions.functions.agg.AnyValue;
 import org.apache.doris.nereids.trees.expressions.functions.agg.Avg;
-import org.apache.doris.nereids.trees.expressions.functions.agg.AvgWeighted;
 import org.apache.doris.nereids.trees.expressions.functions.agg.Count;
 import org.apache.doris.nereids.trees.expressions.functions.agg.Max;
 import org.apache.doris.nereids.trees.expressions.functions.agg.MaxBy;
@@ -66,8 +65,9 @@ import java.util.Set;
 public class EliminateGroupBy extends OneRewriteRuleFactory {
     private static final ImmutableSet<Class<? extends Expression>> 
supportedBasicFunctions
             = ImmutableSet.of(Sum.class, Avg.class, Min.class, Max.class, 
Median.class, AnyValue.class);
+    // AvgWeighted must retain its aggregate: even one row can have zero 
weight or overflow v * w.
     private static final ImmutableSet<Class<? extends Expression>> 
supportedTwoArgsFunctions
-            = ImmutableSet.of(MinBy.class, MaxBy.class, AvgWeighted.class, 
Percentile.class);
+            = ImmutableSet.of(MinBy.class, MaxBy.class, Percentile.class);
     private static final ImmutableSet<Class<? extends Expression>> 
supportedDevLikeFunctions
             = ImmutableSet.of(Stddev.class, StddevSamp.class, Variance.class, 
VarianceSamp.class);
     private static final ImmutableSet<Class<? extends Expression>> 
supportedFunctionSum0
diff --git 
a/fe/fe-core/src/test/java/org/apache/doris/nereids/rules/rewrite/EliminateGroupByAvgWeightedTest.java
 
b/fe/fe-core/src/test/java/org/apache/doris/nereids/rules/rewrite/EliminateGroupByAvgWeightedTest.java
new file mode 100644
index 00000000000..88e015a758a
--- /dev/null
+++ 
b/fe/fe-core/src/test/java/org/apache/doris/nereids/rules/rewrite/EliminateGroupByAvgWeightedTest.java
@@ -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.doris.nereids.rules.rewrite;
+
+import org.apache.doris.nereids.trees.expressions.functions.agg.AvgWeighted;
+import org.apache.doris.nereids.util.MemoPatternMatchSupported;
+import org.apache.doris.nereids.util.PlanChecker;
+import org.apache.doris.utframe.TestWithFeService;
+
+import org.junit.jupiter.api.Test;
+
+class EliminateGroupByAvgWeightedTest extends TestWithFeService implements 
MemoPatternMatchSupported {
+    @Override
+    protected void runBeforeAll() throws Exception {
+        createDatabase("eliminate_group_by_avg_weighted");
+        connectContext.setDatabase("eliminate_group_by_avg_weighted");
+        
connectContext.getSessionVariable().setDisableNereidsRules("PRUNE_EMPTY_PARTITION");
+        createTable("CREATE TABLE t (k INT NOT NULL, v DOUBLE NOT NULL, w 
DOUBLE NOT NULL)"
+                + " UNIQUE KEY(k) DISTRIBUTED BY HASH(k) BUCKETS 1"
+                + " PROPERTIES ('replication_num' = '1')");
+    }
+
+    @Test
+    void retainAvgWeighted() {
+        for (String weight : new String[] {"w", "0", "2", "-2"}) {
+            PlanChecker.from(connectContext)
+                    .analyze("SELECT k, avg_weighted(v, " + weight + ") FROM t 
GROUP BY k")
+                    .rewrite()
+                    .matches(logicalAggregate().when(agg -> 
agg.getAggregateFunctions().stream()
+                            .anyMatch(function -> function instanceof 
AvgWeighted)));
+        }
+    }
+
+    @Test
+    void retainAvgWeightedWithOtherAggregates() {
+        PlanChecker.from(connectContext)
+                .analyze("SELECT k, avg_weighted(v, w), sum(v), max(v) FROM t 
GROUP BY k")
+                .rewrite()
+                .matches(logicalAggregate().when(agg -> 
agg.getAggregateFunctions().stream()
+                        .anyMatch(function -> function instanceof 
AvgWeighted)));
+    }
+}
diff --git 
a/regression-test/data/nereids_rules_p0/eliminate_gby_key/eliminate_group_by.out
 
b/regression-test/data/nereids_rules_p0/eliminate_gby_key/eliminate_group_by.out
index ab48af56fae..a6bdf691ffd 100644
--- 
a/regression-test/data/nereids_rules_p0/eliminate_gby_key/eliminate_group_by.out
+++ 
b/regression-test/data/nereids_rules_p0/eliminate_gby_key/eliminate_group_by.out
@@ -151,8 +151,10 @@ PhysicalResultSink
 -- !two_args_func_avg_weighted_shape --
 PhysicalResultSink
 --PhysicalProject
-----filter((test_unique2.__DORIS_DELETE_SIGN__ = 0))
-------PhysicalOlapScan[test_unique2]
+----hashAgg[GLOBAL]
+------PhysicalProject
+--------filter((test_unique2.__DORIS_DELETE_SIGN__ = 0))
+----------PhysicalOlapScan[test_unique2]
 
 -- !two_args_func_percentile_shape --
 PhysicalResultSink


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

Reply via email to