提交 1b33a3de authored 作者: Evgenij Ryazanov's avatar Evgenij Ryazanov

Check validity of window frame in Parser

上级 3e61ab53
...@@ -3112,6 +3112,7 @@ public class Parser { ...@@ -3112,6 +3112,7 @@ public class Parser {
starting = readWindowFrameStarting(); starting = readWindowFrameStarting();
following = null; following = null;
} }
int idx = lastParseIndex;
WindowFrameExclusion exclusion = WindowFrameExclusion.EXCLUDE_NO_OTHERS; WindowFrameExclusion exclusion = WindowFrameExclusion.EXCLUDE_NO_OTHERS;
if (readIf("EXCLUDE")) { if (readIf("EXCLUDE")) {
if (readIf("CURRENT")) { if (readIf("CURRENT")) {
...@@ -3126,7 +3127,11 @@ public class Parser { ...@@ -3126,7 +3127,11 @@ public class Parser {
read("OTHERS"); read("OTHERS");
} }
} }
return new WindowFrame(units, starting, following, exclusion); WindowFrame frame = new WindowFrame(units, starting, following, exclusion);
if (!frame.isValid()) {
throw DbException.getSyntaxError(sqlCommand, idx);
}
return frame;
} }
private WindowFrameBound readWindowFrameStarting() { private WindowFrameBound readWindowFrameStarting() {
......
...@@ -11,7 +11,6 @@ import java.util.Collections; ...@@ -11,7 +11,6 @@ import java.util.Collections;
import java.util.Iterator; import java.util.Iterator;
import java.util.NoSuchElementException; import java.util.NoSuchElementException;
import org.h2.api.ErrorCode;
import org.h2.engine.Session; import org.h2.engine.Session;
import org.h2.expression.BinaryOperation; import org.h2.expression.BinaryOperation;
import org.h2.expression.BinaryOperation.OpType; import org.h2.expression.BinaryOperation.OpType;
...@@ -225,6 +224,18 @@ public final class WindowFrame { ...@@ -225,6 +224,18 @@ public final class WindowFrame {
this.exclusion = exclusion; this.exclusion = exclusion;
} }
/**
* Checks validity of this frame.
*
* @return whether bounds of this frame valid
*/
public boolean isValid() {
WindowFrameBoundType s = starting.getType(),
f = following != null ? following.getType() : WindowFrameBoundType.CURRENT_ROW;
return s != WindowFrameBoundType.UNBOUNDED_FOLLOWING && f != WindowFrameBoundType.UNBOUNDED_PRECEDING
&& s.compareTo(f) <= 0;
}
/** /**
* Returns whether window frame specification can be omitted. * Returns whether window frame specification can be omitted.
* *
...@@ -269,10 +280,7 @@ public final class WindowFrame { ...@@ -269,10 +280,7 @@ public final class WindowFrame {
int endIndex = following != null ? getIndex(session, orderedRows, sortOrder, currentRow, following, true) int endIndex = following != null ? getIndex(session, orderedRows, sortOrder, currentRow, following, true)
: currentRow; : currentRow;
if (endIndex < startIndex) { if (endIndex < startIndex) {
startIndex = getIndex(session, orderedRows, sortOrder, currentRow, starting, false); return Collections.emptyIterator();
endIndex = following != null ? getIndex(session, orderedRows, sortOrder, currentRow, following, true)
: currentRow;
throw DbException.get(ErrorCode.SYNTAX_ERROR_1, getSQL());
} }
int size = orderedRows.size(); int size = orderedRows.size();
if (startIndex >= size || endIndex < 0) { if (startIndex >= size || endIndex < 0) {
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论