|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | + |
| 19 | +package org.apache.flink.table.planner.plan.rules.physical.batch; |
| 20 | + |
| 21 | +import org.apache.flink.table.planner.plan.nodes.FlinkConventions; |
| 22 | +import org.apache.flink.table.planner.plan.nodes.physical.batch.BatchPhysicalHashAggregate; |
| 23 | +import org.apache.flink.table.planner.plan.nodes.physical.batch.BatchPhysicalLocalHashAggregate; |
| 24 | + |
| 25 | +import org.apache.calcite.plan.RelOptRuleCall; |
| 26 | +import org.apache.calcite.plan.RelRule; |
| 27 | +import org.apache.calcite.rel.RelNode; |
| 28 | +import org.immutables.value.Value; |
| 29 | + |
| 30 | +/** |
| 31 | + * There maybe exist a subTree like localHashAggregate -> globalHashAggregate which the middle |
| 32 | + * shuffle is removed. The rule could remove redundant localHashAggregate node. |
| 33 | + */ |
| 34 | +@Value.Enclosing |
| 35 | +public class RemoveRedundantLocalHashAggRule |
| 36 | + extends RelRule<RemoveRedundantLocalHashAggRule.RemoveRedundantLocalHashAggRuleConfig> { |
| 37 | + |
| 38 | + public static final RemoveRedundantLocalHashAggRule INSTANCE = |
| 39 | + RemoveRedundantLocalHashAggRule.RemoveRedundantLocalHashAggRuleConfig.DEFAULT.toRule(); |
| 40 | + |
| 41 | + protected RemoveRedundantLocalHashAggRule(RemoveRedundantLocalHashAggRuleConfig config) { |
| 42 | + super(config); |
| 43 | + } |
| 44 | + |
| 45 | + public void onMatch(RelOptRuleCall call) { |
| 46 | + BatchPhysicalHashAggregate globalAgg = call.rel(0); |
| 47 | + BatchPhysicalLocalHashAggregate localAgg = call.rel(1); |
| 48 | + RelNode inputOfLocalAgg = localAgg.getInput(); |
| 49 | + BatchPhysicalHashAggregate newGlobalAgg = |
| 50 | + new BatchPhysicalHashAggregate( |
| 51 | + globalAgg.getCluster(), |
| 52 | + globalAgg.getTraitSet(), |
| 53 | + inputOfLocalAgg, |
| 54 | + globalAgg.getRowType(), |
| 55 | + inputOfLocalAgg.getRowType(), |
| 56 | + inputOfLocalAgg.getRowType(), |
| 57 | + localAgg.grouping(), |
| 58 | + localAgg.auxGrouping(), |
| 59 | + // Use the localAgg agg calls because the global agg call filters was |
| 60 | + // removed, |
| 61 | + // see BatchPhysicalHashAggRule for details. |
| 62 | + localAgg.getAggCallToAggFunction(), |
| 63 | + false); |
| 64 | + call.transformTo(newGlobalAgg); |
| 65 | + } |
| 66 | + |
| 67 | + /** Configuration for {@link RemoveRedundantLocalHashAggRule}. */ |
| 68 | + @Value.Immutable(singleton = false) |
| 69 | + public interface RemoveRedundantLocalHashAggRuleConfig extends RelRule.Config { |
| 70 | + RemoveRedundantLocalHashAggRule.RemoveRedundantLocalHashAggRuleConfig DEFAULT = |
| 71 | + ImmutableRemoveRedundantLocalHashAggRule.RemoveRedundantLocalHashAggRuleConfig |
| 72 | + .builder() |
| 73 | + .build() |
| 74 | + .withOperandSupplier( |
| 75 | + b0 -> |
| 76 | + b0.operand(BatchPhysicalHashAggregate.class) |
| 77 | + .oneInput( |
| 78 | + b1 -> |
| 79 | + b1.operand( |
| 80 | + BatchPhysicalLocalHashAggregate |
| 81 | + .class) |
| 82 | + .oneInput( |
| 83 | + b2 -> |
| 84 | + b2.operand( |
| 85 | + RelNode |
| 86 | + .class) |
| 87 | + .oneInput( |
| 88 | + b3 -> |
| 89 | + b3.operand( |
| 90 | + FlinkConventions |
| 91 | + .BATCH_PHYSICAL() |
| 92 | + .getInterface()) |
| 93 | + .noInputs())))) |
| 94 | + .withDescription("RemoveRedundantLocalHashAggRule"); |
| 95 | + |
| 96 | + @Override |
| 97 | + default RemoveRedundantLocalHashAggRule toRule() { |
| 98 | + return new RemoveRedundantLocalHashAggRule(this); |
| 99 | + } |
| 100 | + } |
| 101 | +} |
0 commit comments