Skip to content
项目
群组
代码片段
帮助
正在加载...
帮助
为 GitLab 提交贡献
登录/注册
切换导航
H
h2database
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
分枝图
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
计划
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
分枝图
统计图
创建新议题
作业
提交
议题看板
打开侧边栏
Administrator
h2database
Commits
02ef458f
提交
02ef458f
authored
8月 14, 2018
作者:
Evgenij Ryazanov
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Implement more arithmetic operations with INTERVAL data type
上级
7ae35403
隐藏空白字符变更
内嵌
并排
正在显示
3 个修改的文件
包含
134 行增加
和
5 行删除
+134
-5
IntervalOperation.java
h2/src/main/org/h2/expression/IntervalOperation.java
+62
-5
Operation.java
h2/src/main/org/h2/expression/Operation.java
+9
-0
interval.sql
h2/src/test/org/h2/test/scripts/datatypes/interval.sql
+63
-0
没有找到文件。
h2/src/main/org/h2/expression/IntervalOperation.java
浏览文件 @
02ef458f
...
...
@@ -8,15 +8,20 @@ package org.h2.expression;
import
java.math.BigDecimal
;
import
java.math.BigInteger
;
import
org.h2.api.ErrorCode
;
import
org.h2.api.IntervalQualifier
;
import
org.h2.engine.Session
;
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.value.DataType
;
import
org.h2.value.Value
;
import
org.h2.value.ValueDate
;
import
org.h2.value.ValueInterval
;
import
org.h2.value.ValueNull
;
import
org.h2.value.ValueTime
;
/**
* A mathematical operation with intervals.
...
...
@@ -55,7 +60,7 @@ public class IntervalOperation extends Expression {
INTERVAL_DIVIDE_NUMERIC
}
private
IntervalOpType
opType
;
private
final
IntervalOpType
opType
;
private
Expression
left
,
right
;
private
int
dataType
;
...
...
@@ -116,8 +121,61 @@ public class IntervalOperation extends Expression {
}
case
DATETIME_PLUS_INTERVAL:
case
DATETIME_MINUS_INTERVAL:
// TODO
throw
DbException
.
throwInternalError
(
"type="
+
opType
);
switch
(
l
.
getType
())
{
case
Value
.
TIME
:
{
if
(
DataType
.
isYearMonthIntervalType
(
r
.
getType
()))
{
throw
DbException
.
throwInternalError
(
"type="
+
r
.
getType
());
}
BigInteger
a1
=
BigInteger
.
valueOf
(((
ValueTime
)
l
).
getNanos
());
BigInteger
a2
=
DateTimeUtils
.
intervalToAbsolute
((
ValueInterval
)
r
);
BigInteger
n
=
opType
==
IntervalOpType
.
DATETIME_PLUS_INTERVAL
?
a1
.
add
(
a2
)
:
a1
.
subtract
(
a2
);
if
(
n
.
signum
()
<
0
||
n
.
compareTo
(
BigInteger
.
valueOf
(
DateTimeUtils
.
NANOS_PER_DAY
))
>=
0
)
{
throw
DbException
.
get
(
ErrorCode
.
NUMERIC_VALUE_OUT_OF_RANGE_1
,
n
.
toString
());
}
return
ValueTime
.
fromNanos
(
n
.
longValue
());
}
case
Value
.
DATE
:
case
Value
.
TIMESTAMP
:
case
Value
.
TIMESTAMP_TZ
:
if
(
DataType
.
isYearMonthIntervalType
(
r
.
getType
()))
{
long
m
=
DateTimeUtils
.
intervalToAbsolute
((
ValueInterval
)
r
).
longValue
();
if
(
opType
==
IntervalOpType
.
DATETIME_MINUS_INTERVAL
)
{
m
=
-
m
;
}
return
DateTimeFunctions
.
dateadd
(
"MONTH"
,
m
,
l
);
}
else
{
BigInteger
a2
=
DateTimeUtils
.
intervalToAbsolute
((
ValueInterval
)
r
);
if
(
l
.
getType
()
==
Value
.
DATE
)
{
BigInteger
a1
=
BigInteger
.
valueOf
(
DateTimeUtils
.
absoluteDayFromDateValue
(((
ValueDate
)
l
).
getDateValue
()));
a2
=
a2
.
divide
(
BigInteger
.
valueOf
(
DateTimeUtils
.
NANOS_PER_DAY
));
BigInteger
n
=
opType
==
IntervalOpType
.
DATETIME_PLUS_INTERVAL
?
a1
.
add
(
a2
)
:
a1
.
subtract
(
a2
);
return
ValueDate
.
fromDateValue
(
DateTimeUtils
.
dateValueFromAbsoluteDay
(
n
.
longValue
()));
}
else
{
long
[]
a
=
DateTimeUtils
.
dateAndTimeFromValue
(
l
);
long
absoluteDay
=
DateTimeUtils
.
absoluteDayFromDateValue
(
a
[
0
]);
long
timeNanos
=
a
[
1
];
BigInteger
[]
dr
=
a2
.
divideAndRemainder
(
BigInteger
.
valueOf
(
DateTimeUtils
.
NANOS_PER_DAY
));
if
(
opType
==
IntervalOpType
.
DATETIME_PLUS_INTERVAL
)
{
absoluteDay
+=
dr
[
0
].
longValue
();
timeNanos
+=
dr
[
1
].
longValue
();
}
else
{
absoluteDay
-=
dr
[
0
].
longValue
();
timeNanos
-=
dr
[
1
].
longValue
();
}
if
(
timeNanos
>=
DateTimeUtils
.
NANOS_PER_DAY
)
{
timeNanos
-=
DateTimeUtils
.
NANOS_PER_DAY
;
absoluteDay
++;
}
else
if
(
timeNanos
<
0
)
{
timeNanos
+=
DateTimeUtils
.
NANOS_PER_DAY
;
absoluteDay
--;
}
return
DateTimeUtils
.
dateTimeToValue
(
l
,
DateTimeUtils
.
dateValueFromAbsoluteDay
(
absoluteDay
),
timeNanos
,
false
);
}
}
}
break
;
case
INTERVAL_MULTIPLY_NUMERIC:
case
INTERVAL_DIVIDE_NUMERIC:
{
BigDecimal
a1
=
new
BigDecimal
(
DateTimeUtils
.
intervalToAbsolute
((
ValueInterval
)
l
));
...
...
@@ -126,9 +184,8 @@ public class IntervalOperation extends Expression {
(
opType
==
IntervalOpType
.
INTERVAL_MULTIPLY_NUMERIC
?
a1
.
multiply
(
a2
)
:
a1
.
divide
(
a2
))
.
toBigInteger
());
}
default
:
throw
DbException
.
throwInternalError
(
"type="
+
opType
);
}
throw
DbException
.
throwInternalError
(
"type="
+
opType
);
}
@Override
...
...
h2/src/main/org/h2/expression/Operation.java
浏览文件 @
02ef458f
...
...
@@ -268,8 +268,14 @@ public class Operation extends Expression {
return
new
IntervalOperation
(
IntervalOpType
.
INTERVAL_PLUS_INTERVAL
,
left
,
right
);
}
}
else
if
(
lInterval
&&
rDateTime
)
{
if
(
r
==
Value
.
TIME
&&
DataType
.
isYearMonthIntervalType
(
l
))
{
break
;
}
return
new
IntervalOperation
(
IntervalOpType
.
DATETIME_PLUS_INTERVAL
,
right
,
left
);
}
else
if
(
lDateTime
&&
rInterval
)
{
if
(
l
==
Value
.
TIME
&&
DataType
.
isYearMonthIntervalType
(
r
))
{
break
;
}
return
new
IntervalOperation
(
IntervalOpType
.
DATETIME_PLUS_INTERVAL
,
left
,
right
);
}
break
;
...
...
@@ -279,6 +285,9 @@ public class Operation extends Expression {
return
new
IntervalOperation
(
IntervalOpType
.
INTERVAL_MINUS_INTERVAL
,
left
,
right
);
}
}
else
if
(
lDateTime
&&
rInterval
)
{
if
(
l
==
Value
.
TIME
&&
DataType
.
isYearMonthIntervalType
(
r
))
{
break
;
}
return
new
IntervalOperation
(
IntervalOpType
.
DATETIME_MINUS_INTERVAL
,
left
,
right
);
}
break
;
...
...
h2/src/test/org/h2/test/scripts/datatypes/interval.sql
浏览文件 @
02ef458f
...
...
@@ -606,3 +606,66 @@ SELECT 2 * INTERVAL '10' YEAR;
SELECT
INTERVAL
'10'
YEAR
/
2
;
>>
INTERVAL
'5'
YEAR
SELECT
TIME
'10:00:00'
+
INTERVAL
'30'
MINUTE
;
>>
10
:
30
:
00
SELECT
INTERVAL
'30'
MINUTE
+
TIME
'10:00:00'
;
>>
10
:
30
:
00
SELECT
TIME
'10:00:00'
-
INTERVAL
'30'
MINUTE
;
>>
09
:
30
:
00
SELECT
DATE
'2000-01-10'
+
INTERVAL
'30'
HOUR
;
>>
2000
-
01
-
11
SELECT
INTERVAL
'30'
HOUR
+
DATE
'2000-01-10'
;
>>
2000
-
01
-
11
SELECT
DATE
'2000-01-10'
-
INTERVAL
'30'
HOUR
;
>>
2000
-
01
-
09
SELECT
DATE
'2000-01-10'
+
INTERVAL
'1-2'
YEAR
TO
MONTH
;
>>
2001
-
03
-
10
SELECT
INTERVAL
'1-2'
YEAR
TO
MONTH
+
DATE
'2000-01-10'
;
>>
2001
-
03
-
10
SELECT
DATE
'2000-01-10'
-
INTERVAL
'1-2'
YEAR
TO
MONTH
;
>>
1998
-
11
-
10
SELECT
TIMESTAMP
'2000-01-01 12:00:00'
+
INTERVAL
'25 13'
DAY
TO
HOUR
;
>>
2000
-
01
-
27
01
:
00
:
00
SELECT
INTERVAL
'25 13'
DAY
TO
HOUR
+
TIMESTAMP
'2000-01-01 12:00:00'
;
>>
2000
-
01
-
27
01
:
00
:
00
SELECT
TIMESTAMP
'2000-01-01 12:00:00'
-
INTERVAL
'25 13'
DAY
TO
HOUR
;
>>
1999
-
12
-
06
23
:
00
:
00
SELECT
TIMESTAMP
'2000-01-01 12:00:00'
+
INTERVAL
'1-2'
YEAR
TO
MONTH
;
>>
2001
-
03
-
01
12
:
00
:
00
SELECT
INTERVAL
'1-2'
YEAR
TO
MONTH
+
TIMESTAMP
'2000-01-01 12:00:00'
;
>>
2001
-
03
-
01
12
:
00
:
00
SELECT
TIMESTAMP
'2000-01-01 12:00:00'
-
INTERVAL
'1-2'
YEAR
TO
MONTH
;
>>
1998
-
11
-
01
12
:
00
:
00
SELECT
TIMESTAMP
WITH
TIME
ZONE
'2000-01-01 12:00:00+01'
+
INTERVAL
'25 13'
DAY
TO
HOUR
;
>>
2000
-
01
-
27
01
:
00
:
00
+
01
SELECT
INTERVAL
'25 13'
DAY
TO
HOUR
+
TIMESTAMP
WITH
TIME
ZONE
'2000-01-01 12:00:00+01'
;
>>
2000
-
01
-
27
01
:
00
:
00
+
01
SELECT
TIMESTAMP
WITH
TIME
ZONE
'2000-01-01 12:00:00+01'
-
INTERVAL
'25 13'
DAY
TO
HOUR
;
>>
1999
-
12
-
06
23
:
00
:
00
+
01
SELECT
TIMESTAMP
WITH
TIME
ZONE
'2000-01-01 12:00:00+01'
+
INTERVAL
'1-2'
YEAR
TO
MONTH
;
>>
2001
-
03
-
01
12
:
00
:
00
+
01
SELECT
INTERVAL
'1-2'
YEAR
TO
MONTH
+
TIMESTAMP
WITH
TIME
ZONE
'2000-01-01 12:00:00+01'
;
>>
2001
-
03
-
01
12
:
00
:
00
+
01
SELECT
TIMESTAMP
WITH
TIME
ZONE
'2000-01-01 12:00:00+01'
-
INTERVAL
'1-2'
YEAR
TO
MONTH
;
>>
1998
-
11
-
01
12
:
00
:
00
+
01
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论