From a27688610afc7cc3f37365e24b65a3940294534f Mon Sep 17 00:00:00 2001 From: James Date: Thu, 19 Jun 2025 20:21:02 +0800 Subject: [PATCH] [improvement](nereids)Support GROUP BY ... WITH ROLLUP syntax (#51948) ### What problem does this PR solve? Support GROUP BY ... WITH ROLLUP syntax mysql> SELECT year, month, SUM(amount) AS total_amount FROM sales GROUP BY year, month WITH ROLLUP order by year desc, month desc; +------+-------+--------------+ | year | month | total_amount | +------+-------+--------------+ | 2024 | 1 | 3400.00 | | 2024 | NULL | 3400.00 | | 2023 | 2 | 4000.00 | | 2023 | 1 | 3000.00 | | 2023 | NULL | 7000.00 | | NULL | NULL | 10400.00 | +------+-------+--------------+ 6 rows in set (0.02 sec) --- .../org/apache/doris/nereids/DorisParser.g4 | 2 +- .../data/nereids_syntax_p0/grouping_sets.out | 29 +++++++++++++++++++ .../nereids_syntax_p0/grouping_sets.groovy | 9 ++++++ 3 files changed, 39 insertions(+), 1 deletion(-) diff --git a/fe/fe-core/src/main/antlr4/org/apache/doris/nereids/DorisParser.g4 b/fe/fe-core/src/main/antlr4/org/apache/doris/nereids/DorisParser.g4 index 67f3e7997fa9ef..7b3ecbafa7e27e 100644 --- a/fe/fe-core/src/main/antlr4/org/apache/doris/nereids/DorisParser.g4 +++ b/fe/fe-core/src/main/antlr4/org/apache/doris/nereids/DorisParser.g4 @@ -1199,7 +1199,7 @@ groupingElement : ROLLUP LEFT_PAREN (expression (COMMA expression)*)? RIGHT_PAREN | CUBE LEFT_PAREN (expression (COMMA expression)*)? RIGHT_PAREN | GROUPING SETS LEFT_PAREN groupingSet (COMMA groupingSet)* RIGHT_PAREN - | expression (COMMA expression)* + | expression (COMMA expression)* (WITH ROLLUP)? ; groupingSet diff --git a/regression-test/data/nereids_syntax_p0/grouping_sets.out b/regression-test/data/nereids_syntax_p0/grouping_sets.out index bbc2997e3c8a0b..973909743943a7 100644 --- a/regression-test/data/nereids_syntax_p0/grouping_sets.out +++ b/regression-test/data/nereids_syntax_p0/grouping_sets.out @@ -132,6 +132,35 @@ 13 0 7 0 +-- !select_with_rollup1 -- +\N \N 36 +2 \N 6 +2 0 \N +2 1 6 +3 \N 12 +3 2 12 +4 \N 18 +4 0 \N +4 3 18 + +-- !select_with_rollup2 -- +\N 1 +2 0 +3 0 +4 0 + +-- !select_with_rollup3 -- +3 0 +8 0 +9 0 +20 1 + +-- !select_with_rollup4 -- +\N 1 +2 0 +3 0 +4 0 + -- !select -- \N \N 36 \N 0 \N diff --git a/regression-test/suites/nereids_syntax_p0/grouping_sets.groovy b/regression-test/suites/nereids_syntax_p0/grouping_sets.groovy index 8ca787fabfb802..99d2ddc7439eb2 100644 --- a/regression-test/suites/nereids_syntax_p0/grouping_sets.groovy +++ b/regression-test/suites/nereids_syntax_p0/grouping_sets.groovy @@ -110,6 +110,15 @@ suite("test_nereids_grouping_sets") { order_qt_select "select sum(k2+1), grouping_id(k1+1) from groupingSetsTable group by grouping sets((k1+1)) having (k1+1) > 1;"; order_qt_select "select sum(k2+1), grouping_id(k1+1) from groupingSetsTable group by grouping sets((k1+1), (k1)) having (k1+1) > 1;"; + // with rollup + qt_select_with_rollup1 """ + select (k1 + 1) k1_, k2, sum(k3) from groupingSetsTable group by + k1_, k2 with rollup order by k1_, k2 + """ + qt_select_with_rollup2 "select k1+1, grouping(k1+1) from groupingSetsTable group by (k1+1) with rollup order by k1+1;"; + qt_select_with_rollup3 "select sum(k2), grouping(k1+1) from groupingSetsTable group by k1+1 with rollup order by sum(k2)" + qt_select_with_rollup4 "select k1+1, grouping_id(k1) from groupingSetsTable group by k1 with rollup order by k1+1;" + // old grouping sets qt_select """ SELECT k1, k2, SUM(k3) FROM groupingSetsTable