Skip to content

Commit a8e9c89

Browse files
[Fix](nereids) fix NormalizeAgg, change the upper project projections rewrite logic (apache#36161) (apache#36622)
cherry-pick apache#36161 to branch-2.1 NormalizeAggregate rewrite logic has a bug, for sql like this: SELECT CASE 1 WHEN CAST( NULL AS SIGNED ) THEN NULL WHEN COUNT( DISTINCT CAST( NULL AS SIGNED ) ) THEN NULL ELSE null END ; This is the plan after NormalizeAggregate, the LogicalAggregate only output `count(DISTINCT cast(NULL as SIGNED))`apache#3, do not output cast(NULL as SIGNED)apache#2, but the upper project use cast(NULL as SIGNED)apache#2, so Doris report error "cast(NULL as SIGNED) not in aggregate's output". LogicalResultSink[29] ( outputExprs=[__case_when_0#1] ) +--LogicalProject[26] ( distinct=false, projects=[CASE WHEN (1 = cast(NULL as SIGNED)apache#2) THEN NULL WHEN (1 = count(DISTINCT cast(NULL as SIGNED))apache#3) THEN NULL ELSE NULL END AS `CASE WHEN (1 = cast(NULL as SIGNED)) THEN NULL WHEN (1 = count(DISTINCT cast(NULL as SIGNED))) THEN NULL ELSE NULL END`#1], excepts=[] ) +--LogicalAggregate[25] ( groupByExpr=[], outputExpr=[count(DISTINCT cast(NULL as SIGNED)apache#2) AS `count(DISTINCT cast(NULL as SIGNED))`apache#3], hasRepeat=false ) +--LogicalProject[24] ( distinct=false, projects=[cast(NULL as SIGNED) AS `cast(NULL as SIGNED)`apache#2], excepts=[] ) +--LogicalOneRowRelation ( projects=[0 AS `0`#0] ) The problem is that the cast(NULL as SIGNED)apache#2 should not outputted by LogicalAggregate, cast(NULL as SIGNED) should be computed in LogicalProject. This pr change the upper project projections rewrite logic: aggregateOutputs is rewritten and become the upper-level LogicalProject projections. During the rewriting process, the expressions inside the agg function can be rewritten with expressions in aggregate function arguments and group by expressions, but the ones outside the agg function can only be rewritten with group by expressions. --------- Co-authored-by: moailing <[email protected]>
1 parent 23cf494 commit a8e9c89

File tree

3 files changed

+77
-9
lines changed

3 files changed

+77
-9
lines changed

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

+48-9
Original file line numberDiff line numberDiff line change
@@ -190,10 +190,14 @@ private LogicalPlan normalizeAgg(LogicalAggregate<Plan> aggregate, Optional<Logi
190190
// 1. group by exprs
191191
// 2. trivialAgg children
192192
// 3. trivialAgg input slots
193-
Set<Expression> allPushDownExprs =
194-
Sets.union(groupingByExprs, Sets.union(needPushSelf, needPushInputSlots));
195-
NormalizeToSlotContext bottomSlotContext =
196-
NormalizeToSlotContext.buildContext(existsAlias, allPushDownExprs);
193+
// We need to distinguish between expressions in aggregate function arguments and group by expressions.
194+
NormalizeToSlotContext groupByExprContext = NormalizeToSlotContext.buildContext(existsAlias, groupingByExprs);
195+
Set<Alias> existsAliasAndGroupByAlias = getExistsAlias(existsAlias, groupByExprContext.getNormalizeToSlotMap());
196+
Set<Expression> argsOfAggFuncNeedPushDown = Sets.union(needPushSelf, needPushInputSlots);
197+
NormalizeToSlotContext argsOfAggFuncNeedPushDownContext = NormalizeToSlotContext
198+
.buildContext(existsAliasAndGroupByAlias, argsOfAggFuncNeedPushDown);
199+
NormalizeToSlotContext bottomSlotContext = argsOfAggFuncNeedPushDownContext.mergeContext(groupByExprContext);
200+
197201
Set<NamedExpression> pushedGroupByExprs =
198202
bottomSlotContext.pushDownToNamedExpression(groupingByExprs);
199203
Set<NamedExpression> pushedTrivialAggChildren =
@@ -256,8 +260,12 @@ private LogicalPlan normalizeAgg(LogicalAggregate<Plan> aggregate, Optional<Logi
256260
aggregate.withNormalized(normalizedGroupExprs, normalizedAggOutput, bottomPlan);
257261

258262
// create upper projects by normalize all output exprs in old LogicalAggregate
263+
// In aggregateOutput, the expressions inside the agg function can be rewritten
264+
// with expressions in aggregate function arguments and group by expressions,
265+
// but the ones outside the agg function can only be rewritten with group by expressions.
266+
// After the above two rewrites are completed, use aggregate output agg functions to rewrite.
259267
List<NamedExpression> upperProjects = normalizeOutput(aggregateOutput,
260-
bottomSlotContext, normalizedAggFuncsToSlotContext);
268+
groupByExprContext, argsOfAggFuncNeedPushDownContext, normalizedAggFuncsToSlotContext);
261269

262270
// create a parent project node
263271
LogicalProject<Plan> project = new LogicalProject<>(upperProjects, newAggregate);
@@ -302,11 +310,18 @@ private LogicalPlan normalizeAgg(LogicalAggregate<Plan> aggregate, Optional<Logi
302310
}
303311

304312
private List<NamedExpression> normalizeOutput(List<NamedExpression> aggregateOutput,
305-
NormalizeToSlotContext groupByToSlotContext, NormalizeToSlotContext normalizedAggFuncsToSlotContext) {
313+
NormalizeToSlotContext groupByToSlotContext, NormalizeToSlotContext argsOfAggFuncNeedPushDownContext,
314+
NormalizeToSlotContext normalizedAggFuncsToSlotContext) {
306315
// build upper project, use two context to do pop up, because agg output maybe contain two part:
307-
// group by keys and agg expressions
308-
List<NamedExpression> upperProjects = groupByToSlotContext
309-
.normalizeToUseSlotRefWithoutWindowFunction(aggregateOutput);
316+
// group by keys and agg expressions
317+
List<NamedExpression> upperProjects = new ArrayList<>();
318+
for (Expression expr : aggregateOutput) {
319+
Expression rewrittenExpr = expr.rewriteDownShortCircuit(
320+
e -> normalizeAggFuncChildren(
321+
argsOfAggFuncNeedPushDownContext, e));
322+
upperProjects.add((NamedExpression) rewrittenExpr);
323+
}
324+
upperProjects = groupByToSlotContext.normalizeToUseSlotRefWithoutWindowFunction(upperProjects);
310325
upperProjects = normalizedAggFuncsToSlotContext.normalizeToUseSlotRefWithoutWindowFunction(upperProjects);
311326

312327
Builder<NamedExpression> builder = new ImmutableList.Builder<>();
@@ -338,4 +353,28 @@ private List<Slot> collectAllUsedSlots(List<NamedExpression> expressions) {
338353
slots.addAll(ExpressionUtils.getInputSlotSet(expressions));
339354
return slots;
340355
}
356+
357+
private Set<Alias> getExistsAlias(Set<Alias> originAliases,
358+
Map<Expression, NormalizeToSlotTriplet> groupingExprMap) {
359+
Set<Alias> existsAlias = Sets.newHashSet();
360+
existsAlias.addAll(originAliases);
361+
for (NormalizeToSlotTriplet triplet : groupingExprMap.values()) {
362+
if (triplet.pushedExpr instanceof Alias) {
363+
Alias alias = (Alias) triplet.pushedExpr;
364+
existsAlias.add(alias);
365+
}
366+
}
367+
return existsAlias;
368+
}
369+
370+
private Expression normalizeAggFuncChildren(NormalizeToSlotContext context, Expression expr) {
371+
if (expr instanceof AggregateFunction) {
372+
AggregateFunction function = (AggregateFunction) expr;
373+
List<Expression> normalizedRealExpressions = context.normalizeToUseSlotRef(function.getArguments());
374+
function = function.withChildren(normalizedRealExpressions);
375+
return function;
376+
} else {
377+
return expr;
378+
}
379+
}
341380
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
-- This file is automatically generated. You should know what you did if you want to edit this
2+
-- !test_upper_project_projections_rewrite2 --
3+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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+
suite("normalize_aggregate") {
18+
sql "SET enable_nereids_planner=true"
19+
sql "SET enable_fallback_to_original_planner=false"
20+
sql "drop table if exists normalize_aggregate_tab"
21+
sql """CREATE TABLE normalize_aggregate_tab(col0 INTEGER, col1 INTEGER, col2 INTEGER) distributed by hash(col0) buckets 10
22+
properties('replication_num' = '1'); """
23+
qt_test_upper_project_projections_rewrite2 """
24+
SELECT - + AVG ( DISTINCT - col0 ) * - col0 FROM
25+
normalize_aggregate_tab WHERE + - col0 IS NULL GROUP BY col0 HAVING NULL IS NULL;"""
26+
}

0 commit comments

Comments
 (0)