-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Remove cross join if one side of input is single row constant
- Loading branch information
1 parent
95da85b
commit d0e47dc
Showing
10 changed files
with
543 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
145 changes: 145 additions & 0 deletions
145
...java/com/facebook/presto/sql/planner/iterative/rule/RemoveCrossJoinWithConstantInput.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
/* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.facebook.presto.sql.planner.iterative.rule; | ||
|
||
import com.facebook.presto.Session; | ||
import com.facebook.presto.matching.Captures; | ||
import com.facebook.presto.matching.Pattern; | ||
import com.facebook.presto.metadata.FunctionAndTypeManager; | ||
import com.facebook.presto.spi.plan.FilterNode; | ||
import com.facebook.presto.spi.plan.JoinType; | ||
import com.facebook.presto.spi.plan.PlanNode; | ||
import com.facebook.presto.spi.plan.ProjectNode; | ||
import com.facebook.presto.spi.plan.ValuesNode; | ||
import com.facebook.presto.spi.relation.RowExpression; | ||
import com.facebook.presto.spi.relation.VariableReferenceExpression; | ||
import com.facebook.presto.sql.planner.iterative.Rule; | ||
import com.facebook.presto.sql.planner.plan.JoinNode; | ||
import com.facebook.presto.sql.relational.RowExpressionDeterminismEvaluator; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
import java.util.function.Function; | ||
import java.util.stream.IntStream; | ||
|
||
import static com.facebook.presto.SystemSessionProperties.isRemoveCrossJoinWithConstantSingleRowInputEnabled; | ||
import static com.facebook.presto.sql.planner.PlannerUtils.addProjections; | ||
import static com.facebook.presto.sql.planner.plan.Patterns.join; | ||
import static com.google.common.base.Preconditions.checkState; | ||
import static com.google.common.collect.ImmutableMap.toImmutableMap; | ||
|
||
/** | ||
* When one side of a cross join is one single row of constant, we can remove the cross join and replace it with a project. | ||
* <pre> | ||
* - Cross Join | ||
* - table scan | ||
* left_field | ||
* - values // only one row | ||
* right_field := 1 | ||
* </pre> | ||
* into | ||
* <pre> | ||
* - project | ||
* left_field := left_field | ||
* right_field := 1 | ||
* - table scan | ||
* left_field | ||
* </pre> | ||
*/ | ||
public class RemoveCrossJoinWithConstantInput | ||
implements Rule<JoinNode> | ||
{ | ||
private final RowExpressionDeterminismEvaluator rowExpressionDeterminismEvaluator; | ||
|
||
public RemoveCrossJoinWithConstantInput(FunctionAndTypeManager functionAndTypeManager) | ||
{ | ||
this.rowExpressionDeterminismEvaluator = new RowExpressionDeterminismEvaluator(functionAndTypeManager); | ||
} | ||
|
||
@Override | ||
public Pattern<JoinNode> getPattern() | ||
{ | ||
return join().matching(x -> x.getType().equals(JoinType.INNER) && x.getCriteria().isEmpty()); | ||
} | ||
|
||
@Override | ||
public boolean isEnabled(Session session) | ||
{ | ||
return isRemoveCrossJoinWithConstantSingleRowInputEnabled(session); | ||
} | ||
|
||
@Override | ||
public Result apply(JoinNode node, Captures captures, Context context) | ||
{ | ||
PlanNode singleValueInput; | ||
PlanNode joinInput; | ||
if (isOutputSingleConstantRow(context.getLookup().resolve(node.getRight()), context)) { | ||
singleValueInput = context.getLookup().resolve(node.getRight()); | ||
joinInput = context.getLookup().resolve(node.getLeft()); | ||
} | ||
else if (isOutputSingleConstantRow((context.getLookup().resolve(node.getLeft())), context)) { | ||
singleValueInput = context.getLookup().resolve(node.getLeft()); | ||
joinInput = context.getLookup().resolve(node.getRight()); | ||
} | ||
else { | ||
return Result.empty(); | ||
} | ||
Optional<Map<VariableReferenceExpression, RowExpression>> mapping = getConstantAssignments(singleValueInput, context); | ||
if (!mapping.isPresent()) { | ||
return Result.empty(); | ||
} | ||
PlanNode resultNode = addProjections(joinInput, context.getIdAllocator(), mapping.get()); | ||
if (node.getFilter().isPresent()) { | ||
resultNode = new FilterNode(node.getSourceLocation(), context.getIdAllocator().getNextId(), resultNode, node.getFilter().get()); | ||
} | ||
return Result.ofPlanNode(resultNode); | ||
} | ||
|
||
private boolean isOutputSingleConstantRow(PlanNode planNode, Context context) | ||
{ | ||
while (planNode instanceof ProjectNode) { | ||
planNode = context.getLookup().resolve(((ProjectNode) planNode).getSource()); | ||
} | ||
if (planNode instanceof ValuesNode) { | ||
return ((ValuesNode) planNode).getRows().size() == 1; | ||
} | ||
return false; | ||
} | ||
|
||
private Optional<Map<VariableReferenceExpression, RowExpression>> getConstantAssignments(PlanNode planNode, Context context) | ||
{ | ||
List<VariableReferenceExpression> outputVariables = planNode.getOutputVariables(); | ||
Map<VariableReferenceExpression, RowExpression> mapping = outputVariables.stream().collect(toImmutableMap(Function.identity(), Function.identity())); | ||
while (planNode instanceof ProjectNode) { | ||
Map<VariableReferenceExpression, RowExpression> assignments = ((ProjectNode) planNode).getAssignments().getMap(); | ||
mapping = mapping.entrySet().stream().collect(toImmutableMap( | ||
Map.Entry::getKey, entry -> entry.getValue() instanceof VariableReferenceExpression ? assignments.get(entry.getValue()) : entry.getValue())); | ||
planNode = context.getLookup().resolve(((ProjectNode) planNode).getSource()); | ||
} | ||
checkState(planNode instanceof ValuesNode); | ||
ValuesNode valuesNode = (ValuesNode) planNode; | ||
if (!valuesNode.getOutputVariables().isEmpty()) { | ||
Map<VariableReferenceExpression, RowExpression> assignments = IntStream.range(0, valuesNode.getOutputVariables().size()).boxed() | ||
.collect(toImmutableMap(idx -> valuesNode.getOutputVariables().get(idx), idx -> valuesNode.getRows().get(0).get(idx))); | ||
mapping = mapping.entrySet().stream().collect(toImmutableMap( | ||
Map.Entry::getKey, entry -> entry.getValue() instanceof VariableReferenceExpression ? assignments.get(entry.getValue()) : entry.getValue())); | ||
} | ||
boolean allDeterministic = mapping.values().stream().allMatch(rowExpressionDeterminismEvaluator::isDeterministic); | ||
if (allDeterministic) { | ||
return Optional.of(mapping); | ||
} | ||
return Optional.empty(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.