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 bf92f67b460 [Fix](function) Fix unexpected result of unix_timestamp when input is out of bound (#49430) bf92f67b460 is described below commit bf92f67b46044bcab38a947f09eaf554434af143 Author: zclllyybb <zhaochan...@selectdb.com> AuthorDate: Mon Apr 28 10:31:49 2025 +0800 [Fix](function) Fix unexpected result of unix_timestamp when input is out of bound (#49430) ### What problem does this PR solve? Issue Number: close #xxx Related PR: #xxx Problem Summary: before: ```sql mysql> SELECT UNIX_TIMESTAMP('9999-12-30 23:59:59.999'); +-------------------------------------------+ | UNIX_TIMESTAMP('9999-12-30 23:59:59.999') | +-------------------------------------------+ | 0.999 | +-------------------------------------------+ ``` now: ```sql mysql> SELECT UNIX_TIMESTAMP('9999-12-30 23:59:59.999'); +-------------------------------------------+ | UNIX_TIMESTAMP('9999-12-30 23:59:59.999') | +-------------------------------------------+ | 0.000 | +-------------------------------------------+ ``` ### Release note Fix unexpected result of `unix_timestamp` when input is out of bound ### Check List (For Author) - Test <!-- At least one of them must be included. --> - [x] Regression test - [ ] Unit Test - [ ] Manual test (add detailed scripts or steps below) - [ ] No need to test or manual test. Explain why: - [ ] This is a refactor/code format and no logic has been changed. - [ ] Previous test can cover this change. - [ ] No code files have been changed. - [ ] Other reason <!-- Add your reason? --> - Behavior changed: - [x] No. - [ ] Yes. <!-- Explain the behavior change --> - Does this need documentation? - [x] No. - [ ] Yes. <!-- Add document PR link here. eg: https://github.com/apache/doris-website/pull/1214 --> ### Check List (For Reviewer who merge this PR) - [ ] Confirm the release note - [ ] Confirm test cases - [ ] Confirm document - [ ] Add branch pick label <!-- Add branch pick label that this PR should merge into --> --------- Co-authored-by: Pxl <x...@selectdb.com> --- be/src/vec/functions/function_timestamp.cpp | 3 +- be/test/vec/function/function_test_util.h | 6 ++- be/test/vec/function/function_time_test.cpp | 53 +++++++++++++++++++++ .../datetime_functions/test_date_function.out | Bin 10110 -> 10207 bytes .../datetime_functions/test_date_function.groovy | 4 ++ 5 files changed, 62 insertions(+), 4 deletions(-) diff --git a/be/src/vec/functions/function_timestamp.cpp b/be/src/vec/functions/function_timestamp.cpp index a2a5184553c..6b36697c1ec 100644 --- a/be/src/vec/functions/function_timestamp.cpp +++ b/be/src/vec/functions/function_timestamp.cpp @@ -666,8 +666,7 @@ struct UnixTimeStampDateImpl { ts_value.unix_timestamp(×tamp, context->state()->timezone_obj()); DCHECK(valid); - auto& [sec, ms] = timestamp; - sec = UnixTimeStampImpl::trim_timestamp(sec); + auto [sec, ms] = UnixTimeStampImpl::trim_timestamp(timestamp); auto ms_str = std::to_string(ms).substr(0, scale); if (ms_str.empty()) { ms_str = "0"; diff --git a/be/test/vec/function/function_test_util.h b/be/test/vec/function/function_test_util.h index 01661b458f8..dff4734a2c0 100644 --- a/be/test/vec/function/function_test_util.h +++ b/be/test/vec/function/function_test_util.h @@ -15,6 +15,8 @@ // specific language governing permissions and limitations // under the License. +#pragma once + #include <gtest/gtest-message.h> #include <gtest/gtest-test-part.h> #include <gtest/gtest.h> @@ -22,6 +24,7 @@ #include <cstdint> #include <ctime> +#include <memory> #include <string> #include <type_traits> #include <utility> @@ -40,6 +43,7 @@ #include "vec/core/wide_integer.h" #include "vec/data_types/data_type.h" #include "vec/data_types/data_type_bitmap.h" +#include "vec/data_types/data_type_decimal.h" #include "vec/data_types/data_type_hll.h" #include "vec/data_types/data_type_nullable.h" #include "vec/data_types/data_type_number.h" @@ -50,8 +54,6 @@ namespace doris::vectorized { class DataTypeJsonb; class TableFunction; -template <typename T> -class DataTypeDecimal; // for an input row with only one column, should use {AnyType(xxx)} to represent it because TestArray is same with // InputCell. just {} will be treated as copy-constructor rather than initializer list. diff --git a/be/test/vec/function/function_time_test.cpp b/be/test/vec/function/function_time_test.cpp index 22709d31baf..b49d0a9f7c4 100644 --- a/be/test/vec/function/function_time_test.cpp +++ b/be/test/vec/function/function_time_test.cpp @@ -24,6 +24,7 @@ #include "vec/core/types.h" #include "vec/data_types/data_type_date.h" #include "vec/data_types/data_type_date_time.h" +#include "vec/data_types/data_type_decimal.h" #include "vec/data_types/data_type_number.h" #include "vec/data_types/data_type_string.h" #include "vec/data_types/data_type_time.h" @@ -182,6 +183,58 @@ TEST(VTimestampFunctionsTest, from_unix_test) { static_cast<void>(check_function<DataTypeString, true>(func_name, input_types, data_set)); } +TEST(VTimestampFunctionsTest, unix_timestamp_test) { + std::string func_name = "unix_timestamp"; + TimezoneUtils::load_timezones_to_cache(); + + { + InputTypeSet input_types = {TypeIndex::DateV2}; + DataSet data_set = {{{std::string("2022-05-24")}, int32_t {1653321600}}, + {{Null()}, Null()}}; + static_cast<void>(check_function<DataTypeInt32, true>(func_name, input_types, data_set)); + } + + { + InputTypeSet input_types = {{TypeIndex::DateTimeV2, 3}}; + DataSet data_set = { + {{std::string("2022-05-24 12:34:56.789")}, DECIMAL64(1653366896, 789, 3)}, + {{Null()}, Null()}}; + static_cast<void>(check_function<DataTypeDecimal<Decimal64>, true, 3, 13>( + func_name, input_types, data_set)); + } + + { + InputTypeSet input_types = {{TypeIndex::DateTimeV2, 6}}; + DataSet data_set = { + {{std::string("2022-05-24 12:34:56.789123")}, DECIMAL64(1653366896, 789123, 6)}, + {{Null()}, Null()}}; + static_cast<void>(check_function<DataTypeDecimal<Decimal64>, true, 6, 16>( + func_name, input_types, data_set)); + } + + // test out of range + { + InputTypeSet input_types = {{TypeIndex::DateTimeV2, 6}}; + DataSet data_set = { + {{std::string("1022-05-24 12:34:56.789123")}, DECIMAL64(0, 0, 0)}, + {{std::string("9022-05-24 12:34:56.789123")}, DECIMAL64(0, 0, 0)}, + {{std::string("9999-12-30 23:59:59.999")}, DECIMAL64(0, 0, 0)}, + }; + static_cast<void>(check_function<DataTypeDecimal<Decimal64>, true, 0, 1>( + func_name, input_types, data_set)); + } + // negative case + { + InputTypeSet input_types = {{TypeIndex::DateTimeV2, 6}}; + DataSet data_set = { + {{std::string("9999-12-30 23:59:59.999")}, DECIMAL64(0, 999, 3)}, + {{std::string("9999-12-30 23:59:59.999")}, DECIMAL64(0, 999, 3)}, + }; + static_cast<void>(check_function<DataTypeDecimal<Decimal64>, true, 0, 1>( + func_name, input_types, data_set, false, true)); + } +} + TEST(VTimestampFunctionsTest, timediff_test) { std::string func_name = "timediff"; diff --git a/regression-test/data/nereids_p0/sql_functions/datetime_functions/test_date_function.out b/regression-test/data/nereids_p0/sql_functions/datetime_functions/test_date_function.out index fe9097969cb..3d36dbf995e 100644 Binary files a/regression-test/data/nereids_p0/sql_functions/datetime_functions/test_date_function.out and b/regression-test/data/nereids_p0/sql_functions/datetime_functions/test_date_function.out differ diff --git a/regression-test/suites/nereids_p0/sql_functions/datetime_functions/test_date_function.groovy b/regression-test/suites/nereids_p0/sql_functions/datetime_functions/test_date_function.groovy index b5d4a027adc..0c383d54a03 100644 --- a/regression-test/suites/nereids_p0/sql_functions/datetime_functions/test_date_function.groovy +++ b/regression-test/suites/nereids_p0/sql_functions/datetime_functions/test_date_function.groovy @@ -385,6 +385,10 @@ suite("test_date_function") { qt_sql_ustamp5 """ select unix_timestamp('2007-11-30 10:30:19.123456') """ qt_sql_ustamp6 """ select unix_timestamp(cast('2007-11-30 10:30:19.123456' as datetimev2(3))) """ qt_sql_ustamp7 """ select unix_timestamp(cast('2007-11-30 10:30:19.123456' as datetimev2(4))) """ + qt_sql_ustamp8 """ SELECT UNIX_TIMESTAMP('9999-12-30 23:59:59.999'); """ + qt_sql_ustamp9 """ SELECT UNIX_TIMESTAMP('9999-12-30 23:59:59'); """ + testFoldConst("SELECT UNIX_TIMESTAMP('9999-12-30 23:59:59.999');") + testFoldConst("SELECT UNIX_TIMESTAMP('9999-12-30 23:59:59');") // UTC_TIMESTAMP def utc_timestamp_str = sql """ select utc_timestamp(),utc_timestamp() + 1 """ --------------------------------------------------------------------- To unsubscribe, e-mail: commits-unsubscr...@doris.apache.org For additional commands, e-mail: commits-h...@doris.apache.org