Skip to content

Commit badc655

Browse files
add rule decompose_repeat
1 parent 262b16e commit badc655

File tree

7 files changed

+598
-2
lines changed

7 files changed

+598
-2
lines changed

fe/fe-core/src/main/java/org/apache/doris/nereids/jobs/executor/Rewriter.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
import org.apache.doris.nereids.rules.rewrite.CountDistinctRewrite;
6161
import org.apache.doris.nereids.rules.rewrite.CountLiteralRewrite;
6262
import org.apache.doris.nereids.rules.rewrite.CreatePartitionTopNFromWindow;
63+
import org.apache.doris.nereids.rules.rewrite.DecomposeRepeatWithPreAggregation;
6364
import org.apache.doris.nereids.rules.rewrite.DecoupleEncodeDecode;
6465
import org.apache.doris.nereids.rules.rewrite.DeferMaterializeTopNResult;
6566
import org.apache.doris.nereids.rules.rewrite.DistinctAggStrategySelector;
@@ -902,7 +903,8 @@ private static List<RewriteJob> getWholeTreeRewriteJobs(
902903
rewriteJobs.addAll(jobs(topic("or expansion",
903904
custom(RuleType.OR_EXPANSION, () -> OrExpansion.INSTANCE))));
904905
}
905-
906+
rewriteJobs.add(topic("repeat rewrite",
907+
custom(RuleType.DECOMPOSE_REPEAT, () -> DecomposeRepeatWithPreAggregation.INSTANCE)));
906908
rewriteJobs.addAll(jobs(topic("split multi distinct",
907909
custom(RuleType.DISTINCT_AGG_STRATEGY_SELECTOR, () -> DistinctAggStrategySelector.INSTANCE))));
908910

