|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to you under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | +package org.apache.calcite.rel.rules; |
| 18 | + |
| 19 | +import org.apache.calcite.plan.RelOptRuleCall; |
| 20 | +import org.apache.calcite.plan.RelRule; |
| 21 | +import org.apache.calcite.rel.RelNode; |
| 22 | +import org.apache.calcite.rel.core.Intersect; |
| 23 | +import org.apache.calcite.rel.logical.LogicalIntersect; |
| 24 | +import org.apache.calcite.rel.metadata.RelMetadataQuery; |
| 25 | +import org.apache.calcite.tools.RelBuilder; |
| 26 | +import org.apache.calcite.util.Pair; |
| 27 | + |
| 28 | +import org.immutables.value.Value; |
| 29 | + |
| 30 | +import java.util.ArrayList; |
| 31 | +import java.util.List; |
| 32 | +import java.util.stream.Collectors; |
| 33 | + |
| 34 | +/** |
| 35 | + * Planner rule that reorders inputs of an {@link Intersect} to put smaller inputs first. |
| 36 | + * This helps reduce the size of intermediate results. |
| 37 | + * |
| 38 | + * <p>Intersect(A, B, ...) where B is smallest will reorder to Intersect(B, A, ...) |
| 39 | + */ |
| 40 | +@Value.Enclosing |
| 41 | +public class IntersectReorderRule extends RelRule<IntersectReorderRule.Config> |
| 42 | + implements SubstitutionRule { |
| 43 | + /** Creates an IntersectReorderRule. */ |
| 44 | + protected IntersectReorderRule(Config config) { |
| 45 | + super(config); |
| 46 | + } |
| 47 | + |
| 48 | + @Override public void onMatch(RelOptRuleCall call) { |
| 49 | + final Intersect intersect = call.rel(0); |
| 50 | + final RelMetadataQuery mq = call.getMetadataQuery(); |
| 51 | + final List<RelNode> inputs = intersect.getInputs(); |
| 52 | + |
| 53 | + final List<Pair<RelNode, Double>> inputsWithRowCounts = new ArrayList<>(); |
| 54 | + |
| 55 | + for (RelNode input : inputs) { |
| 56 | + Double rowCount = mq.getRowCount(input); |
| 57 | + inputsWithRowCounts.add(Pair.of(input, rowCount)); |
| 58 | + } |
| 59 | + |
| 60 | + inputsWithRowCounts.sort((a, b) -> Double.compare(a.right, b.right)); |
| 61 | + |
| 62 | + boolean needsReorder = false; |
| 63 | + for (int i = 0; i < inputs.size(); i++) { |
| 64 | + if (inputs.get(i) != inputsWithRowCounts.get(i).left) { |
| 65 | + needsReorder = true; |
| 66 | + break; |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + if (!needsReorder) { |
| 71 | + return; |
| 72 | + } |
| 73 | + |
| 74 | + final List<RelNode> newInputs = inputsWithRowCounts.stream() |
| 75 | + .map(pair -> pair.left) |
| 76 | + .collect(Collectors.toList()); |
| 77 | + |
| 78 | + final RelBuilder relBuilder = call.builder(); |
| 79 | + relBuilder.pushAll(newInputs); |
| 80 | + relBuilder.intersect(intersect.all, newInputs.size()); |
| 81 | + |
| 82 | + call.transformTo(relBuilder.build()); |
| 83 | + } |
| 84 | + |
| 85 | + /** Rule configuration. */ |
| 86 | + @Value.Immutable |
| 87 | + public interface Config extends RelRule.Config { |
| 88 | + Config DEFAULT = ImmutableIntersectReorderRule.Config.of() |
| 89 | + .withOperandSupplier(b0 -> |
| 90 | + b0.operand(LogicalIntersect.class) |
| 91 | + .predicate(intersect -> intersect.getInputs().size() > 1) |
| 92 | + .anyInputs()) |
| 93 | + .withDescription("IntersectReorderRule"); |
| 94 | + |
| 95 | + @Override default IntersectReorderRule toRule() { |
| 96 | + return new IntersectReorderRule(this); |
| 97 | + } |
| 98 | + } |
| 99 | +} |
0 commit comments