Skip to content

Commit e6d0332

Browse files
committed
Add ReorderJoins rule to pick the best join order
Add a rule to enumerate join order possibilities for a join graph and choose the least cost option. This does a minimal form of cross join elimination, by only partitioning nodes into groups that have at least one edge between them, which eliminates some unnecessary cross joins from consideration. It also means that necessary cross joins will always be executed as late as possible in the plan (which may be worse).
1 parent 3ba7f24 commit e6d0332

File tree

8 files changed

+1268
-2
lines changed

8 files changed

+1268
-2
lines changed

presto-main/src/main/java/com/facebook/presto/sql/planner/PlanOptimizers.java

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
import com.facebook.presto.sql.planner.iterative.rule.RemoveEmptyDelete;
5656
import com.facebook.presto.sql.planner.iterative.rule.RemoveFullSample;
5757
import com.facebook.presto.sql.planner.iterative.rule.RemoveRedundantIdentityProjections;
58+
import com.facebook.presto.sql.planner.iterative.rule.ReorderJoins;
5859
import com.facebook.presto.sql.planner.iterative.rule.SimplifyCountOverConstant;
5960
import com.facebook.presto.sql.planner.iterative.rule.SingleMarkDistinctToGroupBy;
6061
import com.facebook.presto.sql.planner.iterative.rule.SwapAdjacentWindowsBySpecifications;
@@ -322,13 +323,32 @@ public PlanOptimizers(
322323
ImmutableList.of(new com.facebook.presto.sql.planner.optimizations.EliminateCrossJoins()), // This can pull up Filter and Project nodes from between Joins, so we need to push them down again
323324
ImmutableSet.of(new EliminateCrossJoins())
324325
),
326+
325327
new PredicatePushDown(metadata, sqlParser),
326328
new IterativeOptimizer(
327329
stats,
328330
statsCalculator,
329331
estimatedExchangesCostCalculator,
330332
ImmutableSet.of(new PushDownTableConstraints(metadata, sqlParser))),
331-
projectionPushDown);
333+
projectionPushDown,
334+
new PruneUnreferencedOutputs(),
335+
new IterativeOptimizer(
336+
stats,
337+
statsCalculator,
338+
estimatedExchangesCostCalculator,
339+
ImmutableSet.of(new RemoveRedundantIdentityProjections())
340+
),
341+
342+
// Because ReorderJoins runs only once,
343+
// PredicatePushDown, PruneUnreferenedOutputpus and RemoveRedundantIdentityProjections
344+
// need to run beforehand in order to produce an optimal join order
345+
// It also needs to run after EliminateCrossJoins so that its chosen order doesn't get undone.
346+
new IterativeOptimizer(
347+
stats,
348+
statsCalculator,
349+
estimatedExchangesCostCalculator,
350+
ImmutableSet.of(new ReorderJoins(costComparator))
351+
));
332352

333353
if (featuresConfig.isOptimizeSingleDistinct()) {
334354
builder.add(
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
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

Comments
 (0)