adonis0147 commented on code in PR #12129: URL: https://github.com/apache/doris/pull/12129#discussion_r963228201
########## fe/fe-core/src/main/java/org/apache/doris/nereids/rules/analysis/ResolveHaving.java: ########## @@ -0,0 +1,184 @@ +// 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.analysis; + +import org.apache.doris.common.Pair; +import org.apache.doris.nereids.exceptions.AnalysisException; +import org.apache.doris.nereids.rules.Rule; +import org.apache.doris.nereids.rules.RuleType; +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.Slot; +import org.apache.doris.nereids.trees.expressions.SlotReference; +import org.apache.doris.nereids.trees.expressions.functions.AggregateFunction; +import org.apache.doris.nereids.trees.plans.GroupPlan; +import org.apache.doris.nereids.trees.plans.Plan; +import org.apache.doris.nereids.trees.plans.logical.LogicalAggregate; +import org.apache.doris.nereids.trees.plans.logical.LogicalFilter; +import org.apache.doris.nereids.trees.plans.logical.LogicalHaving; +import org.apache.doris.nereids.trees.plans.logical.LogicalProject; + +import com.google.common.collect.Lists; +import com.google.common.collect.Maps; +import com.google.common.collect.Streams; + +import java.util.List; +import java.util.Map; +import java.util.Optional; +import java.util.function.Predicate; +import java.util.stream.Collectors; + +/** + * Resolve having clause to the aggregation. + */ +public class ResolveHaving extends OneAnalysisRuleFactory { + @Override + public Rule build() { + return RuleType.RESOLVE_HAVING.build( + logicalHaving(logicalAggregate()).thenApply(ctx -> { + LogicalHaving<LogicalAggregate<GroupPlan>> having = ctx.root; + LogicalAggregate<GroupPlan> aggregate = having.child(); + Resolver resolver = new Resolver(aggregate); + resolver.resolve(having.getPredicates()); + return createPlan(having, resolver); + }) + ); + } + + static class Resolver { + + private final List<NamedExpression> outputExpressions; + private final List<Expression> groupByExpressions; + private final Map<Expression, Slot> substitution = Maps.newHashMap(); + private final List<NamedExpression> newOutputSlots = Lists.newArrayList(); + + Resolver(LogicalAggregate<? extends Plan> aggregate) { + outputExpressions = aggregate.getOutputExpressions(); + groupByExpressions = aggregate.getGroupByExpressions(); + } + + public void resolve(Expression expression) { + Pair<Optional<Expression>, Boolean> result = lookUp(expression); + Optional<Expression> found = result.first; + boolean isFoundInOutputExpressions = result.second; + + if (found.isPresent()) { + if (found.get() instanceof NamedExpression) { + substitution.put(expression, ((NamedExpression) found.get()).toSlot()); + if (!isFoundInOutputExpressions) { + newOutputSlots.add(((NamedExpression) found.get()).toSlot()); + } + } else { + // This expression was found in group-by expressions. E.g. group by (a + 1) having (a + 1) + generateAliasForNewOutputSlots(expression); + } + } else { + if (expression instanceof SlotReference) { + throw new AnalysisException(expression.toSql() + " in having clause should be grouped by."); + } else if (expression instanceof AggregateFunction) { + if (checkWhetherNestedAggregateFunctionsExist((AggregateFunction) expression)) { + throw new AnalysisException("Aggregate functions in having clause can't be nested: " + + expression.toSql() + "."); + } + generateAliasForNewOutputSlots(expression); + } else { + for (Expression child : expression.children()) { + resolve(child); + } + } + } + } + + private Pair<Optional<Expression>, Boolean> lookUp(Expression expression) { + Optional<Expression> found = outputExpressions.stream() + .filter(source -> isEquivalent(source, expression)) + .map(source -> (Expression) source) + .findFirst(); + if (found.isPresent()) { + return Pair.of(found, true); + } + + found = groupByExpressions.stream().filter(source -> isEquivalent(source, expression)).findFirst(); + return Pair.of(found, false); + } + + private boolean isEquivalent(Expression source, Expression expression) { + if (source.equals(expression)) { + return true; + } else if (source instanceof Alias) { + Alias alias = (Alias) source; + return alias.toSlot().equals(expression) || alias.child().equals(expression); + } + return false; + } + + private boolean checkWhetherNestedAggregateFunctionsExist(AggregateFunction function) { + Predicate<Expression> hasAggregateFunctions = new Predicate<Expression>() { + @Override + public boolean test(Expression expression) { + return expression instanceof AggregateFunction + || expression.children().stream().anyMatch(this); + } + }; + return function.children().stream().anyMatch(hasAggregateFunctions); Review Comment: We need recursion here otherwise we can't cover the case `sum(a1 + a2 + sum(a2))` which is parsed as `new Sum(new Add(new Add(a1, a2), new Sum(a2)))` -- 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