Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ public final class SystemSessionProperties
public static final String ENABLE_INTERMEDIATE_AGGREGATIONS = "enable_intermediate_aggregations";
public static final String PUSH_AGGREGATION_THROUGH_JOIN = "push_aggregation_through_join";
public static final String PUSH_SEMI_JOIN_THROUGH_UNION = "push_semi_join_through_union";
public static final String SIMPLIFY_COALESCE_OVER_JOIN_KEYS = "simplify_coalesce_over_join_keys";
public static final String PUSHDOWN_THROUGH_UNNEST = "pushdown_through_unnest";
public static final String PUSH_PARTIAL_AGGREGATION_THROUGH_JOIN = "push_partial_aggregation_through_join";
public static final String PARSE_DECIMAL_LITERALS_AS_DOUBLE = "parse_decimal_literals_as_double";
Expand Down Expand Up @@ -931,6 +932,11 @@ public SystemSessionProperties(
"Allow pushing semi joins through union",
featuresConfig.isPushSemiJoinThroughUnion(),
false),
booleanProperty(
SIMPLIFY_COALESCE_OVER_JOIN_KEYS,
"Simplify redundant COALESCE expressions over equi-join keys",
featuresConfig.isSimplifyCoalesceOverJoinKeys(),
false),
booleanProperty(
PUSHDOWN_THROUGH_UNNEST,
"Allow pushing projections and filters below unnest",
Expand Down Expand Up @@ -2615,6 +2621,11 @@ public static boolean isPushSemiJoinThroughUnion(Session session)
return session.getSystemProperty(PUSH_SEMI_JOIN_THROUGH_UNION, Boolean.class);
}

public static boolean isSimplifyCoalesceOverJoinKeys(Session session)
{
return session.getSystemProperty(SIMPLIFY_COALESCE_OVER_JOIN_KEYS, Boolean.class);
}

public static boolean isPushdownThroughUnnest(Session session)
{
return session.getSystemProperty(PUSHDOWN_THROUGH_UNNEST, Boolean.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ public class FeaturesConfig
private double defaultWriterReplicationCoefficient = 3;
private boolean pushAggregationThroughJoin = true;
private boolean pushSemiJoinThroughUnion;
private boolean simplifyCoalesceOverJoinKeys;
private boolean pushdownThroughUnnest;
private double memoryRevokingTarget = 0.5;
private double memoryRevokingThreshold = 0.9;
Expand Down Expand Up @@ -1691,6 +1692,19 @@ public FeaturesConfig setPushSemiJoinThroughUnion(boolean pushSemiJoinThroughUni
return this;
}

public boolean isSimplifyCoalesceOverJoinKeys()
{
return simplifyCoalesceOverJoinKeys;
}

@Config("optimizer.simplify-coalesce-over-join-keys")
@ConfigDescription("Simplify redundant COALESCE expressions over equi-join keys based on join type")
public FeaturesConfig setSimplifyCoalesceOverJoinKeys(boolean simplifyCoalesceOverJoinKeys)
{
this.simplifyCoalesceOverJoinKeys = simplifyCoalesceOverJoinKeys;
return this;
}

public boolean isPushdownThroughUnnest()
{
return pushdownThroughUnnest;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@
import com.facebook.presto.sql.planner.iterative.rule.RuntimeReorderJoinSides;
import com.facebook.presto.sql.planner.iterative.rule.ScaledWriterRule;
import com.facebook.presto.sql.planner.iterative.rule.SimplifyCardinalityMap;
import com.facebook.presto.sql.planner.iterative.rule.SimplifyCoalesceOverJoinKeys;
import com.facebook.presto.sql.planner.iterative.rule.SimplifyCountOverConstant;
import com.facebook.presto.sql.planner.iterative.rule.SimplifyRowExpressions;
import com.facebook.presto.sql.planner.iterative.rule.SimplifySortWithConstantInput;
Expand Down Expand Up @@ -582,7 +583,8 @@ public PlanOptimizers(
ruleStats,
statsCalculator,
estimatedExchangesCostCalculator,
ImmutableSet.of(new RemoveCrossJoinWithConstantInput(metadata.getFunctionAndTypeManager()))));
ImmutableSet.of(new RemoveCrossJoinWithConstantInput(metadata.getFunctionAndTypeManager()),
new SimplifyCoalesceOverJoinKeys())));

