-
Notifications
You must be signed in to change notification settings - Fork 5.5k
feat(optimizer): Native TopNRank optimization #24138
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
Open
aditi-pandit
wants to merge
2
commits into
master
Choose a base branch
from
topn
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+359
−40
Open
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -41,11 +41,14 @@ | |
| import com.facebook.presto.sql.relational.RowExpressionDeterminismEvaluator; | ||
| import com.facebook.presto.sql.relational.RowExpressionDomainTranslator; | ||
| import com.google.common.collect.ImmutableList; | ||
| import com.google.common.collect.Iterables; | ||
|
|
||
| import java.util.Map; | ||
| import java.util.Optional; | ||
| import java.util.OptionalInt; | ||
|
|
||
| import static com.facebook.presto.SystemSessionProperties.isNativeExecutionEnabled; | ||
| import static com.facebook.presto.SystemSessionProperties.isOptimizeTopNRank; | ||
| import static com.facebook.presto.SystemSessionProperties.isOptimizeTopNRowNumber; | ||
| import static com.facebook.presto.common.predicate.Marker.Bound.BELOW; | ||
| import static com.facebook.presto.common.type.BigintType.BIGINT; | ||
|
|
@@ -134,6 +137,12 @@ public PlanNode visitWindow(WindowNode node, RewriteContext<Void> context) | |
| return replaceChildren(node, ImmutableList.of(rewrittenSource)); | ||
| } | ||
|
|
||
| private boolean canReplaceWithTopNRowNumber(WindowNode node) | ||
| { | ||
| return (canOptimizeRowNumberFunction(node, metadata.getFunctionAndTypeManager()) && isOptimizeTopNRowNumber(session)) || | ||
| (canOptimizeRankFunction(node, metadata.getFunctionAndTypeManager()) && isOptimizeTopNRank(session) && isNativeExecutionEnabled(session)); | ||
| } | ||
|
|
||
| @Override | ||
| public PlanNode visitLimit(LimitNode node, RewriteContext<Void> context) | ||
| { | ||
|
|
@@ -152,16 +161,18 @@ public PlanNode visitLimit(LimitNode node, RewriteContext<Void> context) | |
| planChanged = true; | ||
| source = rowNumberNode; | ||
| } | ||
| else if (source instanceof WindowNode && canOptimizeWindowFunction((WindowNode) source, metadata.getFunctionAndTypeManager()) && isOptimizeTopNRowNumber(session)) { | ||
| else if (source instanceof WindowNode) { | ||
| WindowNode windowNode = (WindowNode) source; | ||
| // verify that unordered row_number window functions are replaced by RowNumberNode | ||
| verify(windowNode.getOrderingScheme().isPresent()); | ||
| TopNRowNumberNode topNRowNumberNode = convertToTopNRowNumber(windowNode, limit); | ||
| if (windowNode.getPartitionBy().isEmpty()) { | ||
| return topNRowNumberNode; | ||
| if (canReplaceWithTopNRowNumber(windowNode)) { | ||
| TopNRowNumberNode topNRowNumberNode = convertToTopNRowNumber(windowNode, limit); | ||
| // Limit can be entirely skipped for row_number without partitioning. | ||
| if (windowNode.getPartitionBy().isEmpty() && | ||
| canOptimizeRowNumberFunction(windowNode, metadata.getFunctionAndTypeManager())) { | ||
| return topNRowNumberNode; | ||
| } | ||
| planChanged = true; | ||
| source = topNRowNumberNode; | ||
| } | ||
| planChanged = true; | ||
| source = topNRowNumberNode; | ||
| } | ||
| return replaceChildren(node, ImmutableList.of(source)); | ||
| } | ||
|
|
@@ -183,15 +194,17 @@ public PlanNode visitFilter(FilterNode node, RewriteContext<Void> context) | |
| return rewriteFilterSource(node, source, rowNumberVariable, upperBound.getAsInt()); | ||
| } | ||
| } | ||
| else if (source instanceof WindowNode && canOptimizeWindowFunction((WindowNode) source, metadata.getFunctionAndTypeManager()) && isOptimizeTopNRowNumber(session)) { | ||
| else if (source instanceof WindowNode) { | ||
| WindowNode windowNode = (WindowNode) source; | ||
| VariableReferenceExpression rowNumberVariable = getOnlyElement(windowNode.getCreatedVariable()); | ||
| OptionalInt upperBound = extractUpperBound(tupleDomain, rowNumberVariable); | ||
|
|
||
| if (upperBound.isPresent()) { | ||
| source = convertToTopNRowNumber(windowNode, upperBound.getAsInt()); | ||
| planChanged = true; | ||
| return rewriteFilterSource(node, source, rowNumberVariable, upperBound.getAsInt()); | ||
| if (canReplaceWithTopNRowNumber(windowNode)) { | ||
| VariableReferenceExpression rowNumberVariable = getOnlyElement(windowNode.getCreatedVariable()); | ||
| OptionalInt upperBound = extractUpperBound(tupleDomain, rowNumberVariable); | ||
|
|
||
| if (upperBound.isPresent()) { | ||
| source = convertToTopNRowNumber(windowNode, upperBound.getAsInt()); | ||
|
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. nit: It seems that we can call
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. Same comment as above. |
||
| planChanged = true; | ||
| return rewriteFilterSource(node, source, rowNumberVariable, upperBound.getAsInt()); | ||
| } | ||
| } | ||
| } | ||
| return replaceChildren(node, ImmutableList.of(source)); | ||
|
|
@@ -275,11 +288,30 @@ private static RowNumberNode mergeLimit(RowNumberNode node, int newRowCountPerPa | |
|
|
||
| private TopNRowNumberNode convertToTopNRowNumber(WindowNode windowNode, int limit) | ||
| { | ||
| String windowFunction = Iterables.getOnlyElement(windowNode.getWindowFunctions().values()).getFunctionCall().getFunctionHandle().getName(); | ||
| String[] parts = windowFunction.split("\\."); | ||
| String windowFunctionName = parts[parts.length - 1]; | ||
| TopNRowNumberNode.RankingFunction rankingFunction; | ||
| switch (windowFunctionName) { | ||
| case "row_number": | ||
| rankingFunction = TopNRowNumberNode.RankingFunction.ROW_NUMBER; | ||
| break; | ||
| case "rank": | ||
| rankingFunction = TopNRowNumberNode.RankingFunction.RANK; | ||
| break; | ||
| case "dense_rank": | ||
| rankingFunction = TopNRowNumberNode.RankingFunction.DENSE_RANK; | ||
| break; | ||
| default: | ||
| throw new IllegalArgumentException("Unsupported window function for TopNRowNumberNode: " + windowFunctionName); | ||
| } | ||
|
|
||
| return new TopNRowNumberNode( | ||
| windowNode.getSourceLocation(), | ||
| idAllocator.getNextId(), | ||
| windowNode.getSource(), | ||
| windowNode.getSpecification(), | ||
| rankingFunction, | ||
| getOnlyElement(windowNode.getCreatedVariable()), | ||
| limit, | ||
| false, | ||
|
|
@@ -288,22 +320,37 @@ private TopNRowNumberNode convertToTopNRowNumber(WindowNode windowNode, int limi | |
|
|
||
| private static boolean canReplaceWithRowNumber(WindowNode node, FunctionAndTypeManager functionAndTypeManager) | ||
| { | ||
| return canOptimizeWindowFunction(node, functionAndTypeManager) && !node.getOrderingScheme().isPresent(); | ||
| return canOptimizeRowNumberFunction(node, functionAndTypeManager) && !node.getOrderingScheme().isPresent(); | ||
| } | ||
|
|
||
| private static boolean canOptimizeRowNumberFunction(WindowNode node, FunctionAndTypeManager functionAndTypeManager) | ||
| { | ||
| if (node.getWindowFunctions().size() != 1) { | ||
| return false; | ||
| } | ||
| return isRowNumberMetadata(functionAndTypeManager, functionAndTypeManager.getFunctionMetadata(getOnlyElement(node.getWindowFunctions().values()).getFunctionHandle())); | ||
| } | ||
|
|
||
| private static boolean canOptimizeWindowFunction(WindowNode node, FunctionAndTypeManager functionAndTypeManager) | ||
| private static boolean canOptimizeRankFunction(WindowNode node, FunctionAndTypeManager functionAndTypeManager) | ||
| { | ||
| if (node.getWindowFunctions().size() != 1) { | ||
| return false; | ||
| } | ||
| VariableReferenceExpression rowNumberVariable = getOnlyElement(node.getWindowFunctions().keySet()); | ||
| return isRowNumberMetadata(functionAndTypeManager, functionAndTypeManager.getFunctionMetadata(node.getWindowFunctions().get(rowNumberVariable).getFunctionHandle())); | ||
| return isRankMetadata(functionAndTypeManager, functionAndTypeManager.getFunctionMetadata(getOnlyElement(node.getWindowFunctions().values()).getFunctionHandle())); | ||
| } | ||
|
|
||
| private static boolean isRowNumberMetadata(FunctionAndTypeManager functionAndTypeManager, FunctionMetadata functionMetadata) | ||
| { | ||
| FunctionHandle rowNumberFunction = functionAndTypeManager.lookupFunction("row_number", ImmutableList.of()); | ||
| return functionMetadata.equals(functionAndTypeManager.getFunctionMetadata(rowNumberFunction)); | ||
| } | ||
|
|
||
| private static boolean isRankMetadata(FunctionAndTypeManager functionAndTypeManager, FunctionMetadata functionMetadata) | ||
| { | ||
| FunctionHandle rankFunction = functionAndTypeManager.lookupFunction("rank", ImmutableList.of()); | ||
| FunctionHandle denseRankFunction = functionAndTypeManager.lookupFunction("dense_rank", ImmutableList.of()); | ||
| return functionMetadata.equals(functionAndTypeManager.getFunctionMetadata(rankFunction)) || | ||
| functionMetadata.equals(functionAndTypeManager.getFunctionMetadata(denseRankFunction)); | ||
| } | ||
| } | ||
| } | ||
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
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.
Should we call
convertToTopNRankhere?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 was a good catch... I intended to remove the original convertToTopNRowNumber and change convertToTopNRank function to convertToTopNRowNumber to match the naming everywhere.
Have fixed that.