Skip to content
项目
群组
代码片段
帮助
正在加载...
帮助
为 GitLab 提交贡献
登录/注册
切换导航
H
h2database
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
分枝图
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
计划
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
分枝图
统计图
创建新议题
作业
提交
议题看板
打开侧边栏
Administrator
h2database
Commits
86098d30
提交
86098d30
authored
9月 21, 2018
作者:
Evgenij Ryazanov
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Use AggregateDataCollecting for MEDIAN aggregate
上级
f49d2b6a
隐藏空白字符变更
内嵌
并排
正在显示
4 个修改的文件
包含
44 行增加
和
31 行删除
+44
-31
Aggregate.java
h2/src/main/org/h2/expression/aggregate/Aggregate.java
+9
-2
AggregateData.java
h2/src/main/org/h2/expression/aggregate/AggregateData.java
+1
-2
AggregateDataCollecting.java
.../org/h2/expression/aggregate/AggregateDataCollecting.java
+1
-2
AggregateMedian.java
h2/src/main/org/h2/expression/aggregate/AggregateMedian.java
+33
-25
没有找到文件。
h2/src/main/org/h2/expression/aggregate/Aggregate.java
浏览文件 @
86098d30
...
...
@@ -402,7 +402,7 @@ public class Aggregate extends AbstractAggregate {
return
v
;
}
case
MEDIAN:
return
Aggregate
DataMedian
.
getResult
FromIndex
(
session
,
on
,
dataType
);
return
Aggregate
Median
.
median
FromIndex
(
session
,
on
,
dataType
);
case
ENVELOPE:
return
((
MVSpatialIndex
)
AggregateDataEnvelope
.
getGeometryColumnIndex
(
on
)).
getBounds
(
session
);
default
:
...
...
@@ -459,6 +459,13 @@ public class Aggregate extends AbstractAggregate {
}
return
ValueArray
.
get
(
array
);
}
case
MEDIAN:
{
Value
[]
array
=
((
AggregateDataCollecting
)
data
).
getArray
();
if
(
array
==
null
)
{
return
ValueNull
.
INSTANCE
;
}
return
AggregateMedian
.
median
(
session
.
getDatabase
(),
array
,
dataType
);
}
case
MODE:
if
(
orderByList
!=
null
)
{
return
((
AggregateDataMode
)
data
).
getOrderedValue
(
session
.
getDatabase
(),
dataType
,
...
...
@@ -762,7 +769,7 @@ public class Aggregate extends AbstractAggregate {
if
(
distinct
)
{
return
false
;
}
return
Aggregate
Data
Median
.
getMedianColumnIndex
(
on
)
!=
null
;
return
AggregateMedian
.
getMedianColumnIndex
(
on
)
!=
null
;
case
ENVELOPE:
return
AggregateDataEnvelope
.
getGeometryColumnIndex
(
on
)
!=
null
;
default
:
...
...
h2/src/main/org/h2/expression/aggregate/AggregateData.java
浏览文件 @
86098d30
...
...
@@ -26,6 +26,7 @@ abstract class AggregateData {
return
new
AggregateDataSelectivity
();
case
GROUP_CONCAT:
case
ARRAY_AGG:
case
MEDIAN:
return
new
AggregateDataCollecting
();
case
COUNT_ALL:
return
new
AggregateDataCountAll
();
...
...
@@ -33,8 +34,6 @@ abstract class AggregateData {
return
new
AggregateDataCount
();
case
HISTOGRAM:
return
new
AggregateDataHistogram
();
case
MEDIAN:
return
new
AggregateDataMedian
();
case
MODE:
return
new
AggregateDataMode
();
case
ENVELOPE:
...
...
h2/src/main/org/h2/expression/aggregate/AggregateDataCollecting.java
浏览文件 @
86098d30
...
...
@@ -20,8 +20,7 @@ import org.h2.value.ValueNull;
* <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.
* class instead.
* </p>
*/
class
AggregateDataCollecting
extends
AggregateData
{
...
...
h2/src/main/org/h2/expression/aggregate/Aggregate
Data
Median.java
→
h2/src/main/org/h2/expression/aggregate/AggregateMedian.java
浏览文件 @
86098d30
...
...
@@ -41,9 +41,10 @@ import org.h2.value.ValueTimestamp;
import
org.h2.value.ValueTimestampTimeZone
;
/**
*
Data stored while calculating a
MEDIAN aggregate.
* MEDIAN aggregate.
*/
class
AggregateDataMedian
extends
AggregateDataCollecting
{
final
class
AggregateMedian
{
private
static
boolean
isNullsLast
(
Index
index
)
{
IndexColumn
ic
=
index
.
getIndexColumns
()[
0
];
int
sortType
=
ic
.
sortType
;
...
...
@@ -91,14 +92,34 @@ class AggregateDataMedian extends AggregateDataCollecting {
}
/**
* Get the result from the index.
* Get the median from the array of values.
*
* @param database the database
* @param array array with values
* @param dataType the data type
* @return the result
*/
static
Value
median
(
Database
database
,
Value
[]
array
,
int
dataType
)
{
final
CompareMode
compareMode
=
database
.
getCompareMode
();
Arrays
.
sort
(
array
,
compareMode
);
int
len
=
array
.
length
;
int
idx
=
len
/
2
;
Value
v1
=
array
[
idx
];
if
((
len
&
1
)
==
1
)
{
return
v1
.
convertTo
(
dataType
);
}
return
getMedian
(
array
[
idx
-
1
],
v1
,
dataType
,
database
.
getMode
(),
compareMode
);
}
/**
* Get the median from the index.
*
* @param session the session
* @param on the expression
* @param dataType the data type
* @return the result
*/
static
Value
getResult
FromIndex
(
Session
session
,
Expression
on
,
int
dataType
)
{
static
Value
median
FromIndex
(
Session
session
,
Expression
on
,
int
dataType
)
{
Index
index
=
getMedianColumnIndex
(
on
);
long
count
=
index
.
getRowCount
(
session
);
if
(
count
==
0
)
{
...
...
@@ -172,25 +193,9 @@ class AggregateDataMedian extends AggregateDataCollecting {
return
v
;
}
@Override
Value
getValue
(
Database
database
,
int
dataType
,
boolean
distinct
)
{
Value
[]
a
=
getArray
();
if
(
a
==
null
)
{
return
ValueNull
.
INSTANCE
;
}
final
CompareMode
compareMode
=
database
.
getCompareMode
();
Arrays
.
sort
(
a
,
compareMode
);
int
len
=
a
.
length
;
int
idx
=
len
/
2
;
Value
v1
=
a
[
idx
];
if
((
len
&
1
)
==
1
)
{
return
v1
.
convertTo
(
dataType
);
}
return
getMedian
(
a
[
idx
-
1
],
v1
,
dataType
,
database
.
getMode
(),
compareMode
);
}
private
static
Value
getMedian
(
Value
v0
,
Value
v1
,
int
dataType
,
Mode
databaseMode
,
CompareMode
compareMode
)
{
if
(
v0
.
compareTo
(
v1
,
databaseMode
,
compareMode
)
==
0
)
{
int
cmp
=
v0
.
compareTo
(
v1
,
databaseMode
,
compareMode
);
if
(
cmp
==
0
)
{
return
v0
.
convertTo
(
dataType
);
}
switch
(
dataType
)
{
...
...
@@ -266,11 +271,14 @@ class AggregateDataMedian extends AggregateDataCollecting {
case
Value
.
INTERVAL_MINUTE_TO_SECOND
:
return
IntervalUtils
.
intervalFromAbsolute
(
IntervalQualifier
.
valueOf
(
dataType
-
Value
.
INTERVAL_YEAR
),
IntervalUtils
.
intervalToAbsolute
((
ValueInterval
)
v0
)
.
add
(
IntervalUtils
.
intervalToAbsolute
((
ValueInterval
)
v1
)).
shiftRight
(
1
));
.
add
(
IntervalUtils
.
intervalToAbsolute
((
ValueInterval
)
v1
)).
shiftRight
(
1
));
default
:
// Just return
first
return
v0
.
convertTo
(
dataType
);
// Just return
smaller
return
(
cmp
<
0
?
v0
:
v1
)
.
convertTo
(
dataType
);
}
}
private
AggregateMedian
()
{
}
}
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论