builder.add(new IterativeOptimizer(
metadata,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
/*
* 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.Capture;
import com.facebook.presto.matching.Captures;
import com.facebook.presto.matching.Pattern;
import com.facebook.presto.spi.plan.Assignments;
import com.facebook.presto.spi.plan.EquiJoinClause;
import com.facebook.presto.spi.plan.JoinNode;
import com.facebook.presto.spi.plan.JoinType;
import com.facebook.presto.spi.plan.ProjectNode;
import com.facebook.presto.spi.relation.RowExpression;
import com.facebook.presto.spi.relation.SpecialFormExpression;
import com.facebook.presto.spi.relation.VariableReferenceExpression;
import com.facebook.presto.sql.planner.iterative.Rule;
import com.google.common.collect.ImmutableSet;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import static com.facebook.presto.SystemSessionProperties.isSimplifyCoalesceOverJoinKeys;
import static com.facebook.presto.matching.Capture.newCapture;
import static com.facebook.presto.spi.relation.SpecialFormExpression.Form.COALESCE;
import static com.facebook.presto.sql.planner.plan.Patterns.join;
import static com.facebook.presto.sql.planner.plan.Patterns.project;
import static com.facebook.presto.sql.planner.plan.Patterns.source;

/**
* Simplifies redundant COALESCE expressions over equi-join keys based on join type.
* <p>
* For equi-join l.x = r.y:
* <ul>
* <li>LEFT JOIN: COALESCE(l.x, r.y) or COALESCE(r.y, l.x) → l.x (left key is never null)</li>
* <li>RIGHT JOIN: COALESCE(l.x, r.y) or COALESCE(r.y, l.x) → r.y (right key is never null)</li>
* <li>INNER JOIN: COALESCE(l.x, r.y) → l.x, COALESCE(r.y, l.x) → r.y (both non-null, pick first arg)</li>
* <li>FULL JOIN: cannot simplify</li>
* </ul>
* <p>
* This is important because tool-generated queries often produce patterns like
* {@code SELECT COALESCE(l.x, r.y) FROM l LEFT JOIN r ON l.x = r.y} which
* interferes with bucketed join planning.
*/
public class SimplifyCoalesceOverJoinKeys
implements Rule<ProjectNode>
{
private static final Capture<JoinNode> JOIN = newCapture();

private static final Pattern<ProjectNode> PATTERN = project()
.with(source().matching(join().capturedAs(JOIN)));
Comment on lines +63 to +64
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: I remember we can put the join type check within the Pattern here too?


@Override
public Pattern<ProjectNode> getPattern()
{
return PATTERN;
}

@Override
public boolean isEnabled(Session session)
{
return isSimplifyCoalesceOverJoinKeys(session);
}

@Override
public Result apply(ProjectNode project, Captures captures, Context context)
{
JoinNode joinNode = captures.get(JOIN);
JoinType joinType = joinNode.getType();

// FULL JOIN: both sides may be null, cannot simplify
if (joinType == JoinType.FULL) {
return Result.empty();
}

List<EquiJoinClause> criteria = joinNode.getCriteria();
if (criteria.isEmpty()) {
return Result.empty();
}

Set<VariableReferenceExpression> leftVariables = ImmutableSet.copyOf(joinNode.getLeft().getOutputVariables());
Set<VariableReferenceExpression> rightVariables = ImmutableSet.copyOf(joinNode.getRight().getOutputVariables());

// Build a map of join key pairs for quick lookup
// Maps (leftVar, rightVar) pairs from equi-join criteria
Map<VariableReferenceExpression, VariableReferenceExpression> leftToRight = new HashMap<>();
Map<VariableReferenceExpression, VariableReferenceExpression> rightToLeft = new HashMap<>();
for (EquiJoinClause clause : criteria) {
leftToRight.put(clause.getLeft(), clause.getRight());
rightToLeft.put(clause.getRight(), clause.getLeft());
}

Assignments assignments = project.getAssignments();
boolean anySimplified = false;
Assignments.Builder newAssignments = Assignments.builder();

for (Map.Entry<VariableReferenceExpression, RowExpression> entry : assignments.getMap().entrySet()) {
RowExpression expression = entry.getValue();
RowExpression simplified = trySimplifyCoalesce(expression, joinType, leftVariables, rightVariables, leftToRight, rightToLeft);
if (simplified != expression) {
anySimplified = true;
}
newAssignments.put(entry.getKey(), simplified);
}

if (!anySimplified) {
return Result.empty();
}

return Result.ofPlanNode(new ProjectNode(
project.getSourceLocation(),
context.getIdAllocator().getNextId(),
joinNode,
newAssignments.build(),
project.getLocality()));
}

private static RowExpression trySimplifyCoalesce(
RowExpression expression,
JoinType joinType,
Set<VariableReferenceExpression> leftVariables,
Set<VariableReferenceExpression> rightVariables,
Map<VariableReferenceExpression, VariableReferenceExpression> leftToRight,
Map<VariableReferenceExpression, VariableReferenceExpression> rightToLeft)
{
if (!(expression instanceof SpecialFormExpression)) {
return expression;
}

SpecialFormExpression specialForm = (SpecialFormExpression) expression;
if (specialForm.getForm() != COALESCE) {
return expression;
}

List<RowExpression> arguments = specialForm.getArguments();
if (arguments.size() != 2) {
return expression;
}

// Both arguments must be variable references
if (!(arguments.get(0) instanceof VariableReferenceExpression) ||
!(arguments.get(1) instanceof VariableReferenceExpression)) {
return expression;
}

VariableReferenceExpression first = (VariableReferenceExpression) arguments.get(0);
VariableReferenceExpression second = (VariableReferenceExpression) arguments.get(1);

// Check if these two variables form an equi-join key pair
boolean isLeftRight = leftVariables.contains(first) && rightVariables.contains(second) &&
leftToRight.containsKey(first) && leftToRight.get(first).equals(second);
boolean isRightLeft = rightVariables.contains(first) && leftVariables.contains(second) &&
rightToLeft.containsKey(first) && rightToLeft.get(first).equals(second);

if (!isLeftRight && !isRightLeft) {
return expression;
}

VariableReferenceExpression leftKey = isLeftRight ? first : second;
VariableReferenceExpression rightKey = isLeftRight ? second : first;

switch (joinType) {
case INNER:
// Both sides non-null on join keys; pick the first argument
return first;
case LEFT:
// Left key guaranteed non-null
return leftKey;
case RIGHT:
// Right key guaranteed non-null
return rightKey;
default:
return expression;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ public void testDefaults()
.setEnableIntermediateAggregations(false)
.setPushAggregationThroughJoin(true)
.setPushSemiJoinThroughUnion(false)
.setSimplifyCoalesceOverJoinKeys(false)
.setPushdownThroughUnnest(false)
.setForceSingleNodeOutput(true)
.setPagesIndexEagerCompactionEnabled(false)
Expand Down Expand Up @@ -359,6 +360,7 @@ public void testExplicitPropertyMappings()
.put("optimizer.treat-low-confidence-zero-estimation-as-unknown", "true")
.put("optimizer.push-aggregation-through-join", "false")
.put("optimizer.push-semi-join-through-union", "true")
.put("optimizer.simplify-coalesce-over-join-keys", "true")
.put("optimizer.pushdown-through-unnest", "true")
.put("optimizer.aggregation-partition-merging", "top_down")
.put("optimizer.local-exchange-parent-preference-strategy", "automatic")
Expand Down Expand Up @@ -593,6 +595,7 @@ public void testExplicitPropertyMappings()
.setAggregationPartitioningMergingStrategy(TOP_DOWN)
.setPushAggregationThroughJoin(false)
.setPushSemiJoinThroughUnion(true)
.setSimplifyCoalesceOverJoinKeys(true)
.setPushdownThroughUnnest(true)
.setSpillEnabled(true)
.setJoinSpillingEnabled(false)
Expand Down
Loading
Loading