-
Notifications
You must be signed in to change notification settings - Fork 3.9k
[feat](nereids) add merge aggregate rule #31811
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
3a46f99
[feat](nereids) add merge aggregate rule
feiniaofeiafei 4d979a3
[feat](nereids) add merge aggregate rule
feiniaofeiafei da213fa
[feat](nereids) add merge aggregate rule
feiniaofeiafei e0fe2f0
[feat](nereids) add merge aggregate rule
feiniaofeiafei bdbbd54
[feat](nereids) add merge aggregate rule
feiniaofeiafei 6fe78ae
[feat](nereids) add merge aggregate rule
feiniaofeiafei File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
211 changes: 211 additions & 0 deletions
211
fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/MergeAggregate.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,211 @@ | ||
| // 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; | ||
|
|
||
| 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.ExprId; | ||
| 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.agg.AggregateFunction; | ||
| 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.LogicalProject; | ||
| import org.apache.doris.nereids.util.ExpressionUtils; | ||
| import org.apache.doris.nereids.util.PlanUtils; | ||
|
|
||
| import com.google.common.collect.ImmutableList; | ||
| import com.google.common.collect.ImmutableSet; | ||
|
|
||
| import java.util.HashMap; | ||
| import java.util.HashSet; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.Set; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| /**MergeAggregate*/ | ||
| public class MergeAggregate implements RewriteRuleFactory { | ||
| private static final ImmutableSet<String> ALLOW_MERGE_AGGREGATE_FUNCTIONS = | ||
| ImmutableSet.of("min", "max", "sum", "any_value"); | ||
| private Map<ExprId, AggregateFunction> innerAggExprIdToAggFunc = new HashMap<>(); | ||
|
|
||
| @Override | ||
| public List<Rule> buildRules() { | ||
| return ImmutableList.of( | ||
| logicalAggregate(logicalAggregate()).when(this::canMergeAggregateWithoutProject) | ||
| .then(this::mergeTwoAggregate) | ||
| .toRule(RuleType.MERGE_AGGREGATE), | ||
| logicalAggregate(logicalProject(logicalAggregate())) | ||
| .when(this::canMergeAggregateWithProject) | ||
| .then(this::mergeAggProjectAgg) | ||
| .toRule(RuleType.MERGE_AGGREGATE)); | ||
| } | ||
|
|
||
| /** | ||
| * before: | ||
| * LogicalAggregate | ||
| * +--LogicalAggregate | ||
| * after: | ||
| * LogicalAggregate | ||
| */ | ||
| private Plan mergeTwoAggregate(LogicalAggregate<LogicalAggregate<Plan>> outerAgg) { | ||
| LogicalAggregate<Plan> innerAgg = outerAgg.child(); | ||
|
|
||
| List<NamedExpression> newOutputExpressions = outerAgg.getOutputExpressions().stream() | ||
| .map(e -> rewriteAggregateFunction(e, innerAggExprIdToAggFunc)) | ||
| .collect(Collectors.toList()); | ||
| return outerAgg.withAggOutput(newOutputExpressions).withChildren(innerAgg.children()); | ||
| } | ||
|
|
||
| /** | ||
| * before: | ||
| * LogicalAggregate (outputExpressions = [col2, sum(col1)], groupByKeys = [col2]) | ||
| * +--LogicalProject (projects = [a as col2, col1]) | ||
| * +--LogicalAggregate (outputExpressions = [a, b, sum(c) as col1], groupByKeys = [a,b]) | ||
| * after: | ||
| * LogicalProject (projects = [a as col2, sum(col1) as sum(col1)] | ||
| * +--LogicalAggregate (outputExpression = [a, sum(c) as sum(col1)], groupByKeys = [a]) | ||
| */ | ||
| private Plan mergeAggProjectAgg(LogicalAggregate<LogicalProject<LogicalAggregate<Plan>>> outerAgg) { | ||
| LogicalProject<LogicalAggregate<Plan>> project = outerAgg.child(); | ||
| LogicalAggregate<Plan> innerAgg = project.child(); | ||
|
|
||
| // rewrite agg function. e.g. max(max) | ||
| List<NamedExpression> aggFunc = outerAgg.getOutputExpressions().stream() | ||
| .filter(expr -> (expr instanceof Alias) && (expr.child(0) instanceof AggregateFunction)) | ||
| .map(e -> rewriteAggregateFunction(e, innerAggExprIdToAggFunc)) | ||
| .collect(Collectors.toList()); | ||
| // rewrite agg function directly refer to the slot below the project | ||
| List<Expression> replacedAggFunc = PlanUtils.replaceExpressionByProjections(project.getProjects(), | ||
| (List) aggFunc); | ||
| // replace groupByKeys directly refer to the slot below the project | ||
| List<Expression> replacedGroupBy = PlanUtils.replaceExpressionByProjections(project.getProjects(), | ||
| outerAgg.getGroupByExpressions()); | ||
| List<NamedExpression> newOutputExpressions = ImmutableList.<NamedExpression>builder() | ||
| .addAll(replacedGroupBy.stream().map(slot -> (NamedExpression) slot).iterator()) | ||
| .addAll(replacedAggFunc.stream().map(alias -> (NamedExpression) alias).iterator()).build(); | ||
| // construct agg | ||
| LogicalAggregate<Plan> resAgg = outerAgg.withGroupByAndOutput(replacedGroupBy, newOutputExpressions) | ||
| .withChildren(innerAgg.children()); | ||
|
|
||
| // construct upper project | ||
| Map<SlotReference, Alias> childToAlias = project.getProjects().stream() | ||
| .filter(expr -> (expr instanceof Alias) && (expr.child(0) instanceof SlotReference)) | ||
| .collect(Collectors.toMap(alias -> (SlotReference) alias.child(0), alias -> (Alias) alias)); | ||
| List<Expression> projectGroupBy = ExpressionUtils.replace(replacedGroupBy, childToAlias); | ||
| List<NamedExpression> upperProjects = ImmutableList.<NamedExpression>builder() | ||
| .addAll(projectGroupBy.stream().map(namedExpr -> (NamedExpression) namedExpr).iterator()) | ||
| .addAll(replacedAggFunc.stream().map(expr -> ((NamedExpression) expr).toSlot()).iterator()) | ||
| .build(); | ||
| return new LogicalProject<Plan>(upperProjects, resAgg); | ||
| } | ||
|
|
||
| private NamedExpression rewriteAggregateFunction(NamedExpression e, | ||
| Map<ExprId, AggregateFunction> innerAggExprIdToAggFunc) { | ||
| return (NamedExpression) e.rewriteDownShortCircuit(expr -> { | ||
| if (expr instanceof Alias && ((Alias) expr).child() instanceof AggregateFunction) { | ||
| Alias alias = (Alias) expr; | ||
| AggregateFunction aggFunc = (AggregateFunction) alias.child(); | ||
| ExprId childExprId = ((SlotReference) aggFunc.child(0)).getExprId(); | ||
| if (innerAggExprIdToAggFunc.containsKey(childExprId)) { | ||
| return new Alias(alias.getExprId(), innerAggExprIdToAggFunc.get(childExprId), | ||
| alias.getName()); | ||
| } else { | ||
| return expr; | ||
| } | ||
| } else { | ||
| return expr; | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| boolean commonCheck(LogicalAggregate<? extends Plan> outerAgg, LogicalAggregate<Plan> innerAgg, | ||
| boolean sameGroupBy) { | ||
| 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)); | ||
| Set<AggregateFunction> aggregateFunctions = outerAgg.getAggregateFunctions(); | ||
| for (AggregateFunction outerFunc : aggregateFunctions) { | ||
| if (!(ALLOW_MERGE_AGGREGATE_FUNCTIONS.contains(outerFunc.getName()))) { | ||
| return false; | ||
| } | ||
| if (outerFunc.isDistinct() && !sameGroupBy) { | ||
| return false; | ||
| } | ||
| // not support outerAggFunc: sum(a+1),sum(a+b) | ||
| if (!(outerFunc.child(0) instanceof SlotReference)) { | ||
| return false; | ||
| } | ||
| ExprId childExprId = ((SlotReference) outerFunc.child(0)).getExprId(); | ||
| if (innerAggExprIdToAggFunc.containsKey(childExprId)) { | ||
| AggregateFunction innerFunc = innerAggExprIdToAggFunc.get(childExprId); | ||
| if (innerFunc.isDistinct() && !sameGroupBy) { | ||
| return false; | ||
| } | ||
| // support sum(sum),min(min),max(max),any_value(any_value),sum(count) | ||
| // sum(count) -> count() need outerAgg having group by keys (reason: nullable) | ||
| if (!(outerFunc.getName().equals("sum") && innerFunc.getName().equals("count") | ||
| && !outerAgg.getGroupByExpressions().isEmpty()) | ||
| && !innerFunc.getName().equals(outerFunc.getName())) { | ||
| return false; | ||
| } | ||
| } else { | ||
| // select a, max(b), min(b), any_value(b) from (select a,b from t1 group by a, b) group by a; | ||
| // equals select a, max(b), min(b), any_value(b) from t1 group by a; | ||
| if (!outerFunc.getName().equals("max") | ||
| && !outerFunc.getName().equals("min") | ||
| && !outerFunc.getName().equals("any_value")) { | ||
| return false; | ||
| } | ||
| } | ||
| } | ||
| return true; | ||
| } | ||
|
|
||
| private boolean canMergeAggregateWithoutProject(LogicalAggregate<LogicalAggregate<Plan>> outerAgg) { | ||
| LogicalAggregate<Plan> innerAgg = outerAgg.child(); | ||
| if (!new HashSet<>(innerAgg.getGroupByExpressions()).containsAll(outerAgg.getGroupByExpressions())) { | ||
| return false; | ||
| } | ||
| boolean sameGroupBy = (innerAgg.getGroupByExpressions().size() == outerAgg.getGroupByExpressions().size()); | ||
|
|
||
| return commonCheck(outerAgg, innerAgg, sameGroupBy); | ||
| } | ||
|
|
||
| private boolean canMergeAggregateWithProject(LogicalAggregate<LogicalProject<LogicalAggregate<Plan>>> outerAgg) { | ||
| LogicalProject<LogicalAggregate<Plan>> project = outerAgg.child(); | ||
| LogicalAggregate<Plan> innerAgg = project.child(); | ||
|
|
||
| List<Expression> outerAggGroupByKeys = PlanUtils.replaceExpressionByProjections(project.getProjects(), | ||
| outerAgg.getGroupByExpressions()); | ||
| if (!new HashSet<>(innerAgg.getGroupByExpressions()).containsAll(outerAggGroupByKeys)) { | ||
| return false; | ||
| } | ||
| // project cannot have expressions like a+1 | ||
| if (ExpressionUtils.anyMatch(project.getProjects(), | ||
| expr -> !(expr instanceof SlotReference) && !(expr instanceof Alias))) { | ||
|
starocean999 marked this conversation as resolved.
|
||
| return false; | ||
| } | ||
| boolean sameGroupBy = (innerAgg.getGroupByExpressions().size() == outerAgg.getGroupByExpressions().size()); | ||
| return commonCheck(outerAgg, innerAgg, sameGroupBy); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.