diff --git a/be/src/vec/functions/function_timestamp.cpp b/be/src/vec/functions/function_timestamp.cpp index 8107f247cd12c7..a1aa02adb592d5 100644 --- a/be/src/vec/functions/function_timestamp.cpp +++ b/be/src/vec/functions/function_timestamp.cpp @@ -48,6 +48,7 @@ #include "vec/columns/column_string.h" #include "vec/columns/column_vector.h" #include "vec/common/assert_cast.h" +#include "vec/common/int_exp.h" #include "vec/common/pod_array_fwd.h" #include "vec/common/string_ref.h" #include "vec/core/block.h" @@ -672,11 +673,10 @@ struct UnixTimeStampDateImpl { DCHECK(valid); auto [sec, ms] = trim_timestamp(timestamp, NewVersion); - auto ms_str = std::to_string(ms).substr(0, scale); - if (ms_str.empty()) { - ms_str = "0"; - } - col_result_data[i] = Decimal64::from_int_frac(sec, std::stoll(ms_str), scale).value; + col_result_data[i] = + Decimal64::from_int_frac( + sec, ms / static_cast(std::pow(10, 6 - scale)), scale) + .value; } block.replace_by_position(result, std::move(col_result)); } diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/executable/DateTimeExtractAndTransform.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/executable/DateTimeExtractAndTransform.java index f1387fc810d2b4..2dbf7e0a5da1ec 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/executable/DateTimeExtractAndTransform.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/executable/DateTimeExtractAndTransform.java @@ -650,7 +650,7 @@ private static String getTimestamp(LocalDateTime dateTime) { if (duration.getNano() == 0) { return String.valueOf(duration.getSeconds()); } else { - return duration.getSeconds() + "." + (duration.getNano() / 1000); + return duration.getSeconds() + "." + String.format("%06d", duration.getNano() / 1000); } } 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 6036c185a0d1b3..45afa3602884ed 100644 --- 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 @@ -375,6 +375,12 @@ February -- !sql_ustamp9 -- 253402185599 +-- !sql_ustamp10 -- +1447381219.00001 + +-- !sql_ustamp11 -- +1447381219.012000 + -- !sql -- 0 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 93f8ff9a6b0272..bf5d5018d7a58a 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 @@ -387,8 +387,13 @@ suite("test_date_function") { 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'); """ - check_fold_consistency("UNIX_TIMESTAMP('9999-12-30 23:59:59.999')") - check_fold_consistency("UNIX_TIMESTAMP('9999-12-30 23:59:59')") + qt_sql_ustamp10 """ select UNIX_TIMESTAMP('2015-11-13 10:20:19.000010'); """ + qt_sql_ustamp11 """ select UNIX_TIMESTAMP(cast('2015-11-13 10:20:19.012' as datetime(6))); """ + check_fold_consistency(" UNIX_TIMESTAMP('9999-12-30 23:59:59.999')") + check_fold_consistency(" UNIX_TIMESTAMP('9999-12-30 23:59:59')") + check_fold_consistency(" UNIX_TIMESTAMP('2015-11-13 10:20:19.000010')") + check_fold_consistency(" UNIX_TIMESTAMP(cast('2015-11-13 10:20:19.000' as datetime(6)))") + check_fold_consistency(" UNIX_TIMESTAMP(cast('2015-11-13 10:20:19.012' as datetime(6)))") // these two functions may return different value if we call it in different time. so dont use testFoldConst here sql "set debug_skip_fold_constant=true;" sql "SELECT UNIX_TIMESTAMP(current_timestamp()) AS unix_timestamp;"