This is an automated email from the ASF dual-hosted git repository. yiguolei pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/doris.git
The following commit(s) were added to refs/heads/master by this push: new 50e6c4216a [vectorized](function) suppoort date_trunc function truncate week mode (#18334) 50e6c4216a is described below commit 50e6c4216a26ec6a667060bf1bae6fc6dee217df Author: zhangstar333 <87313068+zhangstar...@users.noreply.github.com> AuthorDate: Tue Apr 4 10:24:26 2023 +0800 [vectorized](function) suppoort date_trunc function truncate week mode (#18334) support date_trunc could truncate week eg: select date_trunc('2023-4-3 19:28:30', 'week'); --- be/src/vec/functions/function_timestamp.cpp | 2 ++ be/src/vec/runtime/vdatetime_value.cpp | 17 +++++++++++++++++ .../sql-functions/date-time-functions/date_trunc.md | 12 ++++++------ .../sql-functions/date-time-functions/date_trunc.md | 9 ++++++++- 4 files changed, 33 insertions(+), 7 deletions(-) diff --git a/be/src/vec/functions/function_timestamp.cpp b/be/src/vec/functions/function_timestamp.cpp index 6d033a03ef..cac6393058 100644 --- a/be/src/vec/functions/function_timestamp.cpp +++ b/be/src/vec/functions/function_timestamp.cpp @@ -290,6 +290,8 @@ struct DateTrunc { null_map[i] = !dt.template datetime_trunc<MINUTE>(); } else if (std::strncmp("second", str_data, 6) == 0) { null_map[i] = !dt.template datetime_trunc<SECOND>(); + } else if (std::strncmp("week", str_data, 4) == 0) { + null_map[i] = !dt.template datetime_trunc<WEEK>(); } else { null_map[i] = 1; } diff --git a/be/src/vec/runtime/vdatetime_value.cpp b/be/src/vec/runtime/vdatetime_value.cpp index a2d41b509b..2d4d78d1ab 100644 --- a/be/src/vec/runtime/vdatetime_value.cpp +++ b/be/src/vec/runtime/vdatetime_value.cpp @@ -1691,6 +1691,14 @@ bool VecDateTimeValue::datetime_trunc() { _hour = 0; break; } + case WEEK: { + _second = 0; + _minute = 0; + _hour = 0; + TimeInterval interval(DAY, weekday(), true); + date_add_interval<DAY>(interval); + break; + } case MONTH: { _second = 0; _minute = 0; @@ -2679,6 +2687,15 @@ bool DateV2Value<T>::datetime_trunc() { date_v2_value_.hour_ = 0; break; } + case WEEK: { + date_v2_value_.microsecond_ = 0; + date_v2_value_.second_ = 0; + date_v2_value_.minute_ = 0; + date_v2_value_.hour_ = 0; + TimeInterval interval(DAY, weekday(), true); + date_add_interval<DAY>(interval); + break; + } case MONTH: { date_v2_value_.microsecond_ = 0; date_v2_value_.second_ = 0; diff --git a/docs/en/docs/sql-manual/sql-functions/date-time-functions/date_trunc.md b/docs/en/docs/sql-manual/sql-functions/date-time-functions/date_trunc.md index b1c4c2a1ee..47d5913f3e 100644 --- a/docs/en/docs/sql-manual/sql-functions/date-time-functions/date_trunc.md +++ b/docs/en/docs/sql-manual/sql-functions/date-time-functions/date_trunc.md @@ -42,7 +42,7 @@ Truncates datetime in the specified time unit. datetime is a legal date expression. -unit is the time unit you want to truncate. The optional values are as follows: [`second`,`minute`,`hour`,`day`,`month`,`quarter`,`year`]。 +unit is the time unit you want to truncate. The optional values are as follows: [`second`,`minute`,`hour`,`day`,`week`,`month`,`quarter`,`year`]。 If unit does not meet the above optional values, the result will return NULL. ### example @@ -76,11 +76,11 @@ mysql> select date_trunc('2010-12-02 19:28:30', 'day'); +-------------------------------------------------+ mysql> select date_trunc('2010-12-02 19:28:30', 'week'); -+-------------------------------------------------+ -| date_trunc('2010-12-02 19:28:30', 'week') | -+-------------------------------------------------+ -| 2010-11-28 00:00:00 | -+-------------------------------------------------+ ++-------------------------------------------+ +| date_trunc('2010-12-02 19:28:30', 'week') | ++-------------------------------------------+ +| 2010-11-29 00:00:00 | ++-------------------------------------------+ mysql> select date_trunc('2010-12-02 19:28:30', 'month'); +-------------------------------------------------+ diff --git a/docs/zh-CN/docs/sql-manual/sql-functions/date-time-functions/date_trunc.md b/docs/zh-CN/docs/sql-manual/sql-functions/date-time-functions/date_trunc.md index 25e4232df9..3fd744e6b5 100644 --- a/docs/zh-CN/docs/sql-manual/sql-functions/date-time-functions/date_trunc.md +++ b/docs/zh-CN/docs/sql-manual/sql-functions/date-time-functions/date_trunc.md @@ -42,7 +42,7 @@ date_trunc datetime 参数是合法的日期表达式。 -unit 参数是您希望截断的时间间隔,可选的值如下:[`second`,`minute`,`hour`,`day`,`month`,`quarter`,`year`]。 +unit 参数是您希望截断的时间间隔,可选的值如下:[`second`,`minute`,`hour`,`day`,`week`,`month`,`quarter`,`year`]。 如果unit 不符合上述可选值,结果将返回NULL。 ### example @@ -75,6 +75,13 @@ mysql> select date_trunc('2010-12-02 19:28:30', 'day'); | 2010-12-02 00:00:00 | +-------------------------------------------------+ +mysql> select date_trunc('2023-4-05 19:28:30', 'week'); ++-------------------------------------------+ +| date_trunc('2023-04-05 19:28:30', 'week') | ++-------------------------------------------+ +| 2023-04-03 00:00:00 | ++-------------------------------------------+ + mysql> select date_trunc('2010-12-02 19:28:30', 'month'); +-------------------------------------------------+ | date_trunc('2010-12-02 19:28:30', 'month') | --------------------------------------------------------------------- To unsubscribe, e-mail: commits-unsubscr...@doris.apache.org For additional commands, e-mail: commits-h...@doris.apache.org