Skip to content
项目
群组
代码片段
帮助
正在加载...
帮助
为 GitLab 提交贡献
登录/注册
切换导航
H
h2database
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
分枝图
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
计划
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
分枝图
统计图
创建新议题
作业
提交
议题看板
打开侧边栏
Administrator
h2database
Commits
41bfa75e
提交
41bfa75e
authored
5月 05, 2018
作者:
Noel Grandin
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
#1097 reduce memory overhead of the group by data structures
上级
6641518a
隐藏空白字符变更
内嵌
并排
正在显示
4 个修改的文件
包含
99 行增加
和
50 行删除
+99
-50
Select.java
h2/src/main/org/h2/command/dml/Select.java
+81
-24
Aggregate.java
h2/src/main/org/h2/expression/Aggregate.java
+5
-9
ExpressionColumn.java
h2/src/main/org/h2/expression/ExpressionColumn.java
+5
-7
JavaAggregate.java
h2/src/main/org/h2/expression/JavaAggregate.java
+8
-10
没有找到文件。
h2/src/main/org/h2/command/dml/Select.java
浏览文件 @
41bfa75e
...
@@ -81,9 +81,21 @@ public class Select extends Query {
...
@@ -81,9 +81,21 @@ public class Select extends Query {
boolean
[]
groupByExpression
;
boolean
[]
groupByExpression
;
/**
/**
* The
current group-by values
.
* The
array of current group-by expression data e.g. AggregateData
.
*/
*/
HashMap
<
Expression
,
Object
>
currentGroup
;
Object
[]
currentGroupByExprData
;
/**
* Maps an expression object to an index, to use in accessing the Object[] pointed to by groupByData.
*/
private
final
HashMap
<
Expression
,
Integer
>
exprToIndexInGroupByData
=
new
HashMap
<>();
/**
* Map of group-by key to group-by expression data e.g. AggregateData
*/
private
ValueHashMap
<
Object
[]>
groupByData
;
/**
* Key into groupByData that produces currentGroupByExprData. Not used in lazy mode.
*/
private
ValueArray
currentGroupsKey
;
private
int
havingIndex
;
private
int
havingIndex
;
private
boolean
isGroupQuery
,
isGroupSortedQuery
;
private
boolean
isGroupQuery
,
isGroupSortedQuery
;
...
@@ -151,8 +163,47 @@ public class Select extends Query {
...
@@ -151,8 +163,47 @@ public class Select extends Query {
return
group
;
return
group
;
}
}
public
HashMap
<
Expression
,
Object
>
getCurrentGroup
()
{
/**
return
currentGroup
;
* Is there currently a group-by active
*/
public
boolean
isCurrentGroup
()
{
return
currentGroupByExprData
!=
null
;
}
/**
* Get the group-by data for the current group and the passed in expression.
*/
public
Object
getCurrentGroupExprData
(
Expression
expr
)
{
Integer
index
=
exprToIndexInGroupByData
.
get
(
expr
);
if
(
index
==
null
)
{
return
null
;
}
return
currentGroupByExprData
[
index
];
}
/**
* Set the group-by data for the current group and the passed in expression.
*/
public
void
setCurrentGroupExprData
(
Expression
expr
,
Object
obj
)
{
Integer
index
=
exprToIndexInGroupByData
.
get
(
expr
);
if
(
index
!=
null
)
{
if
(
currentGroupByExprData
[
index
]
!=
null
)
{
throw
DbException
.
throwInternalError
();
}
currentGroupByExprData
[
index
]
=
obj
;
return
;
}
index
=
exprToIndexInGroupByData
.
size
();
exprToIndexInGroupByData
.
put
(
expr
,
index
);
if
(
index
>=
currentGroupByExprData
.
length
)
{
currentGroupByExprData
=
Arrays
.
copyOf
(
currentGroupByExprData
,
currentGroupByExprData
.
length
*
2
);
// this can be null in lazy mode
if
(
currentGroupsKey
!=
null
)
{
// since we changed the size of the array, update the object in the groups map
groupByData
.
put
(
currentGroupsKey
,
currentGroupByExprData
);
}
}
currentGroupByExprData
[
index
]
=
obj
;
}
}
public
int
getCurrentGroupRowId
()
{
public
int
getCurrentGroupRowId
()
{
...
@@ -313,20 +364,20 @@ public class Select extends Query {
...
@@ -313,20 +364,20 @@ public class Select extends Query {
}
}
private
void
queryGroup
(
int
columnCount
,
LocalResult
result
)
{
private
void
queryGroup
(
int
columnCount
,
LocalResult
result
)
{
ValueHashMap
<
HashMap
<
Expression
,
Object
>>
groups
=
groupByData
=
ValueHashMap
.
newInstance
();
ValueHashMap
.
newInstance
();
currentGroupByExprData
=
null
;
currentGroupsKey
=
null
;
exprToIndexInGroupByData
.
clear
();
int
rowNumber
=
0
;
int
rowNumber
=
0
;
setCurrentRowNumber
(
0
);
setCurrentRowNumber
(
0
);
currentGroup
=
null
;
ValueArray
defaultGroup
=
ValueArray
.
get
(
new
Value
[
0
]);
ValueArray
defaultGroup
=
ValueArray
.
get
(
new
Value
[
0
]);
int
sampleSize
=
getSampleSizeValue
(
session
);
int
sampleSize
=
getSampleSizeValue
(
session
);
while
(
topTableFilter
.
next
())
{
while
(
topTableFilter
.
next
())
{
setCurrentRowNumber
(
rowNumber
+
1
);
setCurrentRowNumber
(
rowNumber
+
1
);
if
(
isConditionMet
())
{
if
(
isConditionMet
())
{
Value
key
;
rowNumber
++;
rowNumber
++;
if
(
groupIndex
==
null
)
{
if
(
groupIndex
==
null
)
{
k
ey
=
defaultGroup
;
currentGroupsK
ey
=
defaultGroup
;
}
else
{
}
else
{
Value
[]
keyValues
=
new
Value
[
groupIndex
.
length
];
Value
[]
keyValues
=
new
Value
[
groupIndex
.
length
];
// update group
// update group
...
@@ -335,14 +386,14 @@ public class Select extends Query {
...
@@ -335,14 +386,14 @@ public class Select extends Query {
Expression
expr
=
expressions
.
get
(
idx
);
Expression
expr
=
expressions
.
get
(
idx
);
keyValues
[
i
]
=
expr
.
getValue
(
session
);
keyValues
[
i
]
=
expr
.
getValue
(
session
);
}
}
k
ey
=
ValueArray
.
get
(
keyValues
);
currentGroupsK
ey
=
ValueArray
.
get
(
keyValues
);
}
}
HashMap
<
Expression
,
Object
>
values
=
groups
.
get
(
k
ey
);
Object
[]
values
=
groupByData
.
get
(
currentGroupsK
ey
);
if
(
values
==
null
)
{
if
(
values
==
null
)
{
values
=
new
HashMap
<>()
;
values
=
new
Object
[
Math
.
max
(
exprToIndexInGroupByData
.
size
(),
expressions
.
size
())]
;
group
s
.
put
(
k
ey
,
values
);
group
ByData
.
put
(
currentGroupsK
ey
,
values
);
}
}
currentGroup
=
values
;
currentGroup
ByExprData
=
values
;
currentGroupRowId
++;
currentGroupRowId
++;
for
(
int
i
=
0
;
i
<
columnCount
;
i
++)
{
for
(
int
i
=
0
;
i
<
columnCount
;
i
++)
{
if
(
groupByExpression
==
null
||
!
groupByExpression
[
i
])
{
if
(
groupByExpression
==
null
||
!
groupByExpression
[
i
])
{
...
@@ -355,14 +406,14 @@ public class Select extends Query {
...
@@ -355,14 +406,14 @@ public class Select extends Query {
}
}
}
}
}
}
if
(
groupIndex
==
null
&&
group
s
.
size
()
==
0
)
{
if
(
groupIndex
==
null
&&
group
ByData
.
size
()
==
0
)
{
group
s
.
put
(
defaultGroup
,
new
HashMap
<
Expression
,
Object
>()
);
group
ByData
.
put
(
defaultGroup
,
new
Object
[
Math
.
max
(
exprToIndexInGroupByData
.
size
(),
expressions
.
size
())]
);
}
}
ArrayList
<
Value
>
keys
=
group
s
.
keys
();
ArrayList
<
Value
>
keys
=
group
ByData
.
keys
();
for
(
Value
v
:
keys
)
{
for
(
Value
v
:
keys
)
{
ValueArray
k
ey
=
(
ValueArray
)
v
;
currentGroupsK
ey
=
(
ValueArray
)
v
;
currentGroup
=
groups
.
get
(
k
ey
);
currentGroup
ByExprData
=
groupByData
.
get
(
currentGroupsK
ey
);
Value
[]
keyValues
=
k
ey
.
getList
();
Value
[]
keyValues
=
currentGroupsK
ey
.
getList
();
Value
[]
row
=
new
Value
[
columnCount
];
Value
[]
row
=
new
Value
[
columnCount
];
for
(
int
j
=
0
;
groupIndex
!=
null
&&
j
<
groupIndex
.
length
;
j
++)
{
for
(
int
j
=
0
;
groupIndex
!=
null
&&
j
<
groupIndex
.
length
;
j
++)
{
row
[
groupIndex
[
j
]]
=
keyValues
[
j
];
row
[
groupIndex
[
j
]]
=
keyValues
[
j
];
...
@@ -380,6 +431,10 @@ public class Select extends Query {
...
@@ -380,6 +431,10 @@ public class Select extends Query {
row
=
keepOnlyDistinct
(
row
,
columnCount
);
row
=
keepOnlyDistinct
(
row
,
columnCount
);
result
.
addRow
(
row
);
result
.
addRow
(
row
);
}
}
groupByData
=
null
;
currentGroupsKey
=
null
;
currentGroupByExprData
=
null
;
exprToIndexInGroupByData
.
clear
();
}
}
/**
/**
...
@@ -1475,13 +1530,15 @@ public class Select extends Query {
...
@@ -1475,13 +1530,15 @@ public class Select extends Query {
LazyResultGroupSorted
(
Expression
[]
expressions
,
int
columnCount
)
{
LazyResultGroupSorted
(
Expression
[]
expressions
,
int
columnCount
)
{
super
(
expressions
,
columnCount
);
super
(
expressions
,
columnCount
);
currentGroup
=
null
;
currentGroupByExprData
=
null
;
currentGroupsKey
=
null
;
}
}
@Override
@Override
public
void
reset
()
{
public
void
reset
()
{
super
.
reset
();
super
.
reset
();
currentGroup
=
null
;
currentGroupByExprData
=
null
;
currentGroupsKey
=
null
;
}
}
@Override
@Override
...
@@ -1501,11 +1558,11 @@ public class Select extends Query {
...
@@ -1501,11 +1558,11 @@ public class Select extends Query {
Value
[]
row
=
null
;
Value
[]
row
=
null
;
if
(
previousKeyValues
==
null
)
{
if
(
previousKeyValues
==
null
)
{
previousKeyValues
=
keyValues
;
previousKeyValues
=
keyValues
;
currentGroup
=
new
HashMap
<>()
;
currentGroup
ByExprData
=
new
Object
[
Math
.
max
(
exprToIndexInGroupByData
.
size
(),
expressions
.
size
())]
;
}
else
if
(!
Arrays
.
equals
(
previousKeyValues
,
keyValues
))
{
}
else
if
(!
Arrays
.
equals
(
previousKeyValues
,
keyValues
))
{
row
=
createGroupSortedRow
(
previousKeyValues
,
columnCount
);
row
=
createGroupSortedRow
(
previousKeyValues
,
columnCount
);
previousKeyValues
=
keyValues
;
previousKeyValues
=
keyValues
;
currentGroup
=
new
HashMap
<>()
;
currentGroup
ByExprData
=
new
Object
[
Math
.
max
(
exprToIndexInGroupByData
.
size
(),
expressions
.
size
())]
;
}
}
currentGroupRowId
++;
currentGroupRowId
++;
...
...
h2/src/main/org/h2/expression/Aggregate.java
浏览文件 @
41bfa75e
...
@@ -284,8 +284,7 @@ public class Aggregate extends Expression {
...
@@ -284,8 +284,7 @@ public class Aggregate extends Expression {
// if (on != null) {
// if (on != null) {
// on.updateAggregate();
// on.updateAggregate();
// }
// }
HashMap
<
Expression
,
Object
>
group
=
select
.
getCurrentGroup
();
if
(!
select
.
isCurrentGroup
())
{
if
(
group
==
null
)
{
// this is a different level (the enclosing query)
// this is a different level (the enclosing query)
return
;
return
;
}
}
...
@@ -297,10 +296,10 @@ public class Aggregate extends Expression {
...
@@ -297,10 +296,10 @@ public class Aggregate extends Expression {
}
}
lastGroupRowId
=
groupRowId
;
lastGroupRowId
=
groupRowId
;
AggregateData
data
=
(
AggregateData
)
group
.
get
(
this
);
AggregateData
data
=
(
AggregateData
)
select
.
getCurrentGroupExprData
(
this
);
if
(
data
==
null
)
{
if
(
data
==
null
)
{
data
=
AggregateData
.
create
(
type
);
data
=
AggregateData
.
create
(
type
);
group
.
put
(
this
,
data
);
select
.
setCurrentGroupExprData
(
this
,
data
);
}
}
Value
v
=
on
==
null
?
null
:
on
.
getValue
(
session
);
Value
v
=
on
==
null
?
null
:
on
.
getValue
(
session
);
if
(
type
==
AggregateType
.
GROUP_CONCAT
)
{
if
(
type
==
AggregateType
.
GROUP_CONCAT
)
{
...
@@ -373,13 +372,10 @@ public class Aggregate extends Expression {
...
@@ -373,13 +372,10 @@ public class Aggregate extends Expression {
DbException
.
throwInternalError
(
"type="
+
type
);
DbException
.
throwInternalError
(
"type="
+
type
);
}
}
}
}
HashMap
<
Expression
,
Object
>
group
=
select
.
getCurrentGroup
();
AggregateData
data
=
(
AggregateData
)
select
.
getCurrentGroupExprData
(
this
);
if
(
group
==
null
)
{
throw
DbException
.
get
(
ErrorCode
.
INVALID_USE_OF_AGGREGATE_FUNCTION_1
,
getSQL
());
}
AggregateData
data
=
(
AggregateData
)
group
.
get
(
this
);
if
(
data
==
null
)
{
if
(
data
==
null
)
{
data
=
AggregateData
.
create
(
type
);
data
=
AggregateData
.
create
(
type
);
select
.
setCurrentGroupExprData
(
this
,
data
);
}
}
if
(
type
==
AggregateType
.
GROUP_CONCAT
)
{
if
(
type
==
AggregateType
.
GROUP_CONCAT
)
{
Value
[]
array
=
((
AggregateDataCollecting
)
data
).
getArray
();
Value
[]
array
=
((
AggregateDataCollecting
)
data
).
getArray
();
...
...
h2/src/main/org/h2/expression/ExpressionColumn.java
浏览文件 @
41bfa75e
...
@@ -159,14 +159,13 @@ public class ExpressionColumn extends Expression {
...
@@ -159,14 +159,13 @@ public class ExpressionColumn extends Expression {
if
(
select
==
null
)
{
if
(
select
==
null
)
{
throw
DbException
.
get
(
ErrorCode
.
MUST_GROUP_BY_COLUMN_1
,
getSQL
());
throw
DbException
.
get
(
ErrorCode
.
MUST_GROUP_BY_COLUMN_1
,
getSQL
());
}
}
HashMap
<
Expression
,
Object
>
values
=
select
.
getCurrentGroup
();
if
(!
select
.
isCurrentGroup
())
{
if
(
values
==
null
)
{
// this is a different level (the enclosing query)
// this is a different level (the enclosing query)
return
;
return
;
}
}
Value
v
=
(
Value
)
values
.
get
(
this
);
Value
v
=
(
Value
)
select
.
getCurrentGroupExprData
(
this
);
if
(
v
==
null
)
{
if
(
v
==
null
)
{
values
.
put
(
this
,
now
);
select
.
setCurrentGroupExprData
(
this
,
now
);
}
else
{
}
else
{
if
(!
database
.
areEqual
(
now
,
v
))
{
if
(!
database
.
areEqual
(
now
,
v
))
{
throw
DbException
.
get
(
ErrorCode
.
MUST_GROUP_BY_COLUMN_1
,
getSQL
());
throw
DbException
.
get
(
ErrorCode
.
MUST_GROUP_BY_COLUMN_1
,
getSQL
());
...
@@ -178,9 +177,8 @@ public class ExpressionColumn extends Expression {
...
@@ -178,9 +177,8 @@ public class ExpressionColumn extends Expression {
public
Value
getValue
(
Session
session
)
{
public
Value
getValue
(
Session
session
)
{
Select
select
=
columnResolver
.
getSelect
();
Select
select
=
columnResolver
.
getSelect
();
if
(
select
!=
null
)
{
if
(
select
!=
null
)
{
HashMap
<
Expression
,
Object
>
values
=
select
.
getCurrentGroup
();
if
(
select
.
isCurrentGroup
())
{
if
(
values
!=
null
)
{
Value
v
=
(
Value
)
select
.
getCurrentGroupExprData
(
this
);
Value
v
=
(
Value
)
values
.
get
(
this
);
if
(
v
!=
null
)
{
if
(
v
!=
null
)
{
return
v
;
return
v
;
}
}
...
...
h2/src/main/org/h2/expression/JavaAggregate.java
浏览文件 @
41bfa75e
...
@@ -167,15 +167,14 @@ public class JavaAggregate extends Expression {
...
@@ -167,15 +167,14 @@ public class JavaAggregate extends Expression {
@Override
@Override
public
Value
getValue
(
Session
session
)
{
public
Value
getValue
(
Session
session
)
{
HashMap
<
Expression
,
Object
>
group
=
select
.
getCurrentGroup
();
if
(!
select
.
isCurrentGroup
())
{
if
(
group
==
null
)
{
throw
DbException
.
get
(
ErrorCode
.
INVALID_USE_OF_AGGREGATE_FUNCTION_1
,
getSQL
());
throw
DbException
.
get
(
ErrorCode
.
INVALID_USE_OF_AGGREGATE_FUNCTION_1
,
getSQL
());
}
}
try
{
try
{
Aggregate
agg
;
Aggregate
agg
;
if
(
distinct
)
{
if
(
distinct
)
{
agg
=
getInstance
();
agg
=
getInstance
();
AggregateDataCollecting
data
=
(
AggregateDataCollecting
)
group
.
get
(
this
);
AggregateDataCollecting
data
=
(
AggregateDataCollecting
)
select
.
getCurrentGroupExprData
(
this
);
if
(
data
!=
null
)
{
if
(
data
!=
null
)
{
for
(
Value
value
:
data
.
values
)
{
for
(
Value
value
:
data
.
values
)
{
if
(
args
.
length
==
1
)
{
if
(
args
.
length
==
1
)
{
...
@@ -191,7 +190,7 @@ public class JavaAggregate extends Expression {
...
@@ -191,7 +190,7 @@ public class JavaAggregate extends Expression {
}
}
}
}
}
else
{
}
else
{
agg
=
(
Aggregate
)
group
.
get
(
this
);
agg
=
(
Aggregate
)
select
.
getCurrentGroupExprData
(
this
);
if
(
agg
==
null
)
{
if
(
agg
==
null
)
{
agg
=
getInstance
();
agg
=
getInstance
();
}
}
...
@@ -208,8 +207,7 @@ public class JavaAggregate extends Expression {
...
@@ -208,8 +207,7 @@ public class JavaAggregate extends Expression {
@Override
@Override
public
void
updateAggregate
(
Session
session
)
{
public
void
updateAggregate
(
Session
session
)
{
HashMap
<
Expression
,
Object
>
group
=
select
.
getCurrentGroup
();
if
(!
select
.
isCurrentGroup
())
{
if
(
group
==
null
)
{
// this is a different level (the enclosing query)
// this is a different level (the enclosing query)
return
;
return
;
}
}
...
@@ -229,10 +227,10 @@ public class JavaAggregate extends Expression {
...
@@ -229,10 +227,10 @@ public class JavaAggregate extends Expression {
try
{
try
{
if
(
distinct
)
{
if
(
distinct
)
{
AggregateDataCollecting
data
=
(
AggregateDataCollecting
)
group
.
get
(
this
);
AggregateDataCollecting
data
=
(
AggregateDataCollecting
)
select
.
getCurrentGroupExprData
(
this
);
if
(
data
==
null
)
{
if
(
data
==
null
)
{
data
=
new
AggregateDataCollecting
();
data
=
new
AggregateDataCollecting
();
group
.
put
(
this
,
data
);
select
.
setCurrentGroupExprData
(
this
,
data
);
}
}
Value
[]
argValues
=
new
Value
[
args
.
length
];
Value
[]
argValues
=
new
Value
[
args
.
length
];
Value
arg
=
null
;
Value
arg
=
null
;
...
@@ -243,10 +241,10 @@ public class JavaAggregate extends Expression {
...
@@ -243,10 +241,10 @@ public class JavaAggregate extends Expression {
}
}
data
.
add
(
session
.
getDatabase
(),
dataType
,
true
,
args
.
length
==
1
?
arg
:
ValueArray
.
get
(
argValues
));
data
.
add
(
session
.
getDatabase
(),
dataType
,
true
,
args
.
length
==
1
?
arg
:
ValueArray
.
get
(
argValues
));
}
else
{
}
else
{
Aggregate
agg
=
(
Aggregate
)
group
.
get
(
this
);
Aggregate
agg
=
(
Aggregate
)
select
.
getCurrentGroupExprData
(
this
);
if
(
agg
==
null
)
{
if
(
agg
==
null
)
{
agg
=
getInstance
();
agg
=
getInstance
();
group
.
put
(
this
,
agg
);
select
.
setCurrentGroupExprData
(
this
,
agg
);
}
}
Object
[]
argValues
=
new
Object
[
args
.
length
];
Object
[]
argValues
=
new
Object
[
args
.
length
];
Object
arg
=
null
;
Object
arg
=
null
;
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论