morrySnow commented on code in PR #28607: URL: https://github.com/apache/doris/pull/28607#discussion_r1432188217
########## fe/fe-core/src/main/java/org/apache/doris/nereids/rules/analysis/NormalizeAggregate.java: ########## @@ -100,22 +101,94 @@ public class NormalizeAggregate extends OneRewriteRuleFactory implements Normali @Override public Rule build() { return logicalAggregate().whenNot(LogicalAggregate::isNormalized).then(aggregate -> { + // The LogicalAggregate node may contain window agg functions and usual agg functions + // we call window agg functions as window-agg and usual agg functions as trival-agg for short + // This rule simplify LogicalAggregate node by: + // 1. Push down some exprs from old LogicalAggregate node to a new child LogicalProject Node, + // 2. create a new LogicalAggregate with normalized group by exprs and trival-aggs + // 3. Pull up normalized old LogicalAggregate's output exprs to a new parent LogicalProject Node + // Push down exprs: + // 1. all group by exprs + // 2. child contains subquery expr in trival-agg + // 3. child contains window expr in trival-agg + // 4. all input slots of trival-agg + // 5. expr(including subquery) in distinct trival-agg + // Normalize LogicalAggregate's output. + // 1. normalize group by exprs by outputs of bottom LogicalProject + // 2. normalize trival-aggs by outputs of bottom LogicalProject + // 3. build normalized agg outputs + // Pull up exprs: + // normalize all output exprs in old LogicalAggregate to build a parent project node, typically includes: + // 1. simple slots + // 2. aliases + // a. alias with no aggs child + // b. alias with trival-agg child + // c. alias with window-agg - List<NamedExpression> aggregateOutput = aggregate.getOutputExpressions(); - Set<Alias> existsAlias = ExpressionUtils.mutableCollect(aggregateOutput, Alias.class::isInstance); + // Push down exprs: + // collect group by exprs + Set<Expression> groupingByExprs = + ImmutableSet.copyOf(aggregate.getGroupByExpressions()); + // collect all trival-agg + List<NamedExpression> aggregateOutput = aggregate.getOutputExpressions(); List<AggregateFunction> aggFuncs = Lists.newArrayList(); aggregateOutput.forEach(o -> o.accept(CollectNonWindowedAggFuncs.INSTANCE, aggFuncs)); - // we need push down subquery exprs inside non-window and non-distinct agg functions - Set<SubqueryExpr> subqueryExprs = ExpressionUtils.mutableCollect(aggFuncs.stream() - .filter(aggFunc -> !aggFunc.isDistinct()).collect(Collectors.toList()), - SubqueryExpr.class::isInstance); - Set<Expression> groupingByExprs = ImmutableSet.copyOf(aggregate.getGroupByExpressions()); + // split non-distinct agg child as two part + // TRUE part 1: need push down itself, if it contains subqury or window expression + // FALSE part 2: need push down its input slots, if it DOES NOT contain subqury or window expression + Map<Boolean, Set<Expression>> categorizedNoDistinctAggsChildren = aggFuncs.stream() + .filter(aggFunc -> !aggFunc.isDistinct()) + .flatMap(agg -> agg.children().stream()) + .collect(Collectors.groupingBy( + child -> child.containsType(SubqueryExpr.class, WindowExpression.class), + Collectors.toSet())); + + // split non-distinct agg child as two parts Review Comment: ```suggestion // split distinct agg child as two parts ``` -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: commits-unsubscr...@doris.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: commits-unsubscr...@doris.apache.org For additional commands, e-mail: commits-h...@doris.apache.org