提交 2f0271fb authored 作者: Evgenij Ryazanov's avatar Evgenij Ryazanov

Fix CURRENT ROW handling in window functions

上级 5ea4c890
......@@ -352,7 +352,8 @@ public final class WindowFrame {
int currentRow, boolean reverse) {
int startIndex = getIndex(session, orderedRows, sortOrder, currentRow, starting, false);
int endIndex = following != null ? getIndex(session, orderedRows, sortOrder, currentRow, following, true)
: currentRow;
: units == WindowFrameUnits.ROWS ? currentRow
: toGroupEnd(orderedRows, sortOrder, currentRow, orderedRows.size() - 1);
if (endIndex < startIndex) {
return Collections.emptyIterator();
}
......@@ -442,8 +443,19 @@ public final class WindowFrame {
}
break;
case CURRENT_ROW:
switch (units) {
case ROWS:
index = currentRow;
break;
case GROUPS:
case RANGE:
index = forFollowing ? toGroupEnd(orderedRows, sortOrder, currentRow, last)
: toGroupStart(orderedRows, sortOrder, currentRow, 0);
break;
default:
throw DbException.getUnsupportedException("units=" + units);
}
break;
case FOLLOWING:
switch (units) {
case ROWS: {
......
......@@ -449,6 +449,26 @@ SELECT ID, VALUE, ARRAY_AGG(ID) OVER (ORDER BY VALUE RANGE BETWEEN 2 PRECEDING A
> 8 9 (4, 5, 6)
> rows: 8
SELECT ID, VALUE,
ARRAY_AGG(ID) OVER (ORDER BY VALUE ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) CP,
ARRAY_AGG(ID) OVER (ORDER BY VALUE ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING) CF,
ARRAY_AGG(ID) OVER (ORDER BY VALUE RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) RP,
ARRAY_AGG(ID) OVER (ORDER BY VALUE RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING) RF,
ARRAY_AGG(ID) OVER (ORDER BY VALUE GROUPS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) GP,
ARRAY_AGG(ID) OVER (ORDER BY VALUE GROUPS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING) GF
FROM TEST;
> ID VALUE CP CF RP RF GP GF
> -- ----- ------------------------ ------------------------ ------------------------ ------------------------ ------------------------ ------------------------
> 1 1 (1) (1, 2, 3, 4, 5, 6, 7, 8) (1, 2) (1, 2, 3, 4, 5, 6, 7, 8) (1, 2) (1, 2, 3, 4, 5, 6, 7, 8)
> 2 1 (1, 2) (2, 3, 4, 5, 6, 7, 8) (1, 2) (1, 2, 3, 4, 5, 6, 7, 8) (1, 2) (1, 2, 3, 4, 5, 6, 7, 8)
> 3 5 (1, 2, 3) (3, 4, 5, 6, 7, 8) (1, 2, 3) (3, 4, 5, 6, 7, 8) (1, 2, 3) (3, 4, 5, 6, 7, 8)
> 4 8 (1, 2, 3, 4) (4, 5, 6, 7, 8) (1, 2, 3, 4, 5, 6) (4, 5, 6, 7, 8) (1, 2, 3, 4, 5, 6) (4, 5, 6, 7, 8)
> 5 8 (1, 2, 3, 4, 5) (5, 6, 7, 8) (1, 2, 3, 4, 5, 6) (4, 5, 6, 7, 8) (1, 2, 3, 4, 5, 6) (4, 5, 6, 7, 8)
> 6 8 (1, 2, 3, 4, 5, 6) (6, 7, 8) (1, 2, 3, 4, 5, 6) (4, 5, 6, 7, 8) (1, 2, 3, 4, 5, 6) (4, 5, 6, 7, 8)
> 7 9 (1, 2, 3, 4, 5, 6, 7) (7, 8) (1, 2, 3, 4, 5, 6, 7, 8) (7, 8) (1, 2, 3, 4, 5, 6, 7, 8) (7, 8)
> 8 9 (1, 2, 3, 4, 5, 6, 7, 8) (8) (1, 2, 3, 4, 5, 6, 7, 8) (7, 8) (1, 2, 3, 4, 5, 6, 7, 8) (7, 8)
> rows: 8
SELECT *, ARRAY_AGG(ID) OVER (ORDER BY ID RANGE BETWEEN CURRENT ROW AND 1 PRECEDING) FROM TEST;
> exception SYNTAX_ERROR_1
......@@ -523,5 +543,25 @@ SELECT ID, VALUE, ARRAY_AGG(ID) OVER (ORDER BY VALUE RANGE BETWEEN 1 FOLLOWING A
> 8 4 null
> rows: 8
SELECT ID, VALUE,
ARRAY_AGG(ID) OVER (ORDER BY VALUE ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) CP,
ARRAY_AGG(ID) OVER (ORDER BY VALUE ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING) CF,
ARRAY_AGG(ID) OVER (ORDER BY VALUE RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) RP,
ARRAY_AGG(ID) OVER (ORDER BY VALUE RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING) RF,
ARRAY_AGG(ID) OVER (ORDER BY VALUE GROUPS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) GP,
ARRAY_AGG(ID) OVER (ORDER BY VALUE GROUPS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING) GF
FROM TEST;
> ID VALUE CP CF RP RF GP GF
> -- ----- ------------------------ ------------------------ ------------------------ ------------------------ ------------------------ ------------------------
> 1 1 (1) (1, 2, 3, 4, 5, 6, 7, 8) (1, 2) (1, 2, 3, 4, 5, 6, 7, 8) (1, 2) (1, 2, 3, 4, 5, 6, 7, 8)
> 2 1 (1, 2) (2, 3, 4, 5, 6, 7, 8) (1, 2) (1, 2, 3, 4, 5, 6, 7, 8) (1, 2) (1, 2, 3, 4, 5, 6, 7, 8)
> 3 2 (1, 2, 3) (3, 4, 5, 6, 7, 8) (1, 2, 3, 4) (3, 4, 5, 6, 7, 8) (1, 2, 3, 4) (3, 4, 5, 6, 7, 8)
> 4 2 (1, 2, 3, 4) (4, 5, 6, 7, 8) (1, 2, 3, 4) (3, 4, 5, 6, 7, 8) (1, 2, 3, 4) (3, 4, 5, 6, 7, 8)
> 5 3 (1, 2, 3, 4, 5) (5, 6, 7, 8) (1, 2, 3, 4, 5, 6) (5, 6, 7, 8) (1, 2, 3, 4, 5, 6) (5, 6, 7, 8)
> 6 3 (1, 2, 3, 4, 5, 6) (6, 7, 8) (1, 2, 3, 4, 5, 6) (5, 6, 7, 8) (1, 2, 3, 4, 5, 6) (5, 6, 7, 8)
> 7 4 (1, 2, 3, 4, 5, 6, 7) (7, 8) (1, 2, 3, 4, 5, 6, 7, 8) (7, 8) (1, 2, 3, 4, 5, 6, 7, 8) (7, 8)
> 8 4 (1, 2, 3, 4, 5, 6, 7, 8) (8) (1, 2, 3, 4, 5, 6, 7, 8) (7, 8) (1, 2, 3, 4, 5, 6, 7, 8) (7, 8)
> rows: 8
DROP TABLE TEST;
> ok
......@@ -136,25 +136,26 @@ SELECT *,
> rows (ordered): 13
SELECT ID, CATEGORY,
NTH_VALUE(CATEGORY, 2) OVER (ORDER BY CATEGORY RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) C,
NTH_VALUE(CATEGORY, 2) OVER (ORDER BY CATEGORY RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW EXCLUDE CURRENT ROW)
NTH_VALUE(CATEGORY, 2) OVER (ORDER BY CATEGORY ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) C,
NTH_VALUE(CATEGORY, 2) OVER (ORDER BY CATEGORY ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW EXCLUDE CURRENT ROW)
FROM TEST FETCH FIRST 3 ROWS ONLY;
> ID CATEGORY C NTH_VALUE(CATEGORY, 2) OVER (ORDER BY CATEGORY RANGE UNBOUNDED PRECEDING EXCLUDE CURRENT ROW)
> -- -------- ---- ---------------------------------------------------------------------------------------------
> ID CATEGORY C NTH_VALUE(CATEGORY, 2) OVER (ORDER BY CATEGORY ROWS UNBOUNDED PRECEDING EXCLUDE CURRENT ROW)
> -- -------- ---- --------------------------------------------------------------------------------------------
> 1 1 null null
> 2 1 1 null
> 3 1 1 1
> rows: 3
SELECT ID, CATEGORY,
NTH_VALUE(CATEGORY, 2) FROM LAST OVER (ORDER BY CATEGORY RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING) C,
NTH_VALUE(CATEGORY, 2) FROM LAST OVER (ORDER BY CATEGORY RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING) C2,
NTH_VALUE(CATEGORY, 3) FROM LAST OVER (ORDER BY CATEGORY RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING) C3,
NTH_VALUE(CATEGORY, 2) FROM LAST OVER (ORDER BY CATEGORY RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING EXCLUDE CURRENT ROW)
FROM TEST OFFSET 10 ROWS;
> ID CATEGORY C NTH_VALUE(CATEGORY, 2) FROM LAST OVER (ORDER BY CATEGORY RANGE BETWEEN CURRENT_ROW AND UNBOUNDED FOLLOWING EXCLUDE CURRENT ROW)
> -- -------- ---- -------------------------------------------------------------------------------------------------------------------------------
> 11 3 4 4
> 12 4 4 null
> 13 4 null null
> ID CATEGORY C2 C3 NTH_VALUE(CATEGORY, 2) FROM LAST OVER (ORDER BY CATEGORY RANGE BETWEEN CURRENT_ROW AND UNBOUNDED FOLLOWING EXCLUDE CURRENT ROW)
> -- -------- -- ---- -------------------------------------------------------------------------------------------------------------------------------
> 11 3 4 3 4
> 12 4 4 null null
> 13 4 4 null null
> rows: 3
SELECT ID, CATEGORY,
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论