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

Optimize EXCLUDE GROUP part 1

上级 eca0feef
......@@ -535,21 +535,25 @@ public final class WindowFrame {
} else {
return biIterator(orderedRows, startIndex, currentRow - 1, currentRow + 1, endIndex, reverse);
}
return plainIterator(orderedRows, startIndex, endIndex, reverse);
} else {
BitSet set = new BitSet(endIndex + 1);
set.set(startIndex, endIndex + 1);
int exStart = toGroupStart(orderedRows, sortOrder, currentRow, startIndex);
int exEnd = toGroupEnd(orderedRows, sortOrder, currentRow, endIndex);
set.clear(exStart, exEnd + 1);
if (exclusion == WindowFrameExclusion.EXCLUDE_TIES) {
set.set(currentRow);
}
if (set.isEmpty()) {
return Collections.emptyIterator();
if (exEnd < startIndex || exStart > endIndex) {
// Nothing to do
} else {
BitSet set = new BitSet(endIndex + 1);
set.set(startIndex, endIndex + 1);
set.clear(exStart, exEnd + 1);
if (exclusion == WindowFrameExclusion.EXCLUDE_TIES) {
set.set(currentRow);
}
if (set.isEmpty()) {
return Collections.emptyIterator();
}
return reverse ? new BitSetReverseItr(orderedRows, set) : new BitSetItr(orderedRows, set);
}
return reverse ? new BitSetReverseItr(orderedRows, set) : new BitSetItr(orderedRows, set);
}
return plainIterator(orderedRows, startIndex, endIndex, reverse);
}
/**
......
......@@ -384,6 +384,40 @@ SELECT *,
> 8 9 (4, 5, 6, 7, 8) (7, 8)
> rows: 8
SELECT ID, VALUE,
ARRAY_AGG(ID) OVER (ORDER BY VALUE ROWS BETWEEN 2 PRECEDING AND 2 FOLLOWING EXCLUDE GROUP) G,
ARRAY_AGG(ID) OVER (ORDER BY VALUE ROWS BETWEEN 2 PRECEDING AND 2 FOLLOWING EXCLUDE TIES) T
FROM TEST;
> ID VALUE G T
> -- ----- ------------ ---------------
> 1 1 (3) (1, 3)
> 2 1 (3, 4) (2, 3, 4)
> 3 5 (1, 2, 4, 5) (1, 2, 3, 4, 5)
> 4 8 (2, 3) (2, 3, 4)
> 5 8 (3, 7) (3, 5, 7)
> 6 8 (7, 8) (6, 7, 8)
> 7 9 (5, 6) (5, 6, 7)
> 8 9 (6) (6, 8)
> rows: 8
SELECT ID, VALUE, ARRAY_AGG(ID) OVER(ORDER BY VALUE ROWS BETWEEN 1 FOLLOWING AND 2 FOLLOWING EXCLUDE GROUP) G
FROM TEST ORDER BY ID FETCH FIRST 3 ROWS ONLY;
> ID VALUE G
> -- ----- ------
> 1 1 (3)
> 2 1 (3, 4)
> 3 5 (4, 5)
> rows (ordered): 3
SELECT ID, VALUE, ARRAY_AGG(ID) OVER(ORDER BY VALUE ROWS BETWEEN 2 PRECEDING AND 1 PRECEDING EXCLUDE GROUP) G
FROM TEST ORDER BY ID FETCH FIRST 3 ROWS ONLY;
> ID VALUE G
> -- ----- ------
> 1 1 null
> 2 1 null
> 3 5 (1, 2)
> rows (ordered): 3
SELECT *, ARRAY_AGG(ID) OVER (ORDER BY ID RANGE BETWEEN CURRENT ROW AND 1 PRECEDING) FROM TEST;
> exception SYNTAX_ERROR_1
......
......@@ -167,5 +167,55 @@ SELECT ID, CATEGORY,
> 13 4 3
> rows: 3
SELECT ID, CATEGORY,
NTH_VALUE(CATEGORY, 1) OVER (ORDER BY CATEGORY RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE GROUP) F1,
NTH_VALUE(CATEGORY, 2) OVER (ORDER BY CATEGORY RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE GROUP) F2,
NTH_VALUE(CATEGORY, 5) OVER (ORDER BY CATEGORY RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE GROUP) F5,
NTH_VALUE(CATEGORY, 5) FROM LAST OVER (ORDER BY CATEGORY RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE GROUP) L5,
NTH_VALUE(CATEGORY, 2) FROM LAST OVER (ORDER BY CATEGORY RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE GROUP) L2,
NTH_VALUE(CATEGORY, 1) FROM LAST OVER (ORDER BY CATEGORY RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE GROUP) L1
FROM TEST ORDER BY ID;
> ID CATEGORY F1 F2 F5 L5 L2 L1
> -- -------- -- -- -- -- -- --
> 1 1 2 2 3 3 4 4
> 2 1 2 2 3 3 4 4
> 3 1 2 2 3 3 4 4
> 4 1 2 2 3 3 4 4
> 5 1 2 2 3 3 4 4
> 6 1 2 2 3 3 4 4
> 7 2 1 1 1 3 4 4
> 8 2 1 1 1 3 4 4
> 9 3 1 1 1 1 4 4
> 10 3 1 1 1 1 4 4
> 11 3 1 1 1 1 4 4
> 12 4 1 1 1 2 3 3
> 13 4 1 1 1 2 3 3
> rows (ordered): 13
SELECT ID, CATEGORY,
NTH_VALUE(CATEGORY, 1) OVER (ORDER BY CATEGORY RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE TIES) F1,
NTH_VALUE(CATEGORY, 2) OVER (ORDER BY CATEGORY RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE TIES) F2,
NTH_VALUE(CATEGORY, 5) OVER (ORDER BY CATEGORY RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE TIES) F5,
NTH_VALUE(CATEGORY, 5) FROM LAST OVER (ORDER BY CATEGORY RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE TIES) L5,
NTH_VALUE(CATEGORY, 2) FROM LAST OVER (ORDER BY CATEGORY RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE TIES) L2,
NTH_VALUE(CATEGORY, 1) FROM LAST OVER (ORDER BY CATEGORY RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING EXCLUDE TIES) L1
FROM TEST ORDER BY ID;
> ID CATEGORY F1 F2 F5 L5 L2 L1
> -- -------- -- -- -- -- -- --
> 1 1 1 2 3 3 4 4
> 2 1 1 2 3 3 4 4
> 3 1 1 2 3 3 4 4
> 4 1 1 2 3 3 4 4
> 5 1 1 2 3 3 4 4
> 6 1 1 2 3 3 4 4
> 7 2 1 1 1 3 4 4
> 8 2 1 1 1 3 4 4
> 9 3 1 1 1 2 4 4
> 10 3 1 1 1 2 4 4
> 11 3 1 1 1 2 4 4
> 12 4 1 1 1 2 3 4
> 13 4 1 1 1 2 3 4
> rows (ordered): 13
DROP TABLE TEST;
> ok
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论