|
| 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 | + |
| 20 | +package org.apache.iotdb.db.queryengine.plan.relational.planner.iterative.rule; |
| 21 | + |
| 22 | +import org.apache.iotdb.db.queryengine.plan.planner.plan.node.PlanNode; |
| 23 | +import org.apache.iotdb.db.queryengine.plan.relational.planner.Symbol; |
| 24 | +import org.apache.iotdb.db.queryengine.plan.relational.planner.iterative.Lookup; |
| 25 | +import org.apache.iotdb.db.queryengine.plan.relational.planner.iterative.Rule; |
| 26 | +import org.apache.iotdb.db.queryengine.plan.relational.planner.node.SetOperationNode; |
| 27 | +import org.apache.iotdb.db.queryengine.plan.relational.planner.node.UnionNode; |
| 28 | + |
| 29 | +import com.google.common.collect.ImmutableListMultimap; |
| 30 | +import com.google.common.collect.Iterables; |
| 31 | + |
| 32 | +import java.util.ArrayList; |
| 33 | +import java.util.Collection; |
| 34 | +import java.util.List; |
| 35 | +import java.util.Map; |
| 36 | +import java.util.Optional; |
| 37 | + |
| 38 | +import static com.google.common.base.Preconditions.checkState; |
| 39 | +import static com.google.common.collect.ImmutableList.toImmutableList; |
| 40 | + |
| 41 | +public class SetOperationMerge { |
| 42 | + |
| 43 | + private final Rule.Context context; |
| 44 | + private final SetOperationNode node; |
| 45 | + private final List<PlanNode> newSources; |
| 46 | + |
| 47 | + public SetOperationMerge(SetOperationNode node, Rule.Context context) { |
| 48 | + this.node = node; |
| 49 | + this.context = context; |
| 50 | + this.newSources = new ArrayList<>(); |
| 51 | + } |
| 52 | + |
| 53 | + // Merge multiple union into one union |
| 54 | + public Optional<SetOperationNode> merge() { |
| 55 | + |
| 56 | + checkState( |
| 57 | + node instanceof UnionNode, "unexpected node type: %s", node.getClass().getSimpleName()); |
| 58 | + Lookup lookup = context.getLookup(); |
| 59 | + // Pre-check |
| 60 | + boolean anyMerge = |
| 61 | + node.getChildren().stream() |
| 62 | + .map(lookup::resolve) |
| 63 | + .anyMatch(child -> node.getClass().equals(child.getClass())); |
| 64 | + if (!anyMerge) { |
| 65 | + return Optional.empty(); |
| 66 | + } |
| 67 | + |
| 68 | + List<PlanNode> childrenOfUnion = |
| 69 | + node.getChildren().stream().map(lookup::resolve).collect(toImmutableList()); |
| 70 | + |
| 71 | + ImmutableListMultimap.Builder<Symbol, Symbol> newMappingsBuilder = |
| 72 | + ImmutableListMultimap.builder(); |
| 73 | + |
| 74 | + boolean rewritten = false; |
| 75 | + |
| 76 | + for (int i = 0; i < childrenOfUnion.size(); i++) { |
| 77 | + PlanNode child = childrenOfUnion.get(i); |
| 78 | + |
| 79 | + // Determine if set operations can be merged and whether the resulting set operation is |
| 80 | + // quantified DISTINCT or ALL |
| 81 | + Optional<Boolean> mergedQuantifier = mergedQuantifierIsDistinct(node, child); |
| 82 | + if (mergedQuantifier.isPresent()) { |
| 83 | + addMergedMappings((SetOperationNode) child, i, newMappingsBuilder); |
| 84 | + rewritten = true; |
| 85 | + } else { |
| 86 | + // Keep mapping as it is |
| 87 | + addOriginalMappings(child, i, newMappingsBuilder); |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + if (!rewritten) { |
| 92 | + return Optional.empty(); |
| 93 | + } |
| 94 | + |
| 95 | + // the union has merged |
| 96 | + return Optional.of( |
| 97 | + new UnionNode( |
| 98 | + node.getPlanNodeId(), newSources, newMappingsBuilder.build(), node.getOutputSymbols())); |
| 99 | + } |
| 100 | + |
| 101 | + private void addMergedMappings( |
| 102 | + SetOperationNode child, |
| 103 | + int childIndex, |
| 104 | + ImmutableListMultimap.Builder<Symbol, Symbol> newMappingsBuilder) { |
| 105 | + |
| 106 | + newSources.addAll(child.getChildren()); |
| 107 | + Map<Symbol, Collection<Symbol>> symbolMappings = node.getSymbolMapping().asMap(); |
| 108 | + for (Map.Entry<Symbol, Collection<Symbol>> mapping : symbolMappings.entrySet()) { |
| 109 | + Symbol input = Iterables.get(mapping.getValue(), childIndex); |
| 110 | + newMappingsBuilder.putAll(mapping.getKey(), child.getSymbolMapping().get(input)); |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + private void addOriginalMappings( |
| 115 | + PlanNode child, |
| 116 | + int childIndex, |
| 117 | + ImmutableListMultimap.Builder<Symbol, Symbol> newMappingsBuilder) { |
| 118 | + |
| 119 | + newSources.add(child); |
| 120 | + Map<Symbol, Collection<Symbol>> symbolMappings = node.getSymbolMapping().asMap(); |
| 121 | + for (Map.Entry<Symbol, Collection<Symbol>> mapping : symbolMappings.entrySet()) { |
| 122 | + newMappingsBuilder.put(mapping.getKey(), Iterables.get(mapping.getValue(), childIndex)); |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + /** |
| 127 | + * Check if node and child are mergeable based on their set operation type and quantifier. |
| 128 | + * |
| 129 | + * <p>Optional.empty() indicates that merge is not possible. |
| 130 | + */ |
| 131 | + private Optional<Boolean> mergedQuantifierIsDistinct(SetOperationNode node, PlanNode child) { |
| 132 | + |
| 133 | + if (!node.getClass().equals(child.getClass())) { |
| 134 | + return Optional.empty(); |
| 135 | + } |
| 136 | + |
| 137 | + if (node instanceof UnionNode) { |
| 138 | + return Optional.of(false); |
| 139 | + } |
| 140 | + |
| 141 | + // the Judgment logic for intersect and except wait for supplying |
| 142 | + return Optional.empty(); |
| 143 | + } |
| 144 | +} |
0 commit comments