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

Do not use ValueArray as key for PARTITION BY with 1 column

上级 85a79304
...@@ -298,7 +298,7 @@ public abstract class SelectGroups { ...@@ -298,7 +298,7 @@ public abstract class SelectGroups {
* a key of partition * a key of partition
* @return expression data or null * @return expression data or null
*/ */
public final PartitionData getWindowExprData(DataAnalysisOperation expr, ValueArray partitionKey) { public final PartitionData getWindowExprData(DataAnalysisOperation expr, Value partitionKey) {
if (partitionKey == null) { if (partitionKey == null) {
return windowData.get(expr); return windowData.get(expr);
} else { } else {
...@@ -317,7 +317,7 @@ public abstract class SelectGroups { ...@@ -317,7 +317,7 @@ public abstract class SelectGroups {
* @param object * @param object
* window expression data to set * window expression data to set
*/ */
public final void setWindowExprData(DataAnalysisOperation expr, ValueArray partitionKey, PartitionData obj) { public final void setWindowExprData(DataAnalysisOperation expr, Value partitionKey, PartitionData obj) {
if (partitionKey == null) { if (partitionKey == null) {
Object old = windowData.put(expr, obj); Object old = windowData.put(expr, obj);
assert old == null; assert old == null;
......
...@@ -21,7 +21,6 @@ import org.h2.result.SortOrder; ...@@ -21,7 +21,6 @@ import org.h2.result.SortOrder;
import org.h2.table.ColumnResolver; import org.h2.table.ColumnResolver;
import org.h2.table.TableFilter; import org.h2.table.TableFilter;
import org.h2.value.Value; import org.h2.value.Value;
import org.h2.value.ValueArray;
import org.h2.value.ValueInt; import org.h2.value.ValueInt;
/** /**
...@@ -184,7 +183,7 @@ public abstract class DataAnalysisOperation extends Expression { ...@@ -184,7 +183,7 @@ public abstract class DataAnalysisOperation extends Expression {
protected Object getWindowData(Session session, SelectGroups groupData, boolean forOrderBy) { protected Object getWindowData(Session session, SelectGroups groupData, boolean forOrderBy) {
Object data; Object data;
ValueArray key = over.getCurrentKey(session); Value key = over.getCurrentKey(session);
PartitionData partition = groupData.getWindowExprData(this, key); PartitionData partition = groupData.getWindowExprData(this, key);
if (partition == null) { if (partition == null) {
data = forOrderBy ? new ArrayList<>() : createAggregateData(); data = forOrderBy ? new ArrayList<>() : createAggregateData();
...@@ -258,7 +257,7 @@ public abstract class DataAnalysisOperation extends Expression { ...@@ -258,7 +257,7 @@ public abstract class DataAnalysisOperation extends Expression {
PartitionData partition; PartitionData partition;
Object data; Object data;
boolean forOrderBy = over.getOrderBy() != null; boolean forOrderBy = over.getOrderBy() != null;
ValueArray key = over.getCurrentKey(session); Value key = over.getCurrentKey(session);
partition = groupData.getWindowExprData(this, key); partition = groupData.getWindowExprData(this, key);
if (partition == null) { if (partition == null) {
// Window aggregates with FILTER clause may have no collected values // Window aggregates with FILTER clause may have no collected values
......
...@@ -185,18 +185,22 @@ public final class Window { ...@@ -185,18 +185,22 @@ public final class Window {
* session * session
* @return key for the current group, or null * @return key for the current group, or null
*/ */
public ValueArray getCurrentKey(Session session) { public Value getCurrentKey(Session session) {
if (partitionBy == null) { if (partitionBy == null) {
return null; return null;
} }
int len = partitionBy.size(); int len = partitionBy.size();
Value[] keyValues = new Value[len]; if (len == 1) {
// update group return partitionBy.get(0).getValue(session);
for (int i = 0; i < len; i++) { } else {
Expression expr = partitionBy.get(i); Value[] keyValues = new Value[len];
keyValues[i] = expr.getValue(session); // update group
for (int i = 0; i < len; i++) {
Expression expr = partitionBy.get(i);
keyValues[i] = expr.getValue(session);
}
return ValueArray.get(keyValues);
} }
return ValueArray.get(keyValues);
} }
/** /**
......
...@@ -133,7 +133,7 @@ INSERT INTO TEST VALUES ...@@ -133,7 +133,7 @@ INSERT INTO TEST VALUES
(4, 'b', 8); (4, 'b', 8);
> update count: 4 > update count: 4
SELECT ROW_NUMBER() OVER(ORDER /**/ BY TYPE) RN, TYPE, SUM(CNT) SUM FROM TEST GROUP BY TYPE; SELECT ROW_NUMBER() OVER (ORDER /**/ BY TYPE) RN, TYPE, SUM(CNT) SUM FROM TEST GROUP BY TYPE;
> RN TYPE SUM > RN TYPE SUM
> -- ---- --- > -- ---- ---
> 1 a 1 > 1 a 1
...@@ -141,6 +141,16 @@ SELECT ROW_NUMBER() OVER(ORDER /**/ BY TYPE) RN, TYPE, SUM(CNT) SUM FROM TEST GR ...@@ -141,6 +141,16 @@ SELECT ROW_NUMBER() OVER(ORDER /**/ BY TYPE) RN, TYPE, SUM(CNT) SUM FROM TEST GR
> 3 c 4 > 3 c 4
> rows: 3 > rows: 3
SELECT A, B, C, ROW_NUMBER() OVER (PARTITION BY A, B) N FROM
VALUES (1, 1, 1), (1, 1, 2), (1, 2, 3), (2, 1, 4) T(A, B, C);
> A B C N
> - - - -
> 1 1 1 1
> 1 1 2 2
> 1 2 3 1
> 2 1 4 1
> rows: 4
SELECT RANK () OVER () FROM TEST; SELECT RANK () OVER () FROM TEST;
> exception SYNTAX_ERROR_2 > exception SYNTAX_ERROR_2
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论