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

Throw syntax error on RANGE without single sort key

上级 d24688b4
......@@ -162,26 +162,43 @@ public abstract class DataAnalysisOperation extends Expression {
WindowFrame frame = over.getWindowFrame();
if (frame != null) {
int index = getNumExpressions();
int orderBySize = 0;
if (orderBy != null) {
index += orderBy.size();
orderBySize = orderBy.size();
index += orderBySize;
}
int n = 0;
WindowFrameBound bound = frame.getStarting();
if (bound.isParameterized()) {
if (orderBySize != 1) {
throw getSingleSortKeyException();
}
if (bound.isVariable()) {
bound.setExpressionIndex(index);
n++;
}
}
bound = frame.getFollowing();
if (bound != null && bound.isVariable()) {
if (bound != null && bound.isParameterized()) {
if (orderBySize != 1) {
throw getSingleSortKeyException();
}
if (bound.isVariable()) {
bound.setExpressionIndex(index + n);
n++;
}
}
numFrameExpressions = n;
}
}
return this;
}
private DbException getSingleSortKeyException() {
String sql = getSQL();
return DbException.getSyntaxError(sql, sql.length() - 1, "single sort key is required for RANGE units");
}
@Override
public void setEvaluatable(TableFilter tableFilter, boolean b) {
if (over != null) {
......
......@@ -57,6 +57,15 @@ public class WindowFrameBound {
return value;
}
/**
* Returns whether bound is defined as n PRECEDING or n FOLLOWING.
*
* @return whether bound is defined as n PRECEDING or n FOLLOWING
*/
public boolean isParameterized() {
return type == WindowFrameBoundType.PRECEDING || type == WindowFrameBoundType.FOLLOWING;
}
/**
* Returns whether bound is defined with a variable. This method may be used
* only after {@link #optimize(Session)} invocation.
......
......@@ -218,3 +218,12 @@ SELECT
> [00:00:00] null 00:00:00
> [00:00:00, 01:30:00] [00:00:00] 01:30:00
> rows (ordered): 2
SELECT SUM(A) OVER (ORDER BY A, B RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) S FROM VALUES (1, 2) T(A, B);
>> 1
SELECT SUM(A) OVER (ORDER BY A, B RANGE BETWEEN 1 PRECEDING AND UNBOUNDED FOLLOWING) S FROM VALUES (1, 2) T(A, B);
> exception SYNTAX_ERROR_2
SELECT SUM(A) OVER (ORDER BY A, B RANGE BETWEEN UNBOUNDED PRECEDING AND 1 FOLLOWING) S FROM VALUES (1, 2) T(A, B);
> exception SYNTAX_ERROR_2
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论