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

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


The following commit(s) were added to refs/heads/branch-2.1 by this push:
     new 191c86bb8bc branch-2.1: [fix](nereids) fix merge_percentile_to_array 
when has same agg function #44783 (#44879)
191c86bb8bc is described below

commit 191c86bb8bcd5128568d96a62017fb8e6b716698
Author: github-actions[bot] 
<41898282+github-actions[bot]@users.noreply.github.com>
AuthorDate: Wed Dec 4 22:02:16 2024 +0800

    branch-2.1: [fix](nereids) fix merge_percentile_to_array when has same agg 
function #44783 (#44879)
    
    Cherry-picked from #44783
    
    Co-authored-by: feiniaofeiafei <moail...@selectdb.com>
---
 .../rules/rewrite/MergePercentileToArray.java      | 26 ++++++++++------------
 .../merge_percentile_to_array.out                  | 12 ++++++++++
 .../merge_percentile_to_array.groovy               |  4 ++++
 3 files changed, 28 insertions(+), 14 deletions(-)

diff --git 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/MergePercentileToArray.java
 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/MergePercentileToArray.java
index f92ad84bde8..fe81adf13bf 100644
--- 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/MergePercentileToArray.java
+++ 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/MergePercentileToArray.java
@@ -152,10 +152,10 @@ public class MergePercentileToArray extends 
OneRewriteRuleFactory {
                 (List<Expression>) (List) newPercentileArrays);
         ImmutableList.Builder<NamedExpression> newProjectOutputExpressions = 
ImmutableList.builder();
         newProjectOutputExpressions.addAll((List<NamedExpression>) (List) 
notChangeForProject);
-        Map<Expression, Alias> existsAliasMap = Maps.newHashMap();
+        Map<Expression, List<Alias>> existsAliasMap = Maps.newHashMap();
         // existsAliasMap is used to keep upper plan refer the same expr
         for (Alias alias : existsAliases) {
-            existsAliasMap.put(alias.child(), alias);
+            existsAliasMap.computeIfAbsent(alias.child(), k -> new 
ArrayList<>()).add(alias);
         }
         Map<DistinctAndExpr, Slot> slotMap = Maps.newHashMap();
         // slotMap is used to find the correspondence
@@ -169,20 +169,22 @@ public class MergePercentileToArray extends 
OneRewriteRuleFactory {
         for (Map.Entry<DistinctAndExpr, List<AggregateFunction>> entry : 
funcMap.entrySet()) {
             for (int i = 0; i < entry.getValue().size(); i++) {
                 AggregateFunction aggFunc = entry.getValue().get(i);
-                Alias originAlias = existsAliasMap.get(aggFunc);
-                DistinctAndExpr distinctAndExpr = new 
DistinctAndExpr(aggFunc.child(0), aggFunc.isDistinct());
-                Alias newAlias = new Alias(originAlias.getExprId(), new 
ElementAt(slotMap.get(distinctAndExpr),
-                        new IntegerLiteral(i + 1)), originAlias.getName());
-                newProjectOutputExpressions.add(newAlias);
+                List<Alias> originAliases = existsAliasMap.get(aggFunc);
+                for (Alias originAlias : originAliases) {
+                    DistinctAndExpr distinctAndExpr = new 
DistinctAndExpr(aggFunc.child(0), aggFunc.isDistinct());
+                    Alias newAlias = new Alias(originAlias.getExprId(), new 
ElementAt(slotMap.get(distinctAndExpr),
+                            new IntegerLiteral(i + 1)), originAlias.getName());
+                    newProjectOutputExpressions.add(newAlias);
+                }
             }
         }
         newProjectOutputExpressions.addAll(groupBySlots);
-        return new LogicalProject(newProjectOutputExpressions.build(), 
newAggregate);
+        return new LogicalProject<>(newProjectOutputExpressions.build(), 
newAggregate);
     }
 
     private static class DistinctAndExpr {
-        private Expression expression;
-        private boolean isDistinct;
+        private final Expression expression;
+        private final boolean isDistinct;
 
         public DistinctAndExpr(Expression expression, boolean isDistinct) {
             this.expression = expression;
@@ -193,10 +195,6 @@ public class MergePercentileToArray extends 
OneRewriteRuleFactory {
             return expression;
         }
 
-        public boolean isDistinct() {
-            return isDistinct;
-        }
-
         @Override
         public boolean equals(Object o) {
             if (this == o) {
diff --git 
a/regression-test/data/nereids_rules_p0/merge_percentile_to_array/merge_percentile_to_array.out
 
b/regression-test/data/nereids_rules_p0/merge_percentile_to_array/merge_percentile_to_array.out
index b495302e80d..1b2f876cfba 100644
--- 
a/regression-test/data/nereids_rules_p0/merge_percentile_to_array/merge_percentile_to_array.out
+++ 
b/regression-test/data/nereids_rules_p0/merge_percentile_to_array/merge_percentile_to_array.out
@@ -41,3 +41,15 @@
 7.0    \N      \N
 7.0    7.0     7
 
+-- !same_percentile --
+52     1.0     1.0     2.0
+
+-- !same_percentile_group_by --
+\N     6.0     6.0     6.0
+2      3.0     3.0     3.0
+25     3.0     3.0     3.0
+4      2.0     2.0     2.0
+5      1.0     1.0     1.6
+7      6.0     6.0     6.0
+9      1.2     1.2     1.8
+
diff --git 
a/regression-test/suites/nereids_rules_p0/merge_percentile_to_array/merge_percentile_to_array.groovy
 
b/regression-test/suites/nereids_rules_p0/merge_percentile_to_array/merge_percentile_to_array.groovy
index 2071d75ae85..5bb13c6336c 100644
--- 
a/regression-test/suites/nereids_rules_p0/merge_percentile_to_array/merge_percentile_to_array.groovy
+++ 
b/regression-test/suites/nereids_rules_p0/merge_percentile_to_array/merge_percentile_to_array.groovy
@@ -57,4 +57,8 @@ suite("merge_percentile_to_array") {
             percentile(abs(a), 0.55) as c2 from test_merge_percentile group by 
a) t;      
     """
 
+    order_qt_same_percentile """select sum(a),percentile(pk, 0.1) as c1 , 
percentile(pk, 0.1) as c2 ,
+            percentile(pk, 0.4) as c2 from test_merge_percentile;"""
+    order_qt_same_percentile_group_by """select sum(a),percentile(pk, 0.1) as 
c1 , percentile(pk, 0.1) as c2 ,
+            percentile(pk, 0.4) as c2 from test_merge_percentile group by a;"""
 }
\ No newline at end of file


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscr...@doris.apache.org
For additional commands, e-mail: commits-h...@doris.apache.org

Reply via email to