Skip to content
项目
群组
代码片段
帮助
正在加载...
帮助
为 GitLab 提交贡献
登录/注册
切换导航
H
h2database
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
分枝图
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
计划
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
分枝图
统计图
创建新议题
作业
提交
议题看板
打开侧边栏
Administrator
h2database
Commits
3fc0af12
提交
3fc0af12
authored
3月 22, 2018
作者:
Evgenij Ryazanov
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Refactor AggregateDataCollecting and AggregateDataMedian to reduce code duplication
上级
9eeb3295
隐藏空白字符变更
内嵌
并排
正在显示
4 个修改的文件
包含
85 行增加
和
48 行删除
+85
-48
Aggregate.java
h2/src/main/org/h2/expression/Aggregate.java
+13
-14
AggregateData.java
h2/src/main/org/h2/expression/AggregateData.java
+10
-10
AggregateDataCollecting.java
h2/src/main/org/h2/expression/AggregateDataCollecting.java
+59
-0
AggregateDataMedian.java
h2/src/main/org/h2/expression/AggregateDataMedian.java
+3
-24
没有找到文件。
h2/src/main/org/h2/expression/Aggregate.java
浏览文件 @
3fc0af12
...
@@ -6,7 +6,7 @@
...
@@ -6,7 +6,7 @@
package
org
.
h2
.
expression
;
package
org
.
h2
.
expression
;
import
java.util.ArrayList
;
import
java.util.ArrayList
;
import
java.util.
Collection
s
;
import
java.util.
Array
s
;
import
java.util.Comparator
;
import
java.util.Comparator
;
import
java.util.HashMap
;
import
java.util.HashMap
;
import
org.h2.api.ErrorCode
;
import
org.h2.api.ErrorCode
;
...
@@ -257,9 +257,9 @@ public class Aggregate extends Expression {
...
@@ -257,9 +257,9 @@ public class Aggregate extends Expression {
return
new
SortOrder
(
session
.
getDatabase
(),
index
,
sortType
,
null
);
return
new
SortOrder
(
session
.
getDatabase
(),
index
,
sortType
,
null
);
}
}
private
void
sortWithOrderBy
(
ArrayList
<
Value
>
list
)
{
private
void
sortWithOrderBy
(
Value
[]
array
)
{
final
SortOrder
sortOrder
=
orderBySort
;
final
SortOrder
sortOrder
=
orderBySort
;
Collections
.
sort
(
list
,
new
Comparator
<
Value
>()
{
Arrays
.
sort
(
array
,
new
Comparator
<
Value
>()
{
@Override
@Override
public
int
compare
(
Value
v1
,
Value
v2
)
{
public
int
compare
(
Value
v1
,
Value
v2
)
{
return
sortOrder
.
compare
(((
ValueArray
)
v1
).
getList
(),
((
ValueArray
)
v2
).
getList
());
return
sortOrder
.
compare
(((
ValueArray
)
v1
).
getList
(),
((
ValueArray
)
v2
).
getList
());
...
@@ -370,19 +370,18 @@ public class Aggregate extends Expression {
...
@@ -370,19 +370,18 @@ public class Aggregate extends Expression {
if
(
data
==
null
)
{
if
(
data
==
null
)
{
data
=
AggregateData
.
create
(
type
);
data
=
AggregateData
.
create
(
type
);
}
}
Value
v
=
data
.
getValue
(
session
.
getDatabase
(),
dataType
,
distinct
);
if
(
type
==
AggregateType
.
GROUP_CONCAT
)
{
if
(
type
==
AggregateType
.
GROUP_CONCAT
)
{
ArrayList
<
Value
>
list
=
((
AggregateDataArrayCollecting
)
data
).
getList
();
Value
[]
array
=
((
AggregateDataCollecting
)
data
).
getArray
();
if
(
list
==
null
||
list
.
isEmpty
()
)
{
if
(
array
==
null
)
{
return
ValueNull
.
INSTANCE
;
return
ValueNull
.
INSTANCE
;
}
}
if
(
orderByList
!=
null
)
{
if
(
orderByList
!=
null
)
{
sortWithOrderBy
(
list
);
sortWithOrderBy
(
array
);
}
}
StatementBuilder
buff
=
new
StatementBuilder
();
StatementBuilder
buff
=
new
StatementBuilder
();
String
sep
=
groupConcatSeparator
==
null
?
String
sep
=
groupConcatSeparator
==
null
?
","
:
groupConcatSeparator
.
getValue
(
session
).
getString
();
","
:
groupConcatSeparator
.
getValue
(
session
).
getString
();
for
(
Value
val
:
list
)
{
for
(
Value
val
:
array
)
{
String
s
;
String
s
;
if
(
val
.
getType
()
==
Value
.
ARRAY
)
{
if
(
val
.
getType
()
==
Value
.
ARRAY
)
{
s
=
((
ValueArray
)
val
).
getList
()[
0
].
getString
();
s
=
((
ValueArray
)
val
).
getList
()[
0
].
getString
();
...
@@ -397,18 +396,18 @@ public class Aggregate extends Expression {
...
@@ -397,18 +396,18 @@ public class Aggregate extends Expression {
}
}
buff
.
append
(
s
);
buff
.
append
(
s
);
}
}
v
=
ValueString
.
get
(
buff
.
toString
());
return
ValueString
.
get
(
buff
.
toString
());
}
else
if
(
type
==
AggregateType
.
ARRAY_AGG
)
{
}
else
if
(
type
==
AggregateType
.
ARRAY_AGG
)
{
ArrayList
<
Value
>
list
=
((
AggregateDataArrayCollecting
)
data
).
getList
();
Value
[]
array
=
((
AggregateDataCollecting
)
data
).
getArray
();
if
(
list
==
null
||
list
.
isEmpty
()
)
{
if
(
array
==
null
)
{
return
ValueNull
.
INSTANCE
;
return
ValueNull
.
INSTANCE
;
}
}
if
(
orderByList
!=
null
)
{
if
(
orderByList
!=
null
)
{
sortWithOrderBy
(
list
);
sortWithOrderBy
(
array
);
}
}
v
=
ValueArray
.
get
(
list
.
toArray
(
new
Value
[
list
.
size
()])
);
return
ValueArray
.
get
(
array
);
}
}
return
v
;
return
data
.
getValue
(
session
.
getDatabase
(),
dataType
,
distinct
)
;
}
}
@Override
@Override
...
...
h2/src/main/org/h2/expression/AggregateData.java
浏览文件 @
3fc0af12
...
@@ -21,21 +21,21 @@ abstract class AggregateData {
...
@@ -21,21 +21,21 @@ abstract class AggregateData {
* @return the aggregate data object of the specified type
* @return the aggregate data object of the specified type
*/
*/
static
AggregateData
create
(
AggregateType
aggregateType
)
{
static
AggregateData
create
(
AggregateType
aggregateType
)
{
if
(
aggregateType
==
AggregateType
.
SELECTIVITY
)
{
switch
(
aggregateType
)
{
case
SELECTIVITY:
return
new
AggregateDataSelectivity
();
return
new
AggregateDataSelectivity
();
}
else
if
(
aggregateType
==
AggregateType
.
GROUP_CONCAT
)
{
case
GROUP_CONCAT:
return
new
AggregateDataArrayCollecting
();
case
ARRAY_AGG:
}
else
if
(
aggregateType
==
AggregateType
.
ARRAY_AGG
)
{
return
new
AggregateDataCollecting
();
return
new
AggregateDataArrayCollecting
();
case
COUNT_ALL:
}
else
if
(
aggregateType
==
AggregateType
.
COUNT_ALL
)
{
return
new
AggregateDataCountAll
();
return
new
AggregateDataCountAll
();
}
else
if
(
aggregateType
==
AggregateType
.
COUNT
)
{
case
COUNT:
return
new
AggregateDataCount
();
return
new
AggregateDataCount
();
}
else
if
(
aggregateType
==
AggregateType
.
HISTOGRAM
)
{
case
HISTOGRAM:
return
new
AggregateDataHistogram
();
return
new
AggregateDataHistogram
();
}
else
if
(
aggregateType
==
AggregateType
.
MEDIAN
)
{
case
MEDIAN:
return
new
AggregateDataMedian
();
return
new
AggregateDataMedian
();
}
else
{
default
:
return
new
AggregateDataDefault
(
aggregateType
);
return
new
AggregateDataDefault
(
aggregateType
);
}
}
}
}
...
...
h2/src/main/org/h2/expression/AggregateData
Array
Collecting.java
→
h2/src/main/org/h2/expression/AggregateDataCollecting.java
浏览文件 @
3fc0af12
...
@@ -6,55 +6,54 @@
...
@@ -6,55 +6,54 @@
package
org
.
h2
.
expression
;
package
org
.
h2
.
expression
;
import
java.util.ArrayList
;
import
java.util.ArrayList
;
import
java.util.Collection
;
import
java.util.HashSet
;
import
org.h2.engine.Database
;
import
org.h2.engine.Database
;
import
org.h2.util.New
;
import
org.h2.util.ValueHashMap
;
import
org.h2.value.Value
;
import
org.h2.value.Value
;
import
org.h2.value.ValueNull
;
import
org.h2.value.ValueNull
;
/**
/**
* Data stored while calculating a GROUP_CONCAT/ARRAY_AGG aggregate.
* Data stored while calculating an aggregate that needs collecting of all
* values.
*
* <p>
* NULL values are not collected. {@link #getValue(Database, int, boolean)}
* method returns {@code null}. Use {@link #getArray()} for instances of this
* class instead. Notice that subclasses like {@link AggregateDataMedian} may
* override {@link #getValue(Database, int, boolean)} to return useful result.
* </p>
*/
*/
class
AggregateDataArrayCollecting
extends
AggregateData
{
class
AggregateDataCollecting
extends
AggregateData
{
private
ArrayList
<
Value
>
list
;
Collection
<
Value
>
values
;
private
ValueHashMap
<
AggregateDataArrayCollecting
>
distinctValues
;
@Override
@Override
void
add
(
Database
database
,
int
dataType
,
boolean
distinct
,
Value
v
)
{
void
add
(
Database
database
,
int
dataType
,
boolean
distinct
,
Value
v
)
{
if
(
v
==
ValueNull
.
INSTANCE
)
{
if
(
v
==
ValueNull
.
INSTANCE
)
{
return
;
return
;
}
}
if
(
distinct
)
{
Collection
<
Value
>
c
=
values
;
if
(
distinctValues
==
null
)
{
if
(
c
==
null
)
{
distinctValues
=
ValueHashMap
.
newInstance
();
values
=
c
=
distinct
?
new
HashSet
<
Value
>()
:
new
ArrayList
<
Value
>();
}
distinctValues
.
put
(
v
,
this
);
return
;
}
if
(
list
==
null
)
{
list
=
New
.
arrayList
();
}
}
list
.
add
(
v
);
c
.
add
(
v
);
}
}
@Override
@Override
Value
getValue
(
Database
database
,
int
dataType
,
boolean
distinct
)
{
Value
getValue
(
Database
database
,
int
dataType
,
boolean
distinct
)
{
if
(
distinct
)
{
distinct
(
database
,
dataType
);
}
return
null
;
return
null
;
}
}
ArrayList
<
Value
>
getList
()
{
/**
return
list
;
* Returns array with values or {@code null}.
}
*
* @return array with values or {@code null}
private
void
distinct
(
Database
database
,
int
dataType
)
{
*/
if
(
distinctValues
==
null
)
{
Value
[]
getArray
()
{
return
;
Collection
<
Value
>
values
=
this
.
values
;
}
if
(
values
==
null
)
{
for
(
Value
v
:
distinctValues
.
keys
())
{
return
null
;
add
(
database
,
dataType
,
false
,
v
);
}
}
return
values
.
toArray
(
new
Value
[
0
]);
}
}
}
}
h2/src/main/org/h2/expression/AggregateDataMedian.java
浏览文件 @
3fc0af12
...
@@ -8,9 +8,7 @@ package org.h2.expression;
...
@@ -8,9 +8,7 @@ package org.h2.expression;
import
java.math.BigDecimal
;
import
java.math.BigDecimal
;
import
java.util.ArrayList
;
import
java.util.ArrayList
;
import
java.util.Arrays
;
import
java.util.Arrays
;
import
java.util.Collection
;
import
java.util.Comparator
;
import
java.util.Comparator
;
import
java.util.HashSet
;
import
org.h2.engine.Database
;
import
org.h2.engine.Database
;
import
org.h2.engine.Session
;
import
org.h2.engine.Session
;
...
@@ -39,9 +37,7 @@ import org.h2.value.ValueTimestampTimeZone;
...
@@ -39,9 +37,7 @@ import org.h2.value.ValueTimestampTimeZone;
/**
/**
* Data stored while calculating a MEDIAN aggregate.
* Data stored while calculating a MEDIAN aggregate.
*/
*/
class
AggregateDataMedian
extends
AggregateData
{
class
AggregateDataMedian
extends
AggregateDataCollecting
{
private
Collection
<
Value
>
values
;
private
static
boolean
isNullsLast
(
Index
index
)
{
private
static
boolean
isNullsLast
(
Index
index
)
{
IndexColumn
ic
=
index
.
getIndexColumns
()[
0
];
IndexColumn
ic
=
index
.
getIndexColumns
()[
0
];
int
sortType
=
ic
.
sortType
;
int
sortType
=
ic
.
sortType
;
...
@@ -168,29 +164,12 @@ class AggregateDataMedian extends AggregateData {
...
@@ -168,29 +164,12 @@ class AggregateDataMedian extends AggregateData {
return
v
;
return
v
;
}
}
@Override
void
add
(
Database
database
,
int
dataType
,
boolean
distinct
,
Value
v
)
{
if
(
v
==
ValueNull
.
INSTANCE
)
{
return
;
}
Collection
<
Value
>
c
=
values
;
if
(
c
==
null
)
{
values
=
c
=
distinct
?
new
HashSet
<
Value
>()
:
new
ArrayList
<
Value
>();
}
c
.
add
(
v
);
}
@Override
@Override
Value
getValue
(
Database
database
,
int
dataType
,
boolean
distinct
)
{
Value
getValue
(
Database
database
,
int
dataType
,
boolean
distinct
)
{
Collection
<
Value
>
c
=
values
;
Value
[]
a
=
getArray
();
// Non-null collection cannot be empty here
if
(
a
==
null
)
{
if
(
c
==
null
)
{
return
ValueNull
.
INSTANCE
;
return
ValueNull
.
INSTANCE
;
}
}
if
(
distinct
&&
c
instanceof
ArrayList
)
{
c
=
new
HashSet
<>(
c
);
}
Value
[]
a
=
c
.
toArray
(
new
Value
[
0
]);
final
CompareMode
mode
=
database
.
getCompareMode
();
final
CompareMode
mode
=
database
.
getCompareMode
();
Arrays
.
sort
(
a
,
new
Comparator
<
Value
>()
{
Arrays
.
sort
(
a
,
new
Comparator
<
Value
>()
{
@Override
@Override
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论