morrySnow commented on code in PR #12013: URL: https://github.com/apache/doris/pull/12013#discussion_r953438977
########## fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/logical/NormalizeAggregate.java: ########## @@ -0,0 +1,138 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +package org.apache.doris.nereids.rules.rewrite.logical; + +import org.apache.doris.nereids.rules.Rule; +import org.apache.doris.nereids.rules.RuleType; +import org.apache.doris.nereids.rules.rewrite.OneRewriteRuleFactory; +import org.apache.doris.nereids.trees.expressions.Alias; +import org.apache.doris.nereids.trees.expressions.Expression; +import org.apache.doris.nereids.trees.expressions.NamedExpression; +import org.apache.doris.nereids.trees.expressions.SlotReference; +import org.apache.doris.nereids.trees.expressions.functions.AggregateFunction; +import org.apache.doris.nereids.trees.expressions.visitor.ExpressionReplacer; +import org.apache.doris.nereids.trees.plans.logical.LogicalAggregate; +import org.apache.doris.nereids.trees.plans.logical.LogicalPlan; +import org.apache.doris.nereids.trees.plans.logical.LogicalProject; + +import com.google.common.collect.Lists; +import com.google.common.collect.Maps; + +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; +import java.util.Set; +import java.util.stream.Collectors; + +/** + * normalize aggregate's group keys to SlotReference and generate a LogicalProject top on LogicalAggregate + * to hold to order of aggregate output, since aggregate output's order could change when we do translate. + * + * Apply this rule could simplify the processing of enforce and translate. + * + * Original Plan: + * Aggregate( + * keys:[k1#1, K2#2 + 1], + * outputs:[k1#1, Alias(K2# + 1)#4, Alias(k1#1 + 1)#5, Alias(SUM(v1#3))#6, + * Alias(SUM(v1#3 + 1))#7, Alias(SUM(v1#3) + 1)#8]) + * + * After rule: + * Project(k1#1, Alias(SR#9)#4, Alias(k1#1 + 1)#5, Alias(SR#10))#6, Alias(SR#11))#7, Alias(SR#10 + 1)#8) + * +-- Aggregate(keys:[k1#1, SR#9], outputs:[k1#1, SR#9, Alias(SUM(v1#3))#10, Alias(SUM(v1#3 + 1))#11]) + * +-- Project(k1#1, Alias(K2#2 + 1)#9, v1#3) + * + * More example could get from UT {@link NormalizeAggregateTest} + */ +public class NormalizeAggregate extends OneRewriteRuleFactory { + @Override + public Rule build() { + return logicalAggregate().when(aggregate -> !aggregate.isNormalized()).then(aggregate -> { + // substitution map used to substitute expression in aggregate's output to use it as top projections + Map<Expression, Expression> substitutionMap = Maps.newHashMap(); + List<Expression> keys = aggregate.getGroupByExpressions(); + List<NamedExpression> newOutputs = Lists.newArrayList(); + + // keys + Map<Boolean, List<Expression>> partitionedKeys = keys.stream() + .collect(Collectors.groupingBy(SlotReference.class::isInstance)); + List<Expression> newKeys = Lists.newArrayList(); + List<NamedExpression> bottomProjections = Lists.newArrayList(); + if (partitionedKeys.containsKey(false)) { + // process non-SlotReference keys + newKeys.addAll(partitionedKeys.get(false).stream() + .map(e -> new Alias(e, e.toSql())) + .peek(a -> substitutionMap.put(a.child(), a.toSlot())) + .peek(bottomProjections::add) + .map(Alias::toSlot) + .collect(Collectors.toList())); + } + if (partitionedKeys.containsKey(true)) { + // process SlotReference keys + partitionedKeys.get(true).stream() + .map(SlotReference.class::cast) + .peek(s -> substitutionMap.put(s, s)) Review Comment: merge into one need use lambda statement instead of lambda expression. It is look ugly -- 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