Skip to content

Commit aaa8f18

Browse files
committed
Restrict logical navigations to running match
1 parent 41f0e8e commit aaa8f18

File tree

2 files changed

+9
-15
lines changed

2 files changed

+9
-15
lines changed

core/trino-main/src/main/java/io/trino/operator/window/pattern/LogicalIndexNavigation.java

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -76,26 +76,20 @@ public int resolvePosition(int currentRow, ArrayView matchedLabels, int searchSt
7676
checkArgument(currentRow >= patternStart && currentRow < patternStart + matchedLabels.length(), "current row is out of bounds of the match");
7777

7878
int relativePosition;
79+
int patternEndInclusive = running ? currentRow - patternStart : matchedLabels.length() - 1;
7980
if (last) {
80-
int start;
81-
if (running) {
82-
start = currentRow - patternStart;
83-
}
84-
else {
85-
start = matchedLabels.length() - 1;
86-
}
87-
relativePosition = findLastAndBackwards(start, matchedLabels);
81+
relativePosition = findLastAndBackwards(patternEndInclusive, matchedLabels);
8882
}
8983
else {
90-
relativePosition = findFirstAndForward(matchedLabels);
84+
relativePosition = findFirstAndForward(patternEndInclusive, matchedLabels);
9185
}
9286
return adjustPosition(relativePosition, patternStart, searchStart, searchEnd);
9387
}
9488

9589
// LAST(A.price, 3): find the last occurrence of label "A" and go 3 occurrences backwards
96-
private int findLastAndBackwards(int searchStart, ArrayView matchedLabels)
90+
private int findLastAndBackwards(int patternEndInclusive, ArrayView matchedLabels)
9791
{
98-
int position = searchStart + 1;
92+
int position = patternEndInclusive + 1;
9993
int found = 0;
10094
while (found <= logicalOffset && position > 0) {
10195
position--;
@@ -110,11 +104,11 @@ private int findLastAndBackwards(int searchStart, ArrayView matchedLabels)
110104
}
111105

112106
// FIRST(A.price, 3): find the first occurrence of label "A" and go 3 occurrences forward
113-
private int findFirstAndForward(ArrayView matchedLabels)
107+
private int findFirstAndForward(int patternEndInclusive, ArrayView matchedLabels)
114108
{
115109
int position = -1;
116110
int found = 0;
117-
while (found <= logicalOffset && position < matchedLabels.length() - 1) {
111+
while (found <= logicalOffset && position < patternEndInclusive) {
118112
position++;
119113
if (labels.isEmpty() || labels.contains(matchedLabels.get(position))) {
120114
found++;

core/trino-main/src/test/java/io/trino/sql/query/TestRowPatternMatching.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1020,8 +1020,8 @@ public void testNavigationFunctions()
10201020

10211021
assertThat(assertions.query(format(query, "FIRST(value, 2)")))
10221022
.matches("VALUES " +
1023-
" (1, 30), " +
1024-
" (2, 30), " +
1023+
" (1, null), " +
1024+
" (2, null), " +
10251025
" (3, 30) ");
10261026

10271027
assertThat(assertions.query(format(query, "LAST(value, 10)")))

0 commit comments

Comments
 (0)