Skip to content
项目
群组
代码片段
帮助
正在加载...
帮助
为 GitLab 提交贡献
登录/注册
切换导航
H
h2database
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
分枝图
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
计划
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
分枝图
统计图
创建新议题
作业
提交
议题看板
打开侧边栏
Administrator
h2database
Commits
35b50ffc
提交
35b50ffc
authored
9月 17, 2018
作者:
Evgenij Ryazanov
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add initial implementation of FIRST_VALUE(), LAST_VALUE(), and NTH_VALUE()
上级
bd2fb602
隐藏空白字符变更
内嵌
并排
正在显示
4 个修改的文件
包含
237 行增加
和
12 行删除
+237
-12
Parser.java
h2/src/main/org/h2/command/Parser.java
+12
-1
WindowFunction.java
h2/src/main/org/h2/expression/aggregate/WindowFunction.java
+160
-10
TestScript.java
h2/src/test/org/h2/test/scripts/TestScript.java
+1
-1
nth_value.sql
...c/test/org/h2/test/scripts/functions/window/nth_value.sql
+64
-0
没有找到文件。
h2/src/main/org/h2/command/Parser.java
浏览文件 @
35b50ffc
...
...
@@ -3270,8 +3270,19 @@ public class Parser {
if
(
currentSelect
==
null
)
{
throw
getSyntaxError
();
}
int
numArgs
=
WindowFunction
.
getArgumentCount
(
type
);
Expression
[]
args
=
null
;
if
(
numArgs
>
0
)
{
args
=
new
Expression
[
numArgs
];
for
(
int
i
=
0
;
i
<
numArgs
;
i
++)
{
if
(
i
>
0
)
{
read
(
COMMA
);
}
args
[
i
]
=
readExpression
();
}
}
read
(
CLOSE_PAREN
);
WindowFunction
function
=
new
WindowFunction
(
type
,
currentSelect
);
WindowFunction
function
=
new
WindowFunction
(
type
,
currentSelect
,
args
);
readFilterAndOver
(
function
);
return
function
;
}
...
...
h2/src/main/org/h2/expression/aggregate/WindowFunction.java
浏览文件 @
35b50ffc
...
...
@@ -10,10 +10,14 @@ import java.util.HashMap;
import
org.h2.command.dml.Select
;
import
org.h2.engine.Session
;
import
org.h2.expression.Expression
;
import
org.h2.message.DbException
;
import
org.h2.table.ColumnResolver
;
import
org.h2.table.TableFilter
;
import
org.h2.value.Value
;
import
org.h2.value.ValueDouble
;
import
org.h2.value.ValueInt
;
import
org.h2.value.ValueNull
;
/**
* A window function.
...
...
@@ -50,6 +54,21 @@ public class WindowFunction extends AbstractAggregate {
*/
CUME_DIST
,
/**
* The type for FIRST_VALUE() window function.
*/
FIRST_VALUE
,
/**
* The type for LAST_VALUE() window function.
*/
LAST_VALUE
,
/**
* The type for NTH_VALUE() window function.
*/
NTH_VALUE
,
;
/**
...
...
@@ -62,15 +81,21 @@ public class WindowFunction extends AbstractAggregate {
public
static
WindowFunctionType
get
(
String
name
)
{
switch
(
name
)
{
case
"ROW_NUMBER"
:
return
WindowFunctionType
.
ROW_NUMBER
;
return
ROW_NUMBER
;
case
"RANK"
:
return
RANK
;
case
"DENSE_RANK"
:
return
WindowFunctionType
.
DENSE_RANK
;
return
DENSE_RANK
;
case
"PERCENT_RANK"
:
return
WindowFunctionType
.
PERCENT_RANK
;
return
PERCENT_RANK
;
case
"CUME_DIST"
:
return
WindowFunctionType
.
CUME_DIST
;
return
CUME_DIST
;
case
"FIRST_VALUE"
:
return
FIRST_VALUE
;
case
"LAST_VALUE"
:
return
LAST_VALUE
;
case
"NTH_VALUE"
:
return
NTH_VALUE
;
default
:
return
null
;
}
...
...
@@ -80,6 +105,27 @@ public class WindowFunction extends AbstractAggregate {
private
final
WindowFunctionType
type
;
private
final
Expression
[]
args
;
/**
* Returns number of arguments for the specified type.
*
* @param type
* the type of a window function
* @return number of arguments
*/
public
static
int
getArgumentCount
(
WindowFunctionType
type
)
{
switch
(
type
)
{
case
FIRST_VALUE:
case
LAST_VALUE:
return
1
;
case
NTH_VALUE:
return
2
;
default
:
return
0
;
}
}
/**
* Creates new instance of a window function.
*
...
...
@@ -87,10 +133,13 @@ public class WindowFunction extends AbstractAggregate {
* the type
* @param select
* the select statement
* @param args
* arguments, or null
*/
public
WindowFunction
(
WindowFunctionType
type
,
Select
select
)
{
public
WindowFunction
(
WindowFunctionType
type
,
Select
select
,
Expression
[]
args
)
{
super
(
select
,
false
);
this
.
type
=
type
;
this
.
args
=
args
;
}
@Override
...
...
@@ -105,17 +154,24 @@ public class WindowFunction extends AbstractAggregate {
@Override
protected
void
updateGroupAggregates
(
Session
session
,
int
stage
)
{
// Nothing to do
if
(
args
!=
null
)
{
for
(
Expression
expr
:
args
)
{
expr
.
updateAggregate
(
session
,
stage
);
}
}
}
@Override
protected
int
getNumExpressions
()
{
return
0
;
return
getArgumentCount
(
type
)
;
}
@Override
protected
void
rememberExpressions
(
Session
session
,
Value
[]
array
)
{
// Nothing to do
int
cnt
=
getNumExpressions
();
for
(
int
i
=
0
;
i
<
cnt
;
i
++)
{
array
[
i
]
=
args
[
i
].
getValue
(
session
);
}
}
@Override
...
...
@@ -175,6 +231,25 @@ public class WindowFunction extends AbstractAggregate {
v
=
ValueDouble
.
get
((
double
)
nm
/
size
);
break
;
}
case
FIRST_VALUE:
v
=
ordered
.
get
(
0
)[
0
];
break
;
case
LAST_VALUE:
v
=
row
[
0
];
break
;
case
NTH_VALUE:
{
int
n
=
row
[
1
].
getInt
();
if
(
n
<=
0
)
{
throw
DbException
.
getInvalidValueException
(
"nth row"
,
n
);
}
n
--;
if
(
n
<
0
||
n
>
i
)
{
v
=
ValueNull
.
INSTANCE
;
}
else
{
v
=
ordered
.
get
(
n
)[
0
];
}
break
;
}
default
:
throw
DbException
.
throwInternalError
(
"type="
+
type
);
}
...
...
@@ -205,6 +280,37 @@ public class WindowFunction extends AbstractAggregate {
throw
DbException
.
getUnsupportedException
(
"Window function"
);
}
@Override
public
void
mapColumns
(
ColumnResolver
resolver
,
int
level
)
{
if
(
args
!=
null
)
{
for
(
Expression
arg
:
args
)
{
arg
.
mapColumns
(
resolver
,
level
);
}
}
super
.
mapColumns
(
resolver
,
level
);
}
@Override
public
Expression
optimize
(
Session
session
)
{
super
.
optimize
(
session
);
if
(
args
!=
null
)
{
for
(
int
i
=
0
;
i
<
args
.
length
;
i
++)
{
args
[
i
]
=
args
[
i
].
optimize
(
session
);
}
}
return
this
;
}
@Override
public
void
setEvaluatable
(
TableFilter
tableFilter
,
boolean
b
)
{
if
(
args
!=
null
)
{
for
(
Expression
e
:
args
)
{
e
.
setEvaluatable
(
tableFilter
,
b
);
}
}
super
.
setEvaluatable
(
tableFilter
,
b
);
}
@Override
public
int
getType
()
{
switch
(
type
)
{
...
...
@@ -215,6 +321,10 @@ public class WindowFunction extends AbstractAggregate {
case
PERCENT_RANK:
case
CUME_DIST:
return
Value
.
DOUBLE
;
case
FIRST_VALUE:
case
LAST_VALUE:
case
NTH_VALUE:
return
args
[
0
].
getType
();
default
:
throw
DbException
.
throwInternalError
(
"type="
+
type
);
}
...
...
@@ -222,7 +332,14 @@ public class WindowFunction extends AbstractAggregate {
@Override
public
int
getScale
()
{
return
0
;
switch
(
type
)
{
case
FIRST_VALUE:
case
LAST_VALUE:
case
NTH_VALUE:
return
args
[
0
].
getScale
();
default
:
return
0
;
}
}
@Override
...
...
@@ -235,6 +352,10 @@ public class WindowFunction extends AbstractAggregate {
case
PERCENT_RANK:
case
CUME_DIST:
return
ValueDouble
.
PRECISION
;
case
FIRST_VALUE:
case
LAST_VALUE:
case
NTH_VALUE:
return
args
[
0
].
getPrecision
();
default
:
throw
DbException
.
throwInternalError
(
"type="
+
type
);
}
...
...
@@ -250,6 +371,10 @@ public class WindowFunction extends AbstractAggregate {
case
PERCENT_RANK:
case
CUME_DIST:
return
ValueDouble
.
DISPLAY_SIZE
;
case
FIRST_VALUE:
case
LAST_VALUE:
case
NTH_VALUE:
return
args
[
0
].
getDisplaySize
();
default
:
throw
DbException
.
throwInternalError
(
"type="
+
type
);
}
...
...
@@ -258,6 +383,7 @@ public class WindowFunction extends AbstractAggregate {
@Override
public
String
getSQL
()
{
String
text
;
int
numArgs
=
0
;
switch
(
type
)
{
case
ROW_NUMBER:
text
=
"ROW_NUMBER"
;
...
...
@@ -274,16 +400,40 @@ public class WindowFunction extends AbstractAggregate {
case
CUME_DIST:
text
=
"CUME_DIST"
;
break
;
case
FIRST_VALUE:
text
=
"FIRST_VALUE"
;
numArgs
=
1
;
break
;
case
LAST_VALUE:
text
=
"LAST_VALUE"
;
numArgs
=
1
;
break
;
case
NTH_VALUE:
text
=
"NTH_VALUE"
;
numArgs
=
2
;
break
;
default
:
throw
DbException
.
throwInternalError
(
"type="
+
type
);
}
StringBuilder
builder
=
new
StringBuilder
().
append
(
text
).
append
(
"()"
);
StringBuilder
builder
=
new
StringBuilder
().
append
(
text
).
append
(
'('
);
for
(
int
i
=
0
;
i
<
numArgs
;
i
++)
{
if
(
i
>
0
)
{
builder
.
append
(
", "
);
}
builder
.
append
(
args
[
i
].
getSQL
());
}
builder
.
append
(
')'
);
return
appendTailConditions
(
builder
).
toString
();
}
@Override
public
int
getCost
()
{
int
cost
=
1
;
if
(
args
!=
null
)
{
for
(
Expression
expr
:
args
)
{
cost
+=
expr
.
getCost
();
}
}
return
cost
;
}
...
...
h2/src/test/org/h2/test/scripts/TestScript.java
浏览文件 @
35b50ffc
...
...
@@ -179,7 +179,7 @@ public class TestScript extends TestDb {
"parsedatetime"
,
"quarter"
,
"second"
,
"truncate"
,
"week"
,
"year"
,
"date_trunc"
})
{
testScript
(
"functions/timeanddate/"
+
s
+
".sql"
);
}
for
(
String
s
:
new
String
[]
{
"row_number"
})
{
for
(
String
s
:
new
String
[]
{
"row_number"
,
"nth_value"
})
{
testScript
(
"functions/window/"
+
s
+
".sql"
);
}
...
...
h2/src/test/org/h2/test/scripts/functions/window/nth_value.sql
0 → 100644
浏览文件 @
35b50ffc
-- Copyright 2004-2018 H2 Group. Multiple-Licensed under the MPL 2.0,
-- and the EPL 1.0 (http://h2database.com/html/license.html).
-- Initial Developer: H2 Group
--
CREATE
TABLE
TEST
(
ID
INT
PRIMARY
KEY
,
CATEGORY
INT
,
VALUE
INT
);
>
ok
INSERT
INTO
TEST
VALUES
(
1
,
1
,
NULL
),
(
2
,
1
,
12
),
(
3
,
1
,
NULL
),
(
4
,
1
,
13
),
(
5
,
1
,
NULL
),
(
6
,
1
,
13
),
(
7
,
2
,
21
),
(
8
,
2
,
22
),
(
9
,
3
,
31
),
(
10
,
3
,
32
),
(
11
,
3
,
33
),
(
12
,
4
,
41
),
(
13
,
4
,
NULL
);
>
update
count
:
13
SELECT
*
,
FIRST_VALUE
(
VALUE
)
OVER
(
ORDER
BY
ID
)
FIRST
,
LAST_VALUE
(
VALUE
)
OVER
(
ORDER
BY
ID
)
LAST
,
NTH_VALUE
(
VALUE
,
2
)
OVER
(
ORDER
BY
ID
)
NTH
FROM
TEST
FETCH
FIRST
4
ROWS
ONLY
;
>
ID
CATEGORY
VALUE
FIRST
LAST
NTH
>
-- -------- ----- ----- ---- ----
>
1
1
null
null
null
null
>
2
1
12
null
12
12
>
3
1
null
null
null
12
>
4
1
13
null
13
12
>
rows
(
ordered
):
4
SELECT
NTH_VALUE
(
VALUE
,
0
)
OVER
(
ORDER
BY
ID
)
FROM
TEST
;
>
exception
INVALID_VALUE_2
SELECT
*
,
FIRST_VALUE
(
VALUE
)
OVER
(
PARTITION
BY
CATEGORY
ORDER
BY
ID
)
FIRST
,
LAST_VALUE
(
VALUE
)
OVER
(
PARTITION
BY
CATEGORY
ORDER
BY
ID
)
LAST
,
NTH_VALUE
(
VALUE
,
2
)
OVER
(
PARTITION
BY
CATEGORY
ORDER
BY
ID
)
NTH
FROM
TEST
;
>
ID
CATEGORY
VALUE
FIRST
LAST
NTH
>
-- -------- ----- ----- ---- ----
>
1
1
null
null
null
null
>
2
1
12
null
12
12
>
3
1
null
null
null
12
>
4
1
13
null
13
12
>
5
1
null
null
null
12
>
6
1
13
null
13
12
>
7
2
21
21
21
null
>
8
2
22
21
22
22
>
9
3
31
31
31
null
>
10
3
32
31
32
32
>
11
3
33
31
33
32
>
12
4
41
41
41
null
>
13
4
null
41
null
null
>
rows
(
ordered
):
13
DROP
TABLE
TEST
;
>
ok
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论