Skip to content
项目
群组
代码片段
帮助
正在加载...
帮助
为 GitLab 提交贡献
登录/注册
切换导航
H
h2database
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
分枝图
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
计划
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
分枝图
统计图
创建新议题
作业
提交
议题看板
打开侧边栏
Administrator
h2database
Commits
21036320
提交
21036320
authored
11月 22, 2018
作者:
Evgenij Ryazanov
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add UNNEST table function
上级
f33de19b
隐藏空白字符变更
内嵌
并排
正在显示
6 个修改的文件
包含
117 行增加
和
34 行删除
+117
-34
help.csv
h2/src/docsrc/help/help.csv
+12
-0
Parser.java
h2/src/main/org/h2/command/Parser.java
+17
-0
Function.java
h2/src/main/org/h2/expression/function/Function.java
+6
-5
TableFunction.java
h2/src/main/org/h2/expression/function/TableFunction.java
+39
-28
TestScript.java
h2/src/test/org/h2/test/scripts/TestScript.java
+1
-1
unnest.sql
h2/src/test/org/h2/test/scripts/functions/system/unnest.sql
+42
-0
没有找到文件。
h2/src/docsrc/help/help.csv
浏览文件 @
21036320
...
...
@@ -5215,6 +5215,18 @@ The method returns a value with the same data type as the first parameter.
CALL TRUNCATE_VALUE(X, 10, TRUE);
"
"Functions (System)","UNNEST","
UNNEST(array, [,...]) [WITH ORDINALITY]
","
Returns the result set.
Number of columns is equal to number of arguments,
plus one additional column with row number if WITH ORDINALITY is specified.
Number of rows is equal to length of longest specified array.
If multiple arguments are specified and they have different length, cells with missing values will contain null values.
","
SELECT * FROM UNNEST(ARRAY['a', 'b', 'c']);
"
"Functions (System)","USER","
{ USER | CURRENT_USER } ()
","
...
...
h2/src/main/org/h2/command/Parser.java
浏览文件 @
21036320
...
...
@@ -3406,6 +3406,23 @@ public class Parser {
tf
.
setColumns
(
columns
);
break
;
}
case
Function
.
UNNEST
:
{
ArrayList
<
Column
>
columns
=
Utils
.
newSmallArrayList
();
if
(!
readIf
(
CLOSE_PAREN
))
{
int
i
=
0
;
do
{
function
.
setParameter
(
i
++,
readExpression
());
columns
.
add
(
new
Column
(
"C"
+
i
,
Value
.
NULL
));
}
while
(
readIfMore
(
true
));
}
if
(
readIf
(
WITH
))
{
read
(
"ORDINALITY"
);
columns
.
add
(
new
Column
(
"NORD"
,
Value
.
INT
));
}
TableFunction
tf
=
(
TableFunction
)
function
;
tf
.
setColumns
(
columns
);
break
;
}
default
:
if
(!
readIf
(
CLOSE_PAREN
))
{
int
i
=
0
;
...
...
h2/src/main/org/h2/expression/function/Function.java
浏览文件 @
21036320
...
...
@@ -134,7 +134,8 @@ public class Function extends Expression implements FunctionCall {
ARRAY_LENGTH
=
217
,
LINK_SCHEMA
=
218
,
GREATEST
=
219
,
LEAST
=
220
,
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
;
NVL2
=
228
,
DECODE
=
229
,
ARRAY_CONTAINS
=
230
,
FILE_WRITE
=
232
,
UNNEST
=
233
;
public
static
final
int
REGEXP_LIKE
=
240
;
...
...
@@ -459,10 +460,9 @@ public class Function extends Expression implements FunctionCall {
addFunction
(
"H2VERSION"
,
H2VERSION
,
0
,
Value
.
STRING
);
// TableFunction
addFunctionWithNull
(
"TABLE"
,
TABLE
,
VAR_ARGS
,
Value
.
RESULT_SET
);
addFunctionWithNull
(
"TABLE_DISTINCT"
,
TABLE_DISTINCT
,
VAR_ARGS
,
Value
.
RESULT_SET
);
addFunctionWithNull
(
"TABLE"
,
TABLE
,
VAR_ARGS
,
Value
.
RESULT_SET
);
addFunctionWithNull
(
"TABLE_DISTINCT"
,
TABLE_DISTINCT
,
VAR_ARGS
,
Value
.
RESULT_SET
);
addFunctionWithNull
(
"UNNEST"
,
UNNEST
,
VAR_ARGS
,
Value
.
RESULT_SET
);
// ON DUPLICATE KEY VALUES function
addFunction
(
"VALUES"
,
VALUES
,
1
,
Value
.
NULL
,
false
,
true
,
false
,
true
);
...
...
@@ -538,6 +538,7 @@ public class Function extends Expression implements FunctionCall {
switch
(
info
.
type
)
{
case
TABLE:
case
TABLE_DISTINCT:
case
UNNEST:
return
new
TableFunction
(
database
,
info
,
Long
.
MAX_VALUE
);
default
:
return
new
Function
(
database
,
info
);
...
...
h2/src/main/org/h2/expression/function/TableFunction.java
浏览文件 @
21036320
...
...
@@ -17,6 +17,7 @@ import org.h2.result.LocalResult;
import
org.h2.table.Column
;
import
org.h2.value.Value
;
import
org.h2.value.ValueArray
;
import
org.h2.value.ValueInt
;
import
org.h2.value.ValueNull
;
import
org.h2.value.ValueResultSet
;
...
...
@@ -24,19 +25,17 @@ import org.h2.value.ValueResultSet;
* Implementation of the functions TABLE(..) and TABLE_DISTINCT(..).
*/
public
class
TableFunction
extends
Function
{
private
final
boolean
distinct
;
private
final
long
rowCount
;
private
Column
[]
column
List
;
private
Column
[]
column
s
;
TableFunction
(
Database
database
,
FunctionInfo
info
,
long
rowCount
)
{
super
(
database
,
info
);
distinct
=
info
.
type
==
Function
.
TABLE_DISTINCT
;
this
.
rowCount
=
rowCount
;
}
@Override
public
Value
getValue
(
Session
session
)
{
return
getTable
(
session
,
args
,
false
,
distinct
);
return
getTable
(
session
,
false
);
}
@Override
...
...
@@ -48,52 +47,60 @@ public class TableFunction extends Function {
@Override
public
StringBuilder
getSQL
(
StringBuilder
builder
)
{
if
(
info
.
type
==
UNNEST
)
{
super
.
getSQL
(
builder
);
if
(
args
.
length
<
columns
.
length
)
{
builder
.
append
(
" WITH ORDINALITY"
);
}
return
builder
;
}
builder
.
append
(
getName
()).
append
(
'('
);
for
(
int
i
=
0
;
i
<
args
.
length
;
i
++)
{
if
(
i
>
0
)
{
builder
.
append
(
", "
);
}
builder
.
append
(
column
List
[
i
].
getCreateSQL
()).
append
(
'='
);
builder
.
append
(
column
s
[
i
].
getCreateSQL
()).
append
(
'='
);
args
[
i
].
getSQL
(
builder
);
}
return
builder
.
append
(
')'
);
}
@Override
public
String
getName
()
{
return
distinct
?
"TABLE_DISTINCT"
:
"TABLE"
;
}
@Override
public
ValueResultSet
getValueForColumnList
(
Session
session
,
Expression
[]
nullArgs
)
{
return
getTable
(
session
,
args
,
true
,
fals
e
);
return
getTable
(
session
,
tru
e
);
}
public
void
setColumns
(
ArrayList
<
Column
>
columns
)
{
this
.
column
List
=
columns
.
toArray
(
new
Column
[
0
]);
this
.
column
s
=
columns
.
toArray
(
new
Column
[
0
]);
}
private
ValueResultSet
getTable
(
Session
session
,
Expression
[]
argList
,
boolean
onlyColumnList
,
boolean
distinctRows
)
{
int
len
=
columnList
.
length
;
Expression
[]
header
=
new
Expression
[
len
];
private
ValueResultSet
getTable
(
Session
session
,
boolean
onlyColumnList
)
{
int
totalColumns
=
columns
.
length
;
Expression
[]
header
=
new
Expression
[
totalColumns
];
Database
db
=
session
.
getDatabase
();
for
(
int
i
=
0
;
i
<
len
;
i
++)
{
Column
c
=
column
List
[
i
];
for
(
int
i
=
0
;
i
<
totalColumns
;
i
++)
{
Column
c
=
column
s
[
i
];
ExpressionColumn
col
=
new
ExpressionColumn
(
db
,
c
);
header
[
i
]
=
col
;
}
LocalResult
result
=
db
.
getResultFactory
().
create
(
session
,
header
,
len
);
if
(
distinctRows
)
{
LocalResult
result
=
db
.
getResultFactory
().
create
(
session
,
header
,
totalColumns
);
if
(
!
onlyColumnList
&&
info
.
type
==
TABLE_DISTINCT
)
{
result
.
setDistinct
();
}
if
(!
onlyColumnList
)
{
int
len
=
totalColumns
;
boolean
unnest
=
info
.
type
==
UNNEST
,
addNumber
=
false
;
if
(
unnest
)
{
len
=
args
.
length
;
if
(
len
<
totalColumns
)
{
addNumber
=
true
;
}
}
Value
[][]
list
=
new
Value
[
len
][];
int
rows
=
0
;
for
(
int
i
=
0
;
i
<
len
;
i
++)
{
Value
v
=
arg
List
[
i
].
getValue
(
session
);
Value
v
=
arg
s
[
i
].
getValue
(
session
);
if
(
v
==
ValueNull
.
INSTANCE
)
{
list
[
i
]
=
new
Value
[
0
];
}
else
{
...
...
@@ -104,21 +111,25 @@ public class TableFunction extends Function {
}
}
for
(
int
row
=
0
;
row
<
rows
;
row
++)
{
Value
[]
r
=
new
Value
[
len
];
Value
[]
r
=
new
Value
[
totalColumns
];
for
(
int
j
=
0
;
j
<
len
;
j
++)
{
Value
[]
l
=
list
[
j
];
Value
v
;
if
(
l
.
length
<=
row
)
{
v
=
ValueNull
.
INSTANCE
;
}
else
{
Column
c
=
column
List
[
j
];
Column
c
=
column
s
[
j
];
v
=
l
[
row
];
v
=
c
.
convert
(
v
);
v
=
v
.
convertPrecision
(
c
.
getPrecision
(),
false
);
v
=
v
.
convertScale
(
true
,
c
.
getScale
());
if
(!
unnest
)
{
v
=
c
.
convert
(
v
).
convertPrecision
(
c
.
getPrecision
(),
false
)
.
convertScale
(
true
,
c
.
getScale
());
}
}
r
[
j
]
=
v
;
}
if
(
addNumber
)
{
r
[
len
]
=
ValueInt
.
get
(
row
+
1
);
}
result
.
addRow
(
r
);
}
}
...
...
@@ -132,7 +143,7 @@ public class TableFunction extends Function {
@Override
public
Expression
[]
getExpressionColumns
(
Session
session
)
{
return
getExpressionColumns
(
session
,
get
Table
(
session
,
getArgs
(),
true
,
false
).
getResult
());
return
getExpressionColumns
(
session
,
get
ValueForColumnList
(
session
,
null
).
getResult
());
}
}
h2/src/test/org/h2/test/scripts/TestScript.java
浏览文件 @
21036320
...
...
@@ -194,7 +194,7 @@ public class TestScript extends TestDb {
"ifnull"
,
"least"
,
"link-schema"
,
"lock-mode"
,
"lock-timeout"
,
"memory-free"
,
"memory-used"
,
"nextval"
,
"nullif"
,
"nvl2"
,
"readonly"
,
"rownum"
,
"schema"
,
"scope-identity"
,
"session-id"
,
"set"
,
"table"
,
"transaction-id"
,
"truncate-value"
,
"user"
})
{
"set"
,
"table"
,
"transaction-id"
,
"truncate-value"
,
"u
nnest"
,
"u
ser"
})
{
testScript
(
"functions/system/"
+
s
+
".sql"
);
}
for
(
String
s
:
new
String
[]
{
"add_months"
,
"current_date"
,
"current_timestamp"
,
...
...
h2/src/test/org/h2/test/scripts/functions/system/unnest.sql
0 → 100644
浏览文件 @
21036320
-- 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
--
SELECT
*
FROM
UNNEST
();
>
exception
INVALID_PARAMETER_COUNT_2
SELECT
*
FROM
UNNEST
(
ARRAY
[]);
>
C1
>
--
>
rows
:
0
SELECT
*
FROM
UNNEST
(
ARRAY
[
1
,
2
,
3
]);
>
C1
>
--
>
1
>
2
>
3
>
rows
:
3
SELECT
*
FROM
UNNEST
(
ARRAY
[
1
],
ARRAY
[
2
,
3
,
4
],
ARRAY
[
5
,
6
]);
>
C1
C2
C3
>
---- -- ----
>
1
2
5
>
null
3
6
>
null
4
null
>
rows
:
3
SELECT
*
FROM
UNNEST
(
ARRAY
[
1
],
ARRAY
[
2
,
3
,
4
],
ARRAY
[
5
,
6
])
WITH
ORDINALITY
;
>
C1
C2
C3
NORD
>
---- -- ---- ----
>
1
2
5
1
>
null
3
6
2
>
null
4
null
3
>
rows
:
3
EXPLAIN
SELECT
*
FROM
UNNEST
(
ARRAY
[
1
]);
>>
SELECT
UNNEST
.
C1
FROM
UNNEST
((
1
,))
/* function */
EXPLAIN
SELECT
*
FROM
UNNEST
(
ARRAY
[
1
])
WITH
ORDINALITY
;
>>
SELECT
UNNEST
.
C1
,
UNNEST
.
NORD
FROM
UNNEST
((
1
,))
WITH
ORDINALITY
/* function */
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论