Skip to content
项目
群组
代码片段
帮助
正在加载...
帮助
为 GitLab 提交贡献
登录/注册
切换导航
H
h2database
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
分枝图
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
计划
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
分枝图
统计图
创建新议题
作业
提交
议题看板
打开侧边栏
Administrator
h2database
Commits
0cb3d7fc
提交
0cb3d7fc
authored
8月 10, 2018
作者:
Evgenij Ryazanov
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Forbid mixed OFFSET/FETCH/LIMIT/TOP clauses and improve documentation
上级
90b08cd0
隐藏空白字符变更
内嵌
并排
正在显示
3 个修改的文件
包含
83 行增加
和
48 行删除
+83
-48
help.csv
h2/src/docsrc/help/help.csv
+11
-3
Parser.java
h2/src/main/org/h2/command/Parser.java
+50
-45
select.sql
h2/src/test/org/h2/test/scripts/dml/select.sql
+22
-0
没有找到文件。
h2/src/docsrc/help/help.csv
浏览文件 @
0cb3d7fc
...
...
@@ -22,9 +22,16 @@ HAVING filter rows after grouping.
ORDER BY sorts the result by the given column(s) or expression(s).
UNION combines the result of this query with the results of another query.
Number of rows in output can be limited either with standard OFFSET / FETCH,
with non-standard LIMIT / OFFSET, or with non-standard TOP clauses.
Different clauses cannot be used together.
FETCH FIRST/NEXT, LIMIT or TOP limits the number of rows returned by the query (no limit if null or smaller than zero).
OFFSET specified how many rows to skip.
Please note using high offset values should be avoided because it can cause performance problems.
If PERCENT is specified number of rows is specified as a percent of the total number of rows
and should be an integer value between 0 and 100 inclusive.
WITH TIES can be used only together with ORDER BY and means that all additional rows that have the same sorting position
as the last row will be also returned.
OFFSET specifies how many rows to skip.
Please note that queries with high offset values can be slow.
SAMPLE_SIZE limits the number of rows read for aggregate queries.
Multiple set operators (UNION, INTERSECT, MINUS, EXCEPT) are evaluated
...
...
@@ -41,7 +48,8 @@ SELECT * FROM TEST ORDER BY NAME;
SELECT ID, COUNT(*) FROM TEST GROUP BY ID;
SELECT NAME, COUNT(*) FROM TEST GROUP BY NAME HAVING COUNT(*) > 2;
SELECT 'ID' COL, MAX(ID) AS MAX FROM TEST UNION SELECT 'NAME', MAX(NAME) FROM TEST;
SELECT * FROM TEST LIMIT 1000;
SELECT * FROM TEST OFFSET 1000 ROWS FETCH NEXT 1000 ROWS ONLY;
SELECT A, B FROM TEST ORDER BY A FETCH NEXT 10 ROWS WITH TIES;
SELECT * FROM (SELECT ID, COUNT(*) FROM TEST
GROUP BY ID UNION SELECT NULL, COUNT(*) FROM TEST)
ORDER BY 1 NULLS LAST;
...
...
h2/src/main/org/h2/command/Parser.java
浏览文件 @
0cb3d7fc
...
...
@@ -2358,59 +2358,64 @@ public class Parser {
command
.
setOrder
(
orderList
);
currentSelect
=
oldSelect
;
}
// make sure aggregate functions will not work here
Select
temp
=
currentSelect
;
currentSelect
=
null
;
// Standard SQL OFFSET / FETCH
if
(
readIf
(
OFFSET
))
{
command
.
setOffset
(
readExpression
().
optimize
(
session
));
if
(!
readIf
(
"ROW"
))
{
readIf
(
"ROWS"
);
if
(
command
.
getLimit
()
==
null
)
{
// make sure aggregate functions will not work here
Select
temp
=
currentSelect
;
currentSelect
=
null
;
boolean
hasOffsetOrFetch
=
false
;
// Standard SQL OFFSET / FETCH
if
(
readIf
(
OFFSET
))
{
hasOffsetOrFetch
=
true
;
command
.
setOffset
(
readExpression
().
optimize
(
session
));
if
(!
readIf
(
"ROW"
))
{
readIf
(
"ROWS"
);
}
}
}
if
(
readIf
(
FETCH
))
{
if
(!
readIf
(
"FIRST"
))
{
read
(
"NEXT"
);
if
(
readIf
(
FETCH
))
{
hasOffsetOrFetch
=
true
;
if
(!
readIf
(
"FIRST"
))
{
read
(
"NEXT"
);
}
if
(
readIf
(
"ROW"
)
||
readIf
(
"ROWS"
))
{
command
.
setLimit
(
ValueExpression
.
get
(
ValueInt
.
get
(
1
)));
}
else
{
Expression
limit
=
readExpression
().
optimize
(
session
);
command
.
setLimit
(
limit
);
if
(
readIf
(
"PERCENT"
))
{
command
.
setFetchPercent
(
true
);
}
if
(!
readIf
(
"ROW"
))
{
read
(
"ROWS"
);
}
}
if
(
readIf
(
WITH
))
{
read
(
"TIES"
);
command
.
setWithTies
(
true
);
}
else
{
read
(
"ONLY"
);
}
}
if
(
readIf
(
"ROW"
)
||
readIf
(
"ROWS"
))
{
command
.
setLimit
(
ValueExpression
.
get
(
ValueInt
.
get
(
1
)));
}
else
{
// MySQL-style LIMIT / OFFSET
if
(!
hasOffsetOrFetch
&&
readIf
(
LIMIT
))
{
Expression
limit
=
readExpression
().
optimize
(
session
);
command
.
setLimit
(
limit
);
if
(
readIf
(
"PERCENT"
))
{
command
.
setFetchPercent
(
true
);
}
if
(!
readIf
(
"ROW"
))
{
read
(
"ROWS"
);
if
(
readIf
(
OFFSET
))
{
Expression
offset
=
readExpression
().
optimize
(
session
);
command
.
setOffset
(
offset
);
}
else
if
(
readIf
(
COMMA
))
{
// MySQL: [offset, ] rowcount
Expression
offset
=
limit
;
limit
=
readExpression
().
optimize
(
session
);
command
.
setOffset
(
offset
);
command
.
setLimit
(
limit
);
}
}
if
(
readIf
(
WITH
))
{
read
(
"TIES"
);
command
.
setWithTies
(
true
);
}
else
{
read
(
"ONLY"
);
if
(
readIf
(
"SAMPLE_SIZE"
))
{
Expression
sampleSize
=
readExpression
().
optimize
(
session
);
command
.
setSampleSize
(
sampleSize
);
}
currentSelect
=
temp
;
}
// MySQL-style LIMIT / OFFSET
if
(
readIf
(
LIMIT
))
{
Expression
limit
=
readExpression
().
optimize
(
session
);
command
.
setLimit
(
limit
);
if
(
readIf
(
OFFSET
))
{
Expression
offset
=
readExpression
().
optimize
(
session
);
command
.
setOffset
(
offset
);
}
else
if
(
readIf
(
COMMA
))
{
// MySQL: [offset, ] rowcount
Expression
offset
=
limit
;
limit
=
readExpression
().
optimize
(
session
);
command
.
setOffset
(
offset
);
command
.
setLimit
(
limit
);
}
}
if
(
readIf
(
"SAMPLE_SIZE"
))
{
Expression
sampleSize
=
readExpression
().
optimize
(
session
);
command
.
setSampleSize
(
sampleSize
);
}
currentSelect
=
temp
;
if
(
readIf
(
FOR
))
{
if
(
readIf
(
"UPDATE"
))
{
if
(
readIf
(
"OF"
))
{
...
...
h2/src/test/org/h2/test/scripts/dml/select.sql
浏览文件 @
0cb3d7fc
...
...
@@ -267,3 +267,25 @@ DROP TABLE TEST1;
DROP
TABLE
TEST2
;
>
ok
-- Disallowed mixed OFFSET/FETCH/LIMIT/TOP clauses
CREATE
TABLE
TEST
(
ID
BIGINT
);
>
ok
SELECT
TOP
1
ID
FROM
TEST
OFFSET
1
ROW
;
>
exception
SYNTAX_ERROR_1
SELECT
TOP
1
ID
FROM
TEST
FETCH
NEXT
ROW
ONLY
;
>
exception
SYNTAX_ERROR_1
SELECT
TOP
1
ID
FROM
TEST
LIMIT
1
;
>
exception
SYNTAX_ERROR_1
SELECT
ID
FROM
TEST
OFFSET
1
ROW
LIMIT
1
;
>
exception
SYNTAX_ERROR_1
SELECT
ID
FROM
TEST
FETCH
NEXT
ROW
ONLY
LIMIT
1
;
>
exception
SYNTAX_ERROR_1
DROP
TABLE
TEST
;
>
ok
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论