Skip to content
项目
群组
代码片段
帮助
正在加载...
帮助
为 GitLab 提交贡献
登录/注册
切换导航
H
h2database
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
分枝图
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
计划
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
分枝图
统计图
创建新议题
作业
提交
议题看板
打开侧边栏
Administrator
h2database
Commits
279be7f8
提交
279be7f8
authored
1月 13, 2019
作者:
Evgenij Ryazanov
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add RATIO_TO_REPORT window function
上级
54d25b13
显示空白字符变更
内嵌
并排
正在显示
4 个修改的文件
包含
61 行增加
和
1 行删除
+61
-1
help.csv
h2/src/docsrc/help/help.csv
+13
-0
WindowFunction.java
h2/src/main/org/h2/expression/analysis/WindowFunction.java
+40
-0
WindowFunctionType.java
...c/main/org/h2/expression/analysis/WindowFunctionType.java
+7
-0
TestScript.java
h2/src/test/org/h2/test/scripts/TestScript.java
+1
-1
没有找到文件。
h2/src/docsrc/help/help.csv
浏览文件 @
279be7f8
...
@@ -5547,6 +5547,19 @@ SELECT NTH_VALUE(X) IGNORE NULLS OVER (
...
@@ -5547,6 +5547,19 @@ SELECT NTH_VALUE(X) IGNORE NULLS OVER (
), * FROM TEST;
), * FROM TEST;
"
"
"Functions (Window)","RATIO_TO_REPORT","
RATIO_TO_REPORT(value)
OVER windowNameOrSpecification
","
Returns the ratio of a value to the sum of all values.
Is argument is NULL or sum of all values is 0, then the value of function is NULL.
Window functions are currently experimental in H2 and should be used with caution.
They also may require a lot of memory for large queries.
","
SELECT X, RATIO_TO_REPORT(X) OVER (PARTITION BY CATEGORY), CATEGORY FROM TEST;
"
"System Tables","Information Schema","
"System Tables","Information Schema","
INFORMATION_SCHEMA
INFORMATION_SCHEMA
","
","
...
...
h2/src/main/org/h2/expression/analysis/WindowFunction.java
浏览文件 @
279be7f8
...
@@ -48,6 +48,7 @@ public class WindowFunction extends DataAnalysisOperation {
...
@@ -48,6 +48,7 @@ public class WindowFunction extends DataAnalysisOperation {
case
LAG:
case
LAG:
case
FIRST_VALUE:
case
FIRST_VALUE:
case
LAST_VALUE:
case
LAST_VALUE:
case
RATIO_TO_REPORT:
return
1
;
return
1
;
case
NTH_VALUE:
case
NTH_VALUE:
return
2
;
return
2
;
...
@@ -68,6 +69,7 @@ public class WindowFunction extends DataAnalysisOperation {
...
@@ -68,6 +69,7 @@ public class WindowFunction extends DataAnalysisOperation {
case
NTILE:
case
NTILE:
case
FIRST_VALUE:
case
FIRST_VALUE:
case
LAST_VALUE:
case
LAST_VALUE:
case
RATIO_TO_REPORT:
return
1
;
return
1
;
case
LEAD:
case
LEAD:
case
LAG:
case
LAG:
...
@@ -207,6 +209,9 @@ public class WindowFunction extends DataAnalysisOperation {
...
@@ -207,6 +209,9 @@ public class WindowFunction extends DataAnalysisOperation {
case
NTH_VALUE:
case
NTH_VALUE:
getNth
(
session
,
result
,
ordered
,
rowIdColumn
);
getNth
(
session
,
result
,
ordered
,
rowIdColumn
);
break
;
break
;
case
RATIO_TO_REPORT:
getRatioToReport
(
session
,
result
,
ordered
,
rowIdColumn
);
break
;
default
:
default
:
throw
DbException
.
throwInternalError
(
"type="
+
type
);
throw
DbException
.
throwInternalError
(
"type="
+
type
);
}
}
...
@@ -375,6 +380,38 @@ public class WindowFunction extends DataAnalysisOperation {
...
@@ -375,6 +380,38 @@ public class WindowFunction extends DataAnalysisOperation {
}
}
}
}
private
static
void
getRatioToReport
(
Session
session
,
HashMap
<
Integer
,
Value
>
result
,
ArrayList
<
Value
[]>
ordered
,
int
rowIdColumn
)
{
int
size
=
ordered
.
size
();
Value
value
=
null
;
for
(
int
i
=
0
;
i
<
size
;
i
++)
{
Value
v
=
ordered
.
get
(
i
)[
0
];
if
(
v
!=
ValueNull
.
INSTANCE
)
{
if
(
value
==
null
)
{
value
=
v
.
convertTo
(
Value
.
DOUBLE
);
}
else
{
value
=
value
.
add
(
v
.
convertTo
(
Value
.
DOUBLE
));
}
}
}
if
(
value
!=
null
&&
value
.
getSignum
()
==
0
)
{
value
=
null
;
}
for
(
int
i
=
0
;
i
<
size
;
i
++)
{
Value
[]
row
=
ordered
.
get
(
i
);
Value
v
;
if
(
value
==
null
)
{
v
=
ValueNull
.
INSTANCE
;
}
else
{
v
=
row
[
0
];
if
(
v
!=
ValueNull
.
INSTANCE
)
{
v
=
v
.
convertTo
(
Value
.
DOUBLE
).
divide
(
value
);
}
}
result
.
put
(
row
[
rowIdColumn
].
getInt
(),
v
);
}
}
@Override
@Override
protected
Value
getAggregatedValue
(
Session
session
,
Object
aggregateData
)
{
protected
Value
getAggregatedValue
(
Session
session
,
Object
aggregateData
)
{
throw
DbException
.
getUnsupportedException
(
"Window function"
);
throw
DbException
.
getUnsupportedException
(
"Window function"
);
...
@@ -444,6 +481,7 @@ public class WindowFunction extends DataAnalysisOperation {
...
@@ -444,6 +481,7 @@ public class WindowFunction extends DataAnalysisOperation {
return
Value
.
LONG
;
return
Value
.
LONG
;
case
PERCENT_RANK:
case
PERCENT_RANK:
case
CUME_DIST:
case
CUME_DIST:
case
RATIO_TO_REPORT:
return
Value
.
DOUBLE
;
return
Value
.
DOUBLE
;
case
LEAD:
case
LEAD:
case
LAG:
case
LAG:
...
@@ -480,6 +518,7 @@ public class WindowFunction extends DataAnalysisOperation {
...
@@ -480,6 +518,7 @@ public class WindowFunction extends DataAnalysisOperation {
return
ValueLong
.
PRECISION
;
return
ValueLong
.
PRECISION
;
case
PERCENT_RANK:
case
PERCENT_RANK:
case
CUME_DIST:
case
CUME_DIST:
case
RATIO_TO_REPORT:
return
ValueDouble
.
PRECISION
;
return
ValueDouble
.
PRECISION
;
case
LEAD:
case
LEAD:
case
LAG:
case
LAG:
...
@@ -502,6 +541,7 @@ public class WindowFunction extends DataAnalysisOperation {
...
@@ -502,6 +541,7 @@ public class WindowFunction extends DataAnalysisOperation {
return
ValueLong
.
DISPLAY_SIZE
;
return
ValueLong
.
DISPLAY_SIZE
;
case
PERCENT_RANK:
case
PERCENT_RANK:
case
CUME_DIST:
case
CUME_DIST:
case
RATIO_TO_REPORT:
return
ValueDouble
.
DISPLAY_SIZE
;
return
ValueDouble
.
DISPLAY_SIZE
;
case
LEAD:
case
LEAD:
case
LAG:
case
LAG:
...
...
h2/src/main/org/h2/expression/analysis/WindowFunctionType.java
浏览文件 @
279be7f8
...
@@ -65,6 +65,11 @@ public enum WindowFunctionType {
...
@@ -65,6 +65,11 @@ public enum WindowFunctionType {
*/
*/
NTH_VALUE
,
NTH_VALUE
,
/**
* The type for RATIO_TO_REPORT() window function.
*/
RATIO_TO_REPORT
,
;
;
/**
/**
...
@@ -98,6 +103,8 @@ public enum WindowFunctionType {
...
@@ -98,6 +103,8 @@ public enum WindowFunctionType {
return
LAST_VALUE
;
return
LAST_VALUE
;
case
"NTH_VALUE"
:
case
"NTH_VALUE"
:
return
NTH_VALUE
;
return
NTH_VALUE
;
case
"RATIO_TO_REPORT"
:
return
RATIO_TO_REPORT
;
default
:
default
:
return
null
;
return
null
;
}
}
...
...
h2/src/test/org/h2/test/scripts/TestScript.java
浏览文件 @
279be7f8
...
@@ -204,7 +204,7 @@ public class TestScript extends TestDb {
...
@@ -204,7 +204,7 @@ public class TestScript extends TestDb {
"parsedatetime"
,
"quarter"
,
"second"
,
"truncate"
,
"week"
,
"year"
,
"date_trunc"
})
{
"parsedatetime"
,
"quarter"
,
"second"
,
"truncate"
,
"week"
,
"year"
,
"date_trunc"
})
{
testScript
(
"functions/timeanddate/"
+
s
+
".sql"
);
testScript
(
"functions/timeanddate/"
+
s
+
".sql"
);
}
}
for
(
String
s
:
new
String
[]
{
"lead"
,
"nth_value"
,
"ntile"
,
"row_number"
})
{
for
(
String
s
:
new
String
[]
{
"lead"
,
"nth_value"
,
"ntile"
,
"r
atio_to_report"
,
"r
ow_number"
})
{
testScript
(
"functions/window/"
+
s
+
".sql"
);
testScript
(
"functions/window/"
+
s
+
".sql"
);
}
}
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论