From bc015ef6ef8ea86ee3d7563169070131b75d9957 Mon Sep 17 00:00:00 2001 From: feiniaofeiafei Date: Wed, 26 Mar 2025 14:32:30 +0800 Subject: [PATCH] [fix](nereids) EliminateGroupByConstant should replace agg's group by after removing constant group by keys (#49473) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ### What problem does this PR solve? Issue Number: close #xxx Related PR: #xxx Problem Summary: ### Release note ```sql SELECT IF( t.`gender` IN ('女'), ( TIMESTAMPDIFF( YEAR, NOW(), NOW() ) ), 1 ) AS x0, TIMESTAMPDIFF( YEAR, NOW(), NOW() ) AS x1 FROM t1 AS t GROUP BY x0, x1; ``` after EliminateGroupByConstant, this sql will be rewritten to ```sql SELECT IF( t.`gender` IN ('女'), 0, 1 ) AS x0, 0 AS x1 FROM t1 AS t GROUP BY IF( t.`gender` IN ('女'), ( TIMESTAMPDIFF( YEAR, NOW(), NOW() ) ), 1 ) ; ``` The select expression and the group by expression is different, and will report error in normalizeagg. This pr changes using the foldmap rewrite the group by expresssion, and after change the sql after EliminateGroupByConstant become: ```sql SELECT IF( t.`gender` IN ('女'), 0, 1 ) AS x0, 0 AS x1 FROM t1 AS t GROUP BY IF( t.`gender` IN ('女'), 0, 1 ) ; ``` the select expression and the group by expression becomes same. --- .../analysis/EliminateGroupByConstant.java | 5 ++-- .../aggregate_groupby_constant.groovy | 26 +++++++++++++++++++ 2 files changed, 29 insertions(+), 2 deletions(-) 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 7f7e229da319f4..a58d98ff1217bd 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 @@ -27,8 +27,8 @@ 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 org.apache.doris.nereids.util.Utils; -import com.google.common.collect.ImmutableList; import com.google.common.collect.Sets; import java.util.HashMap; @@ -75,7 +75,8 @@ public Rule build() { if (slotGroupByExprs.isEmpty() && lit != null) { slotGroupByExprs.add(lit); } - return aggregate.withGroupByAndOutput(ImmutableList.copyOf(slotGroupByExprs), + return aggregate.withGroupByAndOutput( + ExpressionUtils.replace(Utils.fastToImmutableList(slotGroupByExprs), constantExprsReplaceMap), 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 index 9b1f186a4cc5eb..bbc3cd8a72ae46 100644 --- a/regression-test/suites/nereids_p0/aggregate/aggregate_groupby_constant.groovy +++ b/regression-test/suites/nereids_p0/aggregate/aggregate_groupby_constant.groovy @@ -18,6 +18,7 @@ suite("aggregate_groupby_constant") { sql "SET enable_nereids_planner=true" sql "SET enable_fallback_to_original_planner=false" + sql "set disable_nereids_rules='prune_empty_partition'" sql """ DROP TABLE IF EXISTS table_500_undef_partitions2_keys3_properties4_distributed_by5; """ sql """ @@ -47,4 +48,29 @@ suite("aggregate_groupby_constant") { OR table1 . `col_int_undef_signed` <> NULL ) GROUP BY field1,field2 ORDER BY field1,field2 LIMIT 10000;""" + + sql """ + SELECT + IF( + t.`col_varchar_10__undef_signed` IN ('女'), + ( + TIMESTAMPDIFF( + YEAR, + NOW(), + NOW() + ) + ), + 1 + ) AS x0, + TIMESTAMPDIFF( + YEAR, + NOW(), + NOW() + ) AS x1 + FROM + table_500_undef_partitions2_keys3_properties4_distributed_by5 AS t + GROUP BY + x0, + x1; + """ }