From 2335902abaf87a6e1215851f3ba092afd36cad23 Mon Sep 17 00:00:00 2001 From: xiaojunjie <971308896@qq.com> Date: Thu, 20 Oct 2022 09:02:33 +0800 Subject: [PATCH] [bugfix](VecDateTimeValue) eat the value of microsecond in function from_date_format_str (#13446) * [bugfix](VecDateTimeValue) eat the value of microsecond in function from_date_format_str * add sql based regression test Co-authored-by: xiaojunjie --- be/src/vec/runtime/vdatetime_value.cpp | 6 ++++++ be/test/vec/runtime/vdatetime_value_test.cpp | 21 +++++++++++++++++++ .../datetime_functions/test_date_function.out | 3 +++ .../test_date_function.groovy | 1 + 4 files changed, 31 insertions(+) diff --git a/be/src/vec/runtime/vdatetime_value.cpp b/be/src/vec/runtime/vdatetime_value.cpp index 1458349e0cb56e6..b8622b2a84db6bc 100644 --- a/be/src/vec/runtime/vdatetime_value.cpp +++ b/be/src/vec/runtime/vdatetime_value.cpp @@ -1275,6 +1275,12 @@ bool VecDateTimeValue::from_date_format_str(const char* format, int format_len, break; // Micro second case 'f': + // _microsecond is removed, but need to eat this val + tmp = val + min(6, val_end - val); + if (!str_to_int64(val, &tmp, &int_value)) { + return false; + } + val = tmp; break; // AM/PM case 'p': diff --git a/be/test/vec/runtime/vdatetime_value_test.cpp b/be/test/vec/runtime/vdatetime_value_test.cpp index 312b0ed423ec3b5..39b91bde778abf1 100644 --- a/be/test/vec/runtime/vdatetime_value_test.cpp +++ b/be/test/vec/runtime/vdatetime_value_test.cpp @@ -481,6 +481,27 @@ TEST(VDateTimeValueTest, date_diff_test) { EXPECT_TRUE(datetime_diff(date_v2_1, date_v2_2) == 31 * 24 * 60); EXPECT_TRUE(datetime_diff(date_v2_1, date_v2_2) == 31 * 24 * 60 * 60); } + + { + VecDateTimeValue date_v2_1; + std::string origin_date1 = "2022-05-24 06:00:00"; + std::string date_format1 = "%Y-%m-%d %H:%i:%s"; + EXPECT_TRUE(date_v2_1.from_date_format_str(date_format1.data(), date_format1.size(), + origin_date1.data(), origin_date1.size())); + + VecDateTimeValue date_v2_2; + std::string origin_date2 = "2022-06-24 06:00:00.123 AM"; + std::string date_format2 = "%Y-%m-%d %h:%i:%s.%f %p"; + EXPECT_TRUE(date_v2_2.from_date_format_str(date_format2.data(), date_format2.size(), + origin_date2.data(), origin_date2.size())); + + EXPECT_TRUE(datetime_diff(date_v2_1, date_v2_2) == 31); + EXPECT_TRUE(datetime_diff(date_v2_1, date_v2_2) == 0); + EXPECT_TRUE(datetime_diff(date_v2_1, date_v2_2) == 1); + EXPECT_TRUE(datetime_diff(date_v2_1, date_v2_2) == 31 * 24); + EXPECT_TRUE(datetime_diff(date_v2_1, date_v2_2) == 31 * 24 * 60); + EXPECT_TRUE(datetime_diff(date_v2_1, date_v2_2) == 31 * 24 * 60 * 60); + } } TEST(VDateTimeValueTest, date_v2_to_string_test) { 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 c435725dbd0a939..4c8b24b8c72a9b7 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 @@ -173,6 +173,9 @@ February -- !sql -- 2014-12-21T12:34:56 +-- !sql -- +2014-12-21T12:34:56 + -- !sql -- 2004-10-18 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 a78120b34a94400..5250447e4559f91 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 @@ -180,6 +180,7 @@ suite("test_date_function") { sql """ insert into ${tableName} values ("2014-12-21 12:34:56") """ qt_sql """ select str_to_date(test_datetime, '%Y-%m-%d %H:%i:%s') from ${tableName}; """ qt_sql """ select str_to_date("2014-12-21 12:34%3A56", '%Y-%m-%d %H:%i%%3A%s'); """ + qt_sql """ select str_to_date("2014-12-21 12:34:56.789 PM", '%Y-%m-%d %h:%i:%s.%f %p'); """ qt_sql """ select str_to_date('200442 Monday', '%X%V %W') """ sql """ truncate table ${tableName} """ sql """ insert into ${tableName} values ("2020-09-01") """