Skip to content
项目
群组
代码片段
帮助
正在加载...
帮助
为 GitLab 提交贡献
登录/注册
切换导航
H
h2database
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
分枝图
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
计划
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
分枝图
统计图
创建新议题
作业
提交
议题看板
打开侧边栏
Administrator
h2database
Commits
696b3512
Unverified
提交
696b3512
authored
1月 09, 2019
作者:
Evgenij Ryazanov
提交者:
GitHub
1月 09, 2019
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #1648 from ilm-informatique/arrayFunctions
Add functions ARRAY_CAT(), ARRAY_APPEND() and ARRAY_SLICE()
上级
143635c9
e98f75fc
隐藏空白字符变更
内嵌
并排
正在显示
7 个修改的文件
包含
163 行增加
和
3 行删除
+163
-3
help.csv
h2/src/docsrc/help/help.csv
+28
-0
changelog.html
h2/src/docsrc/html/changelog.html
+2
-0
features.html
h2/src/docsrc/html/features.html
+1
-0
Function.java
h2/src/main/org/h2/expression/function/Function.java
+62
-1
TestScript.java
h2/src/test/org/h2/test/scripts/TestScript.java
+2
-2
array-cat.sql
...c/test/org/h2/test/scripts/functions/system/array-cat.sql
+22
-0
array-slice.sql
...test/org/h2/test/scripts/functions/system/array-slice.sql
+46
-0
没有找到文件。
h2/src/docsrc/help/help.csv
浏览文件 @
696b3512
...
...
@@ -4842,6 +4842,34 @@ Returns NULL if the specified array is NULL.
CALL ARRAY_CONTAINS(ARRAY['Hello', 'World'], 'Hello')
"
"Functions (System)","ARRAY_CAT","
ARRAY_CAT(arrayExpression, arrayExpression)
","
Returns the concatenation of two arrays.
Returns NULL if any parameter is NULL.
","
CALL ARRAY_CAT(ARRAY[1, 2], ARRAY[3, 4])
"
"Functions (System)","ARRAY_APPEND","
ARRAY_APPEND(arrayExpression, value)
","
Append an element to the end of an array.
Returns NULL if any parameter is NULL.
","
CALL ARRAY_APPEND(ARRAY[1, 2], 3)
"
"Functions (System)","ARRAY_SLICE","
ARRAY_SLICE(arrayExpression, lowerBoundInt, upperBoundInt)
","
Returns elements from the array as specified by the lower and upper bound parameters.
Both parameters are inclusive and the first element has index 1, i.e. ARRAY_SLICE(a, 2, 2) has only the second element.
Returns NULL if any parameter is NULL or if an index is out of bounds.
","
CALL ARRAY_SLICE(ARRAY[1, 2, 3, 4], 1, 3)
"
"Functions (System)","AUTOCOMMIT","
AUTOCOMMIT()
","
...
...
h2/src/docsrc/html/changelog.html
浏览文件 @
696b3512
...
...
@@ -21,6 +21,8 @@ Change Log
<h2>
Next Version (unreleased)
</h2>
<ul>
<li>
PR #1648: Add functions ARRAY_CAT(), ARRAY_APPEND() and ARRAY_SLICE()
</li>
<li>
PR #1638: Add support for Java 11 to test suite
</li>
<li>
PR #1637: Remove explicit unboxing
...
...
h2/src/docsrc/html/features.html
浏览文件 @
696b3512
...
...
@@ -959,6 +959,7 @@ or the SQL statement <code>SET MODE PostgreSQL</code>.
</li><li>
Fixed-width strings are padded with spaces.
</li><li>
MONEY data type is treated like NUMERIC(19, 2) data type.
</li><li>
Datetime value functions return the same value within a transaction.
</li><li>
ARRAY_SLICE() out of bounds parameters are silently corrected.
</li></ul>
<h3>
Ignite Compatibility Mode
</h3>
...
...
h2/src/main/org/h2/expression/function/Function.java
浏览文件 @
696b3512
...
...
@@ -16,6 +16,7 @@ import java.sql.Connection;
import
java.sql.ResultSet
;
import
java.sql.SQLException
;
import
java.util.ArrayList
;
import
java.util.Arrays
;
import
java.util.HashMap
;
import
java.util.regex.Matcher
;
import
java.util.regex.Pattern
;
...
...
@@ -27,6 +28,7 @@ import org.h2.command.Parser;
import
org.h2.engine.Constants
;
import
org.h2.engine.Database
;
import
org.h2.engine.Mode
;
import
org.h2.engine.Mode.ModeEnum
;
import
org.h2.engine.Session
;
import
org.h2.expression.Expression
;
import
org.h2.expression.ExpressionColumn
;
...
...
@@ -61,6 +63,7 @@ import org.h2.util.Utils;
import
org.h2.value.DataType
;
import
org.h2.value.ExtTypeInfo
;
import
org.h2.value.Value
;
import
org.h2.value.ValueArray
;
import
org.h2.value.ValueBoolean
;
import
org.h2.value.ValueBytes
;
import
org.h2.value.ValueCollectionBase
;
...
...
@@ -135,7 +138,7 @@ public class Function extends Expression implements FunctionCall {
CANCEL_SESSION
=
221
,
SET
=
222
,
TABLE
=
223
,
TABLE_DISTINCT
=
224
,
FILE_READ
=
225
,
TRANSACTION_ID
=
226
,
TRUNCATE_VALUE
=
227
,
NVL2
=
228
,
DECODE
=
229
,
ARRAY_CONTAINS
=
230
,
FILE_WRITE
=
232
,
UNNEST
=
233
;
UNNEST
=
233
,
ARRAY_CONCAT
=
234
,
ARRAY_APPEND
=
235
,
ARRAY_SLICE
=
236
;
public
static
final
int
REGEXP_LIKE
=
240
;
...
...
@@ -419,6 +422,9 @@ public class Function extends Expression implements FunctionCall {
addFunction
(
"ARRAY_GET"
,
ARRAY_GET
,
2
,
Value
.
NULL
);
addFunctionWithNull
(
"ARRAY_CONTAINS"
,
ARRAY_CONTAINS
,
2
,
Value
.
BOOLEAN
);
addFunction
(
"ARRAY_CAT"
,
ARRAY_CONCAT
,
2
,
Value
.
ARRAY
);
addFunction
(
"ARRAY_APPEND"
,
ARRAY_APPEND
,
2
,
Value
.
ARRAY
);
addFunction
(
"ARRAY_SLICE"
,
ARRAY_SLICE
,
3
,
Value
.
ARRAY
);
addFunction
(
"CSVREAD"
,
CSVREAD
,
VAR_ARGS
,
Value
.
RESULT_SET
,
false
,
false
,
false
,
true
);
addFunction
(
"CSVWRITE"
,
CSVWRITE
,
...
...
@@ -1542,6 +1548,61 @@ public class Function extends Expression implements FunctionCall {
}
break
;
}
case
ARRAY_CONCAT:
{
final
ValueArray
array
=
(
ValueArray
)
v0
.
convertTo
(
Value
.
ARRAY
);
final
ValueArray
array2
=
(
ValueArray
)
v1
.
convertTo
(
Value
.
ARRAY
);
if
(!
array
.
getComponentType
().
equals
(
array2
.
getComponentType
()))
throw
DbException
.
get
(
ErrorCode
.
GENERAL_ERROR_1
,
"Expected component type "
+
array
.
getComponentType
()
+
" but got "
+
array2
.
getComponentType
());
final
Value
[]
res
=
Arrays
.
copyOf
(
array
.
getList
(),
array
.
getList
().
length
+
array2
.
getList
().
length
);
System
.
arraycopy
(
array2
.
getList
(),
0
,
res
,
array
.
getList
().
length
,
array2
.
getList
().
length
);
result
=
ValueArray
.
get
(
array
.
getComponentType
(),
res
);
break
;
}
case
ARRAY_APPEND:
{
final
ValueArray
array
=
(
ValueArray
)
v0
.
convertTo
(
Value
.
ARRAY
);
if
(
v1
!=
ValueNull
.
INSTANCE
&&
array
.
getComponentType
()
!=
Object
.
class
&&
!
array
.
getComponentType
().
isInstance
(
v1
.
getObject
()))
throw
DbException
.
get
(
ErrorCode
.
GENERAL_ERROR_1
,
"Expected component type "
+
array
.
getComponentType
()
+
" but got "
+
v1
.
getClass
());
final
Value
[]
res
=
Arrays
.
copyOf
(
array
.
getList
(),
array
.
getList
().
length
+
1
);
res
[
array
.
getList
().
length
]
=
v1
;
result
=
ValueArray
.
get
(
array
.
getComponentType
(),
res
);
break
;
}
case
ARRAY_SLICE:
{
result
=
null
;
final
ValueArray
array
=
(
ValueArray
)
v0
.
convertTo
(
Value
.
ARRAY
);
// SQL is 1-based
int
index1
=
v1
.
getInt
()
-
1
;
// 1-based and inclusive as postgreSQL (-1+1)
int
index2
=
v2
.
getInt
();
// https://www.postgresql.org/docs/current/arrays.html#ARRAYS-ACCESSING
// For historical reasons postgreSQL ignore invalid indexes
final
boolean
isPG
=
database
.
getMode
().
getEnum
()
==
ModeEnum
.
PostgreSQL
;
if
(
index1
>
index2
)
{
if
(
isPG
)
result
=
ValueArray
.
get
(
array
.
getComponentType
(),
new
Value
[
0
]);
else
result
=
ValueNull
.
INSTANCE
;
}
else
{
if
(
index1
<
0
)
{
if
(
isPG
)
index1
=
0
;
else
result
=
ValueNull
.
INSTANCE
;
}
if
(
index2
>
array
.
getList
().
length
)
{
if
(
isPG
)
index2
=
array
.
getList
().
length
;
else
result
=
ValueNull
.
INSTANCE
;
}
}
if
(
result
==
null
)
result
=
ValueArray
.
get
(
array
.
getComponentType
(),
Arrays
.
copyOfRange
(
array
.
getList
(),
index1
,
index2
));
break
;
}
case
LINK_SCHEMA:
{
session
.
getUser
().
checkAdmin
();
Connection
conn
=
session
.
createConnection
(
false
);
...
...
h2/src/test/org/h2/test/scripts/TestScript.java
浏览文件 @
696b3512
...
...
@@ -186,8 +186,8 @@ public class TestScript extends TestDb {
"xmlnode"
,
"xmlstartdoc"
,
"xmltext"
})
{
testScript
(
"functions/string/"
+
s
+
".sql"
);
}
for
(
String
s
:
new
String
[]
{
"array-contains"
,
"array-get"
,
"array-length"
,
"autocommit"
,
"cancel-session"
,
"casewhen"
,
for
(
String
s
:
new
String
[]
{
"array-c
at"
,
"array-c
ontains"
,
"array-get"
,
"array-length"
,
"array-slice"
,
"autocommit"
,
"cancel-session"
,
"casewhen"
,
"cast"
,
"coalesce"
,
"convert"
,
"csvread"
,
"csvwrite"
,
"currval"
,
"database-path"
,
"database"
,
"decode"
,
"disk-space-used"
,
"file-read"
,
"file-write"
,
"greatest"
,
"h2version"
,
"identity"
,
...
...
h2/src/test/org/h2/test/scripts/functions/system/array-cat.sql
0 → 100644
浏览文件 @
696b3512
-- Copyright 2004-2019 H2 Group. Multiple-Licensed under the MPL 2.0,
-- and the EPL 1.0 (http://h2database.com/html/license.html).
-- Initial Developer: H2 Group
--
select
array_cat
(
ARRAY
[
1
,
2
],
ARRAY
[
3
,
4
])
=
ARRAY
[
1
,
2
,
3
,
4
];
>>
TRUE
select
array_cat
(
ARRAY
[
1
,
2
],
null
)
is
null
;
>>
TRUE
select
array_cat
(
null
,
ARRAY
[
1
,
2
])
is
null
;
>>
TRUE
select
array_append
(
ARRAY
[
1
,
2
],
3
)
=
ARRAY
[
1
,
2
,
3
];
>>
TRUE
select
array_append
(
ARRAY
[
1
,
2
],
null
)
is
null
;
>>
TRUE
select
array_append
(
null
,
3
)
is
null
;
>>
TRUE
h2/src/test/org/h2/test/scripts/functions/system/array-slice.sql
0 → 100644
浏览文件 @
696b3512
-- Copyright 2004-2019 H2 Group. Multiple-Licensed under the MPL 2.0,
-- and the EPL 1.0 (http://h2database.com/html/license.html).
-- Initial Developer: H2 Group
--
select
array_slice
(
ARRAY
[
1
,
2
,
3
,
4
],
1
,
1
)
=
ARRAY
[
1
];
>>
TRUE
select
array_slice
(
ARRAY
[
1
,
2
,
3
,
4
],
1
,
3
)
=
ARRAY
[
1
,
2
,
3
];
>>
TRUE
-- test invalid indexes
select
array_slice
(
ARRAY
[
1
,
2
,
3
,
4
],
3
,
1
)
is
null
;
>>
TRUE
select
array_slice
(
ARRAY
[
1
,
2
,
3
,
4
],
0
,
3
)
is
null
;
>>
TRUE
select
array_slice
(
ARRAY
[
1
,
2
,
3
,
4
],
1
,
5
)
is
null
;
>>
TRUE
-- in PostgreSQL, indexes are corrected
SET
MODE
PostgreSQL
;
>
ok
select
array_slice
(
ARRAY
[
1
,
2
,
3
,
4
],
3
,
1
)
=
ARRAY
[];
>>
TRUE
select
array_slice
(
ARRAY
[
1
,
2
,
3
,
4
],
0
,
3
)
=
ARRAY
[
1
,
2
,
3
];
>>
TRUE
select
array_slice
(
ARRAY
[
1
,
2
,
3
,
4
],
1
,
5
)
=
ARRAY
[
1
,
2
,
3
,
4
];
>>
TRUE
SET
MODE
Regular
;
>
ok
-- null parameters
select
array_slice
(
null
,
1
,
3
)
is
null
;
>>
TRUE
select
array_slice
(
ARRAY
[
1
,
2
,
3
,
4
],
null
,
3
)
is
null
;
>>
TRUE
select
array_slice
(
ARRAY
[
1
,
2
,
3
,
4
],
1
,
null
)
is
null
;
>>
TRUE
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论