Skip to content

Commit 8e40532

Browse files
authored
[fix](Nereids) should not put bound expr into unbound group by list (#25938) (#26222)
we put bound expr into unbound group by list by mistake. This will lead to bind twice on some exprssion. Since binding is not idempotent, below exception will be thrown for sql ```sql select k5 / k5 as nu, sum(k1) from test group by nu order by nu nulls first ``` ``` Caused by: org.apache.doris.nereids.exceptions.AnalysisException: Input slot(s) not in child's output: k5#5 in plan: LogicalProject[176] ( distinct=false, projects=[(cast(k5#5 as DECIMALV3(16, 10)) / k5#5) AS `nu`#14, sum(k1)#15], excepts=[] ), child output is: [nu#16, sum(k1)#15] plan tree: LogicalProject[176] ( distinct=false, projects=[(cast(k5#5 as DECIMALV3(16, 10)) / k5#5) AS `nu`#14, sum(k1)#15], excepts=[] ) +--LogicalAggregate[168] ( groupByExpr=[nu#16], outputExpr=[nu#16, sum(k1#1) AS `sum(k1)`#15], hasRepeat=false ) +--LogicalProject[156] ( distinct=false, projects=[k1#1, (cast(k5#5 as DECIMALV3(16, 10)) / k5#5) AS `nu`#16], excepts=[] ) +--LogicalOlapScan ( qualified=default_cluster:regression_test_nereids_syntax_p0.test, indexName=test, selectedIndexId=503229, preAgg=OFF, Aggregate function sum(k1) contains key column k1. ) at org.apache.doris.nereids.rules.analysis.CheckAfterRewrite.checkAllSlotReferenceFromChildren(CheckAfterRewrite.java:108) ~[classes/:?] ```
1 parent b27e07a commit 8e40532

2 files changed

Lines changed: 16 additions & 8 deletions

File tree

‎fe/fe-core/src/main/java/org/apache/doris/nereids/rules/analysis/BindExpression.java‎

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -272,13 +272,17 @@ protected boolean condition(Rule rule, Plan plan) {
272272
group_by_key is bound on t1.a
273273
*/
274274
duplicatedSlotNames.forEach(childOutputsToExpr::remove);
275-
output.stream()
276-
.filter(ne -> ne instanceof Alias)
277-
.map(Alias.class::cast)
278-
// agg function cannot be bound with group_by_key
279-
.filter(alias -> !alias.child()
280-
.anyMatch(expr -> expr instanceof AggregateFunction))
281-
.forEach(alias -> childOutputsToExpr.putIfAbsent(alias.getName(), alias.child()));
275+
for (int i = 0; i < output.size(); i++) {
276+
if (!(output.get(i) instanceof Alias)) {
277+
continue;
278+
}
279+
Alias alias = (Alias) output.get(i);
280+
if (alias.child().anyMatch(expr -> expr instanceof AggregateFunction)) {
281+
continue;
282+
}
283+
// NOTICE: must use unbound expressions, because we will bind them in binding group by expr.
284+
childOutputsToExpr.putIfAbsent(alias.getName(), agg.getOutputExpressions().get(i).child(0));
285+
}
282286

283287
List<Expression> replacedGroupBy = agg.getGroupByExpressions().stream()
284288
.map(groupBy -> {

‎regression-test/suites/nereids_syntax_p0/analyze_agg.groovy‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ suite("analyze_agg") {
4343
d VARCHAR(30),
4444
e VARCHAR(32),
4545
a VARCHAR(32),
46-
f VARCHAR(32)
46+
f VARCHAR(32),
47+
g DECIMAL(9, 3)
4748
)ENGINE = OLAP
4849
UNIQUE KEY(id)
4950
DISTRIBUTED BY HASH(id) BUCKETS 30
@@ -73,4 +74,7 @@ suite("analyze_agg") {
7374
sql "select count(distinct t2.b), variance(distinct t2.c) from t2"
7475
exception "variance(DISTINCT c#2) can't support multi distinct."
7576
}
77+
78+
// should not bind g /g in group by again, otherwise will throw exception
79+
sql "select g / g as nu, sum(c) from t2 group by nu"
7680
}

0 commit comments

Comments
 (0)