Skip to content
项目
群组
代码片段
帮助
正在加载...
帮助
为 GitLab 提交贡献
登录/注册
切换导航
H
h2database
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
分枝图
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
计划
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
分枝图
统计图
创建新议题
作业
提交
议题看板
打开侧边栏
Administrator
h2database
Commits
ee65a1bb
提交
ee65a1bb
authored
3月 12, 2018
作者:
Alex Nordlund
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Adds ARRAY_AGG function.
上级
606eebb5
隐藏空白字符变更
内嵌
并排
正在显示
7 个修改的文件
包含
183 行增加
和
13 行删除
+183
-13
help.csv
h2/src/docsrc/help/help.csv
+13
-0
Parser.java
h2/src/main/org/h2/command/Parser.java
+9
-0
Aggregate.java
h2/src/main/org/h2/expression/Aggregate.java
+107
-6
AggregateData.java
h2/src/main/org/h2/expression/AggregateData.java
+3
-1
AggregateDataArrayCollecting.java
.../main/org/h2/expression/AggregateDataArrayCollecting.java
+5
-5
TestScript.java
h2/src/test/org/h2/test/scripts/TestScript.java
+1
-1
array-agg.sql
...est/org/h2/test/scripts/functions/aggregate/array-agg.sql
+45
-0
没有找到文件。
h2/src/docsrc/help/help.csv
浏览文件 @
ee65a1bb
...
...
@@ -2949,6 +2949,19 @@ Aggregates are only allowed in select statements.
GROUP_CONCAT(NAME ORDER BY ID SEPARATOR ', ')
"
"Functions (Aggregate)","ARRAY_AGG","
ARRAY_AGG ( [ DISTINCT ] string
[ ORDER BY { expression [ ASC | DESC ] } [,...] ] )
[ FILTER ( WHERE expression ) ]
","
Aggregate the value into an array.
This method returns an array.
If no rows are selected, the result is NULL.
Aggregates are only allowed in select statements.
","
ARRAY_AGG(NAME ORDER BY ID)
"
"Functions (Aggregate)","MAX","
MAX(value) [ FILTER ( WHERE expression ) ]
","
...
...
h2/src/main/org/h2/command/Parser.java
浏览文件 @
ee65a1bb
...
...
@@ -2651,6 +2651,15 @@ public class Parser {
}
else
{
r
=
null
;
}
}
else
if
(
aggregateType
==
AggregateType
.
ARRAY_AGG
)
{
boolean
distinct
=
readIf
(
"DISTINCT"
);
r
=
new
Aggregate
(
AggregateType
.
ARRAY_AGG
,
readExpression
(),
currentSelect
,
distinct
);
if
(
readIf
(
"ORDER"
))
{
read
(
"BY"
);
r
.
setArrayAggOrder
(
parseSimpleOrderList
());
}
}
else
{
boolean
distinct
=
readIf
(
"DISTINCT"
);
r
=
new
Aggregate
(
aggregateType
,
readExpression
(),
currentSelect
,
...
...
h2/src/main/org/h2/expression/Aggregate.java
浏览文件 @
ee65a1bb
...
...
@@ -128,7 +128,11 @@ public class Aggregate extends Expression {
/**
* The aggregate type for MEDIAN(expression).
*/
MEDIAN
MEDIAN
,
/**
* The aggregate type for ARRAY_AGG(expression).
*/
ARRAY_AGG
}
private
static
final
HashMap
<
String
,
AggregateType
>
AGGREGATES
=
new
HashMap
<>(
25
);
...
...
@@ -140,7 +144,9 @@ public class Aggregate extends Expression {
private
Expression
on
;
private
Expression
groupConcatSeparator
;
private
ArrayList
<
SelectOrderBy
>
groupConcatOrderList
;
private
ArrayList
<
SelectOrderBy
>
arrayAggOrderList
;
private
SortOrder
groupConcatSort
;
private
SortOrder
arrayOrderSort
;
private
int
dataType
,
scale
;
private
long
precision
;
private
int
displaySize
;
...
...
@@ -195,6 +201,7 @@ public class Aggregate extends Expression {
addAggregate
(
"BIT_OR"
,
AggregateType
.
BIT_OR
);
addAggregate
(
"BIT_AND"
,
AggregateType
.
BIT_AND
);
addAggregate
(
"MEDIAN"
,
AggregateType
.
MEDIAN
);
addAggregate
(
"ARRAY_AGG"
,
AggregateType
.
ARRAY_AGG
);
}
private
static
void
addAggregate
(
String
name
,
AggregateType
type
)
{
...
...
@@ -221,6 +228,15 @@ public class Aggregate extends Expression {
this
.
groupConcatOrderList
=
orderBy
;
}
/**
* Set the order for ARRAY_AGG() aggregate.
*
* @param orderBy the order by list
*/
public
void
setArrayAggOrder
(
ArrayList
<
SelectOrderBy
>
orderBy
)
{
this
.
arrayAggOrderList
=
orderBy
;
}
/**
* Set the separator for the GROUP_CONCAT() aggregate.
*
...
...
@@ -239,12 +255,12 @@ public class Aggregate extends Expression {
this
.
filterCondition
=
filterCondition
;
}
private
SortOrder
initOrder
(
Session
session
)
{
int
size
=
groupConcatO
rderList
.
size
();
private
SortOrder
initOrder
(
ArrayList
<
SelectOrderBy
>
orderList
,
Session
session
)
{
int
size
=
o
rderList
.
size
();
int
[]
index
=
new
int
[
size
];
int
[]
sortType
=
new
int
[
size
];
for
(
int
i
=
0
;
i
<
size
;
i
++)
{
SelectOrderBy
o
=
groupConcatO
rderList
.
get
(
i
);
SelectOrderBy
o
=
o
rderList
.
get
(
i
);
index
[
i
]
=
i
+
1
;
int
order
=
o
.
descending
?
SortOrder
.
DESCENDING
:
SortOrder
.
ASCENDING
;
sortType
[
i
]
=
order
;
...
...
@@ -292,6 +308,20 @@ public class Aggregate extends Expression {
}
}
}
if
(
type
==
AggregateType
.
ARRAY_AGG
)
{
if
(
v
!=
ValueNull
.
INSTANCE
)
{
if
(
arrayAggOrderList
!=
null
)
{
int
size
=
arrayAggOrderList
.
size
();
Value
[]
array
=
new
Value
[
1
+
size
];
array
[
0
]
=
v
;
for
(
int
i
=
0
;
i
<
size
;
i
++)
{
SelectOrderBy
o
=
arrayAggOrderList
.
get
(
i
);
array
[
i
+
1
]
=
o
.
expression
.
getValue
(
session
);
}
v
=
ValueArray
.
get
(
array
);
}
}
}
if
(
filterCondition
!=
null
)
{
if
(!
filterCondition
.
getBooleanValue
(
session
))
{
return
;
...
...
@@ -343,7 +373,7 @@ public class Aggregate extends Expression {
}
Value
v
=
data
.
getValue
(
session
.
getDatabase
(),
dataType
,
distinct
);
if
(
type
==
AggregateType
.
GROUP_CONCAT
)
{
ArrayList
<
Value
>
list
=
((
AggregateData
GroupConcat
)
data
).
getList
();
ArrayList
<
Value
>
list
=
((
AggregateData
ArrayCollecting
)
data
).
getList
();
if
(
list
==
null
||
list
.
isEmpty
())
{
return
ValueNull
.
INSTANCE
;
}
...
...
@@ -377,6 +407,23 @@ public class Aggregate extends Expression {
buff
.
append
(
s
);
}
v
=
ValueString
.
get
(
buff
.
toString
());
}
else
if
(
type
==
AggregateType
.
ARRAY_AGG
)
{
ArrayList
<
Value
>
list
=
((
AggregateDataArrayCollecting
)
data
).
getList
();
if
(
list
==
null
||
list
.
isEmpty
())
{
return
ValueNull
.
INSTANCE
;
}
if
(
arrayAggOrderList
!=
null
)
{
final
SortOrder
sortOrder
=
arrayOrderSort
;
Collections
.
sort
(
list
,
new
Comparator
<
Value
>()
{
@Override
public
int
compare
(
Value
v1
,
Value
v2
)
{
Value
[]
a1
=
((
ValueArray
)
v1
).
getList
();
Value
[]
a2
=
((
ValueArray
)
v2
).
getList
();
return
sortOrder
.
compare
(
a1
,
a2
);
}
});
}
v
=
ValueArray
.
get
(
list
.
toArray
(
new
Value
[
list
.
size
()]));
}
return
v
;
}
...
...
@@ -396,6 +443,11 @@ public class Aggregate extends Expression {
o
.
expression
.
mapColumns
(
resolver
,
level
);
}
}
if
(
arrayAggOrderList
!=
null
)
{
for
(
SelectOrderBy
o
:
arrayAggOrderList
)
{
o
.
expression
.
mapColumns
(
resolver
,
level
);
}
}
if
(
groupConcatSeparator
!=
null
)
{
groupConcatSeparator
.
mapColumns
(
resolver
,
level
);
}
...
...
@@ -417,7 +469,13 @@ public class Aggregate extends Expression {
for
(
SelectOrderBy
o
:
groupConcatOrderList
)
{
o
.
expression
=
o
.
expression
.
optimize
(
session
);
}
groupConcatSort
=
initOrder
(
session
);
groupConcatSort
=
initOrder
(
groupConcatOrderList
,
session
);
}
if
(
arrayAggOrderList
!=
null
)
{
for
(
SelectOrderBy
o
:
arrayAggOrderList
)
{
o
.
expression
=
o
.
expression
.
optimize
(
session
);
}
arrayOrderSort
=
initOrder
(
arrayAggOrderList
,
session
);
}
if
(
groupConcatSeparator
!=
null
)
{
groupConcatSeparator
=
groupConcatSeparator
.
optimize
(
session
);
...
...
@@ -490,6 +548,11 @@ public class Aggregate extends Expression {
throw
DbException
.
get
(
ErrorCode
.
SUM_OR_AVG_ON_WRONG_DATATYPE_1
,
getSQL
());
}
break
;
case
ARRAY_AGG:
dataType
=
Value
.
ARRAY
;
scale
=
0
;
precision
=
displaySize
=
Integer
.
MAX_VALUE
;
break
;
default
:
DbException
.
throwInternalError
(
"type="
+
type
);
}
...
...
@@ -506,6 +569,11 @@ public class Aggregate extends Expression {
o
.
expression
.
setEvaluatable
(
tableFilter
,
b
);
}
}
if
(
arrayAggOrderList
!=
null
)
{
for
(
SelectOrderBy
o
:
arrayAggOrderList
)
{
o
.
expression
.
setEvaluatable
(
tableFilter
,
b
);
}
}
if
(
groupConcatSeparator
!=
null
)
{
groupConcatSeparator
.
setEvaluatable
(
tableFilter
,
b
);
}
...
...
@@ -555,6 +623,29 @@ public class Aggregate extends Expression {
return
buff
.
toString
();
}
private
String
getSQLArrayAggregate
()
{
StatementBuilder
buff
=
new
StatementBuilder
(
"ARRAY_AGG("
);
if
(
distinct
)
{
buff
.
append
(
"DISTINCT "
);
}
buff
.
append
(
on
.
getSQL
());
if
(
arrayAggOrderList
!=
null
)
{
buff
.
append
(
" ORDER BY "
);
for
(
SelectOrderBy
o
:
arrayAggOrderList
)
{
buff
.
appendExceptFirst
(
", "
);
buff
.
append
(
o
.
expression
.
getSQL
());
if
(
o
.
descending
)
{
buff
.
append
(
" DESC"
);
}
}
}
buff
.
append
(
')'
);
if
(
filterCondition
!=
null
)
{
buff
.
append
(
" FILTER (WHERE "
).
append
(
filterCondition
.
getSQL
()).
append
(
')'
);
}
return
buff
.
toString
();
}
@Override
public
String
getSQL
()
{
String
text
;
...
...
@@ -611,6 +702,8 @@ public class Aggregate extends Expression {
case
MEDIAN:
text
=
"MEDIAN"
;
break
;
case
ARRAY_AGG:
return
getSQLArrayAggregate
();
default
:
throw
DbException
.
throwInternalError
(
"type="
+
type
);
}
...
...
@@ -681,6 +774,14 @@ public class Aggregate extends Expression {
}
}
}
if
(
arrayAggOrderList
!=
null
)
{
for
(
int
i
=
0
,
size
=
arrayAggOrderList
.
size
();
i
<
size
;
i
++)
{
SelectOrderBy
o
=
arrayAggOrderList
.
get
(
i
);
if
(!
o
.
expression
.
isEverything
(
visitor
))
{
return
false
;
}
}
}
return
true
;
}
...
...
h2/src/main/org/h2/expression/AggregateData.java
浏览文件 @
ee65a1bb
...
...
@@ -24,7 +24,9 @@ abstract class AggregateData {
if
(
aggregateType
==
AggregateType
.
SELECTIVITY
)
{
return
new
AggregateDataSelectivity
();
}
else
if
(
aggregateType
==
AggregateType
.
GROUP_CONCAT
)
{
return
new
AggregateDataGroupConcat
();
return
new
AggregateDataArrayCollecting
();
}
else
if
(
aggregateType
==
AggregateType
.
ARRAY_AGG
)
{
return
new
AggregateDataArrayCollecting
();
}
else
if
(
aggregateType
==
AggregateType
.
COUNT_ALL
)
{
return
new
AggregateDataCountAll
();
}
else
if
(
aggregateType
==
AggregateType
.
COUNT
)
{
...
...
h2/src/main/org/h2/expression/AggregateData
GroupConcat
.java
→
h2/src/main/org/h2/expression/AggregateData
ArrayCollecting
.java
浏览文件 @
ee65a1bb
...
...
@@ -13,11 +13,11 @@ import org.h2.value.Value;
import
org.h2.value.ValueNull
;
/**
* Data stored while calculating a GROUP_CONCAT aggregate.
* Data stored while calculating a GROUP_CONCAT
/ARRAY_AGG
aggregate.
*/
class
AggregateData
GroupConcat
extends
AggregateData
{
class
AggregateData
ArrayCollecting
extends
AggregateData
{
private
ArrayList
<
Value
>
list
;
private
ValueHashMap
<
AggregateData
GroupConcat
>
distinctValues
;
private
ValueHashMap
<
AggregateData
ArrayCollecting
>
distinctValues
;
@Override
void
add
(
Database
database
,
int
dataType
,
boolean
distinct
,
Value
v
)
{
...
...
@@ -40,7 +40,7 @@ class AggregateDataGroupConcat extends AggregateData {
@Override
Value
getValue
(
Database
database
,
int
dataType
,
boolean
distinct
)
{
if
(
distinct
)
{
groupD
istinct
(
database
,
dataType
);
d
istinct
(
database
,
dataType
);
}
return
null
;
}
...
...
@@ -49,7 +49,7 @@ class AggregateDataGroupConcat extends AggregateData {
return
list
;
}
private
void
groupD
istinct
(
Database
database
,
int
dataType
)
{
private
void
d
istinct
(
Database
database
,
int
dataType
)
{
if
(
distinctValues
==
null
)
{
return
;
}
...
...
h2/src/test/org/h2/test/scripts/TestScript.java
浏览文件 @
ee65a1bb
...
...
@@ -110,7 +110,7 @@ public class TestScript extends TestBase {
}
for
(
String
s
:
new
String
[]
{
"avg"
,
"bit-and"
,
"bit-or"
,
"count"
,
"group-concat"
,
"max"
,
"median"
,
"min"
,
"selectivity"
,
"stddev-pop"
,
"stddev-samp"
,
"sum"
,
"var-pop"
,
"var-samp"
})
{
"stddev-samp"
,
"sum"
,
"var-pop"
,
"var-samp"
,
"array-agg"
})
{
testScript
(
"functions/aggregate/"
+
s
+
".sql"
);
}
for
(
String
s
:
new
String
[]
{
"abs"
,
"acos"
,
"asin"
,
"atan"
,
"atan2"
,
...
...
h2/src/test/org/h2/test/scripts/functions/aggregate/array-agg.sql
0 → 100644
浏览文件 @
ee65a1bb
-- 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: Alex Nordlund
--
-- with filter condition
create
table
test
(
v
varchar
);
>
ok
insert
into
test
values
(
'1'
),
(
'2'
),
(
'3'
),
(
'4'
),
(
'5'
),
(
'6'
),
(
'7'
),
(
'8'
),
(
'9'
);
>
update
count
:
9
select
array_agg
(
v
order
by
v
asc
),
array_agg
(
v
order
by
v
desc
)
filter
(
where
v
>=
'4'
)
from
test
where
v
>=
'2'
;
>
ARRAY_AGG
(
V
ORDER
BY
V
)
ARRAY_AGG
(
V
ORDER
BY
V
DESC
)
FILTER
(
WHERE
(
V
>=
'4'
))
>
---------------------------------------------------------------- ------------------------------------------------------
------------------------------
>
(
2
,
3
,
4
,
5
,
6
,
7
,
8
,
9
)
(
9
,
8
,
7
,
6
,
5
,
4
)
>
rows
(
ordered
):
1
create
index
test_idx
on
test
(
v
);
select
ARRAY_AGG
(
v
order
by
v
asc
),
ARRAY_AGG
(
v
order
by
v
desc
)
filter
(
where
v
>=
'4'
)
from
test
where
v
>=
'2'
;
>
ARRAY_AGG
(
V
ORDER
BY
V
)
ARRAY_AGG
(
V
ORDER
BY
V
DESC
)
FILTER
(
WHERE
(
V
>=
'4'
))
>
---------------------------------------------------------------- ------------------------------------------------------
------------------------------
>
(
2
,
3
,
4
,
5
,
6
,
7
,
8
,
9
)
(
9
,
8
,
7
,
6
,
5
,
4
)
>
rows
(
ordered
):
1
select
ARRAY_AGG
(
v
order
by
v
asc
),
ARRAY_AGG
(
v
order
by
v
desc
)
filter
(
where
v
>=
'4'
)
from
test
;
>
ARRAY_AGG
(
V
ORDER
BY
V
)
ARRAY_AGG
(
V
ORDER
BY
V
DESC
)
FILTER
(
WHERE
(
V
>=
'4'
))
>
------------------------------------------------------------------------ ------------------------------------------------------
------------------------------
>
(
1
,
2
,
3
,
4
,
5
,
6
,
7
,
8
,
9
)
(
9
,
8
,
7
,
6
,
5
,
4
)
>
rows
(
ordered
):
1
drop
table
test
;
>
ok
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论