提交 c7dd089e authored 作者: Stéphane Eintrazi's avatar Stéphane Eintrazi

Implement 'YEAR' in 'DATE_TRUNC'

上级 493a7e8b
...@@ -464,14 +464,13 @@ public final class DateTimeFunctions { ...@@ -464,14 +464,13 @@ public final class DateTimeFunctions {
long yearForQuarter = DateTimeUtils.yearFromDateValue(dateValue); long yearForQuarter = DateTimeUtils.yearFromDateValue(dateValue);
int monthForQuarter = DateTimeUtils.monthFromDateValue(dateValue); int monthForQuarter = DateTimeUtils.monthFromDateValue(dateValue);
if (monthForQuarter >= 10) {
if(monthForQuarter >= 10) {
monthForQuarter = 10; monthForQuarter = 10;
}else if (monthForQuarter >= 7) { } else if (monthForQuarter >= 7) {
monthForQuarter = 7; monthForQuarter = 7;
}else if(monthForQuarter >= 4) { } else if (monthForQuarter >= 4) {
monthForQuarter = 4; monthForQuarter = 4;
}else { } else {
monthForQuarter = 1; monthForQuarter = 1;
} }
...@@ -479,6 +478,13 @@ public final class DateTimeFunctions { ...@@ -479,6 +478,13 @@ public final class DateTimeFunctions {
timeNanos = 0l; timeNanos = 0l;
break; break;
case YEAR:
long yearForYear = DateTimeUtils.yearFromDateValue(dateValue);
dateValue = DateTimeUtils.dateValue(yearForYear, 1, 1);
timeNanos = 0l;
break;
} }
if (timeNanos == null) { if (timeNanos == null) {
......
...@@ -818,18 +818,63 @@ SELECT DATE_TRUNC('quarter', '2015-12-29 15:14:13'); ...@@ -818,18 +818,63 @@ SELECT DATE_TRUNC('quarter', '2015-12-29 15:14:13');
SELECT DATE_TRUNC('QUARTER', '2015-12-29 15:14:13'); SELECT DATE_TRUNC('QUARTER', '2015-12-29 15:14:13');
>> 2015-10-01 00:00:00 >> 2015-10-01 00:00:00
-- --
-- Test unhandled time unit -- Test time unit 'YEAR'
-- --
SELECT DATE_TRUNC('---', '2015-05-29 15:14:13'); select DATE_TRUNC('year', time '00:00:00');
> exception >> 1970-01-01 00:00:00
select DATE_TRUNC('YEAR', time '00:00:00');
>> 1970-01-01 00:00:00
select DATE_TRUNC('year', time '15:14:13');
>> 1970-01-01 00:00:00
select DATE_TRUNC('YEAR', time '15:14:13');
>> 1970-01-01 00:00:00
select DATE_TRUNC('year', date '2015-05-28');
>> 2015-01-01 00:00:00
select DATE_TRUNC('YEAR', date '2015-05-28');
>> 2015-01-01 00:00:00
select DATE_TRUNC('year', timestamp '2015-05-29 15:14:13');
>> 2015-01-01 00:00:00
select DATE_TRUNC('YEAR', timestamp '2015-05-29 15:14:13');
>> 2015-01-01 00:00:00
select DATE_TRUNC('year', timestamp with time zone '2015-05-29 15:14:13');
>> 2015-01-01 00:00:00+00
select DATE_TRUNC('YEAR', timestamp with time zone '2015-05-29 15:14:13');
>> 2015-01-01 00:00:00+00
select DATE_TRUNC('year', timestamp with time zone '2015-05-29 05:14:13-06');
>> 2015-01-01 00:00:00-06
select DATE_TRUNC('YEAR', timestamp with time zone '2015-05-29 05:14:13-06');
>> 2015-01-01 00:00:00-06
select DATE_TRUNC('year', timestamp with time zone '2015-05-29 15:14:13+10');
>> 2015-01-01 00:00:00+10
select DATE_TRUNC('YEAR', timestamp with time zone '2015-05-29 15:14:13+10');
>> 2015-01-01 00:00:00+10
SELECT DATE_TRUNC('year', '2015-05-29 15:14:13'); SELECT DATE_TRUNC('year', '2015-05-29 15:14:13');
> exception >> 2015-01-01 00:00:00
SELECT DATE_TRUNC('YEAR', '2015-05-29 15:14:13'); SELECT DATE_TRUNC('YEAR', '2015-05-29 15:14:13');
>> 2015-01-01 00:00:00
--
-- Test unhandled time unit
--
SELECT DATE_TRUNC('---', '2015-05-29 15:14:13');
> exception > exception
SELECT DATE_TRUNC('decade', '2015-05-29 15:14:13'); SELECT DATE_TRUNC('decade', '2015-05-29 15:14:13');
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论