-
Notifications
You must be signed in to change notification settings - Fork 5.5k
Add an optimizer to add distinct below build side of semi join #25238
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
feilong-liu
merged 1 commit into
prestodb:master
from
feilong-liu:distinct_before_semijoin
Jun 5, 2025
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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
95 changes: 95 additions & 0 deletions
95
...main/java/com/facebook/presto/sql/planner/iterative/rule/AddDistinctForSemiJoinBuild.java
This file contains hidden or 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,95 @@ | ||
| /* | ||
| * 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.spi.plan.AggregationNode; | ||
| import com.facebook.presto.spi.plan.FilterNode; | ||
| import com.facebook.presto.spi.plan.PlanNode; | ||
| import com.facebook.presto.spi.plan.ProjectNode; | ||
| import com.facebook.presto.spi.plan.SemiJoinNode; | ||
| import com.facebook.presto.spi.relation.RowExpression; | ||
| import com.facebook.presto.spi.relation.VariableReferenceExpression; | ||
| import com.facebook.presto.sql.planner.iterative.Rule; | ||
| import com.google.common.collect.ImmutableList; | ||
| import com.google.common.collect.ImmutableMap; | ||
|
|
||
| import java.util.Optional; | ||
|
|
||
| import static com.facebook.presto.SystemSessionProperties.isAddDistinctBelowSemiJoinBuildEnabled; | ||
| import static com.facebook.presto.spi.plan.AggregationNode.isDistinct; | ||
| import static com.facebook.presto.spi.plan.AggregationNode.singleGroupingSet; | ||
| import static com.facebook.presto.sql.planner.plan.Patterns.semiJoin; | ||
|
|
||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please add small comment explaining the rule
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| public class AddDistinctForSemiJoinBuild | ||
| implements Rule<SemiJoinNode> | ||
| { | ||
| @Override | ||
| public Pattern<SemiJoinNode> getPattern() | ||
| { | ||
| return semiJoin(); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean isEnabled(Session session) | ||
| { | ||
| return isAddDistinctBelowSemiJoinBuildEnabled(session); | ||
| } | ||
|
|
||
| @Override | ||
| public Result apply(SemiJoinNode node, Captures captures, Context context) | ||
| { | ||
| PlanNode filterSource = context.getLookup().resolve(node.getFilteringSource()); | ||
| VariableReferenceExpression filteringSourceVariable = node.getFilteringSourceJoinVariable(); | ||
| if (isOutputDistinct(filterSource, filteringSourceVariable, context)) { | ||
| return Result.empty(); | ||
| } | ||
| AggregationNode.GroupingSetDescriptor groupingSetDescriptor = singleGroupingSet(ImmutableList.of(node.getFilteringSourceJoinVariable())); | ||
| AggregationNode distinctAggregation = new AggregationNode( | ||
| node.getSourceLocation(), | ||
| context.getIdAllocator().getNextId(), | ||
| filterSource, | ||
| ImmutableMap.of(), | ||
| groupingSetDescriptor, | ||
| ImmutableList.of(), | ||
| AggregationNode.Step.SINGLE, | ||
| Optional.empty(), | ||
| Optional.empty(), | ||
| Optional.empty()); | ||
|
|
||
| return Result.ofPlanNode(node.replaceChildren(ImmutableList.of(node.getSource(), distinctAggregation))); | ||
| } | ||
|
|
||
| boolean isOutputDistinct(PlanNode node, VariableReferenceExpression output, Context context) | ||
kaikalur marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| { | ||
| if (node instanceof AggregationNode) { | ||
| AggregationNode aggregationNode = (AggregationNode) node; | ||
| return isDistinct(aggregationNode) && aggregationNode.getGroupingKeys().size() == 1 && aggregationNode.getGroupingKeys().contains(output); | ||
| } | ||
| else if (node instanceof ProjectNode) { | ||
| ProjectNode projectNode = (ProjectNode) node; | ||
| RowExpression inputExpression = projectNode.getAssignments().get(output); | ||
| if (inputExpression instanceof VariableReferenceExpression) { | ||
| return isOutputDistinct(context.getLookup().resolve(projectNode.getSource()), (VariableReferenceExpression) inputExpression, context); | ||
| } | ||
| return false; | ||
| } | ||
| else if (node instanceof FilterNode) { | ||
| return isOutputDistinct(context.getLookup().resolve(((FilterNode) node).getSource()), output, context); | ||
| } | ||
| return false; | ||
| } | ||
| } | ||
This file contains hidden or 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
160 changes: 160 additions & 0 deletions
160
.../java/com/facebook/presto/sql/planner/iterative/rule/TestAddDistinctForSemiJoinBuild.java
This file contains hidden or 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,160 @@ | ||
| /* | ||
| * 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.spi.plan.AggregationNode; | ||
| import com.facebook.presto.spi.relation.VariableReferenceExpression; | ||
| import com.facebook.presto.sql.planner.iterative.rule.test.BaseRuleTest; | ||
| import com.google.common.collect.ImmutableMap; | ||
| import org.testng.annotations.Test; | ||
|
|
||
| import java.util.Optional; | ||
|
|
||
| import static com.facebook.presto.SystemSessionProperties.ADD_DISTINCT_BELOW_SEMI_JOIN_BUILD; | ||
| import static com.facebook.presto.sql.planner.assertions.PlanMatchPattern.aggregation; | ||
| import static com.facebook.presto.sql.planner.assertions.PlanMatchPattern.semiJoin; | ||
| import static com.facebook.presto.sql.planner.assertions.PlanMatchPattern.singleGroupingSet; | ||
| import static com.facebook.presto.sql.planner.assertions.PlanMatchPattern.values; | ||
| import static com.facebook.presto.sql.planner.iterative.rule.test.PlanBuilder.assignment; | ||
|
|
||
| public class TestAddDistinctForSemiJoinBuild | ||
| extends BaseRuleTest | ||
| { | ||
| @Test | ||
| public void testTrigger() | ||
| { | ||
| tester().assertThat(new AddDistinctForSemiJoinBuild()) | ||
| .setSystemProperty(ADD_DISTINCT_BELOW_SEMI_JOIN_BUILD, "true") | ||
| .on(p -> | ||
| { | ||
| VariableReferenceExpression sourceJoinVariable = p.variable("sourceJoinVariable"); | ||
| VariableReferenceExpression filteringSourceJoinVariable = p.variable("filteringSourceJoinVariable"); | ||
| VariableReferenceExpression semiJoinOutput = p.variable("semiJoinOutput"); | ||
| return p.semiJoin( | ||
| sourceJoinVariable, | ||
| filteringSourceJoinVariable, | ||
| semiJoinOutput, | ||
| Optional.empty(), | ||
| Optional.empty(), | ||
| p.values(sourceJoinVariable), | ||
| p.values(filteringSourceJoinVariable)); | ||
| }).matches( | ||
| semiJoin( | ||
| "sourceJoinVariable", | ||
| "filteringSourceJoinVariable", | ||
| "semiJoinOutput", | ||
| values("sourceJoinVariable"), | ||
| aggregation( | ||
| singleGroupingSet("filteringSourceJoinVariable"), | ||
| ImmutableMap.of(), | ||
| ImmutableMap.of(), | ||
| Optional.empty(), | ||
| AggregationNode.Step.SINGLE, | ||
| values("filteringSourceJoinVariable")))); | ||
| } | ||
|
|
||
| @Test | ||
| public void testTriggerOverNonQualifiedDistinctAggregation() | ||
| { | ||
| tester().assertThat(new AddDistinctForSemiJoinBuild()) | ||
| .setSystemProperty(ADD_DISTINCT_BELOW_SEMI_JOIN_BUILD, "true") | ||
| .on(p -> | ||
| { | ||
| VariableReferenceExpression sourceJoinVariable = p.variable("sourceJoinVariable"); | ||
| VariableReferenceExpression filteringSourceJoinVariable = p.variable("filteringSourceJoinVariable"); | ||
| VariableReferenceExpression semiJoinOutput = p.variable("semiJoinOutput"); | ||
| VariableReferenceExpression col1 = p.variable("col1"); | ||
| return p.semiJoin( | ||
| sourceJoinVariable, | ||
| filteringSourceJoinVariable, | ||
| semiJoinOutput, | ||
| Optional.empty(), | ||
| Optional.empty(), | ||
| p.values(sourceJoinVariable), | ||
| p.aggregation((a) -> a | ||
| .singleGroupingSet(filteringSourceJoinVariable, col1) | ||
| .step(AggregationNode.Step.SINGLE) | ||
| .source(p.values(filteringSourceJoinVariable, col1)))); | ||
| }).matches( | ||
| semiJoin( | ||
| "sourceJoinVariable", | ||
| "filteringSourceJoinVariable", | ||
| "semiJoinOutput", | ||
| values("sourceJoinVariable"), | ||
| aggregation( | ||
| singleGroupingSet("filteringSourceJoinVariable"), | ||
| ImmutableMap.of(), | ||
| ImmutableMap.of(), | ||
| Optional.empty(), | ||
| AggregationNode.Step.SINGLE, | ||
| aggregation( | ||
| singleGroupingSet("filteringSourceJoinVariable", "col1"), | ||
| ImmutableMap.of(), | ||
| ImmutableMap.of(), | ||
| Optional.empty(), | ||
| AggregationNode.Step.SINGLE, | ||
| values("filteringSourceJoinVariable", "col1"))))); | ||
| } | ||
|
|
||
| @Test | ||
| public void testNotTriggerOverDistinct() | ||
| { | ||
| tester().assertThat(new AddDistinctForSemiJoinBuild()) | ||
| .setSystemProperty(ADD_DISTINCT_BELOW_SEMI_JOIN_BUILD, "true") | ||
| .on(p -> | ||
| { | ||
| VariableReferenceExpression sourceJoinVariable = p.variable("sourceJoinVariable"); | ||
| VariableReferenceExpression filteringSourceJoinVariable = p.variable("filteringSourceJoinVariable"); | ||
| VariableReferenceExpression semiJoinOutput = p.variable("semiJoinOutput"); | ||
| return p.semiJoin( | ||
| sourceJoinVariable, | ||
| filteringSourceJoinVariable, | ||
| semiJoinOutput, | ||
| Optional.empty(), | ||
| Optional.empty(), | ||
| p.values(sourceJoinVariable), | ||
| p.aggregation((a) -> a | ||
| .singleGroupingSet(filteringSourceJoinVariable) | ||
| .step(AggregationNode.Step.SINGLE) | ||
| .source(p.values(filteringSourceJoinVariable)))); | ||
| }).doesNotFire(); | ||
| } | ||
|
|
||
| @Test | ||
| public void testNotTriggerOverDistinctUnderProject() | ||
| { | ||
| tester().assertThat(new AddDistinctForSemiJoinBuild()) | ||
| .setSystemProperty(ADD_DISTINCT_BELOW_SEMI_JOIN_BUILD, "true") | ||
| .on(p -> | ||
| { | ||
| VariableReferenceExpression sourceJoinVariable = p.variable("sourceJoinVariable"); | ||
| VariableReferenceExpression filteringSourceJoinVariable = p.variable("filteringSourceJoinVariable"); | ||
| VariableReferenceExpression semiJoinOutput = p.variable("semiJoinOutput"); | ||
| VariableReferenceExpression col1 = p.variable("col1"); | ||
| return p.semiJoin( | ||
| sourceJoinVariable, | ||
| filteringSourceJoinVariable, | ||
| semiJoinOutput, | ||
| Optional.empty(), | ||
| Optional.empty(), | ||
| p.values(sourceJoinVariable), | ||
| p.project( | ||
| assignment(filteringSourceJoinVariable, p.rowExpression("col1")), | ||
| p.aggregation((a) -> a | ||
| .singleGroupingSet(col1) | ||
| .step(AggregationNode.Step.SINGLE) | ||
| .source(p.values(col1))))); | ||
| }).doesNotFire(); | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This can be on by default?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd like to run it for some time before turning it on as default.