Unverified 提交 fd30a141 authored 作者: Evgenij Ryazanov's avatar Evgenij Ryazanov 提交者: GitHub

Merge pull request #1470 from katzyn/having

Fix window functions in queries with HAVING
...@@ -400,12 +400,16 @@ public class Select extends Query { ...@@ -400,12 +400,16 @@ public class Select extends Query {
initGroupData(columnCount); initGroupData(columnCount);
try { try {
gatherGroup(columnCount, Aggregate.STAGE_GROUP); gatherGroup(columnCount, Aggregate.STAGE_GROUP);
while (groupData.next() != null) {
updateAgg(columnCount, Aggregate.STAGE_WINDOW);
}
groupData.done();
try { try {
isGroupWindowStage2 = true; isGroupWindowStage2 = true;
while (groupData.next() != null) {
if (havingIndex < 0 || expressions.get(havingIndex).getBooleanValue(session)) {
updateAgg(columnCount, Aggregate.STAGE_WINDOW);
} else {
groupData.remove();
}
}
groupData.done();
processGroupResult(columnCount, result, offset, quickOffset); processGroupResult(columnCount, result, offset, quickOffset);
} finally { } finally {
isGroupWindowStage2 = false; isGroupWindowStage2 = false;
......
...@@ -130,6 +130,13 @@ public abstract class SelectGroups { ...@@ -130,6 +130,13 @@ public abstract class SelectGroups {
return null; return null;
} }
@Override
public void remove() {
cursor.remove();
currentGroupByExprData = null;
currentGroupRowId--;
}
@Override @Override
public void resetLazy() { public void resetLazy() {
super.resetLazy(); super.resetLazy();
...@@ -374,6 +381,15 @@ public abstract class SelectGroups { ...@@ -374,6 +381,15 @@ public abstract class SelectGroups {
*/ */
public abstract ValueArray next(); 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. * Resets this group data for reuse in lazy mode.
*/ */
......
...@@ -154,7 +154,6 @@ public class ExpressionColumn extends Expression { ...@@ -154,7 +154,6 @@ public class ExpressionColumn extends Expression {
@Override @Override
public void updateAggregate(Session session, int stage) { public void updateAggregate(Session session, int stage) {
Value now = columnResolver.getValue(column);
Select select = columnResolver.getSelect(); Select select = columnResolver.getSelect();
if (select == null) { if (select == null) {
throw DbException.get(ErrorCode.MUST_GROUP_BY_COLUMN_1, getSQL()); throw DbException.get(ErrorCode.MUST_GROUP_BY_COLUMN_1, getSQL());
...@@ -166,9 +165,9 @@ public class ExpressionColumn extends Expression { ...@@ -166,9 +165,9 @@ public class ExpressionColumn extends Expression {
} }
Value v = (Value) groupData.getCurrentGroupExprData(this); Value v = (Value) groupData.getCurrentGroupExprData(this);
if (v == null) { if (v == null) {
groupData.setCurrentGroupExprData(this, now); groupData.setCurrentGroupExprData(this, columnResolver.getValue(column));
} else { } else if (!select.isGroupWindowStage2()) {
if (!database.areEqual(now, v)) { if (!database.areEqual(columnResolver.getValue(column), v)) {
throw DbException.get(ErrorCode.MUST_GROUP_BY_COLUMN_1, getSQL()); throw DbException.get(ErrorCode.MUST_GROUP_BY_COLUMN_1, getSQL());
} }
} }
......
...@@ -50,3 +50,64 @@ SELECT MAX(MAX(X) OVER ()) FROM VALUES (1); ...@@ -50,3 +50,64 @@ SELECT MAX(MAX(X) OVER ()) FROM VALUES (1);
SELECT MAX(MAX(X)) FROM VALUES (1); SELECT MAX(MAX(X)) FROM VALUES (1);
> exception INVALID_USE_OF_AGGREGATE_FUNCTION_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: 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: 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: 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: 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: 1
DROP TABLE TEST;
> ok
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论