Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -76,26 +76,20 @@ public int resolvePosition(int currentRow, ArrayView matchedLabels, int searchSt
checkArgument(currentRow >= patternStart && currentRow < patternStart + matchedLabels.length(), "current row is out of bounds of the match");

int relativePosition;
int patternEndInclusive = running ? currentRow - patternStart : matchedLabels.length() - 1;
if (last) {
int start;
if (running) {
start = currentRow - patternStart;
}
else {
start = matchedLabels.length() - 1;
}
relativePosition = findLastAndBackwards(start, matchedLabels);
relativePosition = findLastAndBackwards(patternEndInclusive, matchedLabels);
}
else {
relativePosition = findFirstAndForward(matchedLabels);
relativePosition = findFirstAndForward(matchedLabels, patternEndInclusive);
}
return adjustPosition(relativePosition, patternStart, searchStart, searchEnd);
}

// LAST(A.price, 3): find the last occurrence of label "A" and go 3 occurrences backwards
private int findLastAndBackwards(int searchStart, ArrayView matchedLabels)
private int findLastAndBackwards(int patternEndInclusive, ArrayView matchedLabels)
{
int position = searchStart + 1;
int position = patternEndInclusive + 1;
int found = 0;
while (found <= logicalOffset && position > 0) {
position--;
Expand All @@ -110,11 +104,11 @@ private int findLastAndBackwards(int searchStart, ArrayView matchedLabels)
}

// FIRST(A.price, 3): find the first occurrence of label "A" and go 3 occurrences forward
private int findFirstAndForward(ArrayView matchedLabels)
private int findFirstAndForward(ArrayView matchedLabels, int patternEndInclusive)
{
int position = -1;
int found = 0;
while (found <= logicalOffset && position < matchedLabels.length() - 1) {
while (found <= logicalOffset && position < patternEndInclusive) {
position++;
if (labels.isEmpty() || labels.contains(matchedLabels.get(position))) {
found++;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1020,8 +1020,8 @@ public void testNavigationFunctions()

assertThat(assertions.query(format(query, "FIRST(value, 2)")))
.matches("VALUES " +
" (1, 30), " +
" (2, 30), " +
" (1, null), " +
" (2, null), " +
" (3, 30) ");

assertThat(assertions.query(format(query, "LAST(value, 10)")))
Expand Down
Loading