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

Fix window functions in queries with HAVING

上级 d9a7cf0d
......@@ -400,12 +400,16 @@ public class Select extends Query {
initGroupData(columnCount);
try {
gatherGroup(columnCount, Aggregate.STAGE_GROUP);
try {
isGroupWindowStage2 = true;
while (groupData.next() != null) {
if (havingIndex < 0 || expressions.get(havingIndex).getBooleanValue(session)) {
updateAgg(columnCount, Aggregate.STAGE_WINDOW);
} else {
groupData.remove();
}
}
groupData.done();
try {
isGroupWindowStage2 = true;
processGroupResult(columnCount, result, offset, quickOffset);
} finally {
isGroupWindowStage2 = false;
......
......@@ -130,6 +130,13 @@ public abstract class SelectGroups {
return null;
}
@Override
public void remove() {
cursor.remove();
currentGroupByExprData = null;
currentGroupRowId--;
}
@Override
public void resetLazy() {
super.resetLazy();
......@@ -374,6 +381,15 @@ public abstract class SelectGroups {
*/
public abstract ValueArray next();
/**
* Removes the data for the current key.
*
* @see #next()
*/
public void remove() {
throw new UnsupportedOperationException();
}
/**
* Resets this group data for reuse in lazy mode.
*/
......
......@@ -154,7 +154,6 @@ public class ExpressionColumn extends Expression {
@Override
public void updateAggregate(Session session, int stage) {
Value now = columnResolver.getValue(column);
Select select = columnResolver.getSelect();
if (select == null) {
throw DbException.get(ErrorCode.MUST_GROUP_BY_COLUMN_1, getSQL());
......@@ -166,9 +165,9 @@ public class ExpressionColumn extends Expression {
}
Value v = (Value) groupData.getCurrentGroupExprData(this);
if (v == null) {
groupData.setCurrentGroupExprData(this, now);
} else {
if (!database.areEqual(now, v)) {
groupData.setCurrentGroupExprData(this, columnResolver.getValue(column));
} else if (!select.isGroupWindowStage2()) {
if (!database.areEqual(columnResolver.getValue(column), v)) {
throw DbException.get(ErrorCode.MUST_GROUP_BY_COLUMN_1, getSQL());
}
}
......
......@@ -50,3 +50,64 @@ SELECT MAX(MAX(X) OVER ()) FROM VALUES (1);
SELECT MAX(MAX(X)) FROM VALUES (1);
> exception INVALID_USE_OF_AGGREGATE_FUNCTION_1
CREATE TABLE TEST(ID INT, CATEGORY INT);
> ok
INSERT INTO TEST VALUES
(1, 1),
(2, 1),
(4, 2),
(8, 2),
(16, 3),
(32, 3);
> update count: 6
SELECT ROW_NUMBER() OVER (ORDER BY CATEGORY), SUM(ID) FROM TEST GROUP BY CATEGORY HAVING SUM(ID) = 12;
> ROW_NUMBER() OVER (ORDER BY CATEGORY) SUM(ID)
> ------------------------------------- -------
> 1 12
> rows (ordered): 1
SELECT ROW_NUMBER() OVER (ORDER BY CATEGORY), SUM(ID) FROM TEST GROUP BY CATEGORY HAVING CATEGORY = 2;
> ROW_NUMBER() OVER (ORDER BY CATEGORY) SUM(ID)
> ------------------------------------- -------
> 1 12
> rows (ordered): 1
SELECT ROW_NUMBER() OVER (ORDER BY CATEGORY), SUM(ID) FROM TEST GROUP BY CATEGORY HAVING CATEGORY > 1;
> ROW_NUMBER() OVER (ORDER BY CATEGORY) SUM(ID)
> ------------------------------------- -------
> 1 12
> 2 48
> rows (ordered): 2
DROP TABLE TEST;
> ok
CREATE TABLE TEST(ID INT, CATEGORY BOOLEAN);
> ok
INSERT INTO TEST VALUES
(1, FALSE),
(2, FALSE),
(4, TRUE),
(8, TRUE),
(16, FALSE),
(32, FALSE);
> update count: 6
SELECT ROW_NUMBER() OVER (ORDER BY CATEGORY), SUM(ID) FROM TEST GROUP BY CATEGORY HAVING SUM(ID) = 12;
> ROW_NUMBER() OVER (ORDER BY CATEGORY) SUM(ID)
> ------------------------------------- -------
> 1 12
> rows (ordered): 1
SELECT ROW_NUMBER() OVER (ORDER BY CATEGORY), SUM(ID) FROM TEST GROUP BY CATEGORY HAVING CATEGORY;
> ROW_NUMBER() OVER (ORDER BY CATEGORY) SUM(ID)
> ------------------------------------- -------
> 1 12
> rows (ordered): 1
DROP TABLE TEST;
> ok
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论