diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/jobs/batch/NereidsRewriteJobExecutor.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/jobs/batch/NereidsRewriteJobExecutor.java index 56491cced9d032b..9936a44d74e4d0c 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/jobs/batch/NereidsRewriteJobExecutor.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/jobs/batch/NereidsRewriteJobExecutor.java @@ -28,6 +28,7 @@ import org.apache.doris.nereids.rules.mv.SelectMaterializedIndexWithAggregate; import org.apache.doris.nereids.rules.mv.SelectMaterializedIndexWithoutAggregate; import org.apache.doris.nereids.rules.rewrite.logical.ColumnPruning; +import org.apache.doris.nereids.rules.rewrite.logical.EliminateAggregate; import org.apache.doris.nereids.rules.rewrite.logical.EliminateFilter; import org.apache.doris.nereids.rules.rewrite.logical.EliminateGroupByConstant; import org.apache.doris.nereids.rules.rewrite.logical.EliminateLimit; @@ -100,6 +101,7 @@ public NereidsRewriteJobExecutor(CascadesContext cascadesContext) { .add(topDownBatch(ImmutableList.of(new EliminateGroupByConstant()))) .add(topDownBatch(ImmutableList.of(new EliminateOrderByConstant()))) .add(topDownBatch(ImmutableList.of(new EliminateUnnecessaryProject()))) + .add(topDownBatch(ImmutableList.of(new EliminateAggregate()))) // this rule batch must keep at the end of rewrite to do some plan check .add(bottomUpBatch(ImmutableList.of(new CheckAfterRewrite()))) .build(); diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/RuleType.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/RuleType.java index 8e4352170055b20..598a3ba64c9c394 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/RuleType.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/RuleType.java @@ -74,6 +74,7 @@ public enum RuleType { CHECK_ROW_POLICY(RuleTypeClass.REWRITE), ELIMINATE_EXCEPT(RuleTypeClass.REWRITE), + ELIMINATE_AGGREGATE(RuleTypeClass.REWRITE), RESOLVE_ORDINAL_IN_ORDER_BY(RuleTypeClass.REWRITE), RESOLVE_ORDINAL_IN_GROUP_BY(RuleTypeClass.REWRITE), diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/logical/EliminateAggregate.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/logical/EliminateAggregate.java new file mode 100644 index 000000000000000..8e3d4977af443d4 --- /dev/null +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/logical/EliminateAggregate.java @@ -0,0 +1,58 @@ +// 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.Expression; +import org.apache.doris.nereids.trees.expressions.NamedExpression; +import org.apache.doris.nereids.trees.expressions.SlotReference; +import org.apache.doris.nereids.trees.plans.GroupPlan; +import org.apache.doris.nereids.trees.plans.algebra.Project; +import org.apache.doris.nereids.trees.plans.logical.LogicalAggregate; + +import java.util.List; + +/** EliminateAggregate */ +public class EliminateAggregate extends OneRewriteRuleFactory { + @Override + public Rule build() { + return logicalAggregate(logicalAggregate()).then(outerAgg -> { + LogicalAggregate innerAgg = outerAgg.child(); + + if (!isSame(outerAgg.getGroupByExpressions(), innerAgg.getGroupByExpressions())) { + return outerAgg; + } + if (!onlyHasSlots(outerAgg.getOutputExpressions())) { + return outerAgg; + } + List prunedInnerAggOutput = Project.findProject(outerAgg.getOutputSet(), + innerAgg.getOutputExpressions()); + return innerAgg.withAggOutput(prunedInnerAggOutput); + }).toRule(RuleType.ELIMINATE_AGGREGATE); + } + + private boolean isSame(List list1, List list2) { + return list1.size() == list2.size() && list2.containsAll(list1); + } + + private boolean onlyHasSlots(List exprs) { + return exprs.stream().allMatch(SlotReference.class::isInstance); + } +} diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/logical/LogicalLimit.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/logical/LogicalLimit.java index 94ea4616e490be6..48f94727d7870b0 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/logical/LogicalLimit.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/logical/LogicalLimit.java @@ -43,7 +43,9 @@ * limit: 10 * offset 100 */ -public class LogicalLimit extends LogicalUnary implements Limit { +public class LogicalLimit + extends LogicalUnary + implements Limit { private final long limit; private final long offset;