|
| 1 | +/* |
| 2 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | + * you may not use this file except in compliance with the License. |
| 4 | + * You may obtain a copy of the License at |
| 5 | + * |
| 6 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | + * |
| 8 | + * Unless required by applicable law or agreed to in writing, software |
| 9 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | + * See the License for the specific language governing permissions and |
| 12 | + * limitations under the License. |
| 13 | + */ |
| 14 | + |
| 15 | +package com.facebook.presto.sql.planner.iterative.rule; |
| 16 | + |
| 17 | +import com.facebook.presto.sql.planner.Symbol; |
| 18 | +import com.facebook.presto.sql.planner.iterative.Lookup; |
| 19 | +import com.facebook.presto.sql.planner.plan.JoinNode; |
| 20 | +import com.facebook.presto.sql.planner.plan.PlanNode; |
| 21 | +import com.facebook.presto.sql.tree.Expression; |
| 22 | +import com.google.common.collect.ImmutableList; |
| 23 | + |
| 24 | +import java.util.ArrayList; |
| 25 | +import java.util.List; |
| 26 | + |
| 27 | +import static com.facebook.presto.sql.ExpressionUtils.and; |
| 28 | +import static com.facebook.presto.sql.planner.DeterminismEvaluator.isDeterministic; |
| 29 | +import static com.facebook.presto.sql.planner.plan.JoinNode.Type.INNER; |
| 30 | +import static com.facebook.presto.sql.tree.BooleanLiteral.TRUE_LITERAL; |
| 31 | +import static com.google.common.base.Preconditions.checkArgument; |
| 32 | +import static com.google.common.base.Preconditions.checkState; |
| 33 | +import static com.google.common.collect.ImmutableList.toImmutableList; |
| 34 | +import static java.util.Objects.requireNonNull; |
| 35 | + |
| 36 | +/** |
| 37 | + * This class represents a set of inner joins that can be executed in any order. |
| 38 | + */ |
| 39 | +class MultiJoinNode |
| 40 | +{ |
| 41 | + private static final int JOIN_LIMIT = 10; |
| 42 | + |
| 43 | + private final List<PlanNode> sources; |
| 44 | + private final Expression filter; |
| 45 | + private final List<Symbol> outputSymbols; |
| 46 | + |
| 47 | + public MultiJoinNode(List<PlanNode> sources, Expression filter, List<Symbol> outputSymbols) |
| 48 | + { |
| 49 | + this.sources = ImmutableList.copyOf(requireNonNull(sources, "sources is null")); |
| 50 | + this.filter = requireNonNull(filter, "filter is null"); |
| 51 | + this.outputSymbols = ImmutableList.copyOf(requireNonNull(outputSymbols, "outputSymbols is null")); |
| 52 | + |
| 53 | + List<Symbol> inputSymbols = sources.stream().flatMap(source -> source.getOutputSymbols().stream()).collect(toImmutableList()); |
| 54 | + checkArgument(inputSymbols.containsAll(outputSymbols), "inputs do not contain all output symbols"); |
| 55 | + } |
| 56 | + |
| 57 | + public Expression getFilter() |
| 58 | + { |
| 59 | + return filter; |
| 60 | + } |
| 61 | + |
| 62 | + public List<PlanNode> getSources() |
| 63 | + { |
| 64 | + return sources; |
| 65 | + } |
| 66 | + |
| 67 | + public List<Symbol> getOutputSymbols() |
| 68 | + { |
| 69 | + return outputSymbols; |
| 70 | + } |
| 71 | + |
| 72 | + static MultiJoinNode toMultiJoinNode(JoinNode joinNode, Lookup lookup) |
| 73 | + { |
| 74 | + return new MultiJoinNodeBuilder(joinNode, lookup).toMultiJoinNode(); |
| 75 | + } |
| 76 | + |
| 77 | + private static class MultiJoinNodeBuilder |
| 78 | + { |
| 79 | + private final List<PlanNode> sources = new ArrayList<>(); |
| 80 | + private final List<Expression> filters = new ArrayList<>(); |
| 81 | + private final List<Symbol> outputSymbols; |
| 82 | + private final Lookup lookup; |
| 83 | + |
| 84 | + MultiJoinNodeBuilder(JoinNode node, Lookup lookup) |
| 85 | + { |
| 86 | + requireNonNull(node, "node is null"); |
| 87 | + checkState(node.getType() == INNER, "join type must be INNER"); |
| 88 | + this.outputSymbols = node.getOutputSymbols(); |
| 89 | + this.lookup = requireNonNull(lookup, "lookup is null"); |
| 90 | + flattenNode(node); |
| 91 | + } |
| 92 | + |
| 93 | + private void flattenNode(PlanNode node) |
| 94 | + { |
| 95 | + PlanNode resolved = lookup.resolve(node); |
| 96 | + if (resolved instanceof JoinNode && sources.size() < JOIN_LIMIT) { |
| 97 | + JoinNode joinNode = (JoinNode) resolved; |
| 98 | + if (joinNode.getType() == INNER && isDeterministic(joinNode.getFilter().orElse(TRUE_LITERAL))) { |
| 99 | + flattenNode(joinNode.getLeft()); |
| 100 | + flattenNode(joinNode.getRight()); |
| 101 | + joinNode.getCriteria().stream() |
| 102 | + .map(JoinNode.EquiJoinClause::toExpression) |
| 103 | + .forEach(filters::add); |
| 104 | + joinNode.getFilter().ifPresent(filters::add); |
| 105 | + return; |
| 106 | + } |
| 107 | + } |
| 108 | + sources.add(node); |
| 109 | + } |
| 110 | + |
| 111 | + MultiJoinNode toMultiJoinNode() |
| 112 | + { |
| 113 | + return new MultiJoinNode(sources, and(filters), outputSymbols); |
| 114 | + } |
| 115 | + } |
| 116 | +} |
0 commit comments