fe/fe-core/src/main/java/org/apache/doris/nereids/rules/RuleType.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -415,7 +415,7 @@ public enum RuleType {
415415
SHOR_CIRCUIT_POINT_QUERY(RuleTypeClass.REWRITE),
416416
// skew rewrtie
417417
SALT_JOIN(RuleTypeClass.REWRITE),
418-
418+
DECOMPOSE_REPEAT(RuleTypeClass.REWRITE),
419419
DISTINCT_AGGREGATE_SPLIT(RuleTypeClass.REWRITE),
420420
PROCESS_SCALAR_AGG_MUST_USE_MULTI_DISTINCT(RuleTypeClass.REWRITE),
421421

fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/DecomposeRepeatWithPreAggregation.java

Lines changed: 339 additions & 0 deletions
Large diffs are not rendered by default.

fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/Alias.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,10 @@ public Alias withChildren(List<Expression> children) {
171171
return new Alias(exprId, children, name, qualifier, nameFromChild);
172172
}
173173

174+
public Alias withExprId(ExprId exprId) {
175+
return new Alias(exprId, children, name, qualifier, nameFromChild);
176+
}
177+
174178
public <R, C> R accept(ExpressionVisitor<R, C> visitor, C context) {
175179
return visitor.visitAlias(this, context);
176180
}
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
// Licensed to the Apache Software Foundation (ASF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The ASF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
package org.apache.doris.nereids.rules.rewrite;
19+
20+
import org.apache.doris.nereids.util.MemoPatternMatchSupported;
21+
import org.apache.doris.nereids.util.PlanChecker;
22+
import org.apache.doris.utframe.TestWithFeService;
23+
24+
import org.junit.jupiter.api.Test;
25+
26+
/**
27+
* UT for {@link DecomposeRepeatWithPreAggregation}.
28+
*/
29+
public class DecomposeRepeatWithPreAggregationTest extends TestWithFeService implements MemoPatternMatchSupported {
30+
31+
@Override
32+
protected void runBeforeAll() throws Exception {
33+
createDatabase("decompose_repeat_with_preagg");
34+
createTable(
35+
"create table decompose_repeat_with_preagg.t1 (\n"
36+
+ "a int, b int, c int, d int\n"
37+
+ ")\n"
38+
+ "distributed by hash(a) buckets 1\n"
39+
+ "properties('replication_num' = '1');"
40+
);
41+
connectContext.setDatabase("default_cluster:decompose_repeat_with_preagg");
42+
connectContext.getSessionVariable().setDisableNereidsRules("PRUNE_EMPTY_PARTITION");
43+
}
44+
45+
@Test
46+
void rewriteRollupSumShouldGenerateCteAndUnion() {
47+
String sql = "select a,b,c,sum(d) from t1 group by rollup(a,b,c);";
48+
PlanChecker.from(connectContext)
49+
.analyze(sql)
50+
.rewrite()
51+
.matches(logicalCTEAnchor());
52+
}
53+
54+
@Test
55+
void noRewriteWhenGroupingSetsSizeLe3() {
56+
String sql = "select a,b,sum(d) from t1 group by rollup(a,b);";
57+
PlanChecker.from(connectContext)
58+
.analyze(sql)
59+
.rewrite()
60+
.nonMatch(logicalCTEAnchor());
61+
}
62+
63+
@Test
64+
void noRewriteWhenDistinctAgg() {
65+
String sql = "select a,b,c,sum(distinct d) from t1 group by rollup(a,b,c);";
66+
PlanChecker.from(connectContext)
67+
.analyze(sql)
68+
.rewrite()
69+
.nonMatch(logicalCTEAnchor());
70+
}
71+
72+
@Test
73+
void noRewriteWhenUnsupportedAgg() {
74+
String sql = "select a,b,c,count(d) from t1 group by rollup(a,b,c);";
75+
PlanChecker.from(connectContext)
76+
.analyze(sql)
77+
.rewrite()
78+
.nonMatch(logicalCTEAnchor());
79+
80+
}
81+
82+
@Test
83+
void noRewriteWhenHasGroupingScalarFunction() {
84+
String sql = "select a,b,c,sum(d),grouping_id(a) from t1 group by rollup(a,b,c);";
85+
PlanChecker.from(connectContext)
86+
.analyze(sql)
87+
.rewrite()
88+
.nonMatch(logicalCTEAnchor());
89+
}
90+
91+
@Test
92+
void rewriteWhenMaxGroupingSetNotFirst() {
93+
String sql = "select a,b,c,sum(d) from t1 group by grouping sets((a),(a,b,c),(a,b),());";
94+
PlanChecker.from(connectContext)
95+
.analyze(sql)
96+
.rewrite()
97+
.matches(logicalCTEAnchor());
98+
}
99+
}
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
-- This file is automatically generated. You should know what you did if you want to edit this
2+
-- !sum --
3+
\N \N \N 10
4+
1 \N \N 10
5+
1 2 \N 8
6+
1 2 1 1
7+
1 2 3 7
8+
1 3 \N 2
9+
1 3 2 2
10+
11+
-- !agg_func_gby_key_same_col --
12+
\N \N \N \N 10
13+
1 \N \N \N 10
14+
1 2 \N \N 8
15+
1 2 1 \N 1
16+
1 2 1 1 1
17+
1 2 3 \N 7
18+
1 2 3 3 3
19+
1 2 3 4 4
20+
1 3 \N \N 2
21+
1 3 2 \N 2
22+
1 3 2 2 2
23+
24+
-- !multi_agg_func --
25+
\N \N \N 10 9 1
26+
1 \N \N 10 9 1
27+
1 2 \N 8 7 1
28+
1 2 1 1 1 1
29+
1 2 1 1 1 1
30+
1 2 3 3 3 1
31+
1 2 3 4 3 1
32+
1 2 3 7 6 1
33+
1 3 \N 2 2 1
34+
1 3 2 2 2 1
35+
1 3 2 2 2 1
36+
37+
-- !nest_rewrite --
38+
\N \N \N \N
39+
1 \N \N \N
40+
1 \N \N \N
41+
1 \N \N \N
42+
1 \N \N 10
43+
1 2 \N \N
44+
1 2 1 \N
45+
1 2 1 1
46+
1 2 3 \N
47+
1 2 3 3
48+
1 2 3 4
49+
1 2 3 7
50+
1 3 \N \N
51+
1 3 2 \N
52+
1 3 2 2
53+
54+
-- !upper_ref --
55+
11 1 2 1
56+
12 1 3 \N
57+
12 1 3 2
58+
17 1 2 3
59+
18 1 2 \N
60+
20 \N \N \N
61+
20 1 \N \N
62+
63+
-- !grouping_func --
64+
1 \N \N \N 10 0
65+
1 2 1 \N 1 0
66+
1 2 1 \N 1 0
67+
1 2 1 1 1 0
68+
1 2 3 \N 7 0
69+
1 2 3 \N 7 0
70+
1 2 3 3 3 0
71+
1 2 3 4 4 0
72+
1 3 2 \N 2 0
73+
1 3 2 \N 2 0
74+
1 3 2 2 2 0
75+
76+
-- !avg --
77+
1 \N \N \N 2.5
78+
1 2 1 \N 1
79+
1 2 1 \N 1
80+
1 2 1 1 1
81+
1 2 3 \N 3.5
82+
1 2 3 \N 3.5
83+
1 2 3 3 3
84+
1 2 3 4 4
85+
1 3 2 \N 2
86+
1 3 2 \N 2
87+
1 3 2 2 2
88+
89+
-- !distinct --
90+
1 \N \N \N 10
91+
1 2 1 \N 1
92+
1 2 1 \N 1
93+
1 2 1 1 1
94+
1 2 3 \N 7
95+
1 2 3 \N 7
96+
1 2 3 3 3
97+
1 2 3 4 4
98+
1 3 2 \N 2
99+
1 3 2 \N 2
100+
1 3 2 2 2
101+
102+
-- !less_equal_than_3 --
103+
\N \N \N \N 10
104+
1 2 1 \N 1
105+
1 2 1 1 1
106+
1 2 3 \N 7
107+
1 2 3 3 3
108+
1 2 3 4 4
109+
1 3 2 \N 2
110+
1 3 2 2 2
111+
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// Licensed to the Apache Software Foundation (ASF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The ASF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
suite("decompose_repeat") {
19+
// sql "set disable_nereids_rules='DECOMPOSE_REPEAT';"
20+
sql "drop table if exists t1;"
21+
sql "create table t1(a int, b int, c int, d int) distributed by hash(a) properties('replication_num'='1');"
22+
sql "insert into t1 values(1,2,3,4),(1,2,3,3),(1,2,1,1),(1,3,2,2);"
23+
order_qt_sum "select a,b,c,sum(d) from t1 group by rollup(a,b,c);"
24+
order_qt_agg_func_gby_key_same_col "select a,b,c,d,sum(d) from t1 group by rollup(a,b,c,d);"
25+
order_qt_multi_agg_func "select a,b,c,sum(d),sum(c),max(a) from t1 group by rollup(a,b,c,d);"
26+
order_qt_nest_rewrite """
27+
select a,b,c,c1 from (
28+
select a,b,c,d,sum(d) c1 from t1 group by grouping sets((a,b,c),(a,b,c,d),(a),(a,b,c,c))
29+
) t group by rollup(a,b,c,c1);
30+
"""
31+
order_qt_upper_ref """
32+
select c1+10,a,b,c from (select a,b,c,sum(d) c1 from t1 group by rollup(a,b,c)) t group by c1+10,a,b,c;
33+
"""
34+
35+
// negative case
36+
order_qt_grouping_func "select a,b,c,d,sum(d),grouping_id(a) from t1 group by grouping sets((a,b,c),(a,b,c,d),(a),(a,b,c,c))"
37+
order_qt_avg "select a,b,c,d,avg(d) from t1 group by grouping sets((a,b,c),(a,b,c,d),(a),(a,b,c,c));"
38+
order_qt_distinct "select a,b,c,d,sum(distinct d) from t1 group by grouping sets((a,b,c),(a,b,c,d),(a),(a,b,c,c));"
39+
order_qt_less_equal_than_3 "select a,b,c,d,sum(distinct d) from t1 group by grouping sets((a,b,c),(a,b,c,d),());"
40+
41+
}

0 commit comments

Comments
 (0)