Skip to content
项目
群组
代码片段
帮助
正在加载...
帮助
为 GitLab 提交贡献
登录/注册
切换导航
H
h2database
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
分枝图
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
计划
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
分枝图
统计图
创建新议题
作业
提交
议题看板
打开侧边栏
Administrator
h2database
Commits
2a200352
提交
2a200352
authored
8月 28, 2018
作者:
Evgenij Ryazanov
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add MODE aggregate function
上级
57c070e7
隐藏空白字符变更
内嵌
并排
正在显示
7 个修改的文件
包含
133 行增加
和
23 行删除
+133
-23
help.csv
h2/src/docsrc/help/help.csv
+12
-0
Parser.java
h2/src/main/org/h2/command/Parser.java
+21
-21
Aggregate.java
h2/src/main/org/h2/expression/aggregate/Aggregate.java
+12
-1
AggregateData.java
h2/src/main/org/h2/expression/aggregate/AggregateData.java
+2
-0
AggregateDataMode.java
...c/main/org/h2/expression/aggregate/AggregateDataMode.java
+54
-0
TestScript.java
h2/src/test/org/h2/test/scripts/TestScript.java
+1
-1
mode.sql
h2/src/test/org/h2/test/scripts/functions/aggregate/mode.sql
+31
-0
没有找到文件。
h2/src/docsrc/help/help.csv
浏览文件 @
2a200352
...
...
@@ -3416,6 +3416,18 @@ Aggregates are only allowed in select statements.
MEDIAN(X)
"
"Functions (Aggregate)","MODE","
MODE( value ) [ FILTER ( WHERE expression ) ]
","
Returns the value that occurs with the greatest frequency.
If there are multiple values with the same frequency only one value will be returned.
NULL values are ignored in the calculation.
If no rows are selected, the result is NULL.
Aggregates are only allowed in select statements.
","
MODE(X)
"
"Functions (Numeric)","ABS","
ABS ( { numeric } )
","
...
...
h2/src/main/org/h2/command/Parser.java
浏览文件 @
2a200352
...
...
@@ -2916,40 +2916,35 @@ public class Parser {
}
currentSelect
.
setGroupQuery
();
Aggregate
r
;
if
(
aggregateType
==
AggregateType
.
COUNT
)
{
switch
(
aggregateType
)
{
case
COUNT:
if
(
readIf
(
ASTERISK
))
{
r
=
new
Aggregate
(
AggregateType
.
COUNT_ALL
,
null
,
currentSelect
,
false
);
r
=
new
Aggregate
(
AggregateType
.
COUNT_ALL
,
null
,
currentSelect
,
false
);
}
else
{
boolean
distinct
=
readIf
(
DISTINCT
);
Expression
on
=
readExpression
();
if
(
on
instanceof
Wildcard
&&
!
distinct
)
{
// PostgreSQL compatibility: count(t.*)
r
=
new
Aggregate
(
AggregateType
.
COUNT_ALL
,
null
,
currentSelect
,
false
);
r
=
new
Aggregate
(
AggregateType
.
COUNT_ALL
,
null
,
currentSelect
,
false
);
}
else
{
r
=
new
Aggregate
(
AggregateType
.
COUNT
,
on
,
currentSelect
,
distinct
);
r
=
new
Aggregate
(
AggregateType
.
COUNT
,
on
,
currentSelect
,
distinct
);
}
}
}
else
if
(
aggregateType
==
AggregateType
.
GROUP_CONCAT
)
{
break
;
case
GROUP_CONCAT:
{
boolean
distinct
=
readIf
(
DISTINCT
);
if
(
equalsToken
(
"GROUP_CONCAT"
,
aggregateName
))
{
r
=
new
Aggregate
(
AggregateType
.
GROUP_CONCAT
,
readExpression
(),
currentSelect
,
distinct
);
r
=
new
Aggregate
(
AggregateType
.
GROUP_CONCAT
,
readExpression
(),
currentSelect
,
distinct
);
if
(
readIf
(
ORDER
))
{
read
(
"BY"
);
r
.
setOrderByList
(
parseSimpleOrderList
());
}
if
(
readIf
(
"SEPARATOR"
))
{
r
.
setGroupConcatSeparator
(
readExpression
());
}
}
else
if
(
equalsToken
(
"STRING_AGG"
,
aggregateName
))
{
// PostgreSQL compatibility: string_agg(expression, delimiter)
r
=
new
Aggregate
(
AggregateType
.
GROUP_CONCAT
,
readExpression
(),
currentSelect
,
distinct
);
r
=
new
Aggregate
(
AggregateType
.
GROUP_CONCAT
,
readExpression
(),
currentSelect
,
distinct
);
read
(
COMMA
);
r
.
setGroupConcatSeparator
(
readExpression
());
if
(
readIf
(
ORDER
))
{
...
...
@@ -2959,19 +2954,24 @@ public class Parser {
}
else
{
r
=
null
;
}
}
else
if
(
aggregateType
==
AggregateType
.
ARRAY_AGG
)
{
break
;
}
case
ARRAY_AGG:
{
boolean
distinct
=
readIf
(
DISTINCT
);
r
=
new
Aggregate
(
AggregateType
.
ARRAY_AGG
,
readExpression
(),
currentSelect
,
distinct
);
r
=
new
Aggregate
(
AggregateType
.
ARRAY_AGG
,
readExpression
(),
currentSelect
,
distinct
);
if
(
readIf
(
ORDER
))
{
read
(
"BY"
);
r
.
setOrderByList
(
parseSimpleOrderList
());
}
}
else
{
break
;
}
case
MODE:
r
=
new
Aggregate
(
aggregateType
,
readExpression
(),
currentSelect
,
false
);
break
;
default
:
boolean
distinct
=
readIf
(
DISTINCT
);
r
=
new
Aggregate
(
aggregateType
,
readExpression
(),
currentSelect
,
distinct
)
;
r
=
new
Aggregate
(
aggregateType
,
readExpression
(),
currentSelect
,
distinct
);
break
;
}
read
(
CLOSE_PAREN
);
if
(
r
!=
null
)
{
...
...
h2/src/main/org/h2/expression/aggregate/Aggregate.java
浏览文件 @
2a200352
...
...
@@ -132,10 +132,16 @@ public class Aggregate extends Expression {
* The aggregate type for MEDIAN(expression).
*/
MEDIAN
,
/**
* The aggregate type for ARRAY_AGG(expression).
*/
ARRAY_AGG
ARRAY_AGG
,
/**
* The aggregate type for MODE(expression).
*/
MODE
,
}
private
static
final
HashMap
<
String
,
AggregateType
>
AGGREGATES
=
new
HashMap
<>(
64
);
...
...
@@ -203,6 +209,7 @@ public class Aggregate extends Expression {
addAggregate
(
"BIT_AND"
,
AggregateType
.
BIT_AND
);
addAggregate
(
"MEDIAN"
,
AggregateType
.
MEDIAN
);
addAggregate
(
"ARRAY_AGG"
,
AggregateType
.
ARRAY_AGG
);
addAggregate
(
"MODE"
,
AggregateType
.
MODE
);
}
private
static
void
addAggregate
(
String
name
,
AggregateType
type
)
{
...
...
@@ -506,6 +513,7 @@ public class Aggregate extends Expression {
case
MIN:
case
MAX:
case
MEDIAN:
case
MODE:
break
;
case
STDDEV_POP:
case
STDDEV_SAMP:
...
...
@@ -676,6 +684,9 @@ public class Aggregate extends Expression {
break
;
case
ARRAY_AGG:
return
getSQLArrayAggregate
();
case
MODE:
text
=
"MODE"
;
break
;
default
:
throw
DbException
.
throwInternalError
(
"type="
+
type
);
}
...
...
h2/src/main/org/h2/expression/aggregate/AggregateData.java
浏览文件 @
2a200352
...
...
@@ -35,6 +35,8 @@ abstract class AggregateData {
return
new
AggregateDataHistogram
();
case
MEDIAN:
return
new
AggregateDataMedian
();
case
MODE:
return
new
AggregateDataMode
();
default
:
return
new
AggregateDataDefault
(
aggregateType
);
}
...
...
h2/src/main/org/h2/expression/aggregate/AggregateDataMode.java
0 → 100644
浏览文件 @
2a200352
/*
* 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
*/
package
org
.
h2
.
expression
.
aggregate
;
import
java.util.Map.Entry
;
import
org.h2.engine.Database
;
import
org.h2.util.ValueHashMap
;
import
org.h2.value.Value
;
import
org.h2.value.ValueNull
;
/**
* Data stored while calculating a MODE aggregate.
*/
class
AggregateDataMode
extends
AggregateData
{
private
ValueHashMap
<
LongDataCounter
>
distinctValues
;
@Override
void
add
(
Database
database
,
int
dataType
,
boolean
distinct
,
Value
v
)
{
if
(
v
==
ValueNull
.
INSTANCE
)
{
return
;
}
if
(
distinctValues
==
null
)
{
distinctValues
=
ValueHashMap
.
newInstance
();
}
LongDataCounter
a
=
distinctValues
.
get
(
v
);
if
(
a
==
null
)
{
a
=
new
LongDataCounter
();
distinctValues
.
put
(
v
,
a
);
}
a
.
count
++;
}
@Override
Value
getValue
(
Database
database
,
int
dataType
,
boolean
distinct
)
{
Value
v
=
ValueNull
.
INSTANCE
;
if
(
distinctValues
!=
null
)
{
long
count
=
0L
;
for
(
Entry
<
Value
,
LongDataCounter
>
entry
:
distinctValues
.
entries
())
{
long
c
=
entry
.
getValue
().
count
;
if
(
c
>
count
)
{
v
=
entry
.
getKey
();
count
=
c
;
}
}
}
return
v
.
convertTo
(
dataType
);
}
}
h2/src/test/org/h2/test/scripts/TestScript.java
浏览文件 @
2a200352
...
...
@@ -137,7 +137,7 @@ public class TestScript extends TestDb {
testScript
(
"other/"
+
s
+
".sql"
);
}
for
(
String
s
:
new
String
[]
{
"avg"
,
"bit-and"
,
"bit-or"
,
"count"
,
"group-concat"
,
"max"
,
"median"
,
"min"
,
"selectivity"
,
"stddev-pop"
,
"group-concat"
,
"max"
,
"median"
,
"min"
,
"
mode"
,
"
selectivity"
,
"stddev-pop"
,
"stddev-samp"
,
"sum"
,
"var-pop"
,
"var-samp"
,
"array-agg"
})
{
testScript
(
"functions/aggregate/"
+
s
+
".sql"
);
}
...
...
h2/src/test/org/h2/test/scripts/functions/aggregate/mode.sql
0 → 100644
浏览文件 @
2a200352
-- 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
(
V
INT
);
>
ok
SELECT
MODE
(
V
)
FROM
TEST
;
>>
null
SELECT
MODE
(
DISTINCT
V
)
FROM
TEST
;
>
exception
SYNTAX_ERROR_2
INSERT
INTO
TEST
VALUES
(
NULL
);
>
update
count
:
1
SELECT
MODE
(
V
)
FROM
TEST
;
>>
null
INSERT
INTO
TEST
VALUES
(
1
),
(
2
),
(
3
),
(
1
),
(
2
),
(
1
);
>
update
count
:
6
SELECT
MODE
(
V
),
MODE
(
V
)
FILTER
(
WHERE
(
V
>
1
)),
MODE
(
V
)
FILTER
(
WHERE
(
V
<
0
))
FROM
TEST
;
>
MODE
(
V
)
MODE
(
V
)
FILTER
(
WHERE
(
V
>
1
))
MODE
(
V
)
FILTER
(
WHERE
(
V
<
0
))
>
------- ------------------------------ ------------------------------
>
1
2
null
>
rows
:
1
DROP
TABLE
TEST
;
>
ok
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论