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 5d89d86a503 [fix](nereids)EliminateGroupByConstant should replace 
agg's output wafter removing constant group by keys (#32877)
5d89d86a503 is described below

commit 5d89d86a50385d37591fc41c91ee50921fe3c8c5
Author: starocean999 <40539150+starocean...@users.noreply.github.com>
AuthorDate: Wed Mar 27 11:04:32 2024 +0800

    [fix](nereids)EliminateGroupByConstant should replace agg's output wafter 
removing constant group by keys (#32877)
---
 .../rules/analysis/EliminateGroupByConstant.java   |  8 +++-
 .../aggregate/aggregate_groupby_constant.groovy    | 50 ++++++++++++++++++++++
 2 files changed, 57 insertions(+), 1 deletion(-)

diff --git 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/analysis/EliminateGroupByConstant.java
 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/analysis/EliminateGroupByConstant.java
index e683153e9a2..4408e64487c 100644
--- 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/analysis/EliminateGroupByConstant.java
+++ 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/analysis/EliminateGroupByConstant.java
@@ -26,11 +26,14 @@ import 
org.apache.doris.nereids.trees.expressions.Expression;
 import org.apache.doris.nereids.trees.expressions.NamedExpression;
 import org.apache.doris.nereids.trees.plans.Plan;
 import org.apache.doris.nereids.trees.plans.logical.LogicalAggregate;
+import org.apache.doris.nereids.util.ExpressionUtils;
 
 import com.google.common.collect.ImmutableList;
 import com.google.common.collect.Sets;
 
+import java.util.HashMap;
 import java.util.List;
+import java.util.Map;
 import java.util.Set;
 
 /**
@@ -54,6 +57,7 @@ public class EliminateGroupByConstant extends 
OneRewriteRuleFactory {
             List<Expression> groupByExprs = aggregate.getGroupByExpressions();
             List<NamedExpression> outputExprs = 
aggregate.getOutputExpressions();
             Set<Expression> slotGroupByExprs = Sets.newLinkedHashSet();
+            Map<Expression, Expression> constantExprsReplaceMap = new 
HashMap<>(groupByExprs.size());
             Expression lit = null;
             for (Expression expression : groupByExprs) {
                 // NOTICE: we should not use the expression after fold as new 
aggregate's output or group expr
@@ -64,13 +68,15 @@ public class EliminateGroupByConstant extends 
OneRewriteRuleFactory {
                 if (!foldExpression.isConstant()) {
                     slotGroupByExprs.add(expression);
                 } else {
+                    constantExprsReplaceMap.put(expression, foldExpression);
                     lit = expression;
                 }
             }
             if (slotGroupByExprs.isEmpty() && lit != null) {
                 slotGroupByExprs.add(lit);
             }
-            return 
aggregate.withGroupByAndOutput(ImmutableList.copyOf(slotGroupByExprs), 
outputExprs);
+            return 
aggregate.withGroupByAndOutput(ImmutableList.copyOf(slotGroupByExprs),
+                    ExpressionUtils.replaceNamedExpressions(outputExprs, 
constantExprsReplaceMap));
         }).toRule(RuleType.ELIMINATE_GROUP_BY_CONSTANT);
     }
 }
diff --git 
a/regression-test/suites/nereids_p0/aggregate/aggregate_groupby_constant.groovy 
b/regression-test/suites/nereids_p0/aggregate/aggregate_groupby_constant.groovy
new file mode 100644
index 00000000000..9b1f186a4cc
--- /dev/null
+++ 
b/regression-test/suites/nereids_p0/aggregate/aggregate_groupby_constant.groovy
@@ -0,0 +1,50 @@
+// 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.
+
+suite("aggregate_groupby_constant") {
+    sql "SET enable_nereids_planner=true"
+    sql "SET enable_fallback_to_original_planner=false"
+
+    sql """ DROP TABLE IF EXISTS 
table_500_undef_partitions2_keys3_properties4_distributed_by5; """
+    sql """
+            create table 
table_500_undef_partitions2_keys3_properties4_distributed_by5 (
+            col_date_undef_signed date   ,
+            pk int,
+            col_int_undef_signed int   ,
+            col_int_undef_signed2 int   ,
+            col_date_undef_signed2 date   ,
+            col_varchar_10__undef_signed varchar(10)   ,
+            col_varchar_1024__undef_signed varchar(1024)   
+            ) engine=olap
+            DUPLICATE KEY(col_date_undef_signed, pk, col_int_undef_signed)
+            PARTITION BY RANGE(col_date_undef_signed) ( FROM ('2023-12-09') TO 
('2027-12-09') INTERVAL 1 DAY )
+            distributed by hash(pk) buckets 10
+            properties("replication_num" = "1");
+        """
+    sql """SELECT table1 . col_int_undef_signed2 AS field1,
+                ( TO_DATE (CASE
+                WHEN ( '2024-01-08' < '2024-02-18' ) THEN
+                '2023-12-19'
+                WHEN ( table1 . `col_date_undef_signed2` < DATE_ADD ( table1 . 
`col_date_undef_signed` , INTERVAL 6 DAY ) ) THEN
+                '2026-02-18'
+                ELSE DATE_ADD( table1 . `col_date_undef_signed` , INTERVAL 365 
DAY ) END)) AS field2, MAX( DISTINCT table1 . `col_varchar_10__undef_signed` ) 
AS field3
+            FROM table_500_undef_partitions2_keys3_properties4_distributed_by5 
AS table1
+            WHERE ( ( table1 . `col_date_undef_signed` is NOT NULL )
+                    OR table1 . `col_int_undef_signed` <> NULL )
+            GROUP BY  field1,field2
+            ORDER BY  field1,field2 LIMIT 10000;"""
+}


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

Reply via email to