Skip to content
项目
群组
代码片段
帮助
正在加载...
帮助
为 GitLab 提交贡献
登录/注册
切换导航
H
h2database
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
分枝图
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
计划
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
分枝图
统计图
创建新议题
作业
提交
议题看板
打开侧边栏
Administrator
h2database
Commits
f2c645a3
Unverified
提交
f2c645a3
authored
2月 06, 2019
作者:
Evgenij Ryazanov
提交者:
GitHub
2月 06, 2019
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #1721 from katzyn/dml
Fix derived column list in complex queries
上级
96c8f626
421d27bb
显示空白字符变更
内嵌
并排
正在显示
5 个修改的文件
包含
83 行增加
和
34 行删除
+83
-34
Select.java
h2/src/main/org/h2/command/dml/Select.java
+16
-23
ExpressionColumn.java
h2/src/main/org/h2/expression/ExpressionColumn.java
+13
-1
TableFilter.java
h2/src/main/org/h2/table/TableFilter.java
+15
-2
select.sql
h2/src/test/org/h2/test/scripts/dml/select.sql
+31
-0
histogram.sql
...est/org/h2/test/scripts/functions/aggregate/histogram.sql
+8
-8
没有找到文件。
h2/src/main/org/h2/command/dml/Select.java
浏览文件 @
f2c645a3
...
...
@@ -118,11 +118,6 @@ public class Select extends Query {
*/
boolean
[]
groupByExpression
;
/**
* Select with grouped data for aggregates.
*/
private
Select
groupSelect
;
/**
* Grouped data for aggregates.
*/
...
...
@@ -219,10 +214,6 @@ public class Select extends Query {
return
group
;
}
void
setGroupSelect
(
Select
groupSelect
)
{
this
.
groupSelect
=
groupSelect
;
}
/**
* Get the group data if there is currently a group-by active.
*
...
...
@@ -230,9 +221,6 @@ public class Select extends Query {
* @return the grouped data
*/
public
SelectGroups
getGroupDataIfCurrent
(
boolean
window
)
{
if
(
groupSelect
!=
null
)
{
return
groupSelect
.
getGroupDataIfCurrent
(
window
);
}
return
groupData
!=
null
&&
(
window
||
groupData
.
isCurrentGroup
())
?
groupData
:
null
;
}
...
...
@@ -481,13 +469,26 @@ public class Select extends Query {
private
void
initGroupData
(
int
columnCount
)
{
if
(
groupData
==
null
)
{
groupData
=
SelectGroups
.
getInstance
(
session
,
expressions
,
isGroupQuery
,
groupIndex
);
setGroupData
(
SelectGroups
.
getInstance
(
session
,
expressions
,
isGroupQuery
,
groupIndex
)
);
}
else
{
updateAgg
(
columnCount
,
DataAnalysisOperation
.
STAGE_RESET
);
}
groupData
.
reset
();
}
void
setGroupData
(
final
SelectGroups
groupData
)
{
this
.
groupData
=
groupData
;
topTableFilter
.
visit
(
new
TableFilterVisitor
()
{
@Override
public
void
accept
(
TableFilter
f
)
{
Select
s
=
f
.
getSelect
();
if
(
s
!=
null
)
{
s
.
groupData
=
groupData
;
}
}
});
}
private
void
gatherGroup
(
int
columnCount
,
int
stage
)
{
long
rowNumber
=
0
;
setCurrentRowNumber
(
0
);
...
...
@@ -1346,15 +1347,6 @@ public class Select extends Query {
isGroupSortedQuery
=
true
;
}
}
topTableFilter
.
visit
(
new
TableFilterVisitor
()
{
@Override
public
void
accept
(
TableFilter
f
)
{
Select
s
=
f
.
getSelect
();
if
(
s
!=
null
&&
s
!=
Select
.
this
)
{
s
.
setGroupSelect
(
Select
.
this
);
}
}
});
expressionArray
=
expressions
.
toArray
(
new
Expression
[
0
]);
isPrepared
=
true
;
}
...
...
@@ -1910,7 +1902,8 @@ public class Select extends Query {
LazyResultGroupSorted
(
Expression
[]
expressions
,
int
columnCount
)
{
super
(
expressions
,
columnCount
);
if
(
groupData
==
null
)
{
groupData
=
SelectGroups
.
getInstance
(
getSession
(),
Select
.
this
.
expressions
,
isGroupQuery
,
groupIndex
);
setGroupData
(
SelectGroups
.
getInstance
(
getSession
(),
Select
.
this
.
expressions
,
isGroupQuery
,
groupIndex
));
}
else
{
// TODO is this branch possible?
updateAgg
(
columnCount
,
DataAnalysisOperation
.
STAGE_RESET
);
...
...
h2/src/main/org/h2/expression/ExpressionColumn.java
浏览文件 @
f2c645a3
...
...
@@ -39,6 +39,7 @@ public class ExpressionColumn extends Expression {
private
ColumnResolver
columnResolver
;
private
int
queryLevel
;
private
Column
column
;
private
String
derivedName
;
public
ExpressionColumn
(
Database
database
,
Column
column
)
{
this
.
database
=
database
;
...
...
@@ -76,7 +77,11 @@ public class ExpressionColumn extends Expression {
builder
.
append
(
'.'
);
}
if
(
column
!=
null
)
{
if
(
derivedName
!=
null
)
{
Parser
.
quoteIdentifier
(
builder
,
derivedName
);
}
else
{
builder
.
append
(
column
.
getSQL
());
}
}
else
if
(
quote
)
{
Parser
.
quoteIdentifier
(
builder
,
columnName
);
}
else
{
...
...
@@ -101,11 +106,18 @@ public class ExpressionColumn extends Expression {
}
for
(
Column
col
:
resolver
.
getColumns
())
{
String
n
=
resolver
.
getDerivedColumnName
(
col
);
boolean
derived
;
if
(
n
==
null
)
{
n
=
col
.
getName
();
derived
=
false
;
}
else
{
derived
=
true
;
}
if
(
database
.
equalsIdentifiers
(
columnName
,
n
))
{
mapColumn
(
resolver
,
col
,
level
);
if
(
derived
)
{
derivedName
=
n
;
}
return
;
}
}
...
...
h2/src/main/org/h2/table/TableFilter.java
浏览文件 @
f2c645a3
...
...
@@ -7,6 +7,7 @@ package org.h2.table;
import
java.util.ArrayList
;
import
java.util.HashMap
;
import
java.util.LinkedHashMap
;
import
org.h2.api.ErrorCode
;
import
org.h2.command.Parser
;
...
...
@@ -122,7 +123,7 @@ public class TableFilter implements ColumnResolver {
private
final
int
hashCode
;
private
final
int
orderInFrom
;
private
HashMap
<
Column
,
String
>
derivedColumnMap
;
private
Linked
HashMap
<
Column
,
String
>
derivedColumnMap
;
/**
* Create a new table filter object.
...
...
@@ -801,6 +802,18 @@ public class TableFilter implements ColumnResolver {
if
(
alias
!=
null
)
{
builder
.
append
(
' '
);
Parser
.
quoteIdentifier
(
builder
,
alias
);
if
(
derivedColumnMap
!=
null
)
{
builder
.
append
(
'('
);
boolean
f
=
false
;
for
(
String
name
:
derivedColumnMap
.
values
())
{
if
(
f
)
{
builder
.
append
(
", "
);
}
f
=
true
;
Parser
.
quoteIdentifier
(
builder
,
name
);
}
builder
.
append
(
')'
);
}
}
if
(
indexHints
!=
null
)
{
builder
.
append
(
" USE INDEX ("
);
...
...
@@ -1091,7 +1104,7 @@ public class TableFilter implements ColumnResolver {
if
(
count
!=
derivedColumnNames
.
size
())
{
throw
DbException
.
get
(
ErrorCode
.
COLUMN_COUNT_DOES_NOT_MATCH
);
}
HashMap
<
Column
,
String
>
map
=
new
HashMap
<>();
LinkedHashMap
<
Column
,
String
>
map
=
new
Linked
HashMap
<>();
for
(
int
i
=
0
;
i
<
count
;
i
++)
{
String
alias
=
derivedColumnNames
.
get
(
i
);
for
(
int
j
=
0
;
j
<
i
;
j
++)
{
...
...
h2/src/test/org/h2/test/scripts/dml/select.sql
浏览文件 @
f2c645a3
...
...
@@ -602,3 +602,34 @@ FROM (SELECT 1 X), (VALUES (1, 2), (2, 1), (3, 3)) T(A, B);
>
1
2
1
>
1
3
3
>
rows
:
3
SELECT
A
,
SUM
(
S
)
OVER
(
ORDER
BY
S
)
FROM
(
SELECT
A
,
SUM
(
B
)
FROM
(
VALUES
(
1
,
2
),
(
1
,
3
),
(
3
,
5
),
(
3
,
10
))
V
(
A
,
B
)
GROUP
BY
A
)
S
(
A
,
S
);
>
A
SUM
(
S
)
OVER
(
ORDER
BY
S
)
>
-
------------------------
>
1
5
>
3
20
>
rows
:
2
SELECT
A
,
B
,
C
FROM
(
SELECT
A
,
B
,
C
FROM
(
VALUES
(
1
,
2
,
3
))
V
(
A
,
B
,
C
));
>
A
B
C
>
-
-
-
>
1
2
3
>
rows
:
1
SELECT
*
FROM
(
SELECT
*
FROM
(
VALUES
(
1
,
2
,
3
))
V
(
A
,
B
,
C
));
>
A
B
C
>
-
-
-
>
1
2
3
>
rows
:
1
SELECT
*
FROM
(
SELECT
X
*
X
,
Y
FROM
(
SELECT
A
+
5
,
B
FROM
(
VALUES
(
1
,
2
))
V
(
A
,
B
)
)
T
(
X
,
Y
)
);
>
X
*
X
Y
>
----- -
>
36
2
>
rows
:
1
h2/src/test/org/h2/test/scripts/functions/aggregate/histogram.sql
浏览文件 @
f2c645a3
...
...
@@ -4,20 +4,20 @@
--
SELECT
HISTOGRAM
(
X
),
HISTOGRAM
(
DISTINCT
X
)
FROM
VALUES
(
1
),
(
2
),
(
3
),
(
1
),
(
2
),
(
NULL
),
(
5
)
T
(
X
);
>
HISTOGRAM
(
C1
)
HISTOGRAM
(
DISTINCT
C1
)
>
HISTOGRAM
(
X
)
HISTOGRAM
(
DISTINCT
X
)
>
------------------------------------------- -------------------------------------------
>
[[
null
,
1
],
[
1
,
2
],
[
2
,
2
],
[
3
,
1
],
[
5
,
1
]]
[[
null
,
1
],
[
1
,
1
],
[
2
,
1
],
[
3
,
1
],
[
5
,
1
]]
>
rows
:
1
SELECT
HISTOGRAM
(
X
)
FILTER
(
WHERE
X
>
1
),
HISTOGRAM
(
DISTINCT
X
)
FILTER
(
WHERE
X
>
1
)
FROM
VALUES
(
1
),
(
2
),
(
3
),
(
1
),
(
2
),
(
NULL
),
(
5
)
T
(
X
);
>
HISTOGRAM
(
C1
)
FILTER
(
WHERE
(
C1
>
1
))
HISTOGRAM
(
DISTINCT
C1
)
FILTER
(
WHERE
(
C1
>
1
))
>
-----------------------------------
-- --
--------------------------------------------
>
HISTOGRAM
(
X
)
FILTER
(
WHERE
(
X
>
1
))
HISTOGRAM
(
DISTINCT
X
)
FILTER
(
WHERE
(
X
>
1
))
>
-----------------------------------
--------------------------------------------
>
[[
2
,
2
],
[
3
,
1
],
[
5
,
1
]]
[[
2
,
1
],
[
3
,
1
],
[
5
,
1
]]
>
rows
:
1
SELECT
HISTOGRAM
(
X
)
FILTER
(
WHERE
X
>
0
),
HISTOGRAM
(
DISTINCT
X
)
FILTER
(
WHERE
X
>
0
)
FROM
VALUES
(
0
)
T
(
X
);
>
HISTOGRAM
(
C1
)
FILTER
(
WHERE
(
C1
>
0
))
HISTOGRAM
(
DISTINCT
C1
)
FILTER
(
WHERE
(
C1
>
0
))
>
-----------------------------------
-- --
--------------------------------------------
>
HISTOGRAM
(
X
)
FILTER
(
WHERE
(
X
>
0
))
HISTOGRAM
(
DISTINCT
X
)
FILTER
(
WHERE
(
X
>
0
))
>
-----------------------------------
--------------------------------------------
>
[]
[]
>
rows
:
1
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论