Skip to content

Commit c0ed5f7

Browse files
authored
[fix](Nereids) storage later agg rule process agg children by mistake (apache#26101)
update Project#findProject agg function's children could be any expression rather than only slot. we use Project#findProject to process them. But this util could only process slot. This PR update this util to let it could process all type expression.
1 parent 1e2a614 commit c0ed5f7

5 files changed

Lines changed: 24 additions & 20 deletions

File tree

‎fe/fe-core/src/main/java/org/apache/doris/nereids/rules/implementation/AggregateStrategies.java‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -397,7 +397,7 @@ private LogicalAggregate<? extends Plan> storageLayerAggregate(
397397

398398
if (project != null) {
399399
argumentsOfAggregateFunction = Project.findProject(
400-
(List<SlotReference>) (List) argumentsOfAggregateFunction, project.getProjects())
400+
argumentsOfAggregateFunction, project.getProjects())
401401
.stream()
402402
.map(p -> p instanceof Alias ? p.child(0) : p)
403403
.collect(ImmutableList.toImmutableList());
@@ -431,8 +431,8 @@ private LogicalAggregate<? extends Plan> storageLayerAggregate(
431431
Set<SlotReference> aggUsedSlots =
432432
ExpressionUtils.collect(argumentsOfAggregateFunction, SlotReference.class::isInstance);
433433

434-
List<SlotReference> usedSlotInTable = (List<SlotReference>) (List) Project.findProject(aggUsedSlots,
435-
(List<NamedExpression>) (List) logicalScan.getOutput());
434+
List<SlotReference> usedSlotInTable = (List<SlotReference>) Project.findProject(aggUsedSlots,
435+
logicalScan.getOutput());
436436

437437
for (SlotReference slot : usedSlotInTable) {
438438
Column column = slot.getColumn().get();

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ public Rule build() {
4141
if (!onlyHasSlots(outerAgg.getOutputExpressions())) {
4242
return outerAgg;
4343
}
44-
List<NamedExpression> prunedInnerAggOutput = Project.findProject(outerAgg.getOutputSet(),
45-
innerAgg.getOutputExpressions());
44+
List<NamedExpression> prunedInnerAggOutput = (List<NamedExpression>) Project.findProject(
45+
outerAgg.getOutputSet(), innerAgg.getOutputExpressions());
4646
return innerAgg.withAggOutput(prunedInnerAggOutput);
4747
}).toRule(RuleType.ELIMINATE_AGGREGATE);
4848
}

‎fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/algebra/Project.java‎

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@
2323
import org.apache.doris.nereids.trees.expressions.Expression;
2424
import org.apache.doris.nereids.trees.expressions.NamedExpression;
2525
import org.apache.doris.nereids.trees.expressions.Slot;
26+
import org.apache.doris.nereids.util.ExpressionUtils;
2627
import org.apache.doris.nereids.util.PlanUtils;
2728

28-
import com.google.common.collect.ImmutableList;
2929
import com.google.common.collect.ImmutableMap;
3030

3131
import java.util.Collection;
@@ -78,22 +78,25 @@ default List<NamedExpression> mergeProjections(Project childProject) {
7878
/**
7979
* find projects, if not found the slot, then throw AnalysisException
8080
*/
81-
static List<NamedExpression> findProject(
82-
Collection<? extends Slot> slotReferences,
83-
List<NamedExpression> projects) throws AnalysisException {
81+
static List<? extends Expression> findProject(
82+
Collection<? extends Expression> expressions,
83+
List<? extends NamedExpression> projects) throws AnalysisException {
8484
Map<ExprId, NamedExpression> exprIdToProject = projects.stream()
85-
.collect(ImmutableMap.toImmutableMap(p -> p.getExprId(), p -> p));
85+
.collect(ImmutableMap.toImmutableMap(NamedExpression::getExprId, p -> p));
8686

87-
return slotReferences.stream()
88-
.map(slot -> {
89-
ExprId exprId = slot.getExprId();
90-
NamedExpression project = exprIdToProject.get(exprId);
91-
if (project == null) {
92-
throw new AnalysisException("ExprId " + slot.getExprId() + " no exists in " + projects);
87+
return ExpressionUtils.rewriteDownShortCircuit(expressions,
88+
expr -> {
89+
if (expr instanceof Slot) {
90+
Slot slot = (Slot) expr;
91+
ExprId exprId = slot.getExprId();
92+
NamedExpression project = exprIdToProject.get(exprId);
93+
if (project == null) {
94+
throw new AnalysisException("ExprId " + slot.getExprId() + " no exists in " + projects);
95+
}
96+
return project;
9397
}
94-
return project;
95-
})
96-
.collect(ImmutableList.toImmutableList());
98+
return expr;
99+
});
97100
}
98101
}
99102

‎fe/fe-core/src/main/java/org/apache/doris/nereids/util/ExpressionUtils.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ public static Set<Expression> replace(Set<Expression> exprs,
282282
}
283283

284284
public static <E extends Expression> List<E> rewriteDownShortCircuit(
285-
List<E> exprs, Function<Expression, Expression> rewriteFunction) {
285+
Collection<E> exprs, Function<Expression, Expression> rewriteFunction) {
286286
return exprs.stream()
287287
.map(expr -> (E) expr.rewriteDownShortCircuit(rewriteFunction))
288288
.collect(ImmutableList.toImmutableList());

‎regression-test/suites/query_p0/aggregate/aggregate_decimal256.groovy‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
suite("aggregate_decimal256") {
1919
sql "set enable_nereids_planner = true;"
20+
sql "set enable_fallback_to_original_planner = false;"
2021
sql "set enable_decimal256 = true;"
2122
sql "drop table if exists test_aggregate_decimal256_sum;"
2223
sql """ create table test_aggregate_decimal256_sum(k1 int, v1 decimal(38, 6), v2 decimal(38, 6))

0 commit comments

Comments
 (0)