From 054a037728d58991db0d0de4ac5e814f504d9429 Mon Sep 17 00:00:00 2001 From: zhaochangle Date: Wed, 27 Mar 2024 09:03:54 +0800 Subject: [PATCH] 2 --- be/src/vec/functions/function_timestamp.cpp | 14 ++++++-- be/src/vec/runtime/vdatetime_value.cpp | 2 +- .../datetime_functions/test_date_function.out | 36 ++++++++++++++----- .../test_date_function.groovy | 12 ++++--- 4 files changed, 47 insertions(+), 17 deletions(-) diff --git a/be/src/vec/functions/function_timestamp.cpp b/be/src/vec/functions/function_timestamp.cpp index dd2310939cfd5c..c2285795c71fc6 100644 --- a/be/src/vec/functions/function_timestamp.cpp +++ b/be/src/vec/functions/function_timestamp.cpp @@ -559,6 +559,13 @@ struct UnixTimeStampImpl { return (Int32)timestamp; } + static std::pair trim_timestamp(std::pair timestamp) { + if (timestamp.first < 0 || timestamp.first > INT_MAX) { + return {0, 0}; + } + return std::make_pair((Int32)timestamp.first, (Int32)timestamp.second); + } + static DataTypes get_variadic_argument_types() { return {}; } static DataTypePtr get_return_type_impl(const ColumnsWithTypeAndName& arguments) { @@ -846,16 +853,17 @@ struct UnixTimeStampStrImpl { std::pair timestamp {}; if (!ts_value.unix_timestamp(×tamp, context->state()->timezone_obj())) { - null_map_data[i] = true; + null_map_data[i] = true; // impossible now } else { null_map_data[i] = false; - auto& [sec, ms] = timestamp; - sec = UnixTimeStampImpl::trim_timestamp(sec); + auto [sec, ms] = UnixTimeStampImpl::trim_timestamp(timestamp); + // trailing ms auto ms_str = std::to_string(ms).substr(0, 6); if (ms_str.empty()) { ms_str = "0"; } + col_result_data[i] = Decimal64::from_int_frac(sec, std::stoll(ms_str), 6).value; } } diff --git a/be/src/vec/runtime/vdatetime_value.cpp b/be/src/vec/runtime/vdatetime_value.cpp index 7d3f7f8be7ef0f..ee384286455b40 100644 --- a/be/src/vec/runtime/vdatetime_value.cpp +++ b/be/src/vec/runtime/vdatetime_value.cpp @@ -3265,7 +3265,7 @@ bool DateV2Value::unix_timestamp(std::pair* timestamp, ctz); timestamp->first = tp.time_since_epoch().count(); timestamp->second = date_v2_value_.microsecond_; - } else { + } else { // just make compiler happy } return true; } 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 7f9f629c00d070..6a59c3c994729c 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 @@ -687,17 +687,35 @@ true 1694966400.000000 1694966400.000000 -- !sql_varchar1 -- -1605024000.000000 -1607702400.000000 -1607788800.000000 +0000-00-00 %Y-%m-%d \N +9999-12-31 23:59:59.999999 %Y-%m-%d %H:%i:%s.%f 0.000000 +9999-12-31 23:59:59.9999999 %Y-%m-%d %H:%i:%s.%f 0.000000 +0000-01-01 %Y-%m-%d 0.000000 +9999-12-31 23:59:59 %Y-%m-%d %H:%i:%s 0.000000 +1999-12-31 23:59:59.9999999 %Y-%m-%d %H:%i:%s.%f 946655999.999999 +20201111 %Y%m%d 1605024000.000000 +2020-12-12 %Y-%m-%d 1607702400.000000 +202012-13 %Y%m-%d 1607788800.000000 -- !sql_varchar1 -- -\N -\N -1607702400.000000 +20201111 \N +0000-00-00 \N +202012-13 \N +0000-01-01 0.000000 +9999-12-31 23:59:59.9999999 0.000000 +9999-12-31 23:59:59.999999 0.000000 +9999-12-31 23:59:59 0.000000 +1999-12-31 23:59:59.9999999 946569600.000000 +2020-12-12 1607702400.000000 -- !sql_varchar1 -- -\N -\N -660931200.000000 +%Y%m-%d \N +%Y%m%d \N +%Y-%m-%d 660931200.000000 +%Y-%m-%d %H:%i:%s.%f 660931200.000000 +%Y-%m-%d %H:%i:%s.%f 660931200.000000 +%Y-%m-%d 660931200.000000 +%Y-%m-%d 660931200.000000 +%Y-%m-%d %H:%i:%s.%f 660931200.000000 +%Y-%m-%d %H:%i:%s 660931200.000000 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 96d31c5c902f88..f174819979d4e3 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 @@ -735,6 +735,7 @@ suite("test_date_function") { SELECT FROM_UNIXTIME(UNIX_TIMESTAMP('20230918', '%Y%m%d'), 'yyyy-MM-dd HH:mm:ss') AS `a` )t """ + sql """ drop table if exists date_varchar """ sql """ create table date_varchar( dt varchar null, @@ -743,8 +744,11 @@ suite("test_date_function") { DISTRIBUTED BY HASH(`dt`) BUCKETS 1 properties("replication_num" = "1"); """ - sql """ insert into date_varchar values ("2020-12-12", "%Y-%m-%d"), ("20201111", "%Y%m%d"), ("202012-13", "%Y%m-%d"); """ - qt_sql_varchar1 """ select unix_timestamp(dt, fmt) as k1 from date_varchar order by k1; """ - qt_sql_varchar1 """ select unix_timestamp(dt, "%Y-%m-%d") as k1 from date_varchar order by k1; """ - qt_sql_varchar1 """ select unix_timestamp("1990-12-12", fmt) as k1 from date_varchar order by k1; """ + sql """ insert into date_varchar values ("2020-12-12", "%Y-%m-%d"), ("20201111", "%Y%m%d"), ("202012-13", "%Y%m-%d"), + ("0000-00-00", "%Y-%m-%d"),("0000-01-01", "%Y-%m-%d"),("9999-12-31 23:59:59", "%Y-%m-%d %H:%i:%s"), + ("9999-12-31 23:59:59.999999", "%Y-%m-%d %H:%i:%s.%f"), ("9999-12-31 23:59:59.9999999", "%Y-%m-%d %H:%i:%s.%f"), + ("1999-12-31 23:59:59.9999999", "%Y-%m-%d %H:%i:%s.%f"); """ + qt_sql_varchar1 """ select dt, fmt, unix_timestamp(dt, fmt) as k1 from date_varchar order by k1; """ + qt_sql_varchar1 """ select dt, unix_timestamp(dt, "%Y-%m-%d") as k1 from date_varchar order by k1; """ + qt_sql_varchar1 """ select fmt, unix_timestamp("1990-12-12", fmt) as k1 from date_varchar order by k1; """ }