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

Implement 'QUARTER' in 'DATE_TRUNC'

上级 99a21c77
...@@ -459,6 +459,26 @@ public final class DateTimeFunctions { ...@@ -459,6 +459,26 @@ public final class DateTimeFunctions {
timeNanos = 0l; timeNanos = 0l;
break; break;
case QUARTER:
long yearForQuarter = DateTimeUtils.yearFromDateValue(dateValue);
int monthForQuarter = DateTimeUtils.monthFromDateValue(dateValue);
if(monthForQuarter >= 10) {
monthForQuarter = 10;
}else if (monthForQuarter >= 7) {
monthForQuarter = 7;
}else if(monthForQuarter >= 4) {
monthForQuarter = 4;
}else {
monthForQuarter = 1;
}
dateValue = DateTimeUtils.dateValue(yearForQuarter, monthForQuarter, 1);
timeNanos = 0l;
break;
} }
if (timeNanos == null) { if (timeNanos == null) {
......
...@@ -719,19 +719,113 @@ SELECT DATE_TRUNC('month', '2015-05-29 15:14:13'); ...@@ -719,19 +719,113 @@ SELECT DATE_TRUNC('month', '2015-05-29 15:14:13');
SELECT DATE_TRUNC('MONTH', '2015-05-29 15:14:13'); SELECT DATE_TRUNC('MONTH', '2015-05-29 15:14:13');
>> 2015-05-01 00:00:00 >> 2015-05-01 00:00:00
SELECT DATE_TRUNC('month', '2015-05-01 15:14:13');
>> 2015-05-01 00:00:00
SELECT DATE_TRUNC('MONTH', '2015-05-01 15:14:13');
>> 2015-05-01 00:00:00
-- --
-- Test unhandled time unit -- Test time unit 'QUARTER'
-- --
SELECT DATE_TRUNC('---', '2015-05-29 15:14:13'); select DATE_TRUNC('quarter', time '00:00:00');
> exception >> 1970-01-01 00:00:00
select DATE_TRUNC('QUARTER', time '00:00:00');
>> 1970-01-01 00:00:00
select DATE_TRUNC('quarter', time '15:14:13');
>> 1970-01-01 00:00:00
select DATE_TRUNC('QUARTER', time '15:14:13');
>> 1970-01-01 00:00:00
select DATE_TRUNC('quarter', date '2015-05-28');
>> 2015-04-01 00:00:00
select DATE_TRUNC('QUARTER', date '2015-05-28');
>> 2015-04-01 00:00:00
select DATE_TRUNC('quarter', timestamp '2015-05-29 15:14:13');
>> 2015-04-01 00:00:00
select DATE_TRUNC('QUARTER', timestamp '2015-05-29 15:14:13');
>> 2015-04-01 00:00:00
select DATE_TRUNC('quarter', timestamp with time zone '2015-05-29 15:14:13');
>> 2015-04-01 00:00:00+00
select DATE_TRUNC('QUARTER', timestamp with time zone '2015-05-29 15:14:13');
>> 2015-04-01 00:00:00+00
select DATE_TRUNC('quarter', timestamp with time zone '2015-05-29 05:14:13-06');
>> 2015-04-01 00:00:00-06
select DATE_TRUNC('QUARTER', timestamp with time zone '2015-05-29 05:14:13-06');
>> 2015-04-01 00:00:00-06
select DATE_TRUNC('quarter', timestamp with time zone '2015-05-29 15:14:13+10');
>> 2015-04-01 00:00:00+10
select DATE_TRUNC('QUARTER', timestamp with time zone '2015-05-29 15:14:13+10');
>> 2015-04-01 00:00:00+10
select DATE_TRUNC('quarter', '2015-05-29 15:14:13');
>> 2015-04-01 00:00:00
select DATE_TRUNC('QUARTER', '2015-05-29 15:14:13');
>> 2015-04-01 00:00:00
SELECT DATE_TRUNC('QUARTER', '2018-03-14 00:00:00.000');
>> 2018-01-01 00:00:00
SELECT DATE_TRUNC('quarter', '2018-03-14 00:00:00.000');
>> 2018-01-01 00:00:00
SELECT DATE_TRUNC('quarter', '2015-05-29 15:14:13'); SELECT DATE_TRUNC('quarter', '2015-05-29 15:14:13');
> exception >> 2015-04-01 00:00:00
SELECT DATE_TRUNC('QUARTER', '2015-05-29 15:14:13'); SELECT DATE_TRUNC('QUARTER', '2015-05-29 15:14:13');
>> 2015-04-01 00:00:00
SELECT DATE_TRUNC('quarter', '2015-05-01 15:14:13');
>> 2015-04-01 00:00:00
SELECT DATE_TRUNC('QUARTER', '2015-05-01 15:14:13');
>> 2015-04-01 00:00:00
SELECT DATE_TRUNC('quarter', '2015-07-29 15:14:13');
>> 2015-07-01 00:00:00
SELECT DATE_TRUNC('QUARTER', '2015-07-29 15:14:13');
>> 2015-07-01 00:00:00
SELECT DATE_TRUNC('quarter', '2015-09-29 15:14:13');
>> 2015-07-01 00:00:00
SELECT DATE_TRUNC('QUARTER', '2015-09-29 15:14:13');
>> 2015-07-01 00:00:00
SELECT DATE_TRUNC('quarter', '2015-10-29 15:14:13');
>> 2015-10-01 00:00:00
SELECT DATE_TRUNC('QUARTER', '2015-10-29 15:14:13');
>> 2015-10-01 00:00:00
SELECT DATE_TRUNC('quarter', '2015-12-29 15:14:13');
>> 2015-10-01 00:00:00
SELECT DATE_TRUNC('QUARTER', '2015-12-29 15:14:13');
>> 2015-10-01 00:00:00
--
-- Test unhandled time unit
--
SELECT DATE_TRUNC('---', '2015-05-29 15:14:13');
> exception > exception
SELECT DATE_TRUNC('year', '2015-05-29 15:14:13'); SELECT DATE_TRUNC('year', '2015-05-29 15:14:13');
> exception > exception
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论