Skip to content
项目
群组
代码片段
帮助
正在加载...
帮助
为 GitLab 提交贡献
登录/注册
切换导航
H
h2database
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
分枝图
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
计划
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
分枝图
统计图
创建新议题
作业
提交
议题看板
打开侧边栏
Administrator
h2database
Commits
8318df51
提交
8318df51
authored
6月 08, 2016
作者:
Vakhurin Sergey
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
add support for "with"-subqueries into "join" & "sub-query" statements
ALSO: fixed plan generation for "with"-subqueries
上级
b76d9b20
隐藏空白字符变更
内嵌
并排
正在显示
5 个修改的文件
包含
75 行增加
和
13 行删除
+75
-13
Parser.java
h2/src/main/org/h2/command/Parser.java
+8
-11
Select.java
h2/src/main/org/h2/command/dml/Select.java
+16
-1
TableFilter.java
h2/src/main/org/h2/table/TableFilter.java
+4
-1
TableView.java
h2/src/main/org/h2/table/TableView.java
+4
-0
testScript.sql
h2/src/test/org/h2/test/testScript.sql
+43
-0
没有找到文件。
h2/src/main/org/h2/command/Parser.java
浏览文件 @
8318df51
...
@@ -477,8 +477,8 @@ public class Parser {
...
@@ -477,8 +477,8 @@ public class Parser {
break
;
break
;
case
'w'
:
case
'w'
:
case
'W'
:
case
'W'
:
if
(
readIf
(
"WITH"
))
{
if
(
isToken
(
"WITH"
))
{
c
=
parse
With
();
c
=
parse
Select
();
}
}
break
;
break
;
case
';'
:
case
';'
:
...
@@ -999,7 +999,7 @@ public class Parser {
...
@@ -999,7 +999,7 @@ public class Parser {
// need to read ahead, it could be a nested union:
// need to read ahead, it could be a nested union:
// ((select 1) union (select 1))
// ((select 1) union (select 1))
}
}
boolean
select
=
isToken
(
"SELECT"
)
||
isToken
(
"FROM"
);
boolean
select
=
isToken
(
"SELECT"
)
||
isToken
(
"FROM"
)
||
isToken
(
"WITH"
)
;
parseIndex
=
start
;
parseIndex
=
start
;
read
();
read
();
return
select
;
return
select
;
...
@@ -1676,7 +1676,7 @@ public class Parser {
...
@@ -1676,7 +1676,7 @@ public class Parser {
readIf
(
"FOR"
);
readIf
(
"FOR"
);
}
}
}
}
if
(
isToken
(
"SELECT"
)
||
isToken
(
"FROM"
)
||
isToken
(
"("
))
{
if
(
isToken
(
"SELECT"
)
||
isToken
(
"FROM"
)
||
isToken
(
"("
)
||
isToken
(
"WITH"
)
)
{
command
.
setCommand
(
parseSelect
());
command
.
setCommand
(
parseSelect
());
}
else
if
(
readIf
(
"DELETE"
))
{
}
else
if
(
readIf
(
"DELETE"
))
{
command
.
setCommand
(
parseDelete
());
command
.
setCommand
(
parseDelete
());
...
@@ -1686,8 +1686,6 @@ public class Parser {
...
@@ -1686,8 +1686,6 @@ public class Parser {
command
.
setCommand
(
parseInsert
());
command
.
setCommand
(
parseInsert
());
}
else
if
(
readIf
(
"MERGE"
))
{
}
else
if
(
readIf
(
"MERGE"
))
{
command
.
setCommand
(
parseMerge
());
command
.
setCommand
(
parseMerge
());
}
else
if
(
readIf
(
"WITH"
))
{
command
.
setCommand
(
parseWith
());
}
else
{
}
else
{
throw
getSyntaxError
();
throw
getSyntaxError
();
}
}
...
@@ -1887,6 +1885,8 @@ public class Parser {
...
@@ -1887,6 +1885,8 @@ public class Parser {
read
(
")"
);
read
(
")"
);
return
command
;
return
command
;
}
}
if
(
readIf
(
"WITH"
))
return
parseWith
();
Select
select
=
parseSelectSimple
();
Select
select
=
parseSelectSimple
();
return
select
;
return
select
;
}
}
...
@@ -2772,12 +2772,9 @@ public class Parser {
...
@@ -2772,12 +2772,9 @@ public class Parser {
r
=
p
;
r
=
p
;
break
;
break
;
case
KEYWORD:
case
KEYWORD:
if
(
isToken
(
"SELECT"
)
||
isToken
(
"FROM"
))
{
if
(
isToken
(
"SELECT"
)
||
isToken
(
"FROM"
)
||
isToken
(
"WITH"
)
)
{
Query
query
=
parseSelect
();
Query
query
=
parseSelect
();
r
=
new
Subquery
(
query
);
r
=
new
Subquery
(
query
);
}
else
if
(
readIf
(
"WITH"
))
{
Query
query
=
parseWith
();
r
=
new
Subquery
(
query
);
}
else
{
}
else
{
throw
getSyntaxError
();
throw
getSyntaxError
();
}
}
...
@@ -4781,7 +4778,7 @@ public class Parser {
...
@@ -4781,7 +4778,7 @@ public class Parser {
view
.
setTemporary
(
true
);
view
.
setTemporary
(
true
);
session
.
addLocalTempTable
(
view
);
session
.
addLocalTempTable
(
view
);
view
.
setOnCommitDrop
(
true
);
view
.
setOnCommitDrop
(
true
);
Query
q
=
parseSelect
();
Query
q
=
parseSelect
Union
();
q
.
setPrepareAlways
(
true
);
q
.
setPrepareAlways
(
true
);
return
q
;
return
q
;
}
}
...
...
h2/src/main/org/h2/command/dml/Select.java
浏览文件 @
8318df51
...
@@ -38,6 +38,7 @@ import org.h2.table.IndexColumn;
...
@@ -38,6 +38,7 @@ import org.h2.table.IndexColumn;
import
org.h2.table.JoinBatch
;
import
org.h2.table.JoinBatch
;
import
org.h2.table.Table
;
import
org.h2.table.Table
;
import
org.h2.table.TableFilter
;
import
org.h2.table.TableFilter
;
import
org.h2.table.TableView
;
import
org.h2.util.New
;
import
org.h2.util.New
;
import
org.h2.util.StatementBuilder
;
import
org.h2.util.StatementBuilder
;
import
org.h2.util.StringUtils
;
import
org.h2.util.StringUtils
;
...
@@ -1082,7 +1083,21 @@ public class Select extends Query {
...
@@ -1082,7 +1083,21 @@ public class Select extends Query {
// but indexes may be set manually as well
// but indexes may be set manually as well
Expression
[]
exprList
=
expressions
.
toArray
(
Expression
[]
exprList
=
expressions
.
toArray
(
new
Expression
[
expressions
.
size
()]);
new
Expression
[
expressions
.
size
()]);
StatementBuilder
buff
=
new
StatementBuilder
(
"SELECT"
);
StatementBuilder
buff
=
new
StatementBuilder
();
for
(
TableFilter
f
:
topFilters
)
{
Table
t
=
f
.
getTable
();
if
((
t
instanceof
TableView
)
&&
((
TableView
)
t
).
isRecursive
())
{
buff
.
append
(
"WITH RECURSIVE "
).
append
(
t
.
getName
()).
append
(
'('
);
buff
.
resetCount
();
for
(
Column
c
:
t
.
getColumns
())
{
buff
.
appendExceptFirst
(
","
);
buff
.
append
(
c
.
getName
());
}
buff
.
append
(
") AS "
).
append
(
t
.
getSQL
()).
append
(
"\n"
);
}
}
buff
.
resetCount
();
buff
.
append
(
"SELECT"
);
if
(
distinct
)
{
if
(
distinct
)
{
buff
.
append
(
" DISTINCT"
);
buff
.
append
(
" DISTINCT"
);
}
}
...
...
h2/src/main/org/h2/table/TableFilter.java
浏览文件 @
8318df51
...
@@ -816,7 +816,10 @@ public class TableFilter implements ColumnResolver {
...
@@ -816,7 +816,10 @@ public class TableFilter implements ColumnResolver {
}
}
return
buff
.
toString
();
return
buff
.
toString
();
}
}
buff
.
append
(
table
.
getSQL
());
if
((
table
instanceof
TableView
)
&&
((
TableView
)
table
).
isRecursive
())
buff
.
append
(
table
.
getName
());
else
buff
.
append
(
table
.
getSQL
());
if
(
table
.
isView
()
&&
((
TableView
)
table
).
isInvalid
())
{
if
(
table
.
isView
()
&&
((
TableView
)
table
).
isInvalid
())
{
throw
DbException
.
get
(
ErrorCode
.
VIEW_IS_INVALID_2
,
table
.
getName
(),
"not compiled"
);
throw
DbException
.
get
(
ErrorCode
.
VIEW_IS_INVALID_2
,
table
.
getName
(),
"not compiled"
);
}
}
...
...
h2/src/main/org/h2/table/TableView.java
浏览文件 @
8318df51
...
@@ -579,6 +579,10 @@ public class TableView extends Table {
...
@@ -579,6 +579,10 @@ public class TableView extends Table {
return
result
;
return
result
;
}
}
public
boolean
isRecursive
()
{
return
recursive
;
}
@Override
@Override
public
boolean
isDeterministic
()
{
public
boolean
isDeterministic
()
{
if
(
recursive
||
viewQuery
==
null
)
{
if
(
recursive
||
viewQuery
==
null
)
{
...
...
h2/src/test/org/h2/test/testScript.sql
浏览文件 @
8318df51
...
@@ -10228,6 +10228,49 @@ select 0 from ((
...
@@ -10228,6 +10228,49 @@ select 0 from ((
}
;
}
;
>
update
count
:
0
>
update
count
:
0
explain
with
recursive
r
(
n
)
as
(
(
select
1
)
union
all
(
select
n
+
1
from
r
where
n
<
3
)
)
select
n
from
r
;
>
PLAN
>
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
>
WITH
RECURSIVE
R
(
N
)
AS
(
(
SELECT
1
FROM
SYSTEM_RANGE
(
1
,
1
)
/* PUBLIC.RANGE_INDEX */
)
UNION
ALL
(
SELECT
(
N
+
1
)
FROM
PUBLIC
.
R
/* PUBLIC.R.tableScan */
WHERE
N
<
3
)
)
SELECT
N
FROM
R
/* null */
>
rows
:
1
select
sum
(
n
)
from
(
with
recursive
r
(
n
)
as
(
(
select
1
)
union
all
(
select
n
+
1
from
r
where
n
<
3
)
)
select
n
from
r
);
>
SUM
(
N
)
>
------
>
6
>
rows
:
1
select
sum
(
n
)
from
(
select
0
)
join
(
with
recursive
r
(
n
)
as
(
(
select
1
)
union
all
(
select
n
+
1
from
r
where
n
<
3
)
)
select
n
from
r
)
on
1
=
1
;
>
SUM
(
N
)
>
------
>
6
>
rows
:
1
select
0
from
(
select
0
where
0
in
(
with
recursive
r
(
n
)
as
(
(
select
1
)
union
all
(
select
n
+
1
from
r
where
n
<
3
)
)
select
n
from
r
)
);
>
0
>
-
>
rows
:
0
create
table
x
(
id
int
not
null
);
create
table
x
(
id
int
not
null
);
>
ok
>
ok
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论