Skip to content
项目
群组
代码片段
帮助
正在加载...
帮助
为 GitLab 提交贡献
登录/注册
切换导航
H
h2database
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
分枝图
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
计划
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
分枝图
统计图
创建新议题
作业
提交
议题看板
打开侧边栏
Administrator
h2database
Commits
0531f3fc
提交
0531f3fc
authored
2月 04, 2018
作者:
Evgenij Ryazanov
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Move MEDIAN-related code to AggregateDataMedian
上级
503cd5f4
显示空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
100 行增加
和
88 行删除
+100
-88
Aggregate.java
h2/src/main/org/h2/expression/Aggregate.java
+2
-86
AggregateDataMedian.java
h2/src/main/org/h2/expression/AggregateDataMedian.java
+98
-2
没有找到文件。
h2/src/main/org/h2/expression/Aggregate.java
浏览文件 @
0531f3fc
...
@@ -20,7 +20,6 @@ import org.h2.result.SearchRow;
...
@@ -20,7 +20,6 @@ import org.h2.result.SearchRow;
import
org.h2.result.SortOrder
;
import
org.h2.result.SortOrder
;
import
org.h2.table.Column
;
import
org.h2.table.Column
;
import
org.h2.table.ColumnResolver
;
import
org.h2.table.ColumnResolver
;
import
org.h2.table.IndexColumn
;
import
org.h2.table.Table
;
import
org.h2.table.Table
;
import
org.h2.table.TableFilter
;
import
org.h2.table.TableFilter
;
import
org.h2.util.StatementBuilder
;
import
org.h2.util.StatementBuilder
;
...
@@ -312,50 +311,7 @@ public class Aggregate extends Expression {
...
@@ -312,50 +311,7 @@ public class Aggregate extends Expression {
return
v
;
return
v
;
}
}
case
MEDIAN:
{
case
MEDIAN:
{
Index
index
=
getMedianColumnIndex
();
return
AggregateDataMedian
.
getFromIndex
(
session
,
on
,
dataType
);
long
count
=
index
.
getRowCount
(
session
);
if
(
count
==
0
)
{
return
ValueNull
.
INSTANCE
;
}
Cursor
cursor
=
index
.
find
(
session
,
null
,
null
);
cursor
.
next
();
// Skip nulls
SearchRow
row
;
while
(
count
>
0
)
{
row
=
cursor
.
getSearchRow
();
if
(
row
==
null
)
{
return
ValueNull
.
INSTANCE
;
}
if
(
row
.
getValue
(
index
.
getColumns
()[
0
].
getColumnId
())
==
ValueNull
.
INSTANCE
)
{
count
--;
cursor
.
next
();
}
else
break
;
}
if
(
count
==
0
)
{
return
ValueNull
.
INSTANCE
;
}
long
skip
=
(
count
-
1
)
/
2
;
for
(
int
i
=
0
;
i
<
skip
;
i
++)
{
cursor
.
next
();
}
row
=
cursor
.
getSearchRow
();
Value
v
;
if
(
row
==
null
)
{
v
=
ValueNull
.
INSTANCE
;
}
else
{
v
=
row
.
getValue
(
index
.
getColumns
()[
0
].
getColumnId
());
}
if
((
count
&
1
)
==
0
)
{
cursor
.
next
();
row
=
cursor
.
getSearchRow
();
if
(
row
==
null
)
{
return
v
;
}
Value
v2
=
row
.
getValue
(
index
.
getColumns
()[
0
].
getColumnId
());
return
AggregateDataMedian
.
getMedian
(
v
,
v2
,
dataType
,
session
.
getDatabase
().
getCompareMode
());
}
return
v
;
}
}
default
:
default
:
DbException
.
throwInternalError
(
"type="
+
type
);
DbException
.
throwInternalError
(
"type="
+
type
);
...
@@ -649,46 +605,6 @@ public class Aggregate extends Expression {
...
@@ -649,46 +605,6 @@ public class Aggregate extends Expression {
return
null
;
return
null
;
}
}
private
Index
getMedianColumnIndex
()
{
if
(
on
instanceof
ExpressionColumn
)
{
ExpressionColumn
col
=
(
ExpressionColumn
)
on
;
Column
column
=
col
.
getColumn
();
TableFilter
filter
=
col
.
getTableFilter
();
if
(
filter
!=
null
)
{
Table
table
=
filter
.
getTable
();
ArrayList
<
Index
>
indexes
=
table
.
getIndexes
();
Index
result
=
null
;
if
(
indexes
!=
null
)
{
for
(
int
i
=
1
,
size
=
indexes
.
size
();
i
<
size
;
i
++)
{
Index
index
=
indexes
.
get
(
i
);
if
(!
index
.
canFindNext
())
{
continue
;
}
if
(!
index
.
isFirstColumn
(
column
))
{
continue
;
}
IndexColumn
ic
=
index
.
getIndexColumns
()[
0
];
if
(
column
.
isNullable
())
{
int
sortType
=
ic
.
sortType
;
// Nulls last is not supported
if
((
sortType
&
SortOrder
.
NULLS_LAST
)
!=
0
)
continue
;
// Descending without nulls first is not supported
if
((
sortType
&
SortOrder
.
DESCENDING
)
!=
0
&&
(
sortType
&
SortOrder
.
NULLS_FIRST
)
==
0
)
{
continue
;
}
}
if
(
result
==
null
||
result
.
getColumns
().
length
>
index
.
getColumns
().
length
)
{
result
=
index
;
}
}
}
return
result
;
}
}
return
null
;
}
@Override
@Override
public
boolean
isEverything
(
ExpressionVisitor
visitor
)
{
public
boolean
isEverything
(
ExpressionVisitor
visitor
)
{
if
(
visitor
.
getType
()
==
ExpressionVisitor
.
OPTIMIZABLE_MIN_MAX_COUNT_ALL
)
{
if
(
visitor
.
getType
()
==
ExpressionVisitor
.
OPTIMIZABLE_MIN_MAX_COUNT_ALL
)
{
...
@@ -708,7 +624,7 @@ public class Aggregate extends Expression {
...
@@ -708,7 +624,7 @@ public class Aggregate extends Expression {
if
(
distinct
)
{
if
(
distinct
)
{
return
false
;
return
false
;
}
}
return
getMedianColumnIndex
(
)
!=
null
;
return
AggregateDataMedian
.
getMedianColumnIndex
(
on
)
!=
null
;
default
:
default
:
return
false
;
return
false
;
}
}
...
...
h2/src/main/org/h2/expression/AggregateDataMedian.java
浏览文件 @
0531f3fc
...
@@ -13,6 +13,15 @@ import java.util.Comparator;
...
@@ -13,6 +13,15 @@ import java.util.Comparator;
import
java.util.HashSet
;
import
java.util.HashSet
;
import
org.h2.engine.Database
;
import
org.h2.engine.Database
;
import
org.h2.engine.Session
;
import
org.h2.index.Cursor
;
import
org.h2.index.Index
;
import
org.h2.result.SearchRow
;
import
org.h2.result.SortOrder
;
import
org.h2.table.Column
;
import
org.h2.table.IndexColumn
;
import
org.h2.table.Table
;
import
org.h2.table.TableFilter
;
import
org.h2.util.DateTimeUtils
;
import
org.h2.util.DateTimeUtils
;
import
org.h2.value.CompareMode
;
import
org.h2.value.CompareMode
;
import
org.h2.value.Value
;
import
org.h2.value.Value
;
...
@@ -33,6 +42,93 @@ import org.h2.value.ValueTimestampTimeZone;
...
@@ -33,6 +42,93 @@ import org.h2.value.ValueTimestampTimeZone;
class
AggregateDataMedian
extends
AggregateData
{
class
AggregateDataMedian
extends
AggregateData
{
private
Collection
<
Value
>
values
;
private
Collection
<
Value
>
values
;
static
Index
getMedianColumnIndex
(
Expression
on
)
{
if
(
on
instanceof
ExpressionColumn
)
{
ExpressionColumn
col
=
(
ExpressionColumn
)
on
;
Column
column
=
col
.
getColumn
();
TableFilter
filter
=
col
.
getTableFilter
();
if
(
filter
!=
null
)
{
Table
table
=
filter
.
getTable
();
ArrayList
<
Index
>
indexes
=
table
.
getIndexes
();
Index
result
=
null
;
if
(
indexes
!=
null
)
{
for
(
int
i
=
1
,
size
=
indexes
.
size
();
i
<
size
;
i
++)
{
Index
index
=
indexes
.
get
(
i
);
if
(!
index
.
canFindNext
())
{
continue
;
}
if
(!
index
.
isFirstColumn
(
column
))
{
continue
;
}
IndexColumn
ic
=
index
.
getIndexColumns
()[
0
];
if
(
column
.
isNullable
())
{
int
sortType
=
ic
.
sortType
;
// Nulls last is not supported
if
((
sortType
&
SortOrder
.
NULLS_LAST
)
!=
0
)
continue
;
// Descending without nulls first is not supported
if
((
sortType
&
SortOrder
.
DESCENDING
)
!=
0
&&
(
sortType
&
SortOrder
.
NULLS_FIRST
)
==
0
)
{
continue
;
}
}
if
(
result
==
null
||
result
.
getColumns
().
length
>
index
.
getColumns
().
length
)
{
result
=
index
;
}
}
}
return
result
;
}
}
return
null
;
}
static
Value
getFromIndex
(
Session
session
,
Expression
on
,
int
dataType
)
{
Index
index
=
getMedianColumnIndex
(
on
);
long
count
=
index
.
getRowCount
(
session
);
if
(
count
==
0
)
{
return
ValueNull
.
INSTANCE
;
}
Cursor
cursor
=
index
.
find
(
session
,
null
,
null
);
cursor
.
next
();
// Skip nulls
SearchRow
row
;
while
(
count
>
0
)
{
row
=
cursor
.
getSearchRow
();
if
(
row
==
null
)
{
return
ValueNull
.
INSTANCE
;
}
if
(
row
.
getValue
(
index
.
getColumns
()[
0
].
getColumnId
())
==
ValueNull
.
INSTANCE
)
{
count
--;
cursor
.
next
();
}
else
break
;
}
if
(
count
==
0
)
{
return
ValueNull
.
INSTANCE
;
}
long
skip
=
(
count
-
1
)
/
2
;
for
(
int
i
=
0
;
i
<
skip
;
i
++)
{
cursor
.
next
();
}
row
=
cursor
.
getSearchRow
();
Value
v
;
if
(
row
==
null
)
{
v
=
ValueNull
.
INSTANCE
;
}
else
{
v
=
row
.
getValue
(
index
.
getColumns
()[
0
].
getColumnId
());
}
if
((
count
&
1
)
==
0
)
{
cursor
.
next
();
row
=
cursor
.
getSearchRow
();
if
(
row
==
null
)
{
return
v
;
}
Value
v2
=
row
.
getValue
(
index
.
getColumns
()[
0
].
getColumnId
());
return
getMedian
(
v
,
v2
,
dataType
,
session
.
getDatabase
().
getCompareMode
());
}
return
v
;
}
@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
)
{
...
@@ -104,7 +200,7 @@ class AggregateDataMedian extends AggregateData {
...
@@ -104,7 +200,7 @@ class AggregateDataMedian extends AggregateData {
+
DateTimeUtils
.
absoluteDayFromDateValue
(
ts1
.
getDateValue
());
+
DateTimeUtils
.
absoluteDayFromDateValue
(
ts1
.
getDateValue
());
long
nanos
=
(
ts0
.
getTimeNanos
()
+
ts1
.
getTimeNanos
())
/
2
;
long
nanos
=
(
ts0
.
getTimeNanos
()
+
ts1
.
getTimeNanos
())
/
2
;
if
((
dateSum
&
1
)
!=
0
)
{
if
((
dateSum
&
1
)
!=
0
)
{
nanos
+=
(
DateTimeUtils
.
NANOS_PER_DAY
/
2
)
;
nanos
+=
DateTimeUtils
.
NANOS_PER_DAY
/
2
;
if
(
nanos
>=
DateTimeUtils
.
NANOS_PER_DAY
)
{
if
(
nanos
>=
DateTimeUtils
.
NANOS_PER_DAY
)
{
nanos
-=
DateTimeUtils
.
NANOS_PER_DAY
;
nanos
-=
DateTimeUtils
.
NANOS_PER_DAY
;
dateSum
++;
dateSum
++;
...
@@ -119,7 +215,7 @@ class AggregateDataMedian extends AggregateData {
...
@@ -119,7 +215,7 @@ class AggregateDataMedian extends AggregateData {
+
DateTimeUtils
.
absoluteDayFromDateValue
(
ts1
.
getDateValue
());
+
DateTimeUtils
.
absoluteDayFromDateValue
(
ts1
.
getDateValue
());
long
nanos
=
(
ts0
.
getTimeNanos
()
+
ts1
.
getTimeNanos
())
/
2
;
long
nanos
=
(
ts0
.
getTimeNanos
()
+
ts1
.
getTimeNanos
())
/
2
;
if
((
dateSum
&
1
)
!=
0
)
{
if
((
dateSum
&
1
)
!=
0
)
{
nanos
+=
(
DateTimeUtils
.
NANOS_PER_DAY
/
2
)
;
nanos
+=
DateTimeUtils
.
NANOS_PER_DAY
/
2
;
if
(
nanos
>=
DateTimeUtils
.
NANOS_PER_DAY
)
{
if
(
nanos
>=
DateTimeUtils
.
NANOS_PER_DAY
)
{
nanos
-=
DateTimeUtils
.
NANOS_PER_DAY
;
nanos
-=
DateTimeUtils
.
NANOS_PER_DAY
;
dateSum
++;
dateSum
++;
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论