Skip to content
项目
群组
代码片段
帮助
正在加载...
帮助
为 GitLab 提交贡献
登录/注册
切换导航
H
h2database
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
分枝图
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
计划
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
分枝图
统计图
创建新议题
作业
提交
议题看板
打开侧边栏
Administrator
h2database
Commits
b26bed64
提交
b26bed64
authored
9月 22, 2018
作者:
Evgenij Ryazanov
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Fix subtraction of timestamps
上级
5b87daab
隐藏空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
31 行增加
和
20 行删除
+31
-20
IntervalOperation.java
h2/src/main/org/h2/expression/IntervalOperation.java
+22
-20
interval.sql
h2/src/test/org/h2/test/scripts/datatypes/interval.sql
+9
-0
没有找到文件。
h2/src/main/org/h2/expression/IntervalOperation.java
浏览文件 @
b26bed64
...
...
@@ -5,6 +5,11 @@
*/
package
org
.
h2
.
expression
;
import
static
org
.
h2
.
util
.
DateTimeUtils
.
NANOS_PER_DAY
;
import
static
org
.
h2
.
util
.
DateTimeUtils
.
absoluteDayFromDateValue
;
import
static
org
.
h2
.
util
.
DateTimeUtils
.
dateAndTimeFromValue
;
import
static
org
.
h2
.
util
.
DateTimeUtils
.
dateTimeToValue
;
import
static
org
.
h2
.
util
.
DateTimeUtils
.
dateValueFromAbsoluteDay
;
import
static
org
.
h2
.
util
.
IntervalUtils
.
NANOS_PER_DAY_BI
;
import
java.math.BigDecimal
;
...
...
@@ -17,7 +22,6 @@ import org.h2.message.DbException;
import
org.h2.table.ColumnResolver
;
import
org.h2.table.TableFilter
;
import
org.h2.util.DateTimeFunctions
;
import
org.h2.util.DateTimeUtils
;
import
org.h2.util.IntervalUtils
;
import
org.h2.value.DataType
;
import
org.h2.value.Value
;
...
...
@@ -73,6 +77,12 @@ public class IntervalOperation extends Expression {
private
Expression
left
,
right
;
private
int
dataType
;
private
static
BigInteger
nanosFromValue
(
Value
v
)
{
long
[]
a
=
dateAndTimeFromValue
(
v
);
return
BigInteger
.
valueOf
(
absoluteDayFromDateValue
(
a
[
0
])).
multiply
(
NANOS_PER_DAY_BI
)
.
add
(
BigInteger
.
valueOf
(
a
[
1
]));
}
public
IntervalOperation
(
IntervalOpType
opType
,
Expression
left
,
Expression
right
)
{
this
.
opType
=
opType
;
this
.
left
=
left
;
...
...
@@ -161,21 +171,15 @@ public class IntervalOperation extends Expression {
return
ValueInterval
.
from
(
IntervalQualifier
.
HOUR_TO_SECOND
,
negative
,
diff
/
3_600_000_000_000L
,
diff
%
3_600_000_000_000L
);
}
else
if
(
lType
==
Value
.
DATE
&&
rType
==
Value
.
DATE
)
{
long
diff
=
DateTimeUtils
.
absoluteDayFromDateValue
(((
ValueDate
)
l
).
getDateValue
())
-
DateTimeUtils
.
absoluteDayFromDateValue
(((
ValueDate
)
r
).
getDateValue
());
long
diff
=
absoluteDayFromDateValue
(((
ValueDate
)
l
).
getDateValue
())
-
absoluteDayFromDateValue
(((
ValueDate
)
r
).
getDateValue
());
boolean
negative
=
diff
<
0
;
if
(
negative
)
{
diff
=
-
diff
;
}
return
ValueInterval
.
from
(
IntervalQualifier
.
DAY
,
negative
,
diff
,
0L
);
}
else
{
long
[]
a
=
DateTimeUtils
.
dateAndTimeFromValue
(
l
);
long
[]
b
=
DateTimeUtils
.
dateAndTimeFromValue
(
r
);
BigInteger
bi1
=
BigInteger
.
valueOf
(
a
[
0
]).
multiply
(
NANOS_PER_DAY_BI
)
.
add
(
BigInteger
.
valueOf
(
a
[
1
]));
BigInteger
bi2
=
BigInteger
.
valueOf
(
b
[
0
]).
multiply
(
NANOS_PER_DAY_BI
)
.
add
(
BigInteger
.
valueOf
(
b
[
1
]));
BigInteger
diff
=
bi1
.
subtract
(
bi2
);
BigInteger
diff
=
nanosFromValue
(
l
).
subtract
(
nanosFromValue
(
r
));
if
(
lType
==
Value
.
TIMESTAMP_TZ
||
rType
==
Value
.
TIMESTAMP_TZ
)
{
l
=
l
.
convertTo
(
Value
.
TIMESTAMP_TZ
);
r
=
r
.
convertTo
(
Value
.
TIMESTAMP_TZ
);
...
...
@@ -214,14 +218,13 @@ public class IntervalOperation extends Expression {
}
else
{
BigInteger
a2
=
IntervalUtils
.
intervalToAbsolute
((
ValueInterval
)
r
);
if
(
lType
==
Value
.
DATE
)
{
BigInteger
a1
=
BigInteger
.
valueOf
(
DateTimeUtils
.
absoluteDayFromDateValue
(((
ValueDate
)
l
).
getDateValue
()));
BigInteger
a1
=
BigInteger
.
valueOf
(
absoluteDayFromDateValue
(((
ValueDate
)
l
).
getDateValue
()));
a2
=
a2
.
divide
(
NANOS_PER_DAY_BI
);
BigInteger
n
=
opType
==
IntervalOpType
.
DATETIME_PLUS_INTERVAL
?
a1
.
add
(
a2
)
:
a1
.
subtract
(
a2
);
return
ValueDate
.
fromDateValue
(
DateTimeUtils
.
dateValueFromAbsoluteDay
(
n
.
longValue
()));
return
ValueDate
.
fromDateValue
(
dateValueFromAbsoluteDay
(
n
.
longValue
()));
}
else
{
long
[]
a
=
DateTimeUtils
.
dateAndTimeFromValue
(
l
);
long
absoluteDay
=
DateTimeUtils
.
absoluteDayFromDateValue
(
a
[
0
]);
long
[]
a
=
dateAndTimeFromValue
(
l
);
long
absoluteDay
=
absoluteDayFromDateValue
(
a
[
0
]);
long
timeNanos
=
a
[
1
];
BigInteger
[]
dr
=
a2
.
divideAndRemainder
(
NANOS_PER_DAY_BI
);
if
(
opType
==
IntervalOpType
.
DATETIME_PLUS_INTERVAL
)
{
...
...
@@ -231,15 +234,14 @@ public class IntervalOperation extends Expression {
absoluteDay
-=
dr
[
0
].
longValue
();
timeNanos
-=
dr
[
1
].
longValue
();
}
if
(
timeNanos
>=
DateTimeUtils
.
NANOS_PER_DAY
)
{
timeNanos
-=
DateTimeUtils
.
NANOS_PER_DAY
;
if
(
timeNanos
>=
NANOS_PER_DAY
)
{
timeNanos
-=
NANOS_PER_DAY
;
absoluteDay
++;
}
else
if
(
timeNanos
<
0
)
{
timeNanos
+=
DateTimeUtils
.
NANOS_PER_DAY
;
timeNanos
+=
NANOS_PER_DAY
;
absoluteDay
--;
}
return
DateTimeUtils
.
dateTimeToValue
(
l
,
DateTimeUtils
.
dateValueFromAbsoluteDay
(
absoluteDay
),
timeNanos
,
false
);
return
dateTimeToValue
(
l
,
dateValueFromAbsoluteDay
(
absoluteDay
),
timeNanos
,
false
);
}
}
}
...
...
h2/src/test/org/h2/test/scripts/datatypes/interval.sql
浏览文件 @
b26bed64
...
...
@@ -775,3 +775,12 @@ DROP TABLE TEST;
CREATE
TABLE
TEST
(
I
INTERVAL
HOUR
TO
SECOND
(
10
));
>
exception
INVALID_VALUE_SCALE_PRECISION
SELECT
TIMESTAMP
'2018-09-10 23:30:00'
-
TIMESTAMP
'2014-09-11 23:30:00'
;
>>
INTERVAL
'1460 00:00:00'
DAY
TO
SECOND
SELECT
TIMESTAMP
WITH
TIME
ZONE
'2014-09-11 23:30:00Z'
-
TIMESTAMP
WITH
TIME
ZONE
'2018-09-10 23:30:00Z'
;
>>
INTERVAL
'-1460 00:00:00'
DAY
TO
SECOND
SELECT
DATE
'2018-09-10'
-
DATE
'2014-09-11'
;
>>
INTERVAL
'1460'
DAY
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论