|
| 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, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | +package org.apache.calcite.rel.rules; |
| 20 | + |
| 21 | +import com.google.common.collect.ImmutableList; |
| 22 | +import org.apache.calcite.plan.RelOptRule; |
| 23 | +import org.apache.calcite.plan.RelOptRuleCall; |
| 24 | +import org.apache.calcite.rel.RelNode; |
| 25 | +import org.apache.calcite.rel.core.Aggregate; |
| 26 | +import org.apache.calcite.rel.hint.PinotHintOptions; |
| 27 | +import org.apache.calcite.rel.hint.PinotHintStrategyTable; |
| 28 | +import org.apache.calcite.rel.hint.RelHint; |
| 29 | +import org.apache.calcite.rel.logical.LogicalAggregate; |
| 30 | +import org.apache.calcite.rel.logical.PinotLogicalExchange; |
| 31 | +import org.apache.calcite.tools.RelBuilderFactory; |
| 32 | +import org.apache.pinot.query.planner.plannode.AggregateNode; |
| 33 | + |
| 34 | + |
| 35 | +public class PinotAggregateExchangeNodeRemoveRule extends RelOptRule { |
| 36 | + public static final PinotAggregateExchangeNodeRemoveRule INSTANCE = |
| 37 | + new PinotAggregateExchangeNodeRemoveRule(PinotRuleUtils.PINOT_REL_FACTORY); |
| 38 | + |
| 39 | + public PinotAggregateExchangeNodeRemoveRule(RelBuilderFactory factory) { |
| 40 | + super(operand(PinotLogicalExchange.class, |
| 41 | + some(operand(LogicalAggregate.class, some(operand(PinotLogicalExchange.class, any()))))), factory, null); |
| 42 | + } |
| 43 | + |
| 44 | + @Override |
| 45 | + public boolean matches(RelOptRuleCall call) { |
| 46 | + if (call.rels.length < 1) { |
| 47 | + return false; |
| 48 | + } |
| 49 | + if (call.rel(1) instanceof Aggregate) { |
| 50 | + Aggregate agg = call.rel(1); |
| 51 | + ImmutableList<RelHint> hints = agg.getHints(); |
| 52 | + return AggregateNode.AggType.LEAF.name().equals( |
| 53 | + PinotHintStrategyTable.getHintOption(hints, PinotHintOptions.INTERNAL_AGG_OPTIONS, |
| 54 | + PinotHintOptions.InternalAggregateOptions.AGG_TYPE)); |
| 55 | + } |
| 56 | + return false; |
| 57 | + } |
| 58 | + |
| 59 | + @Override |
| 60 | + public void onMatch(RelOptRuleCall call) { |
| 61 | + final PinotLogicalExchange topExchange = call.rel(0); |
| 62 | + final Aggregate agg = (Aggregate) PinotRuleUtils.unboxRel(topExchange.getInput()); |
| 63 | + final PinotLogicalExchange bottomExchange = (PinotLogicalExchange) PinotRuleUtils.unboxRel(agg.getInput()); |
| 64 | + final RelNode input = PinotRuleUtils.unboxRel(bottomExchange.getInput()); |
| 65 | + Aggregate newAgg = |
| 66 | + new LogicalAggregate(agg.getCluster(), agg.getTraitSet(), agg.getHints(), input, agg.getGroupSet(), |
| 67 | + agg.getGroupSets(), agg.getAggCallList()); |
| 68 | + PinotLogicalExchange newExchange = PinotLogicalExchange.create(newAgg, topExchange.getDistribution()); |
| 69 | + call.transformTo(newExchange); |
| 70 | + } |
| 71 | +} |
0 commit comments