diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/MergeAggregate.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/MergeAggregate.java index 23fbd9786568eb..4b9e745ee201f4 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/MergeAggregate.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/MergeAggregate.java @@ -52,7 +52,6 @@ public class MergeAggregate implements RewriteRuleFactory { private static final ImmutableSet ALLOW_MERGE_AGGREGATE_FUNCTIONS = ImmutableSet.of("min", "max", "sum", "any_value"); - private Map innerAggExprIdToAggFunc = new HashMap<>(); @Override public List buildRules() { @@ -75,7 +74,7 @@ public List buildRules() { */ private Plan mergeTwoAggregate(LogicalAggregate> outerAgg) { LogicalAggregate innerAgg = outerAgg.child(); - + Map innerAggExprIdToAggFunc = getInnerAggExprIdToAggFuncMap(innerAgg); List newOutputExpressions = outerAgg.getOutputExpressions().stream() .map(e -> rewriteAggregateFunction(e, innerAggExprIdToAggFunc)) .collect(Collectors.toList()); @@ -97,6 +96,7 @@ private Plan mergeAggProjectAgg(LogicalAggregate outputExpressions = outerAgg.getOutputExpressions(); List replacedOutputExpressions = PlanUtils.replaceExpressionByProjections( project.getProjects(), (List) outputExpressions); + Map innerAggExprIdToAggFunc = getInnerAggExprIdToAggFuncMap(innerAgg); // rewrite agg function. e.g. max(max) List replacedAggFunc = replacedOutputExpressions.stream() .filter(expr -> (expr instanceof Alias) && (expr.child(0) instanceof AggregateFunction)) @@ -152,10 +152,7 @@ private NamedExpression rewriteAggregateFunction(NamedExpression e, private boolean commonCheck(LogicalAggregate outerAgg, LogicalAggregate innerAgg, boolean sameGroupBy, Optional projectOptional) { - innerAggExprIdToAggFunc = innerAgg.getOutputExpressions().stream() - .filter(expr -> (expr instanceof Alias) && (expr.child(0) instanceof AggregateFunction)) - .collect(Collectors.toMap(NamedExpression::getExprId, value -> (AggregateFunction) value.child(0), - (existValue, newValue) -> existValue)); + Map innerAggExprIdToAggFunc = getInnerAggExprIdToAggFuncMap(innerAgg); Set aggregateFunctions = outerAgg.getAggregateFunctions(); List replacedAggFunctions = projectOptional.map(project -> (List) PlanUtils.replaceExpressionByProjections( @@ -225,4 +222,11 @@ private boolean canMergeAggregateWithProject(LogicalAggregate getInnerAggExprIdToAggFuncMap(LogicalAggregate innerAgg) { + return innerAgg.getOutputExpressions().stream() + .filter(expr -> (expr instanceof Alias) && (expr.child(0) instanceof AggregateFunction)) + .collect(Collectors.toMap(NamedExpression::getExprId, value -> (AggregateFunction) value.child(0), + (existValue, newValue) -> existValue)); + } }