提交 fca685a7 authored 作者: Evgenij Ryazanov's avatar Evgenij Ryazanov

Fix GROUPS BETWEEN with same direction

上级 e5d78948
......@@ -269,8 +269,8 @@ public final class WindowFrame {
*/
public Iterator<Value[]> iterator(Session session, ArrayList<Value[]> orderedRows, SortOrder sortOrder,
int currentRow, boolean reverse) {
int startIndex = getIndex(session, orderedRows, sortOrder, currentRow, starting);
int endIndex = following != null ? getIndex(session, orderedRows, sortOrder, currentRow, following)
int startIndex = getIndex(session, orderedRows, sortOrder, currentRow, starting, false);
int endIndex = following != null ? getIndex(session, orderedRows, sortOrder, currentRow, following, true)
: currentRow;
if (endIndex < startIndex) {
throw DbException.get(ErrorCode.SYNTAX_ERROR_1, getSQL());
......@@ -293,13 +293,13 @@ public final class WindowFrame {
}
private int getIndex(Session session, ArrayList<Value[]> orderedRows, SortOrder sortOrder, int currentRow,
WindowFrameBound bound) {
WindowFrameBound bound, boolean forFollowing) {
int size = orderedRows.size();
int last = size - 1;
int index;
switch (bound.getType()) {
case UNBOUNDED_PRECEDING:
index = 0;
index = -1;
break;
case PRECEDING:
switch (units) {
......@@ -310,6 +310,7 @@ public final class WindowFrame {
}
case GROUPS: {
int value = getIntOffset(bound, session);
if (!forFollowing) {
index = toGroupStart(orderedRows, sortOrder, currentRow, 0);
while (value > 0 && index > 0) {
value--;
......@@ -318,6 +319,17 @@ public final class WindowFrame {
if (value > 0) {
index = -1;
}
} else {
if (value == 0) {
index = toGroupEnd(orderedRows, sortOrder, currentRow, last);
} else {
index = currentRow;
while (value > 0 && index >= 0) {
value--;
index = toGroupStart(orderedRows, sortOrder, index, 0) - 1;
}
}
}
break;
}
case RANGE: {
......@@ -353,6 +365,7 @@ public final class WindowFrame {
}
case GROUPS: {
int value = getIntOffset(bound, session);
if (forFollowing) {
index = toGroupEnd(orderedRows, sortOrder, currentRow, last);
while (value > 0 && index < last) {
value--;
......@@ -361,6 +374,17 @@ public final class WindowFrame {
if (value > 0) {
index = size;
}
} else {
if (value == 0) {
index = toGroupStart(orderedRows, sortOrder, currentRow, 0);
} else {
index = currentRow;
while (value > 0 && index <= last) {
value--;
index = toGroupEnd(orderedRows, sortOrder, index, last) + 1;
}
}
}
break;
}
case RANGE: {
......@@ -386,7 +410,7 @@ public final class WindowFrame {
}
break;
case UNBOUNDED_FOLLOWING:
index = last;
index = size;
break;
default:
throw DbException.getUnsupportedException("window frame bound type=" + bound.getType());
......
......@@ -323,6 +323,49 @@ SELECT *, ARRAY_AGG(ID) OVER (ORDER BY ID ROWS BETWEEN 2 PRECEDING AND 1 PRECEDI
> 4 8 (2, 3)
> rows (ordered): 4
SELECT *, ARRAY_AGG(ID) OVER (ORDER BY ID ROWS BETWEEN 1 FOLLOWING AND 2 FOLLOWING) FROM TEST OFFSET 4 ROWS;
> ID VALUE ARRAY_AGG(ID) OVER (ORDER BY ID ROWS BETWEEN 1 FOLLOWING AND 2 FOLLOWING)
> -- ----- -------------------------------------------------------------------------
> 5 8 (6, 7)
> 6 8 (7, 8)
> 7 9 (8)
> 8 9 null
> rows (ordered): 4
SELECT *,
ARRAY_AGG(ID) OVER (ORDER BY VALUE GROUPS BETWEEN UNBOUNDED PRECEDING AND 1 PRECEDING) U_P,
ARRAY_AGG(ID) OVER (ORDER BY VALUE GROUPS BETWEEN 2 PRECEDING AND 1 PRECEDING) P,
ARRAY_AGG(ID) OVER (ORDER BY VALUE GROUPS BETWEEN 1 FOLLOWING AND 2 FOLLOWING) F,
ARRAY_AGG(ID) OVER (ORDER BY VALUE GROUPS BETWEEN 1 FOLLOWING AND UNBOUNDED FOLLOWING) U_F
FROM TEST;
> ID VALUE U_P P F U_F
> -- ----- ------------------ ------------ --------------- ------------------
> 1 1 null null (3, 4, 5, 6) (3, 4, 5, 6, 7, 8)
> 2 1 null null (3, 4, 5, 6) (3, 4, 5, 6, 7, 8)
> 3 5 (1, 2) (1, 2) (4, 5, 6, 7, 8) (4, 5, 6, 7, 8)
> 4 8 (1, 2, 3) (1, 2, 3) (7, 8) (7, 8)
> 5 8 (1, 2, 3) (1, 2, 3) (7, 8) (7, 8)
> 6 8 (1, 2, 3) (1, 2, 3) (7, 8) (7, 8)
> 7 9 (1, 2, 3, 4, 5, 6) (3, 4, 5, 6) null null
> 8 9 (1, 2, 3, 4, 5, 6) (3, 4, 5, 6) null null
> rows (ordered): 8
SELECT *,
ARRAY_AGG(ID) OVER (ORDER BY VALUE GROUPS BETWEEN 1 PRECEDING AND 0 PRECEDING) P,
ARRAY_AGG(ID) OVER (ORDER BY VALUE GROUPS BETWEEN 0 FOLLOWING AND 1 FOLLOWING) F
FROM TEST;
> ID VALUE P F
> -- ----- --------------- ---------------
> 1 1 (1, 2) (1, 2, 3)
> 2 1 (1, 2) (1, 2, 3)
> 3 5 (1, 2, 3) (3, 4, 5, 6)
> 4 8 (3, 4, 5, 6) (4, 5, 6, 7, 8)
> 5 8 (3, 4, 5, 6) (4, 5, 6, 7, 8)
> 6 8 (3, 4, 5, 6) (4, 5, 6, 7, 8)
> 7 9 (4, 5, 6, 7, 8) (7, 8)
> 8 9 (4, 5, 6, 7, 8) (7, 8)
> rows (ordered): 8
SELECT *, ARRAY_AGG(ID) OVER (ORDER BY ID RANGE BETWEEN CURRENT ROW AND 1 PRECEDING) FROM TEST;
> exception SYNTAX_ERROR_1
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论