Skip to content
项目
群组
代码片段
帮助
正在加载...
帮助
为 GitLab 提交贡献
登录/注册
切换导航
H
h2database
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
分枝图
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
计划
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
分枝图
统计图
创建新议题
作业
提交
议题看板
打开侧边栏
Administrator
h2database
Commits
18f836ec
提交
18f836ec
authored
8月 14, 2018
作者:
Evgenij Ryazanov
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add support of INTERVAL data type to EXTRACT function
上级
ff41fdeb
显示空白字符变更
内嵌
并排
正在显示
3 个修改的文件
包含
354 行增加
和
49 行删除
+354
-49
DateTimeFunctions.java
h2/src/main/org/h2/util/DateTimeFunctions.java
+107
-49
DateTimeUtils.java
h2/src/main/org/h2/util/DateTimeUtils.java
+145
-0
extract.sql
...est/org/h2/test/scripts/functions/timeanddate/extract.sql
+102
-0
没有找到文件。
h2/src/main/org/h2/util/DateTimeFunctions.java
浏览文件 @
18f836ec
...
@@ -37,10 +37,12 @@ import java.util.TimeZone;
...
@@ -37,10 +37,12 @@ import java.util.TimeZone;
import
org.h2.api.ErrorCode
;
import
org.h2.api.ErrorCode
;
import
org.h2.expression.Function
;
import
org.h2.expression.Function
;
import
org.h2.message.DbException
;
import
org.h2.message.DbException
;
import
org.h2.value.DataType
;
import
org.h2.value.Value
;
import
org.h2.value.Value
;
import
org.h2.value.ValueDate
;
import
org.h2.value.ValueDate
;
import
org.h2.value.ValueDecimal
;
import
org.h2.value.ValueDecimal
;
import
org.h2.value.ValueInt
;
import
org.h2.value.ValueInt
;
import
org.h2.value.ValueInterval
;
import
org.h2.value.ValueTime
;
import
org.h2.value.ValueTime
;
import
org.h2.value.ValueTimestamp
;
import
org.h2.value.ValueTimestamp
;
import
org.h2.value.ValueTimestampTimeZone
;
import
org.h2.value.ValueTimestampTimeZone
;
...
@@ -331,8 +333,25 @@ public final class DateTimeFunctions {
...
@@ -331,8 +333,25 @@ public final class DateTimeFunctions {
if
(
field
!=
EPOCH
)
{
if
(
field
!=
EPOCH
)
{
result
=
ValueInt
.
get
(
getIntDatePart
(
value
,
field
));
result
=
ValueInt
.
get
(
getIntDatePart
(
value
,
field
));
}
else
{
}
else
{
// Case where we retrieve the EPOCH time.
// Case where we retrieve the EPOCH time.
if
(
value
instanceof
ValueInterval
)
{
ValueInterval
interval
=
(
ValueInterval
)
value
;
BigDecimal
bd
;
if
(
interval
.
getQualifier
().
isYearMonth
())
{
interval
=
(
ValueInterval
)
interval
.
convertTo
(
Value
.
INTERVAL_YEAR_TO_MONTH
);
long
leading
=
interval
.
getLeading
();
long
remaining
=
interval
.
getRemaining
();
bd
=
BigDecimal
.
valueOf
(
leading
).
multiply
(
BigDecimal
.
valueOf
(
31557600
))
.
add
(
BigDecimal
.
valueOf
(
remaining
*
2592000
));
if
(
interval
.
isNegative
())
{
bd
=
bd
.
negate
();
}
}
else
{
bd
=
new
BigDecimal
(
DateTimeUtils
.
intervalToAbsolute
(
interval
))
.
divide
(
BigDecimal
.
valueOf
(
1_000_000_000L
));
}
return
ValueDecimal
.
get
(
bd
);
}
// First we retrieve the dateValue and his time in nanoseconds.
// First we retrieve the dateValue and his time in nanoseconds.
long
[]
a
=
DateTimeUtils
.
dateAndTimeFromValue
(
value
);
long
[]
a
=
DateTimeUtils
.
dateAndTimeFromValue
(
value
);
long
dateValue
=
a
[
0
];
long
dateValue
=
a
[
0
];
...
@@ -602,6 +621,44 @@ public final class DateTimeFunctions {
...
@@ -602,6 +621,44 @@ public final class DateTimeFunctions {
* @return the value
* @return the value
*/
*/
public
static
int
getIntDatePart
(
Value
date
,
int
field
)
{
public
static
int
getIntDatePart
(
Value
date
,
int
field
)
{
if
(
date
instanceof
ValueInterval
)
{
ValueInterval
interval
=
(
ValueInterval
)
date
;
long
v
;
switch
(
field
)
{
case
YEAR:
v
=
DateTimeUtils
.
yearsFromInterval
(
interval
);
break
;
case
MONTH:
v
=
DateTimeUtils
.
monthFromInterval
(
interval
);
break
;
case
DAY_OF_MONTH:
case
DAY_OF_WEEK:
case
DAY_OF_YEAR:
v
=
DateTimeUtils
.
daysFromInterval
(
interval
);
break
;
case
HOUR:
v
=
DateTimeUtils
.
hoursFromInterval
(
interval
);
break
;
case
MINUTE:
v
=
DateTimeUtils
.
minutesFromInterval
(
interval
);
break
;
case
SECOND:
v
=
DateTimeUtils
.
nanosFromInterval
(
interval
)
/
1_000_000_000
;
break
;
case
MILLISECOND:
v
=
DateTimeUtils
.
nanosFromInterval
(
interval
)
/
1_000_000
%
1_000
;
break
;
case
MICROSECOND:
v
=
DateTimeUtils
.
nanosFromInterval
(
interval
)
/
1_000
%
1_000_000
;
break
;
case
NANOSECOND:
v
=
DateTimeUtils
.
nanosFromInterval
(
interval
)
%
1_000_000_000
;
break
;
default
:
throw
DbException
.
getUnsupportedException
(
"getDatePart("
+
date
+
", "
+
field
+
')'
);
}
return
(
int
)
v
;
}
else
{
long
[]
a
=
DateTimeUtils
.
dateAndTimeFromValue
(
date
);
long
[]
a
=
DateTimeUtils
.
dateAndTimeFromValue
(
date
);
long
dateValue
=
a
[
0
];
long
dateValue
=
a
[
0
];
long
timeNanos
=
a
[
1
];
long
timeNanos
=
a
[
1
];
...
@@ -653,6 +710,7 @@ public final class DateTimeFunctions {
...
@@ -653,6 +710,7 @@ public final class DateTimeFunctions {
return
offsetMinutes
%
60
;
return
offsetMinutes
%
60
;
}
}
}
}
}
throw
DbException
.
getUnsupportedException
(
"getDatePart("
+
date
+
", "
+
field
+
')'
);
throw
DbException
.
getUnsupportedException
(
"getDatePart("
+
date
+
", "
+
field
+
')'
);
}
}
...
...
h2/src/main/org/h2/util/DateTimeUtils.java
浏览文件 @
18f836ec
...
@@ -2039,4 +2039,149 @@ public class DateTimeUtils {
...
@@ -2039,4 +2039,149 @@ public class DateTimeUtils {
return
Math
.
abs
(
absolute
.
longValue
());
return
Math
.
abs
(
absolute
.
longValue
());
}
}
/**
* @param value interval
* @return years, or 0
*/
public
static
long
yearsFromInterval
(
ValueInterval
value
)
{
IntervalQualifier
qualifier
=
value
.
getQualifier
();
if
(
qualifier
==
IntervalQualifier
.
YEAR
||
qualifier
==
IntervalQualifier
.
YEAR_TO_MONTH
)
{
long
v
=
value
.
getLeading
();
if
(
value
.
isNegative
())
{
v
=
-
v
;
}
return
v
;
}
else
{
return
0
;
}
}
/**
* @param value interval
* @return months, or 0
*/
public
static
long
monthFromInterval
(
ValueInterval
value
)
{
IntervalQualifier
qualifier
=
value
.
getQualifier
();
long
v
;
if
(
qualifier
==
IntervalQualifier
.
MONTH
)
{
v
=
value
.
getLeading
();
}
else
if
(
qualifier
==
IntervalQualifier
.
YEAR_TO_MONTH
){
v
=
value
.
getRemaining
();
}
else
{
return
0
;
}
if
(
value
.
isNegative
())
{
v
=
-
v
;
}
return
v
;
}
/**
* @param value interval
* @return months, or 0
*/
public
static
long
daysFromInterval
(
ValueInterval
value
)
{
switch
(
value
.
getQualifier
())
{
case
DAY:
case
DAY_TO_HOUR:
case
DAY_TO_MINUTE:
case
DAY_TO_SECOND:
long
v
=
value
.
getLeading
();
if
(
value
.
isNegative
())
{
v
=
-
v
;
}
return
v
;
default
:
return
0
;
}
}
/**
* @param value interval
* @return hours, or 0
*/
public
static
long
hoursFromInterval
(
ValueInterval
value
)
{
long
v
;
switch
(
value
.
getQualifier
())
{
case
HOUR:
case
HOUR_TO_MINUTE:
case
HOUR_TO_SECOND:
v
=
value
.
getLeading
();
break
;
case
DAY_TO_HOUR:
v
=
value
.
getRemaining
();
break
;
case
DAY_TO_MINUTE:
v
=
value
.
getRemaining
()
/
60
;
break
;
case
DAY_TO_SECOND:
v
=
value
.
getRemaining
()
/
3_600_000_000_000L
;
break
;
default
:
return
0
;
}
if
(
value
.
isNegative
())
{
v
=
-
v
;
}
return
v
;
}
/**
* @param value interval
* @return minutes, or 0
*/
public
static
long
minutesFromInterval
(
ValueInterval
value
)
{
long
v
;
switch
(
value
.
getQualifier
())
{
case
MINUTE:
case
MINUTE_TO_SECOND:
v
=
value
.
getLeading
();
break
;
case
DAY_TO_MINUTE:
v
=
value
.
getRemaining
()
%
60
;
break
;
case
DAY_TO_SECOND:
v
=
value
.
getRemaining
()
/
60_000_000_000L
%
60
;
break
;
case
HOUR_TO_MINUTE:
v
=
value
.
getRemaining
();
break
;
case
HOUR_TO_SECOND:
v
=
value
.
getRemaining
()
/
60_000_000_000L
;
break
;
default
:
return
0
;
}
if
(
value
.
isNegative
())
{
v
=
-
v
;
}
return
v
;
}
/**
* @param value interval
* @return nanoseconds, or 0
*/
public
static
long
nanosFromInterval
(
ValueInterval
value
)
{
long
v
;
switch
(
value
.
getQualifier
())
{
case
SECOND:
v
=
value
.
getLeading
()
*
1_000_000_000
+
value
.
getRemaining
();
break
;
case
DAY_TO_SECOND:
case
HOUR_TO_SECOND:
v
=
value
.
getRemaining
()
%
60_000_000_000L
;
break
;
case
MINUTE_TO_SECOND:
v
=
value
.
getRemaining
();
break
;
default
:
return
0
;
}
if
(
value
.
isNegative
())
{
v
=
-
v
;
}
return
v
;
}
}
}
h2/src/test/org/h2/test/scripts/functions/timeanddate/extract.sql
浏览文件 @
18f836ec
...
@@ -56,6 +56,24 @@ select EXTRACT (EPOCH from timestamp with time zone '2000-01-03 12:00:00.123456+
...
@@ -56,6 +56,24 @@ select EXTRACT (EPOCH from timestamp with time zone '2000-01-03 12:00:00.123456+
select
extract
(
EPOCH
from
'2001-02-03 14:15:16'
);
select
extract
(
EPOCH
from
'2001-02-03 14:15:16'
);
>>
981209716
>>
981209716
SELECT
EXTRACT
(
EPOCH
FROM
INTERVAL
'10.1'
SECOND
);
>>
10
.
1
SELECT
EXTRACT
(
EPOCH
FROM
INTERVAL
-
'0.000001'
SECOND
);
>>
-
0
.
000001
SELECT
EXTRACT
(
EPOCH
FROM
INTERVAL
'0-1'
YEAR
TO
MONTH
);
>>
2592000
SELECT
EXTRACT
(
EPOCH
FROM
INTERVAL
'-0-1'
YEAR
TO
MONTH
);
>>
-
2592000
SELECT
EXTRACT
(
EPOCH
FROM
INTERVAL
'1-0'
YEAR
TO
MONTH
);
>>
31557600
SELECT
EXTRACT
(
EPOCH
FROM
INTERVAL
'-1-0'
YEAR
TO
MONTH
);
>>
-
31557600
SELECT
EXTRACT
(
TIMEZONE_HOUR
FROM
TIMESTAMP
WITH
TIME
ZONE
'2010-01-02 5:00:00+07:15'
);
SELECT
EXTRACT
(
TIMEZONE_HOUR
FROM
TIMESTAMP
WITH
TIME
ZONE
'2010-01-02 5:00:00+07:15'
);
>>
7
>>
7
...
@@ -76,3 +94,87 @@ select extract(hour from '2001-02-03 14:15:16');
...
@@ -76,3 +94,87 @@ select extract(hour from '2001-02-03 14:15:16');
select
extract
(
week
from
timestamp
'2001-02-03 14:15:16'
);
select
extract
(
week
from
timestamp
'2001-02-03 14:15:16'
);
>>
5
>>
5
SELECT
EXTRACT
(
YEAR
FROM
INTERVAL
'-1'
YEAR
);
>>
-
1
SELECT
EXTRACT
(
YEAR
FROM
INTERVAL
'1-2'
YEAR
TO
MONTH
);
>>
1
SELECT
EXTRACT
(
MONTH
FROM
INTERVAL
'-1-3'
YEAR
TO
MONTH
);
>>
-
3
SELECT
EXTRACT
(
MONTH
FROM
INTERVAL
'3'
MONTH
);
>>
3
SELECT
EXTRACT
(
DAY
FROM
INTERVAL
'1100'
DAY
);
>>
1100
SELECT
EXTRACT
(
DAY
FROM
INTERVAL
'10 23'
DAY
TO
HOUR
);
>>
10
SELECT
EXTRACT
(
DAY
FROM
INTERVAL
'10 23:15'
DAY
TO
MINUTE
);
>>
10
SELECT
EXTRACT
(
DAY
FROM
INTERVAL
'10 23:15:30'
DAY
TO
SECOND
);
>>
10
SELECT
EXTRACT
(
HOUR
FROM
INTERVAL
'15'
HOUR
);
>>
15
SELECT
EXTRACT
(
HOUR
FROM
INTERVAL
'2 15'
DAY
TO
HOUR
);
>>
15
SELECT
EXTRACT
(
HOUR
FROM
INTERVAL
'2 10:30'
DAY
TO
MINUTE
);
>>
10
SELECT
EXTRACT
(
HOUR
FROM
INTERVAL
'2 10:30:15'
DAY
TO
SECOND
);
>>
10
SELECT
EXTRACT
(
HOUR
FROM
INTERVAL
'20:10'
HOUR
TO
MINUTE
);
>>
20
SELECT
EXTRACT
(
HOUR
FROM
INTERVAL
'20:10:22'
HOUR
TO
SECOND
);
>>
20
SELECT
EXTRACT
(
MINUTE
FROM
INTERVAL
'-35'
MINUTE
);
>>
-
35
SELECT
EXTRACT
(
MINUTE
FROM
INTERVAL
'1 20:33'
DAY
TO
MINUTE
);
>>
33
SELECT
EXTRACT
(
MINUTE
FROM
INTERVAL
'1 20:33:10'
DAY
TO
SECOND
);
>>
33
SELECT
EXTRACT
(
MINUTE
FROM
INTERVAL
'20:34'
HOUR
TO
MINUTE
);
>>
34
SELECT
EXTRACT
(
MINUTE
FROM
INTERVAL
'20:34:10'
HOUR
TO
SECOND
);
>>
34
SELECT
EXTRACT
(
MINUTE
FROM
INTERVAL
'-34:10'
MINUTE
TO
SECOND
);
>>
-
34
SELECT
EXTRACT
(
SECOND
FROM
INTERVAL
'-100'
SECOND
);
>>
-
100
SELECT
EXTRACT
(
SECOND
FROM
INTERVAL
'10 11:22:33'
DAY
TO
SECOND
);
>>
33
SELECT
EXTRACT
(
SECOND
FROM
INTERVAL
'1:2:3'
HOUR
TO
SECOND
);
>>
3
SELECT
EXTRACT
(
SECOND
FROM
INTERVAL
'-2:43'
MINUTE
TO
SECOND
);
>>
-
43
SELECT
EXTRACT
(
SECOND
FROM
INTERVAL
'11.123456789'
SECOND
);
>>
11
SELECT
EXTRACT
(
MILLISECOND
FROM
INTERVAL
'11.123456789'
SECOND
);
>>
123
SELECT
EXTRACT
(
MICROSECOND
FROM
INTERVAL
'11.123456789'
SECOND
);
>>
123456
SELECT
EXTRACT
(
NANOSECOND
FROM
INTERVAL
'11.123456789'
SECOND
);
>>
123456789
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论