-
Notifications
You must be signed in to change notification settings - Fork 5.5k
Remove redundant sort columns #21371
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
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
55 changes: 55 additions & 0 deletions
55
.../main/java/com/facebook/presto/sql/planner/iterative/rule/RemoveRedundantSortColumns.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,55 @@ | ||
| /* | ||
| * 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.matching.Captures; | ||
| import com.facebook.presto.matching.Pattern; | ||
| import com.facebook.presto.spi.plan.LogicalProperties; | ||
| import com.facebook.presto.spi.plan.OrderingScheme; | ||
| import com.facebook.presto.sql.planner.iterative.GroupReference; | ||
| import com.facebook.presto.sql.planner.iterative.Rule; | ||
| import com.facebook.presto.sql.planner.plan.SortNode; | ||
|
|
||
| import static com.facebook.presto.sql.planner.iterative.rule.Util.pruneOrderingColumns; | ||
| import static com.facebook.presto.sql.planner.plan.Patterns.sort; | ||
|
|
||
| /** | ||
| * Removes sort columns from input if the source has a Key that refers to the ordering columns | ||
| */ | ||
| public class RemoveRedundantSortColumns | ||
| implements Rule<SortNode> | ||
| { | ||
| private static final Pattern<SortNode> PATTERN = sort().matching(p -> ((GroupReference) p.getSource()).getLogicalProperties().isPresent()); | ||
|
|
||
| @Override | ||
| public Pattern<SortNode> getPattern() | ||
| { | ||
| return PATTERN; | ||
| } | ||
|
|
||
| @Override | ||
| public Result apply(SortNode node, Captures captures, Context context) | ||
| { | ||
| OrderingScheme orderingScheme = node.getOrderingScheme(); | ||
|
|
||
| LogicalProperties sourceLogicalProperties = ((GroupReference) node.getSource()).getLogicalProperties().get(); | ||
| OrderingScheme newOrderingScheme = pruneOrderingColumns(orderingScheme, sourceLogicalProperties); | ||
|
|
||
| if (newOrderingScheme.equals(orderingScheme)) { | ||
| return Result.empty(); | ||
| } | ||
|
|
||
| return Result.ofPlanNode(new SortNode(node.getSourceLocation(), node.getId(), node.getStatsEquivalentPlanNode(), node.getSource(), newOrderingScheme, node.isPartial())); | ||
| } | ||
| } |
55 changes: 55 additions & 0 deletions
55
.../main/java/com/facebook/presto/sql/planner/iterative/rule/RemoveRedundantTopNColumns.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,55 @@ | ||
| /* | ||
| * 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.matching.Captures; | ||
| import com.facebook.presto.matching.Pattern; | ||
| import com.facebook.presto.spi.plan.LogicalProperties; | ||
| import com.facebook.presto.spi.plan.OrderingScheme; | ||
| import com.facebook.presto.spi.plan.TopNNode; | ||
| import com.facebook.presto.sql.planner.iterative.GroupReference; | ||
| import com.facebook.presto.sql.planner.iterative.Rule; | ||
|
|
||
| import static com.facebook.presto.sql.planner.iterative.rule.Util.pruneOrderingColumns; | ||
| import static com.facebook.presto.sql.planner.plan.Patterns.topN; | ||
|
|
||
| /** | ||
| * Removes TopN columns from input if the source has a Key that refers to the ordering columns | ||
| */ | ||
| public class RemoveRedundantTopNColumns | ||
| implements Rule<TopNNode> | ||
| { | ||
| private static final Pattern<TopNNode> PATTERN = topN().matching(p -> ((GroupReference) p.getSource()).getLogicalProperties().isPresent()); | ||
|
|
||
| @Override | ||
| public Pattern<TopNNode> getPattern() | ||
| { | ||
| return PATTERN; | ||
| } | ||
|
|
||
| @Override | ||
| public Result apply(TopNNode node, Captures captures, Context context) | ||
| { | ||
| OrderingScheme orderingScheme = node.getOrderingScheme(); | ||
|
|
||
| LogicalProperties sourceLogicalProperties = ((GroupReference) node.getSource()).getLogicalProperties().get(); | ||
| OrderingScheme newOrderingScheme = pruneOrderingColumns(orderingScheme, sourceLogicalProperties); | ||
|
|
||
| if (newOrderingScheme.equals(orderingScheme)) { | ||
| return Result.empty(); | ||
| } | ||
|
|
||
| return Result.ofPlanNode(new TopNNode(node.getSourceLocation(), node.getId(), node.getSource(), node.getCount(), newOrderingScheme, node.getStep())); | ||
| } | ||
| } | ||
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
124 changes: 124 additions & 0 deletions
124
.../java/com/facebook/presto/sql/planner/iterative/rule/TestRedundantSortColumnsRemoval.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,124 @@ | ||
| /* | ||
| * 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.sql.planner.assertions.PlanMatchPattern; | ||
| import com.facebook.presto.sql.planner.iterative.properties.LogicalPropertiesProviderImpl; | ||
| import com.facebook.presto.sql.planner.iterative.rule.test.BaseRuleTest; | ||
| import com.facebook.presto.sql.planner.iterative.rule.test.RuleTester; | ||
| import com.facebook.presto.sql.relational.FunctionResolution; | ||
| import com.google.common.collect.ImmutableList; | ||
| import com.google.common.collect.ImmutableMap; | ||
| import com.google.common.collect.ImmutableSet; | ||
| import org.testng.annotations.BeforeClass; | ||
| import org.testng.annotations.Test; | ||
|
|
||
| import static com.facebook.presto.sql.planner.assertions.PlanMatchPattern.anyTree; | ||
| import static com.facebook.presto.sql.planner.assertions.PlanMatchPattern.output; | ||
| import static com.facebook.presto.sql.planner.assertions.PlanMatchPattern.sort; | ||
| import static com.facebook.presto.sql.planner.assertions.PlanMatchPattern.tableScan; | ||
| import static com.facebook.presto.sql.planner.assertions.PlanMatchPattern.topN; | ||
| import static com.facebook.presto.sql.tree.SortItem.NullOrdering.LAST; | ||
| import static com.facebook.presto.sql.tree.SortItem.Ordering.ASCENDING; | ||
| import static com.facebook.presto.sql.tree.SortItem.Ordering.DESCENDING; | ||
| import static java.util.Collections.emptyList; | ||
|
|
||
| public class TestRedundantSortColumnsRemoval | ||
| extends BaseRuleTest | ||
| { | ||
| private LogicalPropertiesProviderImpl logicalPropertiesProvider; | ||
|
|
||
| @BeforeClass | ||
| public final void setUp() | ||
| { | ||
| tester = new RuleTester(emptyList(), ImmutableMap.of("exploit_constraints", Boolean.toString(true))); | ||
| logicalPropertiesProvider = new LogicalPropertiesProviderImpl(new FunctionResolution(tester.getMetadata().getFunctionAndTypeManager().getFunctionAndTypeResolver())); | ||
| } | ||
|
|
||
| @Test | ||
| public void testRemoveRedundantColumnsFromTopN() | ||
| { | ||
| // OrderBy prefix matches GroupBy columns clause exactly | ||
| tester().assertThat(ImmutableSet.of(new MergeLimitWithSort(), new RemoveRedundantTopNColumns()), logicalPropertiesProvider) | ||
| .on("SELECT orderkey, custkey, sum(totalprice), min(orderdate) FROM orders GROUP BY orderkey, custkey " + | ||
| "ORDER BY orderkey, custkey, sum(totalprice), min(orderdate) LIMIT 10") | ||
| .matches(topNMatchWith(ImmutableList.of(sort("ORDERKEY", ASCENDING, LAST), sort("CUSTKEY", ASCENDING, LAST)))); | ||
|
|
||
| // Flipped order matches too since the Grouping set remains the same | ||
| tester().assertThat(ImmutableSet.of(new MergeLimitWithSort(), new RemoveRedundantTopNColumns()), logicalPropertiesProvider) | ||
| .on("SELECT orderkey, custkey, sum(totalprice), min(orderdate) FROM orders GROUP BY orderkey, custkey " + | ||
| "ORDER BY custkey, orderkey, sum(totalprice), min(orderdate) LIMIT 10") | ||
| .matches(topNMatchWith(ImmutableList.of(sort("CUSTKEY", ASCENDING, LAST), sort("ORDERKEY", ASCENDING, LAST)))); | ||
|
|
||
| // No impact due to sort direction | ||
| tester().assertThat(ImmutableSet.of(new MergeLimitWithSort(), new RemoveRedundantTopNColumns()), logicalPropertiesProvider) | ||
| .on("SELECT orderkey, custkey, sum(totalprice), min(orderdate) FROM orders GROUP BY orderkey, custkey " + | ||
| "ORDER BY custkey DESC, orderkey ASC, sum(totalprice), min(orderdate) LIMIT 10") | ||
| .matches(topNMatchWith(ImmutableList.of(sort("CUSTKEY", DESCENDING, LAST), sort("ORDERKEY", ASCENDING, LAST)))); | ||
|
|
||
| // Negative test - No prefix matches the grouping set, so TopN columns are not pruned | ||
| tester().assertThat(ImmutableSet.of(new MergeLimitWithSort(), new RemoveRedundantTopNColumns()), logicalPropertiesProvider) | ||
| .on("SELECT orderkey, custkey, sum(totalprice), min(orderdate) FROM orders GROUP BY orderkey, custkey " + | ||
| "ORDER BY orderkey, sum(totalprice), custkey, min(orderdate) LIMIT 10") | ||
| .doesNotMatch(topNMatchWith(ImmutableList.of(sort("ORDERKEY", ASCENDING, LAST), sort("CUSTKEY", ASCENDING, LAST)))); | ||
| } | ||
|
|
||
| @Test | ||
| public void testRemoveRedundantColumnsFromSort() | ||
| { | ||
| // OrderBy prefix matches GroupBy columns clause exactly | ||
| tester().assertThat(ImmutableSet.of(new RemoveRedundantSortColumns()), logicalPropertiesProvider) | ||
| .on("SELECT orderkey, custkey, sum(totalprice), min(orderdate) FROM orders GROUP BY orderkey, custkey " + | ||
| "ORDER BY orderkey, custkey, sum(totalprice), min(orderdate)") | ||
| .matches(sortWith(ImmutableList.of(sort("ORDERKEY", ASCENDING, LAST), sort("CUSTKEY", ASCENDING, LAST)))); | ||
|
|
||
| // Flipped order matches too since the Grouping set remains the same | ||
| tester().assertThat(ImmutableSet.of(new RemoveRedundantSortColumns()), logicalPropertiesProvider) | ||
| .on("SELECT orderkey, custkey, sum(totalprice), min(orderdate) FROM orders GROUP BY orderkey, custkey " + | ||
| "ORDER BY custkey, orderkey, sum(totalprice), min(orderdate)") | ||
| .matches(sortWith(ImmutableList.of(sort("CUSTKEY", ASCENDING, LAST), sort("ORDERKEY", ASCENDING, LAST)))); | ||
|
|
||
| // No impact due to sort direction | ||
| tester().assertThat(ImmutableSet.of(new RemoveRedundantSortColumns()), logicalPropertiesProvider) | ||
| .on("SELECT orderkey, custkey, sum(totalprice), min(orderdate) FROM orders GROUP BY orderkey, custkey " + | ||
| "ORDER BY custkey DESC, orderkey ASC, sum(totalprice), min(orderdate)") | ||
aaneja marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| .matches(sortWith(ImmutableList.of(sort("CUSTKEY", DESCENDING, LAST), sort("ORDERKEY", ASCENDING, LAST)))); | ||
|
|
||
| // Negative test - No prefix matches the grouping set, so TopN columns are not pruned | ||
| tester().assertThat(ImmutableSet.of(new RemoveRedundantSortColumns()), logicalPropertiesProvider) | ||
| .on("SELECT orderkey, custkey, sum(totalprice), min(orderdate) FROM orders GROUP BY orderkey, custkey " + | ||
| "ORDER BY orderkey, sum(totalprice), custkey, min(orderdate)") | ||
| .doesNotMatch(sortWith(ImmutableList.of(sort("ORDERKEY", ASCENDING, LAST), sort("CUSTKEY", ASCENDING, LAST)))); | ||
| } | ||
|
|
||
| private static PlanMatchPattern topNMatchWith(ImmutableList<PlanMatchPattern.Ordering> orderBy) | ||
| { | ||
| return output( | ||
| topN(10, orderBy, | ||
aaneja marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| anyTree( | ||
| tableScan("orders", ImmutableMap.of( | ||
| "ORDERKEY", "orderkey", | ||
| "CUSTKEY", "custkey"))))); | ||
| } | ||
|
|
||
| private static PlanMatchPattern sortWith(ImmutableList<PlanMatchPattern.Ordering> orderBy) | ||
| { | ||
| return output( | ||
| sort(orderBy, | ||
| anyTree( | ||
| tableScan("orders", ImmutableMap.of( | ||
| "ORDERKEY", "orderkey", | ||
| "CUSTKEY", "custkey"))))); | ||
| } | ||
| } | ||
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
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.
Uh oh!
There was an error while loading. Please reload this page.