You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@doris.apache.org by pa...@apache.org on 2022/10/19 03:28:06 UTC

[doris] branch master updated: [fix](year) fix `year()` results are not as expected (#13426)

This is an automated email from the ASF dual-hosted git repository.

panxiaolei 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 c449028a5f [fix](year) fix `year()` results are not as expected (#13426)
c449028a5f is described below

commit c449028a5f8766856ec14f8d8224c6d66eca2621
Author: luozenglin <37...@users.noreply.github.com>
AuthorDate: Wed Oct 19 11:28:00 2022 +0800

    [fix](year) fix `year()` results are not as expected (#13426)
    
    fix `year()` results are not as expected
---
 be/src/vec/functions/date_time_transforms.h        |  2 +-
 be/test/vec/function/function_test_util.cpp        | 55 +++++++++++++---------
 .../datetime_functions/test_date_function.out      |  5 ++
 .../datetime_functions/test_date_function.groovy   |  4 ++
 4 files changed, 43 insertions(+), 23 deletions(-)

diff --git a/be/src/vec/functions/date_time_transforms.h b/be/src/vec/functions/date_time_transforms.h
index 5fca7b1ce3..b4641f9b40 100644
--- a/be/src/vec/functions/date_time_transforms.h
+++ b/be/src/vec/functions/date_time_transforms.h
@@ -308,7 +308,7 @@ struct Transformer<FromType, ToType, ToYearImpl<FromType>> {
         }
 
         for (size_t i = 0; i < size; ++i) {
-            null_map_ptr[i] = to_ptr[i] <= MIN_YEAR || to_ptr[i] >= MAX_YEAR;
+            null_map_ptr[i] = to_ptr[i] > MAX_YEAR;
         }
     }
 };
diff --git a/be/test/vec/function/function_test_util.cpp b/be/test/vec/function/function_test_util.cpp
index 92febd0640..da601eae6d 100644
--- a/be/test/vec/function/function_test_util.cpp
+++ b/be/test/vec/function/function_test_util.cpp
@@ -172,12 +172,37 @@ bool parse_ut_data_type(const std::vector<std::any>& input_types, ut_type::UTDat
     }
     return true;
 }
