Skip to content
项目
群组
代码片段
帮助
正在加载...
帮助
为 GitLab 提交贡献
登录/注册
切换导航
H
h2database
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
分枝图
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
计划
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
分枝图
统计图
创建新议题
作业
提交
议题看板
打开侧边栏
Administrator
h2database
Commits
734243cd
提交
734243cd
authored
2月 25, 2019
作者:
Evgenij Ryazanov
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Fix DISTINCT ON with ORDER BY
上级
f5c5d3a1
显示空白字符变更
内嵌
并排
正在显示
4 个修改的文件
包含
65 行增加
和
6 行删除
+65
-6
Select.java
h2/src/main/org/h2/command/dml/Select.java
+1
-1
MVSortedTempResult.java
h2/src/main/org/h2/mvstore/db/MVSortedTempResult.java
+26
-4
LocalResultImpl.java
h2/src/main/org/h2/result/LocalResultImpl.java
+2
-1
distinct.sql
h2/src/test/org/h2/test/scripts/distinct.sql
+36
-0
没有找到文件。
h2/src/main/org/h2/command/dml/Select.java
浏览文件 @
734243cd
...
...
@@ -1825,7 +1825,7 @@ public class Select extends Query {
@Override
public
boolean
allowGlobalConditions
()
{
return
offsetExpr
==
null
&&
(
limitExpr
==
null
||
sort
==
null
);
return
offsetExpr
==
null
&&
(
limitExpr
==
null
&&
distinctExpressions
==
null
||
sort
==
null
);
}
public
SortOrder
getSortOrder
()
{
...
...
h2/src/main/org/h2/mvstore/db/MVSortedTempResult.java
浏览文件 @
734243cd
...
...
@@ -56,7 +56,12 @@ class MVSortedTempResult extends MVTempResult {
* {@link #contains(Value[])} method is invoked. Only the root result should
* have an index if required.
*/
private
MVMap
<
ValueRow
,
Boolean
>
index
;
private
MVMap
<
ValueRow
,
Object
>
index
;
/**
* Used for DISTINCT ON in presence of ORDER BY.
*/
private
ValueDataType
orderedDistinctOnType
;
/**
* Cursor for the {@link #next()} method.
...
...
@@ -172,7 +177,11 @@ class MVSortedTempResult extends MVTempResult {
if
(
distinct
&&
length
!=
visibleColumnCount
||
distinctIndexes
!=
null
)
{
int
count
=
distinctIndexes
!=
null
?
distinctIndexes
.
length
:
visibleColumnCount
;
ValueDataType
distinctType
=
new
ValueDataType
(
database
,
new
int
[
count
]);
Builder
<
ValueRow
,
Boolean
>
indexBuilder
=
new
MVMap
.
Builder
<
ValueRow
,
Boolean
>().
keyType
(
distinctType
);
Builder
<
ValueRow
,
Object
>
indexBuilder
=
new
MVMap
.
Builder
<
ValueRow
,
Object
>().
keyType
(
distinctType
);
if
(
distinctIndexes
!=
null
&&
sort
!=
null
)
{
indexBuilder
.
valueType
(
keyType
);
orderedDistinctOnType
=
keyType
;
}
index
=
store
.
openMap
(
"idx"
,
indexBuilder
);
}
}
...
...
@@ -189,9 +198,22 @@ class MVSortedTempResult extends MVTempResult {
newValues
[
i
]
=
values
[
distinctIndexes
[
i
]];
}
ValueRow
distinctRow
=
ValueRow
.
get
(
newValues
);
if
(
orderedDistinctOnType
==
null
)
{
if
(
index
.
putIfAbsent
(
distinctRow
,
true
)
!=
null
)
{
return
rowCount
;
}
}
else
{
ValueRow
previous
=
(
ValueRow
)
index
.
get
(
distinctRow
);
if
(
previous
==
null
)
{
index
.
put
(
distinctRow
,
key
);
}
else
if
(
orderedDistinctOnType
.
compare
(
previous
,
key
)
>
0
)
{
map
.
remove
(
previous
);
rowCount
--;
index
.
put
(
distinctRow
,
key
);
}
else
{
return
rowCount
;
}
}
}
else
if
(
expressions
.
length
!=
visibleColumnCount
)
{
ValueRow
distinctRow
=
ValueRow
.
get
(
Arrays
.
copyOf
(
values
,
visibleColumnCount
));
if
(
index
.
putIfAbsent
(
distinctRow
,
true
)
!=
null
)
{
...
...
h2/src/main/org/h2/result/LocalResultImpl.java
浏览文件 @
734243cd
...
...
@@ -316,7 +316,8 @@ public class LocalResultImpl implements LocalResult {
if
(
isAnyDistinct
())
{
if
(
distinctRows
!=
null
)
{
ValueRow
array
=
getDistinctRow
(
values
);
if
(!
distinctRows
.
containsKey
(
array
))
{
Value
[]
previous
=
distinctRows
.
get
(
array
);
if
(
previous
==
null
||
sort
!=
null
&&
sort
.
compare
(
previous
,
values
)
>
0
)
{
distinctRows
.
put
(
array
,
values
);
}
rowCount
=
distinctRows
.
size
();
...
...
h2/src/test/org/h2/test/scripts/distinct.sql
浏览文件 @
734243cd
...
...
@@ -144,6 +144,42 @@ SELECT DISTINCT ON(C1) C2 FROM TEST ORDER BY C1;
>
4
>
rows
(
ordered
):
3
SELECT
DISTINCT
ON
(
C1
)
C1
,
C4
,
C5
FROM
TEST
ORDER
BY
C1
,
C5
;
>
C1
C4
C5
>
-- -- --
>
1
4
5
>
2
8
9
>
3
1
1
>
rows
(
ordered
):
3
SELECT
DISTINCT
ON
(
C1
)
C1
,
C4
,
C5
FROM
TEST
ORDER
BY
C1
,
C5
DESC
;
>
C1
C4
C5
>
-- -- --
>
1
6
7
>
2
8
9
>
3
1
1
>
rows
(
ordered
):
3
SELECT
T1
.
C1
,
T2
.
C5
FROM
TEST
T1
JOIN
(
SELECT
DISTINCT
ON
(
C1
)
C1
,
C4
,
C5
FROM
TEST
ORDER
BY
C1
,
C5
)
T2
ON
T1
.
C4
=
T2
.
C4
ORDER
BY
T1
.
C1
;
>
C1
C5
>
-- --
>
1
5
>
2
9
>
3
1
>
rows
(
ordered
):
3
SELECT
T1
.
C1
,
T2
.
C5
FROM
TEST
T1
JOIN
(
SELECT
DISTINCT
ON
(
C1
)
C1
,
C4
,
C5
FROM
TEST
ORDER
BY
C1
,
C5
DESC
)
T2
ON
T1
.
C4
=
T2
.
C4
ORDER
BY
T1
.
C1
;
>
C1
C5
>
-- --
>
1
7
>
2
9
>
3
1
>
rows
(
ordered
):
3
EXPLAIN
SELECT
DISTINCT
ON
(
C1
)
C2
FROM
TEST
ORDER
BY
C1
;
>>
SELECT
DISTINCT
ON
(
C1
)
C2
FROM
PUBLIC
.
TEST
/* PUBLIC.TEST.tableScan */
ORDER
BY
=
C1
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论