|
| 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.rel.RelNode; |
| 20 | +import org.apache.calcite.rel.core.JoinRelType; |
| 21 | +import org.apache.calcite.rel.metadata.RelMetadataQuery; |
| 22 | +import org.apache.calcite.rex.RexNode; |
| 23 | +import org.apache.calcite.tools.RelBuilder; |
| 24 | + |
| 25 | +import org.checkerframework.checker.nullness.qual.Nullable; |
| 26 | + |
| 27 | +import java.util.HashMap; |
| 28 | +import java.util.List; |
| 29 | + |
| 30 | +import static com.google.common.base.Preconditions.checkArgument; |
| 31 | + |
| 32 | +/** |
| 33 | + * The core process of dphyp enumeration algorithm. |
| 34 | + */ |
| 35 | +public class DpHyp { |
| 36 | + |
| 37 | + private HyperGraph hyperGraph; |
| 38 | + |
| 39 | + private HashMap<Long, RelNode> dpTable; |
| 40 | + |
| 41 | + private RelBuilder builder; |
| 42 | + |
| 43 | + private RelMetadataQuery mq; |
| 44 | + |
| 45 | + public DpHyp(HyperGraph hyperGraph, RelBuilder builder, RelMetadataQuery relMetadataQuery) { |
| 46 | + this.hyperGraph = hyperGraph; |
| 47 | + this.dpTable = new HashMap<>(); |
| 48 | + this.builder = builder; |
| 49 | + this.mq = relMetadataQuery; |
| 50 | + } |
| 51 | + |
| 52 | + public void startEnumerateJoin() { |
| 53 | + int size = hyperGraph.getInputs().size(); |
| 54 | + for (int i = 0; i < size; i++) { |
| 55 | + long singleNode = LongBitmap.newBitmap(i); |
| 56 | + dpTable.put(singleNode, hyperGraph.getInput(i)); |
| 57 | + hyperGraph.initEdgeBitMap(singleNode); |
| 58 | + } |
| 59 | + |
| 60 | + // start enumerating from the second to last |
| 61 | + for (int i = size - 2; i >= 0; i--) { |
| 62 | + long csg = LongBitmap.newBitmap(i); |
| 63 | + long forbidden = csg - 1; |
| 64 | + emitCsg(csg); |
| 65 | + enumerateCsgRec(csg, forbidden); |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + private void emitCsg(long csg) { |
| 70 | + long forbidden = csg | LongBitmap.getBvBitmap(csg); |
| 71 | + long neighbors = hyperGraph.getNeighborBitmap(csg, forbidden); |
| 72 | + |
| 73 | + LongBitmap.ReverseIterator reverseIterator = new LongBitmap.ReverseIterator(neighbors); |
| 74 | + for (long cmp : reverseIterator) { |
| 75 | + List<HyperEdge> edges = hyperGraph.connectCsgCmp(csg, cmp); |
| 76 | + if (!edges.isEmpty()) { |
| 77 | + emitCsgCmp(csg, cmp, edges); |
| 78 | + } |
| 79 | + // forbidden the nodes that smaller than current cmp when extend cmp, e.g. |
| 80 | + // neighbors = {t1, t2}, t1 and t2 are connected. |
| 81 | + // when extented t2, we will get (t1, t2) |
| 82 | + // when extented t1, we will get (t1, t2) repeated |
| 83 | + long newForbidden = |
| 84 | + (cmp | LongBitmap.getBvBitmap(cmp)) & neighbors; |
| 85 | + newForbidden = newForbidden | forbidden; |
| 86 | + enumerateCmpRec(csg, cmp, newForbidden); |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + private void enumerateCsgRec(long csg, long forbidden) { |
| 91 | + long neighbors = hyperGraph.getNeighborBitmap(csg, forbidden); |
| 92 | + LongBitmap.SubsetIterator subsetIterator = new LongBitmap.SubsetIterator(neighbors); |
| 93 | + for (long subNeighbor : subsetIterator) { |
| 94 | + hyperGraph.updateEdgesForUnion(csg, subNeighbor); |
| 95 | + long newCsg = csg | subNeighbor; |
| 96 | + if (dpTable.containsKey(newCsg)) { |
| 97 | + emitCsg(newCsg); |
| 98 | + } |
| 99 | + } |
| 100 | + long newForbidden = forbidden | neighbors; |
| 101 | + subsetIterator.reset(); |
| 102 | + for (long subNeighbor : subsetIterator) { |
| 103 | + long newCsg = csg | subNeighbor; |
| 104 | + enumerateCsgRec(newCsg, newForbidden); |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + private void enumerateCmpRec(long csg, long cmp, long forbidden) { |
| 109 | + long neighbors = hyperGraph.getNeighborBitmap(cmp, forbidden); |
| 110 | + LongBitmap.SubsetIterator subsetIterator = new LongBitmap.SubsetIterator(neighbors); |
| 111 | + for (long subNeighbor : subsetIterator) { |
| 112 | + long newCmp = cmp | subNeighbor; |
| 113 | + hyperGraph.updateEdgesForUnion(cmp, subNeighbor); |
| 114 | + if (dpTable.containsKey(newCmp)) { |
| 115 | + List<HyperEdge> edges = hyperGraph.connectCsgCmp(csg, newCmp); |
| 116 | + if (!edges.isEmpty()) { |
| 117 | + emitCsgCmp(csg, newCmp, edges); |
| 118 | + } |
| 119 | + } |
| 120 | + } |
| 121 | + long newForbidden = forbidden | neighbors; |
| 122 | + subsetIterator.reset(); |
| 123 | + for (long subNeighbor : subsetIterator) { |
| 124 | + long newCmp = cmp | subNeighbor; |
| 125 | + enumerateCmpRec(csg, newCmp, newForbidden); |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + private void emitCsgCmp(long csg, long cmp, List<HyperEdge> edges) { |
| 130 | + checkArgument(dpTable.containsKey(csg)); |
| 131 | + checkArgument(dpTable.containsKey(cmp)); |
| 132 | + RelNode child1 = dpTable.get(csg); |
| 133 | + RelNode child2 = dpTable.get(cmp); |
| 134 | + |
| 135 | + JoinRelType joinType = hyperGraph.extractJoinType(edges); |
| 136 | + if (joinType == null) { |
| 137 | + return; |
| 138 | + } |
| 139 | + RexNode joinCond1 = hyperGraph.extractJoinCond(child1, child2, edges); |
| 140 | + RelNode newPlan1 = builder |
| 141 | + .push(child1) |
| 142 | + .push(child2) |
| 143 | + .join(joinType, joinCond1) |
| 144 | + .build(); |
| 145 | + |
| 146 | + // swap left and right |
| 147 | + RexNode joinCond2 = hyperGraph.extractJoinCond(child2, child1, edges); |
| 148 | + RelNode newPlan2 = builder |
| 149 | + .push(child2) |
| 150 | + .push(child1) |
| 151 | + .join(joinType, joinCond2) |
| 152 | + .build(); |
| 153 | + RelNode winPlan = mq.getCumulativeCost(newPlan1).isLt(mq.getCumulativeCost(newPlan2)) |
| 154 | + ? newPlan1 |
| 155 | + : newPlan2; |
| 156 | + |
| 157 | + if (!dpTable.containsKey(csg | cmp)) { |
| 158 | + dpTable.put(csg | cmp, winPlan); |
| 159 | + } else { |
| 160 | + RelNode oriPlan = dpTable.get(csg | cmp); |
| 161 | + if (mq.getCumulativeCost(winPlan).isLt(mq.getCumulativeCost(oriPlan))) { |
| 162 | + dpTable.put(csg | cmp, winPlan); |
| 163 | + } |
| 164 | + } |
| 165 | + } |
| 166 | + |
| 167 | + public @Nullable RelNode getBestPlan() { |
| 168 | + int size = hyperGraph.getInputs().size(); |
| 169 | + long wholeGraph = LongBitmap.newBitmapBetween(0, size); |
| 170 | + return dpTable.get(wholeGraph); |
| 171 | + } |
| 172 | + |
| 173 | +} |
0 commit comments