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 75fc830573 [Bug](date function) fix wrong year for format '%x' (#11520)
75fc830573 is described below

commit 75fc83057322428d8385be60cc2a16011c3f4212
Author: Gabriel <gabrielleeb...@gmail.com>
AuthorDate: Fri Aug 5 06:22:22 2022 +0800

    [Bug](date function) fix wrong year for format '%x' (#11520)
---
 be/src/vec/runtime/vdatetime_value.cpp                | 19 +++++++++++--------
 be/src/vec/runtime/vdatetime_value.h                  |  6 ++++--
 .../datetime_functions/test_date_function.out         |  3 +++
 .../datetime_functions/test_date_function.groovy      |  2 ++
 4 files changed, 20 insertions(+), 10 deletions(-)

diff --git a/be/src/vec/runtime/vdatetime_value.cpp 
b/be/src/vec/runtime/vdatetime_value.cpp
index 401856f48d..3a4393a706 100644
--- a/be/src/vec/runtime/vdatetime_value.cpp
+++ b/be/src/vec/runtime/vdatetime_value.cpp
@@ -798,7 +798,7 @@ bool VecDateTimeValue::to_format_string(const char* format, 
int len, char* to) c
                 return false;
             }
             uint32_t year = 0;
-            calc_week(*this, mysql_week_mode(3), &year);
+            calc_week(*this, mysql_week_mode(3), &year, true);
             pos = int_to_str(year, buf);
             to = append_with_prefix(buf, pos - buf, '0', 4, to);
             break;
@@ -834,15 +834,18 @@ bool VecDateTimeValue::to_format_string(const char* 
format, int len, char* to) c
     return true;
 }
 
-uint8_t VecDateTimeValue::calc_week(const VecDateTimeValue& value, uint8_t 
mode, uint32_t* year) {
+uint8_t VecDateTimeValue::calc_week(const VecDateTimeValue& value, uint8_t 
mode, uint32_t* year,
+                                    bool disable_lut) {
     // mode=3 is used for week_of_year()
-    if (config::enable_time_lut && mode == 3 && value._year >= 1950 && 
value._year < 2030) {
+    if (config::enable_time_lut && !disable_lut && mode == 3 && value._year >= 
1950 &&
+        value._year < 2030) {
         return doris::TimeLUT::GetImplement()
                 ->week_of_year_table[value._year - 
doris::LUT_START_YEAR][value._month - 1]
                                     [value._day - 1];
     }
     // mode=4 is used for week()
-    if (config::enable_time_lut && mode == 4 && value._year >= 1950 && 
value._year < 2030) {
+    if (config::enable_time_lut && !disable_lut && mode == 4 && value._year >= 
1950 &&
+        value._year < 2030) {
         return doris::TimeLUT::GetImplement()
                 ->week_table[value._year - doris::LUT_START_YEAR][value._month 
- 1][value._day - 1];
     }
@@ -2806,7 +2809,7 @@ bool DateV2Value<T>::to_format_string(const char* format, 
int len, char* to) con
             // numeric, four digits; used with %v
             uint16_t year = 0;
             calc_week(this->daynr(), this->year(), this->month(), this->day(), 
mysql_week_mode(3),
-                      &year);
+                      &year, true);
             pos = int_to_str(year, buf);
             to = append_with_prefix(buf, pos - buf, '0', 4, to);
             break;
@@ -2947,13 +2950,13 @@ bool DateV2Value<T>::from_date_int64(int64_t value) {
 template <typename T>
 uint8_t DateV2Value<T>::calc_week(const uint32_t& day_nr, const uint16_t& year,
                                   const uint8_t& month, const uint8_t& day, 
uint8_t mode,
-                                  uint16_t* to_year) {
-    if (config::enable_time_lut && mode == 3 && year >= 1950 && year < 2030) {
+                                  uint16_t* to_year, bool disable_lut) {
+    if (config::enable_time_lut && !disable_lut && mode == 3 && year >= 1950 
&& year < 2030) {
         return doris::TimeLUT::GetImplement()
                 ->week_of_year_table[year - doris::LUT_START_YEAR][month - 
1][day - 1];
     }
     // mode=4 is used for week()
-    if (config::enable_time_lut && mode == 4 && year >= 1950 && year < 2030) {
+    if (config::enable_time_lut && !disable_lut && mode == 4 && year >= 1950 
&& year < 2030) {
         return doris::TimeLUT::GetImplement()
                 ->week_table[year - doris::LUT_START_YEAR][month - 1][day - 1];
     }
diff --git a/be/src/vec/runtime/vdatetime_value.h 
b/be/src/vec/runtime/vdatetime_value.h
index 71d02db847..1a384e20aa 100644
--- a/be/src/vec/runtime/vdatetime_value.h
+++ b/be/src/vec/runtime/vdatetime_value.h
@@ -674,7 +674,8 @@ private:
     int64_t to_date_int64() const;
     int64_t to_time_int64() const;
 
-    static uint8_t calc_week(const VecDateTimeValue& value, uint8_t mode, 
uint32_t* year);
+    static uint8_t calc_week(const VecDateTimeValue& value, uint8_t mode, 
uint32_t* year,
+                             bool disable_lut = false);
 
     // This is private function which modify date but modify `_type`
     bool get_date_from_daynr(uint64_t);
@@ -1135,7 +1136,8 @@ public:
 
 private:
     static uint8_t calc_week(const uint32_t& day_nr, const uint16_t& year, 
const uint8_t& month,
-                             const uint8_t& day, uint8_t mode, uint16_t* 
to_year);
+                             const uint8_t& day, uint8_t mode, uint16_t* 
to_year,
+                             bool disable_lut = false);
 
     // Used to construct from int value
     int64_t standardize_timevalue(int64_t value);
diff --git 
a/regression-test/data/query/sql_functions/datetime_functions/test_date_function.out
 
b/regression-test/data/query/sql_functions/datetime_functions/test_date_function.out
index 8b68ed3ee6..1e5c5af5f3 100644
--- 
a/regression-test/data/query/sql_functions/datetime_functions/test_date_function.out
+++ 
b/regression-test/data/query/sql_functions/datetime_functions/test_date_function.out
@@ -347,3 +347,6 @@ true
 -- !sql --
 true
 
+-- !sql --
+2022 31 4
+
diff --git 
a/regression-test/suites/query/sql_functions/datetime_functions/test_date_function.groovy
 
b/regression-test/suites/query/sql_functions/datetime_functions/test_date_function.groovy
index 8740bbc0a9..e4a14992d9 100644
--- 
a/regression-test/suites/query/sql_functions/datetime_functions/test_date_function.groovy
+++ 
b/regression-test/suites/query/sql_functions/datetime_functions/test_date_function.groovy
@@ -290,4 +290,6 @@ suite("test_date_function", "query") {
     qt_sql """SELECT unix_timestamp(CURDATE()) = 
unix_timestamp(CURRENT_DATE());"""
 
     sql """ drop table ${tableName} """
+
+    qt_sql """ select date_format('2022-08-04', '%X %V %w'); """
 }


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscr...@doris.apache.org
For additional commands, e-mail: commits-h...@doris.apache.org

Reply via email to