-
Notifications
You must be signed in to change notification settings - Fork 43
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
perf: Utilize ORDER BY LIMIT over ROW_NUMBER where possible #1077
Merged
Merged
Changes from 1 commit
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
0c13884
perf: Utilize ORDER BY LIMIT over ROW_NUMBER where possible
TrevorBergeron 21d8361
more carefully optimize head paths
TrevorBergeron f9eaa5a
Merge remote-tracking branch 'github/main' into slice_pullup
TrevorBergeron fa4e95c
add further slice utils
TrevorBergeron 5e809b1
tighten up fast head check
TrevorBergeron 634936a
fix slice utils
TrevorBergeron b3d92f5
Merge remote-tracking branch 'github/main' into slice_pullup
TrevorBergeron 476b229
cleanup issues
TrevorBergeron 1a83e33
add unit tests for slice rewrite
TrevorBergeron cac849f
Merge remote-tracking branch 'github/main' into slice_pullup
TrevorBergeron 6c580a5
fix limit pullup negative head
TrevorBergeron 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 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
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 |
---|---|---|
|
@@ -385,6 +385,44 @@ def common_selection_root( | |
return None | ||
|
||
|
||
def pullup_limit_from_slice( | ||
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. Are we able to have a test to verify this process? |
||
root: nodes.BigFrameNode, | ||
) -> Tuple[nodes.BigFrameNode, Optional[int]]: | ||
""" | ||
This is a BQ-sql specific optimization that can be helpful as ORDER BY LIMIT is more efficient than ROW_NUMBER() + WHERE. | ||
""" | ||
if isinstance(root, nodes.SliceNode): | ||
new_child, limit = pullup_limit_from_slice(root.child) | ||
# head case | ||
if ( | ||
(not root.start) | ||
and ((root.stop is not None) and root.stop > 0) | ||
and (root.step == 1) | ||
): | ||
limit = root.stop | ||
new_root, prior_limit = pullup_limit_from_slice(root.child) | ||
if prior_limit is not None and prior_limit < limit: | ||
limit = prior_limit | ||
return new_root, limit | ||
# tail case | ||
if ( | ||
(root.start in [None, -1]) | ||
and ((root.stop is not None) and root.stop < 0) | ||
and (root.step == -1) | ||
): | ||
limit = -root.stop | ||
new_root, prior_limit = pullup_limit_from_slice(root.child) | ||
if prior_limit is not None and prior_limit < limit: | ||
limit = prior_limit | ||
return nodes.ReversedNode(new_root), limit | ||
elif isinstance(root, nodes.UnaryNode) and root.row_preserving: | ||
new_child, limit = pullup_limit_from_slice(root.child) | ||
if limit is not None: | ||
return root.transform_children(lambda _: new_child), limit | ||
# Many ops don't support pulling up slice, like filter, agg, join, etc. | ||
return root, None | ||
|
||
|
||
def replace_slice_ops(root: nodes.BigFrameNode) -> nodes.BigFrameNode: | ||
# TODO: we want to pull up some slices into limit op if near root. | ||
if isinstance(root, nodes.SliceNode): | ||
|
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
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 raise a
TypeError
here instead of an assertion error?I don't know how well our public APIs are validating the type of parameters. Not everyone is going to use a type checker.
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.
Yeah, in general, probably best to assume that wrong types can sometimes reach even the lower levels of the code base. In particular want to be safe about what we are putting into the sql strings. Switched to TypeError here.