+
+template <typename Date, TypeIndex type_index = TypeIndex::Nothing>
+bool insert_date_cell(MutableColumnPtr& column, const std::string& format, const std::any& cell) {
+    auto datetime_str = std::any_cast<std::string>(cell);
+    Date v;
+    auto result = v.from_date_format_str(format.c_str(), format.size(), datetime_str.c_str(),
+                                         datetime_str.size());
+    if constexpr (type_index == TypeIndex::Date) {
+        v.cast_to_date();
+    } else if constexpr (type_index == TypeIndex::DateTime) {
+        v.to_datetime();
+    }
+    if (result) {
+        column->insert_data(reinterpret_cast<char*>(&v), 0);
+    } else if (column->is_nullable()) {
+        column->insert_data(nullptr, 0);
+    } else {
+        return false;
+    }
+    return true;
+}
+
 bool insert_cell(MutableColumnPtr& column, DataTypePtr type_ptr, const std::any& cell) {
     if (cell.type() == typeid(Null)) {
         column->insert_data(nullptr, 0);
         return true;
     }
 
+#define RETURN_IF_FALSE(x) \
+    if (UNLIKELY(!(x))) return false
+
     WhichDataType type(type_ptr);
     if (type.is_string()) {
         auto str = std::any_cast<ut_type::STRING>(cell);
@@ -215,34 +240,20 @@ bool insert_cell(MutableColumnPtr& column, DataTypePtr type_ptr, const std::any&
         column->insert_data(reinterpret_cast<char*>(&value), 0);
     } else if (type.is_date_time()) {
         static std::string date_time_format("%Y-%m-%d %H:%i:%s");
-        auto datetime_str = std::any_cast<std::string>(cell);
-        VecDateTimeValue v;
-        v.from_date_format_str(date_time_format.c_str(), date_time_format.size(),
-                               datetime_str.c_str(), datetime_str.size());
-        v.to_datetime();
-        column->insert_data(reinterpret_cast<char*>(&v), 0);
+        RETURN_IF_FALSE((insert_date_cell<VecDateTimeValue, TypeIndex::DateTime>(
+                column, date_time_format, cell)));
     } else if (type.is_date()) {
         static std::string date_time_format("%Y-%m-%d");
-        auto datetime_str = std::any_cast<std::string>(cell);
-        VecDateTimeValue v;
-        v.from_date_format_str(date_time_format.c_str(), date_time_format.size(),
-                               datetime_str.c_str(), datetime_str.size());
-        v.cast_to_date();
-        column->insert_data(reinterpret_cast<char*>(&v), 0);
+        RETURN_IF_FALSE((insert_date_cell<VecDateTimeValue, TypeIndex::Date>(
+                column, date_time_format, cell)));
     } else if (type.is_date_v2()) {
         static std::string date_time_format("%Y-%m-%d");
-        auto datetime_str = std::any_cast<std::string>(cell);
-        DateV2Value<DateV2ValueType> v;
-        v.from_date_format_str(date_time_format.c_str(), date_time_format.size(),
-                               datetime_str.c_str(), datetime_str.size());
-        column->insert_data(reinterpret_cast<char*>(&v), 0);
+        RETURN_IF_FALSE(
+                (insert_date_cell<DateV2Value<DateV2ValueType>>(column, date_time_format, cell)));
     } else if (type.is_date_time_v2()) {
         static std::string date_time_format("%Y-%m-%d %H:%i:%s.%f");
-        auto datetime_str = std::any_cast<std::string>(cell);
-        DateV2Value<DateTimeV2ValueType> v;
-        v.from_date_format_str(date_time_format.c_str(), date_time_format.size(),
-                               datetime_str.c_str(), datetime_str.size());
-        column->insert_data(reinterpret_cast<char*>(&v), 0);
+        RETURN_IF_FALSE((insert_date_cell<DateV2Value<DateTimeV2ValueType>>(
+                column, date_time_format, cell)));
     } else if (type.is_array()) {
         auto v = std::any_cast<Array>(cell);
         column->insert(v);
diff --git a/regression-test/data/query_p0/sql_functions/datetime_functions/test_date_function.out b/regression-test/data/query_p0/sql_functions/datetime_functions/test_date_function.out
index 3d6ec78ee2..c435725dbd 100644
--- a/regression-test/data/query_p0/sql_functions/datetime_functions/test_date_function.out
+++ b/regression-test/data/query_p0/sql_functions/datetime_functions/test_date_function.out
@@ -281,6 +281,11 @@ February
 -- !sql --
 2050
 
+-- !sql --
+0000-08-01T13:21:03	0
+2019-08-01T13:21:03	2019
+9999-08-01T13:21:03	9999
+
 -- !sql --
 202052
 
diff --git a/regression-test/suites/query_p0/sql_functions/datetime_functions/test_date_function.groovy b/regression-test/suites/query_p0/sql_functions/datetime_functions/test_date_function.groovy
index d319265b16..a78120b34a 100644
--- a/regression-test/suites/query_p0/sql_functions/datetime_functions/test_date_function.groovy
+++ b/regression-test/suites/query_p0/sql_functions/datetime_functions/test_date_function.groovy
@@ -242,9 +242,13 @@ suite("test_date_function") {
     // WEEKOFYEAR
     qt_sql """ select weekofyear('2008-02-20 00:00:00') """
 
+    sql """ truncate table ${tableName} """
+    sql """ insert into ${tableName} values ("2019-08-01 13:21:03"), ("9999-08-01 13:21:03"),("0-08-01 13:21:03")"""
+
     // YEAR
     qt_sql """ select year('1987-01-01') """
     qt_sql """ select year('2050-01-01') """
+    qt_sql """ select test_datetime, year(test_datetime) from ${tableName} order by test_datetime """
 
     // YEARWEEK
     qt_sql """ select yearweek('2021-1-1') """


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