diff --git a/fe/fe-core/src/main/java/org/apache/doris/planner/SingleNodePlanner.java b/fe/fe-core/src/main/java/org/apache/doris/planner/SingleNodePlanner.java index 72fbd85979..1003991f46 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/planner/SingleNodePlanner.java +++ b/fe/fe-core/src/main/java/org/apache/doris/planner/SingleNodePlanner.java @@ -2667,8 +2667,10 @@ public class SingleNodePlanner { //eg: select distinct c from ( select distinct c from table) t where c > 1; continue; } - if (stmt.getGroupByClause().isGroupByExtension() - && stmt.getGroupByClause().getGroupingExprs().contains(sourceExpr)) { + + if (sourceExpr.getFn() instanceof AggregateFunction) { + isAllSlotReferToGroupBys = false; + } else if (stmt.getGroupByClause().isGroupByExtension()) { // if grouping type is CUBE or ROLLUP will definitely produce null if (stmt.getGroupByClause().getGroupingType() == GroupByClause.GroupingType.CUBE || stmt.getGroupByClause().getGroupingType() == GroupByClause.GroupingType.ROLLUP) { @@ -2684,9 +2686,6 @@ public class SingleNodePlanner { } } } - if (sourceExpr.getFn() instanceof AggregateFunction) { - isAllSlotReferToGroupBys = false; - } } if (isAllSlotReferToGroupBys) { diff --git a/regression-test/data/query_p0/grouping_sets/test_grouping_sets.out b/regression-test/data/query_p0/grouping_sets/test_grouping_sets.out index f2da1d2f67..564103d871 100644 --- a/regression-test/data/query_p0/grouping_sets/test_grouping_sets.out +++ b/regression-test/data/query_p0/grouping_sets/test_grouping_sets.out @@ -49,3 +49,6 @@ -- !select7 -- +-- !select8 -- +test 2 + diff --git a/regression-test/suites/query_p0/grouping_sets/test_grouping_sets.groovy b/regression-test/suites/query_p0/grouping_sets/test_grouping_sets.groovy index 83e517de53..d0c28ea703 100644 --- a/regression-test/suites/query_p0/grouping_sets/test_grouping_sets.groovy +++ b/regression-test/suites/query_p0/grouping_sets/test_grouping_sets.groovy @@ -60,4 +60,22 @@ suite("test_grouping_sets") { } qt_select7 """ select k1,k2,sum(k3) from test_query_db.test where 1 = 2 group by grouping sets((k1), (k1,k2)) """ + + qt_select8 """ WITH dt AS + (select 'test' as name,1 as score + UNION + all + SELECT 'test' AS name,1 AS score + UNION + all SELECT 'test2' AS name,12 AS score + UNION + all SELECT 'test2' AS name,12 AS score ) ,result_data AS + (SELECT name, + sum(score) AS score + FROM dt + GROUP BY CUBE(name)) + SELECT * + FROM result_data + WHERE name = 'test'; + """ }