提交 2b0a609a authored 作者: Evgenij Ryazanov's avatar Evgenij Ryazanov

Fix window frames for window aggregates with FILTER

上级 dedefffc
...@@ -213,8 +213,8 @@ public abstract class AbstractAggregate extends DataAnalysisOperation { ...@@ -213,8 +213,8 @@ public abstract class AbstractAggregate extends DataAnalysisOperation {
@Override @Override
protected void updateAggregate(Session session, SelectGroups groupData, int groupRowId) { protected void updateAggregate(Session session, SelectGroups groupData, int groupRowId) {
ArrayList<SelectOrderBy> orderBy;
if (filterCondition == null || filterCondition.getBooleanValue(session)) { if (filterCondition == null || filterCondition.getBooleanValue(session)) {
ArrayList<SelectOrderBy> orderBy;
if (over != null) { if (over != null) {
if ((orderBy = over.getOrderBy()) != null) { if ((orderBy = over.getOrderBy()) != null) {
updateOrderedAggregate(session, groupData, groupRowId, orderBy); updateOrderedAggregate(session, groupData, groupRowId, orderBy);
...@@ -224,6 +224,8 @@ public abstract class AbstractAggregate extends DataAnalysisOperation { ...@@ -224,6 +224,8 @@ public abstract class AbstractAggregate extends DataAnalysisOperation {
} else { } else {
updateAggregate(session, getGroupData(groupData, false)); updateAggregate(session, getGroupData(groupData, false));
} }
} else if (over != null && (orderBy = over.getOrderBy()) != null) {
updateOrderedAggregate(session, groupData, groupRowId, orderBy);
} }
} }
......
...@@ -346,6 +346,9 @@ public class Aggregate extends AbstractAggregate { ...@@ -346,6 +346,9 @@ public class Aggregate extends AbstractAggregate {
if (orderByList != null) { if (orderByList != null) {
n += orderByList.size(); n += orderByList.size();
} }
if (filterCondition != null) {
n++;
}
return n; return n;
} }
...@@ -360,13 +363,18 @@ public class Aggregate extends AbstractAggregate { ...@@ -360,13 +363,18 @@ public class Aggregate extends AbstractAggregate {
array[offset++] = o.expression.getValue(session); array[offset++] = o.expression.getValue(session);
} }
} }
if (filterCondition != null) {
array[offset] = ValueBoolean.get(filterCondition.getBooleanValue(session));
}
} }
@Override @Override
protected void updateFromExpressions(Session session, Object aggregateData, Value[] array) { protected void updateFromExpressions(Session session, Object aggregateData, Value[] array) {
AggregateData data = (AggregateData) aggregateData; if (filterCondition == null || array[getNumExpressions() - 1].getBoolean()) {
Value v = on == null ? null : array[0]; AggregateData data = (AggregateData) aggregateData;
updateData(session, data, v, array); Value v = on == null ? null : array[0];
updateData(session, data, v, array);
}
} }
@Override @Override
......
...@@ -20,6 +20,7 @@ import org.h2.table.TableFilter; ...@@ -20,6 +20,7 @@ import org.h2.table.TableFilter;
import org.h2.value.DataType; import org.h2.value.DataType;
import org.h2.value.Value; import org.h2.value.Value;
import org.h2.value.ValueArray; import org.h2.value.ValueArray;
import org.h2.value.ValueBoolean;
import org.h2.value.ValueNull; import org.h2.value.ValueNull;
/** /**
...@@ -232,19 +233,29 @@ public class JavaAggregate extends AbstractAggregate { ...@@ -232,19 +233,29 @@ public class JavaAggregate extends AbstractAggregate {
@Override @Override
protected int getNumExpressions() { protected int getNumExpressions() {
return args.length; int n = args.length;
if (filterCondition != null) {
n++;
}
return n;
} }
@Override @Override
protected void rememberExpressions(Session session, Value[] array) { protected void rememberExpressions(Session session, Value[] array) {
for (int i = 0; i < args.length; i++) { int length = args.length;
for (int i = 0; i < length; i++) {
array[i] = args[i].getValue(session); array[i] = args[i].getValue(session);
} }
if (filterCondition != null) {
array[length] = ValueBoolean.get(filterCondition.getBooleanValue(session));
}
} }
@Override @Override
protected void updateFromExpressions(Session session, Object aggregateData, Value[] array) { protected void updateFromExpressions(Session session, Object aggregateData, Value[] array) {
updateData(session, aggregateData, array); if (filterCondition == null || array[getNumExpressions() - 1].getBoolean()) {
updateData(session, aggregateData, array);
}
} }
@Override @Override
......
...@@ -121,6 +121,20 @@ SELECT ...@@ -121,6 +121,20 @@ SELECT
> [5, 6] [5, 6] 6 c > [5, 6] [5, 6] 6 c
> rows (ordered): 6 > rows (ordered): 6
SELECT
ARRAY_AGG(ID ORDER BY ID) FILTER (WHERE ID < 3 OR ID > 4)
OVER (ORDER BY ID ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING) A,
ID FROM TEST ORDER BY ID;
> A ID
> ------ --
> [1, 2] 1
> [1, 2] 2
> [2] 3
> [5] 4
> [5, 6] 5
> [5, 6] 6
> rows (ordered): 6
SELECT ARRAY_AGG(SUM(ID)) OVER () FROM TEST; SELECT ARRAY_AGG(SUM(ID)) OVER () FROM TEST;
> ARRAY_AGG(SUM(ID)) OVER () > ARRAY_AGG(SUM(ID)) OVER ()
> -------------------------- > --------------------------
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论