Skip to content
项目
群组
代码片段
帮助
正在加载...
帮助
为 GitLab 提交贡献
登录/注册
切换导航
H
h2database
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
分枝图
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
计划
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
分枝图
统计图
创建新议题
作业
提交
议题看板
打开侧边栏
Administrator
h2database
Commits
5ea4c890
提交
5ea4c890
authored
10月 02, 2018
作者:
Evgenij Ryazanov
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Fix window functions with order but without frame
上级
04415644
显示空白字符变更
内嵌
并排
正在显示
6 个修改的文件
包含
63 行增加
和
46 行删除
+63
-46
AbstractAggregate.java
...c/main/org/h2/expression/aggregate/AbstractAggregate.java
+28
-26
Window.java
h2/src/main/org/h2/expression/aggregate/Window.java
+1
-1
WindowFrame.java
h2/src/main/org/h2/expression/aggregate/WindowFrame.java
+11
-15
WindowFunction.java
h2/src/main/org/h2/expression/aggregate/WindowFunction.java
+3
-4
count.sql
...rc/test/org/h2/test/scripts/functions/aggregate/count.sql
+10
-0
nth_value.sql
...c/test/org/h2/test/scripts/functions/window/nth_value.sql
+10
-0
没有找到文件。
h2/src/main/org/h2/expression/aggregate/AbstractAggregate.java
浏览文件 @
5ea4c890
...
@@ -75,37 +75,39 @@ public abstract class AbstractAggregate extends DataAnalysisOperation {
...
@@ -75,37 +75,39 @@ public abstract class AbstractAggregate extends DataAnalysisOperation {
protected
void
getOrderedResultLoop
(
Session
session
,
HashMap
<
Integer
,
Value
>
result
,
ArrayList
<
Value
[]>
ordered
,
protected
void
getOrderedResultLoop
(
Session
session
,
HashMap
<
Integer
,
Value
>
result
,
ArrayList
<
Value
[]>
ordered
,
int
rowIdColumn
)
{
int
rowIdColumn
)
{
WindowFrame
frame
=
over
.
getWindowFrame
();
WindowFrame
frame
=
over
.
getWindowFrame
();
if
(
frame
==
null
||
frame
.
isDefault
())
{
if
(
frame
==
null
)
{
// Aggregate all values before the current row (including)
if
(
over
.
getOrderBy
()
==
null
)
{
Object
aggregateData
=
createAggregateData
();
aggregateWholePartition
(
session
,
result
,
ordered
,
rowIdColumn
);
for
(
Value
[]
row
:
ordered
)
{
return
;
// Collect values one by one
updateFromExpressions
(
session
,
aggregateData
,
row
);
result
.
put
(
row
[
rowIdColumn
].
getInt
(),
getAggregatedValue
(
session
,
aggregateData
));
}
}
}
else
if
(
frame
.
isFullPartition
())
{
}
else
if
(
frame
.
isFullPartition
())
{
// Aggregate values from the whole partition
aggregateWholePartition
(
session
,
result
,
ordered
,
rowIdColumn
);
Object
aggregateData
=
createAggregateData
();
return
;
for
(
Value
[]
row
:
ordered
)
{
updateFromExpressions
(
session
,
aggregateData
,
row
);
}
}
// All rows have the same value
Value
value
=
getAggregatedValue
(
session
,
aggregateData
);
for
(
Value
[]
row
:
ordered
)
{
result
.
put
(
row
[
rowIdColumn
].
getInt
(),
value
);
}
}
else
{
// All other types of frames (slow)
// All other types of frames (slow)
int
size
=
ordered
.
size
();
int
size
=
ordered
.
size
();
for
(
int
i
=
0
;
i
<
size
;
i
++)
{
for
(
int
i
=
0
;
i
<
size
;
i
++)
{
Object
aggregateData
=
createAggregateData
();
Object
aggregateData
=
createAggregateData
();
for
(
Iterator
<
Value
[]>
iter
=
frame
.
iterator
(
session
,
ordered
,
getOverOrderBySort
(),
i
,
false
);
iter
for
(
Iterator
<
Value
[]>
iter
=
WindowFrame
.
iterator
(
over
,
session
,
ordered
,
getOverOrderBySort
(),
i
,
.
hasNext
();)
{
false
);
iter
.
hasNext
();)
{
updateFromExpressions
(
session
,
aggregateData
,
iter
.
next
());
updateFromExpressions
(
session
,
aggregateData
,
iter
.
next
());
}
}
result
.
put
(
ordered
.
get
(
i
)[
rowIdColumn
].
getInt
(),
getAggregatedValue
(
session
,
aggregateData
));
result
.
put
(
ordered
.
get
(
i
)[
rowIdColumn
].
getInt
(),
getAggregatedValue
(
session
,
aggregateData
));
}
}
}
}
private
void
aggregateWholePartition
(
Session
session
,
HashMap
<
Integer
,
Value
>
result
,
ArrayList
<
Value
[]>
ordered
,
int
rowIdColumn
)
{
// Aggregate values from the whole partition
Object
aggregateData
=
createAggregateData
();
for
(
Value
[]
row
:
ordered
)
{
updateFromExpressions
(
session
,
aggregateData
,
row
);
}
// All rows have the same value
Value
value
=
getAggregatedValue
(
session
,
aggregateData
);
for
(
Value
[]
row
:
ordered
)
{
result
.
put
(
row
[
rowIdColumn
].
getInt
(),
value
);
}
}
}
/**
/**
...
...
h2/src/main/org/h2/expression/aggregate/Window.java
浏览文件 @
5ea4c890
...
@@ -224,7 +224,7 @@ public final class Window {
...
@@ -224,7 +224,7 @@ public final class Window {
}
}
}
}
appendOrderBy
(
builder
,
orderBy
);
appendOrderBy
(
builder
,
orderBy
);
if
(
frame
!=
null
&&
!
frame
.
isDefault
()
)
{
if
(
frame
!=
null
)
{
if
(
builder
.
charAt
(
builder
.
length
()
-
1
)
!=
'('
)
{
if
(
builder
.
charAt
(
builder
.
length
()
-
1
)
!=
'('
)
{
builder
.
append
(
' '
);
builder
.
append
(
' '
);
}
}
...
...
h2/src/main/org/h2/expression/aggregate/WindowFrame.java
浏览文件 @
5ea4c890
...
@@ -192,8 +192,8 @@ public final class WindowFrame {
...
@@ -192,8 +192,8 @@ public final class WindowFrame {
* Returns iterator for the specified frame, or default iterator if frame is
* Returns iterator for the specified frame, or default iterator if frame is
* null.
* null.
*
*
* @param
frame
* @param
over
* window
frame, or null
* window
* @param session
* @param session
* the session
* the session
* @param orderedRows
* @param orderedRows
...
@@ -207,10 +207,16 @@ public final class WindowFrame {
...
@@ -207,10 +207,16 @@ public final class WindowFrame {
*
*
* @return iterator
* @return iterator
*/
*/
public
static
Iterator
<
Value
[]>
iterator
(
Window
Frame
frame
,
Session
session
,
ArrayList
<
Value
[]>
orderedRows
,
public
static
Iterator
<
Value
[]>
iterator
(
Window
over
,
Session
session
,
ArrayList
<
Value
[]>
orderedRows
,
SortOrder
sortOrder
,
int
currentRow
,
boolean
reverse
)
{
SortOrder
sortOrder
,
int
currentRow
,
boolean
reverse
)
{
return
frame
!=
null
?
frame
.
iterator
(
session
,
orderedRows
,
sortOrder
,
currentRow
,
reverse
)
WindowFrame
frame
=
over
.
getWindowFrame
();
:
plainIterator
(
orderedRows
,
0
,
currentRow
,
reverse
);
if
(
frame
!=
null
)
{
return
frame
.
iterator
(
session
,
orderedRows
,
sortOrder
,
currentRow
,
reverse
);
}
int
endIndex
=
orderedRows
.
size
()
-
1
;
return
plainIterator
(
orderedRows
,
0
,
over
.
getOrderBy
()
==
null
?
endIndex
:
toGroupEnd
(
orderedRows
,
sortOrder
,
currentRow
,
endIndex
),
reverse
);
}
}
private
static
Iterator
<
Value
[]>
plainIterator
(
ArrayList
<
Value
[]>
orderedRows
,
int
startIndex
,
int
endIndex
,
private
static
Iterator
<
Value
[]>
plainIterator
(
ArrayList
<
Value
[]>
orderedRows
,
int
startIndex
,
int
endIndex
,
...
@@ -314,16 +320,6 @@ public final class WindowFrame {
...
@@ -314,16 +320,6 @@ public final class WindowFrame {
&&
s
.
compareTo
(
f
)
<=
0
;
&&
s
.
compareTo
(
f
)
<=
0
;
}
}
/**
* Returns whether window frame specification can be omitted.
*
* @return whether window frame specification can be omitted
*/
public
boolean
isDefault
()
{
return
starting
.
getType
()
==
WindowFrameBoundType
.
UNBOUNDED_PRECEDING
&&
following
==
null
&&
exclusion
==
WindowFrameExclusion
.
EXCLUDE_NO_OTHERS
;
}
/**
/**
* Returns whether window frame specification contains all rows in
* Returns whether window frame specification contains all rows in
* partition.
* partition.
...
...
h2/src/main/org/h2/expression/aggregate/WindowFunction.java
浏览文件 @
5ea4c890
...
@@ -345,17 +345,16 @@ public class WindowFunction extends DataAnalysisOperation {
...
@@ -345,17 +345,16 @@ public class WindowFunction extends DataAnalysisOperation {
private
void
getNth
(
Session
session
,
HashMap
<
Integer
,
Value
>
result
,
ArrayList
<
Value
[]>
ordered
,
int
rowIdColumn
)
{
private
void
getNth
(
Session
session
,
HashMap
<
Integer
,
Value
>
result
,
ArrayList
<
Value
[]>
ordered
,
int
rowIdColumn
)
{
int
size
=
ordered
.
size
();
int
size
=
ordered
.
size
();
for
(
int
i
=
0
;
i
<
size
;
i
++)
{
for
(
int
i
=
0
;
i
<
size
;
i
++)
{
WindowFrame
frame
=
over
.
getWindowFrame
();
Value
[]
row
=
ordered
.
get
(
i
);
Value
[]
row
=
ordered
.
get
(
i
);
int
rowId
=
row
[
rowIdColumn
].
getInt
();
int
rowId
=
row
[
rowIdColumn
].
getInt
();
Value
v
;
Value
v
;
switch
(
type
)
{
switch
(
type
)
{
case
FIRST_VALUE:
case
FIRST_VALUE:
v
=
getNthValue
(
WindowFrame
.
iterator
(
frame
,
session
,
ordered
,
getOverOrderBySort
(),
i
,
false
),
0
,
v
=
getNthValue
(
WindowFrame
.
iterator
(
over
,
session
,
ordered
,
getOverOrderBySort
(),
i
,
false
),
0
,
ignoreNulls
);
ignoreNulls
);
break
;
break
;
case
LAST_VALUE:
case
LAST_VALUE:
v
=
getNthValue
(
WindowFrame
.
iterator
(
frame
,
session
,
ordered
,
getOverOrderBySort
(),
i
,
true
),
0
,
v
=
getNthValue
(
WindowFrame
.
iterator
(
over
,
session
,
ordered
,
getOverOrderBySort
(),
i
,
true
),
0
,
ignoreNulls
);
ignoreNulls
);
break
;
break
;
case
NTH_VALUE:
{
case
NTH_VALUE:
{
...
@@ -364,7 +363,7 @@ public class WindowFunction extends DataAnalysisOperation {
...
@@ -364,7 +363,7 @@ public class WindowFunction extends DataAnalysisOperation {
throw
DbException
.
getInvalidValueException
(
"nth row"
,
n
);
throw
DbException
.
getInvalidValueException
(
"nth row"
,
n
);
}
}
n
--;
n
--;
Iterator
<
Value
[]>
iter
=
WindowFrame
.
iterator
(
frame
,
session
,
ordered
,
getOverOrderBySort
(),
i
,
Iterator
<
Value
[]>
iter
=
WindowFrame
.
iterator
(
over
,
session
,
ordered
,
getOverOrderBySort
(),
i
,
fromLast
);
fromLast
);
v
=
getNthValue
(
iter
,
n
,
ignoreNulls
);
v
=
getNthValue
(
iter
,
n
,
ignoreNulls
);
break
;
break
;
...
...
h2/src/test/org/h2/test/scripts/functions/aggregate/count.sql
浏览文件 @
5ea4c890
...
@@ -94,3 +94,13 @@ SELECT I, C, COUNT(I) OVER (PARTITION BY C) CNT FROM
...
@@ -94,3 +94,13 @@ SELECT I, C, COUNT(I) OVER (PARTITION BY C) CNT FROM
>
4
2
3
>
4
2
3
>
5
2
3
>
5
2
3
>
rows
:
5
>
rows
:
5
SELECT
X
,
COUNT
(
*
)
OVER
(
ORDER
BY
X
)
C
FROM
VALUES
(
1
),
(
1
),
(
2
),
(
2
),
(
3
)
V
(
X
);
>
X
C
>
-
-
>
1
2
>
1
2
>
2
4
>
2
4
>
3
5
>
rows
:
5
h2/src/test/org/h2/test/scripts/functions/window/nth_value.sql
浏览文件 @
5ea4c890
...
@@ -219,3 +219,13 @@ SELECT ID, CATEGORY,
...
@@ -219,3 +219,13 @@ SELECT ID, CATEGORY,
DROP
TABLE
TEST
;
DROP
TABLE
TEST
;
>
ok
>
ok
SELECT
I
,
X
,
LAST_VALUE
(
I
)
OVER
(
ORDER
BY
X
)
L
FROM
VALUES
(
1
,
1
),
(
2
,
1
),
(
3
,
2
),
(
4
,
2
),
(
5
,
3
)
V
(
I
,
X
);
>
I
X
L
>
-
-
-
>
1
1
2
>
2
1
2
>
3
2
4
>
4
2
4
>
5
3
5
>
rows
:
5
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论