Skip to content

Commit

Permalink
rebase and add EliminateAggregate
Browse files Browse the repository at this point in the history
  • Loading branch information
924060929 committed Dec 17, 2022
1 parent 5125db1 commit 0989b92
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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),

Expand Down
Original file line number Diff line number Diff line change
@@ -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<GroupPlan> innerAgg = outerAgg.child();

if (!isSame(outerAgg.getGroupByExpressions(), innerAgg.getGroupByExpressions())) {
return outerAgg;
}
if (!onlyHasSlots(outerAgg.getOutputExpressions())) {
return outerAgg;
}
List<NamedExpression> prunedInnerAggOutput = Project.findProject(outerAgg.getOutputSet(),
innerAgg.getOutputExpressions());
return innerAgg.withAggOutput(prunedInnerAggOutput);
}).toRule(RuleType.ELIMINATE_AGGREGATE);
}

private boolean isSame(List<Expression> list1, List<Expression> list2) {
return list1.size() == list2.size() && list2.containsAll(list1);
}

private boolean onlyHasSlots(List<? extends Expression> exprs) {
return exprs.stream().allMatch(SlotReference.class::isInstance);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@
* limit: 10
* offset 100
*/
public class LogicalLimit<CHILD_TYPE extends Plan> extends LogicalUnary<CHILD_TYPE> implements Limit {
public class LogicalLimit<CHILD_TYPE extends Plan>
extends LogicalUnary<CHILD_TYPE>
implements Limit {
private final long limit;
private final long offset;

Expand Down

0 comments on commit 0989b92

Please sign in to comment.