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

Do not create default window frame if it was not set

上级 b0105180
......@@ -426,7 +426,7 @@ public abstract class AbstractAggregate extends Expression {
protected void getOrderedResultLoop(Session session, HashMap<Integer, Value> result, ArrayList<Value[]> ordered,
int rowIdColumn) {
WindowFrame frame = over.getWindowFrame();
if (frame.isDefault()) {
if (frame == null || frame.isDefault()) {
Object aggregateData = createAggregateData();
for (Value[] row : ordered) {
updateFromExpressions(session, aggregateData, row);
......
......@@ -64,11 +64,6 @@ public final class Window {
public Window(ArrayList<Expression> partitionBy, ArrayList<SelectOrderBy> orderBy, WindowFrame frame) {
this.partitionBy = partitionBy;
this.orderBy = orderBy;
if (frame == null) {
frame = new WindowFrame(WindowFrameUnits.RANGE,
new WindowFrameBound(WindowFrameBoundType.UNBOUNDED_PRECEDING, null), null,
WindowFrameExclusion.EXCLUDE_NO_OTHERS);
}
this.frame = frame;
}
......@@ -146,9 +141,9 @@ public final class Window {
}
/**
* Returns window frame.
* Returns window frame, or null.
*
* @return window frame
* @return window frame, or null
*/
public WindowFrame getWindowFrame() {
return frame;
......@@ -196,7 +191,7 @@ public final class Window {
}
}
appendOrderBy(builder, orderBy);
if (!frame.isDefault()) {
if (frame != null && !frame.isDefault()) {
builder.append(' ').append(frame.getSQL());
}
return builder.append(')').toString();
......
......@@ -24,7 +24,7 @@ import org.h2.value.Value;
*/
public final class WindowFrame {
private abstract class Itr implements Iterator<Value[]> {
private static abstract class Itr implements Iterator<Value[]> {
final ArrayList<Value[]> orderedRows;
......@@ -39,7 +39,7 @@ public final class WindowFrame {
}
private final class PlainItr extends Itr {
private static final class PlainItr extends Itr {
private final int endIndex;
......@@ -66,7 +66,7 @@ public final class WindowFrame {
}
private final class PlainReverseItr extends Itr {
private static final class PlainReverseItr extends Itr {
private final int startIndex;
......@@ -93,7 +93,7 @@ public final class WindowFrame {
}
private abstract class AbstractBitSetItr extends Itr {
private static abstract class AbstractBitSetItr extends Itr {
final BitSet set;
......@@ -111,7 +111,7 @@ public final class WindowFrame {
}
private final class BitSetItr extends AbstractBitSetItr {
private static final class BitSetItr extends AbstractBitSetItr {
BitSetItr(ArrayList<Value[]> orderedRows, BitSet set) {
super(orderedRows, set);
......@@ -130,7 +130,7 @@ public final class WindowFrame {
}
private final class BitSetReverseItr extends AbstractBitSetItr {
private static final class BitSetReverseItr extends AbstractBitSetItr {
BitSetReverseItr(ArrayList<Value[]> orderedRows, BitSet set) {
super(orderedRows, set);
......@@ -157,6 +157,31 @@ public final class WindowFrame {
private final WindowFrameExclusion exclusion;
/**
* Returns iterator for the specified frame, or default iterator if frame is
* null.
*
* @param frame
* window frame, or null
* @param session
* the session
* @param orderedRows
* ordered rows
* @param sortOrder
* sort order
* @param currentRow
* index of the current row
* @param reverse
* whether iterator should iterate in reverse order
*
* @return iterator
*/
public static Iterator<Value[]> iterator(WindowFrame frame, Session session, ArrayList<Value[]> orderedRows,
SortOrder sortOrder, int currentRow, boolean reverse) {
return frame != null ? frame.iterator(session, orderedRows, sortOrder, currentRow, reverse)
: reverse ? new PlainReverseItr(orderedRows, 0, currentRow) : new PlainItr(orderedRows, 0, currentRow);
}
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) {
......
......@@ -358,10 +358,12 @@ public class WindowFunction extends AbstractAggregate {
Value v;
switch (type) {
case FIRST_VALUE:
v = getNthValue(frame.iterator(session, ordered, getOverOrderBySort(), i, false), 0, ignoreNulls);
v = getNthValue(WindowFrame.iterator(frame, session, ordered, getOverOrderBySort(), i, false), 0,
ignoreNulls);
break;
case LAST_VALUE:
v = getNthValue(frame.iterator(session, ordered, getOverOrderBySort(), i, true), 0, ignoreNulls);
v = getNthValue(WindowFrame.iterator(frame, session, ordered, getOverOrderBySort(), i, true), 0,
ignoreNulls);
break;
case NTH_VALUE: {
int n = row[1].getInt();
......@@ -369,7 +371,8 @@ public class WindowFunction extends AbstractAggregate {
throw DbException.getInvalidValueException("nth row", n);
}
n--;
Iterator<Value[]> iter = frame.iterator(session, ordered, getOverOrderBySort(), i, fromLast);
Iterator<Value[]> iter = WindowFrame.iterator(frame, session, ordered, getOverOrderBySort(), i,
fromLast);
v = getNthValue(iter, n, ignoreNulls);
break;
}
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论