forked from prestodb/presto
-
Notifications
You must be signed in to change notification settings - Fork 21
Implement RANGE bounded window frame #561
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
anusudarsan
wants to merge
2
commits into
sprint-57
Choose a base branch
from
range-bounded
base: sprint-57
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.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,6 +20,7 @@ | |
import com.facebook.presto.sql.tree.FrameBound; | ||
import com.google.common.collect.ImmutableList; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import static com.facebook.presto.spi.StandardErrorCode.INVALID_WINDOW_FRAME; | ||
|
@@ -28,6 +29,7 @@ | |
import static com.facebook.presto.sql.tree.FrameBound.Type.UNBOUNDED_FOLLOWING; | ||
import static com.facebook.presto.sql.tree.FrameBound.Type.UNBOUNDED_PRECEDING; | ||
import static com.facebook.presto.sql.tree.WindowFrame.Type.RANGE; | ||
import static com.facebook.presto.sql.tree.WindowFrame.Type.ROWS; | ||
import static com.facebook.presto.util.Failures.checkCondition; | ||
import static com.google.common.base.Preconditions.checkState; | ||
import static java.lang.Math.toIntExact; | ||
|
@@ -46,6 +48,8 @@ public final class WindowPartition | |
private int peerGroupEnd; | ||
|
||
private int currentPosition; | ||
private List<Integer> peerGroupStartIndices = new ArrayList<>(); | ||
private List<Integer> peerGroupEndIndices = new ArrayList<>(); | ||
|
||
public WindowPartition(PagesIndex pagesIndex, | ||
int partitionStart, | ||
|
@@ -142,6 +146,8 @@ private void updatePeerGroup() | |
while ((peerGroupEnd < partitionEnd) && pagesIndex.positionEqualsPosition(peerGroupHashStrategy, peerGroupStart, peerGroupEnd)) { | ||
peerGroupEnd++; | ||
} | ||
peerGroupStartIndices.add(peerGroupStart); | ||
peerGroupEndIndices.add(peerGroupEnd); | ||
} | ||
|
||
private Range getFrameRange(FrameInfo frameInfo) | ||
|
@@ -150,7 +156,11 @@ private Range getFrameRange(FrameInfo frameInfo) | |
int endPosition = partitionEnd - partitionStart - 1; | ||
|
||
// handle empty frame | ||
if (emptyFrame(frameInfo, rowPosition, endPosition)) { | ||
if (frameInfo.getType() == ROWS && emptyFrame(frameInfo, rowPosition, endPosition - rowPosition)) { | ||
return new Range(-1, -1); | ||
} | ||
|
||
if (frameInfo.getType() == RANGE && emptyFrame(frameInfo, peerGroupStart, endPosition - (peerGroupEnd - 1))) { | ||
return new Range(-1, -1); | ||
} | ||
|
||
|
@@ -161,6 +171,12 @@ private Range getFrameRange(FrameInfo frameInfo) | |
if (frameInfo.getStartType() == UNBOUNDED_PRECEDING) { | ||
frameStart = 0; | ||
} | ||
else if (frameInfo.getType() == RANGE && frameInfo.getStartType() == PRECEDING) { | ||
frameStart = precedingStartRange(getStartValue(frameInfo)); | ||
} | ||
else if (frameInfo.getType() == RANGE && frameInfo.getStartType() == FOLLOWING) { | ||
frameStart = followingRange(rowPosition, endPosition, getStartValue(frameInfo)); | ||
} | ||
else if (frameInfo.getStartType() == PRECEDING) { | ||
frameStart = preceding(rowPosition, getStartValue(frameInfo)); | ||
} | ||
|
@@ -178,6 +194,12 @@ else if (frameInfo.getType() == RANGE) { | |
if (frameInfo.getEndType() == UNBOUNDED_FOLLOWING) { | ||
frameEnd = endPosition; | ||
} | ||
else if (frameInfo.getType() == RANGE && frameInfo.getEndType() == PRECEDING) { | ||
frameEnd = precedingEndRange(getEndValue(frameInfo)); | ||
} | ||
else if (frameInfo.getType() == RANGE && frameInfo.getEndType() == FOLLOWING) { | ||
frameEnd = followingRange(peerGroupEnd, endPosition + 1, getEndValue(frameInfo)) - 1; | ||
} | ||
else if (frameInfo.getEndType() == PRECEDING) { | ||
frameEnd = preceding(rowPosition, getEndValue(frameInfo)); | ||
} | ||
|
@@ -194,19 +216,62 @@ else if (frameInfo.getType() == RANGE) { | |
return new Range(frameStart, frameEnd); | ||
} | ||
|
||
private boolean emptyFrame(FrameInfo frameInfo, int rowPosition, int endPosition) | ||
private int precedingEndRange(long endValue) | ||
{ | ||
int peerGroupEndIndex = peerGroupEndIndices.indexOf(peerGroupEnd); | ||
if (peerGroupEndIndex < endValue) { | ||
return peerGroupEnd - 1; | ||
} | ||
return peerGroupEndIndices.get(toIntExact(peerGroupEndIndex - endValue)) - 1; | ||
} | ||
|
||
private int precedingStartRange(long startValue) | ||
{ | ||
int peerGroupStartIndex = peerGroupStartIndices.indexOf(peerGroupStart); | ||
if (peerGroupStartIndex < startValue) { | ||
return 0; | ||
} | ||
return peerGroupStartIndices.get(toIntExact(peerGroupStartIndex - startValue)); | ||
} | ||
|
||
private int followingRange(int followingPeerGroupStart, int endPosition, long value) | ||
{ | ||
if (value == 0) { | ||
return followingPeerGroupStart; | ||
} | ||
// TODO: Optimize this to *not* look for peers often, probably have pageIndex keep the peer groups | ||
int followingPeerGroupEnd = 0; | ||
int currentValue = 0; | ||
while (currentValue < value) { | ||
boolean peerFound = false; | ||
followingPeerGroupEnd = followingPeerGroupStart + 1; | ||
while ((followingPeerGroupEnd < partitionEnd) && pagesIndex.positionEqualsPosition(peerGroupHashStrategy, followingPeerGroupStart, followingPeerGroupEnd)) { | ||
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. What if there are no peers, just each row is a distinct value within the partition? |
||
followingPeerGroupEnd++; | ||
peerFound = true; | ||
} | ||
if (followingPeerGroupEnd >= partitionEnd) { | ||
return endPosition; | ||
} | ||
|
||
if (!peerFound) { | ||
currentValue++; | ||
} | ||
followingPeerGroupStart++; | ||
} | ||
return followingPeerGroupEnd; | ||
} | ||
|
||
private boolean emptyFrame(FrameInfo frameInfo, int rowPosition, int position) | ||
{ | ||
FrameBound.Type startType = frameInfo.getStartType(); | ||
FrameBound.Type endType = frameInfo.getEndType(); | ||
|
||
int positions = endPosition - rowPosition; | ||
|
||
if ((startType == UNBOUNDED_PRECEDING) && (endType == PRECEDING)) { | ||
return getEndValue(frameInfo) > rowPosition; | ||
} | ||
|
||
if ((startType == FOLLOWING) && (endType == UNBOUNDED_FOLLOWING)) { | ||
return getStartValue(frameInfo) > positions; | ||
return getStartValue(frameInfo) > position; | ||
} | ||
|
||
if (startType != endType) { | ||
|
@@ -225,7 +290,7 @@ private boolean emptyFrame(FrameInfo frameInfo, int rowPosition, int endPosition | |
return (start < end) || ((start > rowPosition) && (end > rowPosition)); | ||
} | ||
|
||
return (start > end) || ((start > positions) && (end > positions)); | ||
return (start > end) || ((start > position) && (end > position)); | ||
} | ||
|
||
private static int preceding(int rowPosition, long value) | ||
|
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.
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.
I'd use
Map<Integer, Integer>
(peer group -> position) instead ofArrayList<Integer>
, to avoid slowness when there are many tiny groups in a large partition.Imagine billions of rows with ~1 row / peer group.
Then
indexOf
will be O(billions). But if you use a Map, it's gonna be O(log(billions)).