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> { } 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& input_types, ut_type::UTDat } return true; } + +template +bool insert_date_cell(MutableColumnPtr& column, const std::string& format, const std::any& cell) { + auto datetime_str = std::any_cast(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(&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(cell); @@ -215,34 +240,20 @@ bool insert_cell(MutableColumnPtr& column, DataTypePtr type_ptr, const std::any& column->insert_data(reinterpret_cast(&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(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(&v), 0); + RETURN_IF_FALSE((insert_date_cell( + 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(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(&v), 0); + RETURN_IF_FALSE((insert_date_cell( + 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(cell); - DateV2Value 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(&v), 0); + RETURN_IF_FALSE( + (insert_date_cell>(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(cell); - DateV2Value 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(&v), 0); + RETURN_IF_FALSE((insert_date_cell>( + column, date_time_format, cell))); } else if (type.is_array()) { auto v = std::any_cast(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') """