|
| 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 | +package com.facebook.presto.sql.planner.optimizations; |
| 15 | + |
| 16 | +import com.facebook.presto.Session; |
| 17 | +import com.facebook.presto.metadata.Metadata; |
| 18 | +import com.facebook.presto.spi.VariableAllocator; |
| 19 | +import com.facebook.presto.spi.WarningCollector; |
| 20 | +import com.facebook.presto.spi.plan.EquiJoinClause; |
| 21 | +import com.facebook.presto.spi.plan.JoinNode; |
| 22 | +import com.facebook.presto.spi.plan.JoinType; |
| 23 | +import com.facebook.presto.spi.plan.MergeJoinNode; |
| 24 | +import com.facebook.presto.spi.plan.Ordering; |
| 25 | +import com.facebook.presto.spi.plan.OrderingScheme; |
| 26 | +import com.facebook.presto.spi.plan.PlanNode; |
| 27 | +import com.facebook.presto.spi.plan.PlanNodeIdAllocator; |
| 28 | +import com.facebook.presto.spi.plan.SortNode; |
| 29 | +import com.facebook.presto.spi.relation.VariableReferenceExpression; |
| 30 | +import com.facebook.presto.sql.planner.TypeProvider; |
| 31 | +import com.facebook.presto.sql.planner.plan.SimplePlanRewriter; |
| 32 | +import com.google.common.collect.ImmutableList; |
| 33 | + |
| 34 | +import java.util.List; |
| 35 | +import java.util.Optional; |
| 36 | + |
| 37 | +import static com.facebook.presto.SystemSessionProperties.preferSortMergeJoin; |
| 38 | +import static com.facebook.presto.common.block.SortOrder.ASC_NULLS_FIRST; |
| 39 | +import static com.google.common.collect.ImmutableList.toImmutableList; |
| 40 | +import static java.util.Objects.requireNonNull; |
| 41 | + |
| 42 | +public class SortMergeJoinOptimizer |
| 43 | + implements PlanOptimizer |
| 44 | +{ |
| 45 | + private final Metadata metadata; |
| 46 | + private final boolean nativeExecution; |
| 47 | + private boolean isEnabledForTesting; |
| 48 | + |
| 49 | + public SortMergeJoinOptimizer(Metadata metadata, boolean nativeExecution) |
| 50 | + { |
| 51 | + this.metadata = requireNonNull(metadata, "metadata is null"); |
| 52 | + this.nativeExecution = nativeExecution; |
| 53 | + } |
| 54 | + |
| 55 | + @Override |
| 56 | + public void setEnabledForTesting(boolean isSet) |
| 57 | + { |
| 58 | + isEnabledForTesting = isSet; |
| 59 | + } |
| 60 | + |
| 61 | + @Override |
| 62 | + public boolean isEnabled(Session session) |
| 63 | + { |
| 64 | + // TODO: Consider group execution and single node execution. |
| 65 | + return isEnabledForTesting || preferSortMergeJoin(session); |
| 66 | + } |
| 67 | + |
| 68 | + @Override |
| 69 | + public PlanOptimizerResult optimize(PlanNode plan, Session session, TypeProvider type, VariableAllocator variableAllocator, PlanNodeIdAllocator idAllocator, WarningCollector warningCollector) |
| 70 | + { |
| 71 | + requireNonNull(plan, "plan is null"); |
| 72 | + requireNonNull(session, "session is null"); |
| 73 | + requireNonNull(variableAllocator, "variableAllocator is null"); |
| 74 | + requireNonNull(idAllocator, "idAllocator is null"); |
| 75 | + |
| 76 | + if (isEnabled(session)) { |
| 77 | + Rewriter rewriter = new SortMergeJoinOptimizer.Rewriter(idAllocator, metadata, session); |
| 78 | + PlanNode rewrittenPlan = SimplePlanRewriter.rewriteWith(rewriter, plan, null); |
| 79 | + return PlanOptimizerResult.optimizerResult(rewrittenPlan, rewriter.isPlanChanged()); |
| 80 | + } |
| 81 | + return PlanOptimizerResult.optimizerResult(plan, false); |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * @param joinNode |
| 86 | + * @return returns true if merge join is supported for the given join node. |
| 87 | + */ |
| 88 | + public boolean isMergeJoinEligible(JoinNode joinNode) |
| 89 | + { |
| 90 | + return (joinNode.getType() == JoinType.INNER || joinNode.getType() == JoinType.LEFT || joinNode.getType() == JoinType.RIGHT) |
| 91 | + && !joinNode.isCrossJoin(); |
| 92 | + } |
| 93 | + |
| 94 | + private class Rewriter |
| 95 | + extends SimplePlanRewriter<Void> |
| 96 | + { |
| 97 | + private final PlanNodeIdAllocator idAllocator; |
| 98 | + private final Metadata metadata; |
| 99 | + private final Session session; |
| 100 | + private boolean planChanged; |
| 101 | + |
| 102 | + private Rewriter(PlanNodeIdAllocator idAllocator, Metadata metadata, Session session) |
| 103 | + { |
| 104 | + this.idAllocator = requireNonNull(idAllocator, "idAllocator is null"); |
| 105 | + this.metadata = requireNonNull(metadata, "metadata is null"); |
| 106 | + this.session = requireNonNull(session, "session is null"); |
| 107 | + } |
| 108 | + |
| 109 | + public boolean isPlanChanged() |
| 110 | + { |
| 111 | + return planChanged; |
| 112 | + } |
| 113 | + |
| 114 | + @Override |
| 115 | + public PlanNode visitJoin(JoinNode node, RewriteContext<Void> context) |
| 116 | + { |
| 117 | + if (!isMergeJoinEligible(node)) { |
| 118 | + return node; |
| 119 | + } |
| 120 | + |
| 121 | + PlanNode left = node.getLeft(); |
| 122 | + PlanNode right = node.getRight(); |
| 123 | + |
| 124 | + List<VariableReferenceExpression> leftJoinColumns = node.getCriteria().stream().map(EquiJoinClause::getLeft).collect(toImmutableList()); |
| 125 | + |
| 126 | + if (!isPlanOutputSortedByColumns(left, leftJoinColumns)) { |
| 127 | + List<Ordering> leftOrdering = node.getCriteria().stream() |
| 128 | + .map(criterion -> new Ordering(criterion.getLeft(), ASC_NULLS_FIRST)) |
| 129 | + .collect(toImmutableList()); |
| 130 | + left = new SortNode( |
| 131 | + Optional.empty(), |
| 132 | + idAllocator.getNextId(), |
| 133 | + left, |
| 134 | + new OrderingScheme(leftOrdering), |
| 135 | + true, |
| 136 | + ImmutableList.of()); |
| 137 | + } |
| 138 | + |
| 139 | + List<VariableReferenceExpression> rightJoinColumns = node.getCriteria().stream() |
| 140 | + .map(EquiJoinClause::getRight) |
| 141 | + .collect(toImmutableList()); |
| 142 | + if (!isPlanOutputSortedByColumns(right, rightJoinColumns)) { |
| 143 | + List<Ordering> rightOrdering = node.getCriteria().stream() |
| 144 | + .map(criterion -> new Ordering(criterion.getRight(), ASC_NULLS_FIRST)) |
| 145 | + .collect(toImmutableList()); |
| 146 | + right = new SortNode( |
| 147 | + Optional.empty(), |
| 148 | + idAllocator.getNextId(), |
| 149 | + right, |
| 150 | + new OrderingScheme(rightOrdering), |
| 151 | + true, |
| 152 | + ImmutableList.of()); |
| 153 | + } |
| 154 | + |
| 155 | + planChanged = true; |
| 156 | + return new MergeJoinNode( |
| 157 | + Optional.empty(), |
| 158 | + node.getId(), |
| 159 | + node.getType(), |
| 160 | + left, |
| 161 | + right, |
| 162 | + node.getCriteria(), |
| 163 | + node.getOutputVariables(), |
| 164 | + node.getFilter(), |
| 165 | + node.getLeftHashVariable(), |
| 166 | + node.getRightHashVariable()); |
| 167 | + } |
| 168 | + |
| 169 | + private boolean isPlanOutputSortedByColumns(PlanNode plan, List<VariableReferenceExpression> columns) |
| 170 | + { |
| 171 | + StreamPropertyDerivations.StreamProperties properties = StreamPropertyDerivations.derivePropertiesRecursively(plan, metadata, session, nativeExecution); |
| 172 | + |
| 173 | + // Check if partitioning columns (bucketed-by columns [B]) are a subset of join columns [J] |
| 174 | + // B = subset (J) |
| 175 | + if (!verifyStreamProperties(properties, columns)) { |
| 176 | + return false; |
| 177 | + } |
| 178 | + |
| 179 | + // Check if the output of the subplan is ordered by the join columns |
| 180 | + return !LocalProperties.match(properties.getLocalProperties(), LocalProperties.sorted(columns, ASC_NULLS_FIRST)).get(0).isPresent(); |
| 181 | + } |
| 182 | + |
| 183 | + private boolean verifyStreamProperties(StreamPropertyDerivations.StreamProperties streamProperties, List<VariableReferenceExpression> joinColumns) |
| 184 | + { |
| 185 | + if (!streamProperties.getPartitioningColumns().isPresent()) { |
| 186 | + return false; |
| 187 | + } |
| 188 | + List<VariableReferenceExpression> partitioningColumns = streamProperties.getPartitioningColumns().get(); |
| 189 | + return partitioningColumns.size() <= joinColumns.size() && joinColumns.containsAll(partitioningColumns); |
| 190 | + } |
| 191 | + } |
| 192 | +} |
0 commit comments