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

Optimize EXCLUDE TIES part 2

上级 96f6476f
......@@ -93,14 +93,14 @@ public final class WindowFrame {
}
private static final class BiItr extends PlainItr {
private static class BiItr extends PlainItr {
private final int endIndex1, startIndex2;
final int end1, start1;
BiItr(ArrayList<Value[]> orderedRows, int startIndex1, int endIndex1, int startIndex2, int endIndex2) {
super(orderedRows, startIndex1, endIndex2);
this.endIndex1 = endIndex1;
this.startIndex2 = startIndex2;
end1 = endIndex1;
start1 = startIndex2;
}
@Override
......@@ -109,20 +109,20 @@ public final class WindowFrame {
throw new NoSuchElementException();
}
Value[] r = orderedRows.get(cursor);
cursor = cursor != endIndex1 ? cursor + 1 : startIndex2;
cursor = cursor != end1 ? cursor + 1 : start1;
return r;
}
}
private static final class BiReverseItr extends PlainReverseItr {
private static class BiReverseItr extends PlainReverseItr {
private final int endIndex1, startIndex2;
final int end1, start1;
BiReverseItr(ArrayList<Value[]> orderedRows, int startIndex1, int endIndex1, int startIndex2, int endIndex2) {
super(orderedRows, startIndex1, endIndex2);
this.endIndex1 = endIndex1;
this.startIndex2 = startIndex2;
end1 = endIndex1;
start1 = startIndex2;
}
@Override
......@@ -131,7 +131,53 @@ public final class WindowFrame {
throw new NoSuchElementException();
}
Value[] r = orderedRows.get(cursor);
cursor = cursor != startIndex2 ? cursor - 1 : endIndex1;
cursor = cursor != start1 ? cursor - 1 : end1;
return r;
}
}
private static final class TriItr extends BiItr {
private final int end2, start2;
TriItr(ArrayList<Value[]> orderedRows, int startIndex1, int endIndex1, int startIndex2, int endIndex2,
int startIndex3, int endIndex3) {
super(orderedRows, startIndex1, endIndex1, startIndex2, endIndex3);
end2 = endIndex2;
start2 = startIndex3;
}
@Override
public Value[] next() {
if (cursor > endIndex) {
throw new NoSuchElementException();
}
Value[] r = orderedRows.get(cursor);
cursor = cursor != end1 ? cursor != end2 ? cursor + 1 : start2 : start1;
return r;
}
}
private static final class TriReverseItr extends BiReverseItr {
private final int end2, start2;
TriReverseItr(ArrayList<Value[]> orderedRows, int startIndex1, int endIndex1, int startIndex2, int endIndex2,
int startIndex3, int endIndex3) {
super(orderedRows, startIndex1, endIndex1, startIndex2, endIndex3);
end2 = endIndex2;
start2 = startIndex3;
}
@Override
public Value[] next() {
if (cursor < startIndex) {
throw new NoSuchElementException();
}
Value[] r = orderedRows.get(cursor);
cursor = cursor != start1 ? cursor != start2 ? cursor - 1 : end2 : end1;
return r;
}
......@@ -241,6 +287,13 @@ public final class WindowFrame {
: new BiItr(orderedRows, startIndex1, endIndex1, startIndex2, endIndex2);
}
private static Iterator<Value[]> triIterator(ArrayList<Value[]> orderedRows, int startIndex1, int endIndex1,
int startIndex2, int endIndex2, int startIndex3, int endIndex3, boolean reverse) {
return reverse ? new TriReverseItr(orderedRows, startIndex1, endIndex1, startIndex2, endIndex2, //
startIndex3, endIndex3)
: new TriItr(orderedRows, startIndex1, endIndex1, startIndex2, endIndex2, startIndex3, endIndex3);
}
private static int toGroupStart(ArrayList<Value[]> orderedRows, SortOrder sortOrder, int offset, int minOffset) {
Value[] row = orderedRows.get(offset);
while (offset > minOffset && sortOrder.compare(row, orderedRows.get(offset - 1)) == 0) {
......@@ -550,13 +603,25 @@ public final class WindowFrame {
}
if (exStart > exEnd || exEnd < startIndex || exStart > endIndex) {
// Nothing to do
} else if (includeCurrentRow) {
if (startIndex == exStart) {
if (endIndex == exEnd) {
return Collections.singleton(orderedRows.get(currentRow)).iterator();
} else {
return biIterator(orderedRows, currentRow, currentRow, exEnd + 1, endIndex, reverse);
}
} else {
if (endIndex == exEnd) {
return biIterator(orderedRows, startIndex, exStart - 1, currentRow, currentRow, reverse);
} else {
return triIterator(orderedRows, startIndex, exStart - 1, currentRow, currentRow, exEnd + 1,
endIndex, reverse);
}
}
} else {
BitSet set = new BitSet(endIndex + 1);
set.set(startIndex, endIndex + 1);
set.clear(exStart, exEnd + 1);
if (includeCurrentRow) {
set.set(currentRow);
}
if (set.isEmpty()) {
return Collections.emptyIterator();
}
......
......@@ -350,6 +350,23 @@ SELECT *, ARRAY_AGG(ID) OVER (ORDER BY ID RANGE BETWEEN 1 FOLLOWING AND 2 FOLLOW
> 8 9 null
> rows: 4
SELECT *,
ARRAY_AGG(ID) OVER (ORDER BY VALUE GROUPS BETWEEN 0 PRECEDING AND 0 FOLLOWING) N,
ARRAY_AGG(ID) OVER (ORDER BY VALUE GROUPS BETWEEN 0 PRECEDING AND 0 FOLLOWING EXCLUDE TIES) T,
ARRAY_AGG(ID) OVER (ORDER BY VALUE GROUPS BETWEEN 1 PRECEDING AND 0 FOLLOWING EXCLUDE TIES) T1
FROM TEST;
> ID VALUE N T T1
> -- ----- --------- --- ------------
> 1 1 (1, 2) (1) (1)
> 2 1 (1, 2) (2) (2)
> 3 5 (3) (3) (1, 2, 3)
> 4 8 (4, 5, 6) (4) (3, 4)
> 5 8 (4, 5, 6) (5) (3, 5)
> 6 8 (4, 5, 6) (6) (3, 6)
> 7 9 (7, 8) (7) (4, 5, 6, 7)
> 8 9 (7, 8) (8) (4, 5, 6, 8)
> rows: 8
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,
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论