From 1d74a422e486fc4162dface801d2350d3106cb41 Mon Sep 17 00:00:00 2001 From: zhiqiang-hhhh Date: Wed, 21 Feb 2024 14:33:04 +0800 Subject: [PATCH 1/3] FINISH --- be/src/vec/runtime/vdatetime_value.cpp | 7 ++- .../datatype_p0/datetimev2/test_round.out | 14 +++++ .../datatype_p0/datetimev2/test_round.groovy | 55 +++++++++++++++++++ 3 files changed, 74 insertions(+), 2 deletions(-) create mode 100644 regression-test/data/datatype_p0/datetimev2/test_round.out create mode 100644 regression-test/suites/datatype_p0/datetimev2/test_round.groovy diff --git a/be/src/vec/runtime/vdatetime_value.cpp b/be/src/vec/runtime/vdatetime_value.cpp index 334779f93901b6..09419045c5e2c8 100644 --- a/be/src/vec/runtime/vdatetime_value.cpp +++ b/be/src/vec/runtime/vdatetime_value.cpp @@ -36,6 +36,7 @@ #include "common/compiler_util.h" #include "common/config.h" #include "common/exception.h" +#include "common/logging.h" #include "common/status.h" #include "util/timezone_utils.h" #include "vec/common/int_exp.h" @@ -1988,6 +1989,7 @@ bool DateV2Value::from_date_str(const char* date_str, int len, template bool DateV2Value::from_date_str_base(const char* date_str, int len, int scale, const cctz::time_zone* local_time_zone) { + LOG_INFO("whole str {}, len {}", std::string_view(date_str, len), len); const char* ptr = date_str; const char* end = date_str + len; // ONLY 2, 6 can follow by a space @@ -2038,11 +2040,12 @@ bool DateV2Value::from_date_str_base(const char* date_str, int len, int scale if (field_idx == 6) { // Microsecond const auto ms_part = ptr - start; + LOG_INFO("ms str {}", std::string_view(start, ptr)); temp_val *= int_exp10(std::max(0L, 6 - ms_part)); if constexpr (is_datetime) { if (scale >= 0) { - if (scale == 6 && ms_part > 6) { - if (ptr < end && isdigit(*ptr) && *ptr >= '5') { + if (scale == 6 && ms_part >= 6) { + if (ptr <= end && isdigit(*ptr) && *ptr >= '5') { temp_val += 1; } } else { diff --git a/regression-test/data/datatype_p0/datetimev2/test_round.out b/regression-test/data/datatype_p0/datetimev2/test_round.out new file mode 100644 index 00000000000000..3a75374bd5be2e --- /dev/null +++ b/regression-test/data/datatype_p0/datetimev2/test_round.out @@ -0,0 +1,14 @@ +-- This file is automatically generated. You should know what you did if you want to edit this +-- !cast -- +2024-02-06 03:37:07.12345655 2024-02-06T03:37:07.123457 + +-- !rount -- +0 + +-- !cast -- +1 2024-02-01 12:13:14.123456 2024-02-01T12:13:14.123456 +2 2024-02-01 12:13:14.1234567 2024-02-01T12:13:14.123457 +3 2024-02-01 12:13:14.12345671 2024-02-01T12:13:14.123457 +4 2024-02-01 12:13:14.1234561 2024-02-01T12:13:14.123456 +5 2024-02-01 12:13:14.12345615 2024-02-01T12:13:14.123456 + diff --git a/regression-test/suites/datatype_p0/datetimev2/test_round.groovy b/regression-test/suites/datatype_p0/datetimev2/test_round.groovy new file mode 100644 index 00000000000000..e8156a2efcd658 --- /dev/null +++ b/regression-test/suites/datatype_p0/datetimev2/test_round.groovy @@ -0,0 +1,55 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +suite("test_time_round") { + qt_cast """ + with tmp as ( + select CONCAT( + YEAR('2024-02-06 03:37:07.157'), '-', + LPAD(MONTH('2024-02-06 03:37:07.157'), 2, '0'), '-', + LPAD(DAY('2024-02-06 03:37:07.157'), 2, '0'), ' ', + LPAD(HOUR('2024-02-06 03:37:07.157'), 2, '0'), ':', + LPAD(MINUTE('2024-02-06 03:37:07.157'), 2, '0'), ':', + LPAD(SECOND('2024-02-06 03:37:07.157'), 2, '0'), '.', "123456789" ) + AS generated_string) + select generated_string, cast(generated_string as DateTime(6)) from tmp + """ + qt_rount """ + CREATE TABLE test_time_round (`rowid` int, str varchar) + ENGINE=OLAP + UNIQUE KEY(`rowid`) + COMMENT "OLAP" + DISTRIBUTED BY HASH(`rowid`) BUCKETS 3 + PROPERTIES ( + "replication_num" = "1", + "colocate_with" = "lineitem_orders", + "enable_unique_key_merge_on_write" = "true" + ); + """ + sql """ + insert into test_time_round values + (1, "2024-02-01 12:13:14.123456"), + (2, "2024-02-01 12:13:14.1234567"), + (3, "2024-02-01 12:13:14.12345671"), + (4, "2024-02-01 12:13:14.1234561"), + (5, "2024-02-01 12:13:14.12345615") + """ + + qt_cast """ + select *, cast (str as Datetime(6)) from test_time_round order by rowid; + """ +} \ No newline at end of file From c0783aed8678b1225c8296a9dc657b3557647b4b Mon Sep 17 00:00:00 2001 From: zhiqiang-hhhh Date: Wed, 21 Feb 2024 14:46:35 +0800 Subject: [PATCH 2/3] BETTER --- be/src/vec/runtime/vdatetime_value.cpp | 2 -- regression-test/data/datatype_p0/datetimev2/test_round.out | 5 +---- .../suites/datatype_p0/datetimev2/test_round.groovy | 5 ++++- 3 files changed, 5 insertions(+), 7 deletions(-) diff --git a/be/src/vec/runtime/vdatetime_value.cpp b/be/src/vec/runtime/vdatetime_value.cpp index 09419045c5e2c8..2e6494ed74d3c8 100644 --- a/be/src/vec/runtime/vdatetime_value.cpp +++ b/be/src/vec/runtime/vdatetime_value.cpp @@ -36,7 +36,6 @@ #include "common/compiler_util.h" #include "common/config.h" #include "common/exception.h" -#include "common/logging.h" #include "common/status.h" #include "util/timezone_utils.h" #include "vec/common/int_exp.h" @@ -2040,7 +2039,6 @@ bool DateV2Value::from_date_str_base(const char* date_str, int len, int scale if (field_idx == 6) { // Microsecond const auto ms_part = ptr - start; - LOG_INFO("ms str {}", std::string_view(start, ptr)); temp_val *= int_exp10(std::max(0L, 6 - ms_part)); if constexpr (is_datetime) { if (scale >= 0) { diff --git a/regression-test/data/datatype_p0/datetimev2/test_round.out b/regression-test/data/datatype_p0/datetimev2/test_round.out index 3a75374bd5be2e..89ca82457c607d 100644 --- a/regression-test/data/datatype_p0/datetimev2/test_round.out +++ b/regression-test/data/datatype_p0/datetimev2/test_round.out @@ -1,9 +1,6 @@ -- This file is automatically generated. You should know what you did if you want to edit this -- !cast -- -2024-02-06 03:37:07.12345655 2024-02-06T03:37:07.123457 - --- !rount -- -0 +2024-02-06 03:37:07.123456789 2024-02-06T03:37:07.123457 -- !cast -- 1 2024-02-01 12:13:14.123456 2024-02-01T12:13:14.123456 diff --git a/regression-test/suites/datatype_p0/datetimev2/test_round.groovy b/regression-test/suites/datatype_p0/datetimev2/test_round.groovy index e8156a2efcd658..e6363bb0d690d4 100644 --- a/regression-test/suites/datatype_p0/datetimev2/test_round.groovy +++ b/regression-test/suites/datatype_p0/datetimev2/test_round.groovy @@ -28,7 +28,10 @@ suite("test_time_round") { AS generated_string) select generated_string, cast(generated_string as DateTime(6)) from tmp """ - qt_rount """ + sql """ + DROP TABLE IF EXISTS test_time_round; + """ + sql """ CREATE TABLE test_time_round (`rowid` int, str varchar) ENGINE=OLAP UNIQUE KEY(`rowid`) From afd0540df96301fccc570198fbd103bbdea249f7 Mon Sep 17 00:00:00 2001 From: zhiqiang-hhhh Date: Wed, 21 Feb 2024 14:47:42 +0800 Subject: [PATCH 3/3] FIX --- be/src/vec/runtime/vdatetime_value.cpp | 1 - .../suites/datatype_p0/datetimev2/test_round.groovy | 4 ++-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/be/src/vec/runtime/vdatetime_value.cpp b/be/src/vec/runtime/vdatetime_value.cpp index 2e6494ed74d3c8..3f0c9ff9de0847 100644 --- a/be/src/vec/runtime/vdatetime_value.cpp +++ b/be/src/vec/runtime/vdatetime_value.cpp @@ -1988,7 +1988,6 @@ bool DateV2Value::from_date_str(const char* date_str, int len, template bool DateV2Value::from_date_str_base(const char* date_str, int len, int scale, const cctz::time_zone* local_time_zone) { - LOG_INFO("whole str {}, len {}", std::string_view(date_str, len), len); const char* ptr = date_str; const char* end = date_str + len; // ONLY 2, 6 can follow by a space diff --git a/regression-test/suites/datatype_p0/datetimev2/test_round.groovy b/regression-test/suites/datatype_p0/datetimev2/test_round.groovy index e6363bb0d690d4..01aee4d7a06b1e 100644 --- a/regression-test/suites/datatype_p0/datetimev2/test_round.groovy +++ b/regression-test/suites/datatype_p0/datetimev2/test_round.groovy @@ -19,12 +19,12 @@ suite("test_time_round") { qt_cast """ with tmp as ( select CONCAT( - YEAR('2024-02-06 03:37:07.157'), '-', + YEAR('2024-02-06 03:37:07.157'), '-', LPAD(MONTH('2024-02-06 03:37:07.157'), 2, '0'), '-', LPAD(DAY('2024-02-06 03:37:07.157'), 2, '0'), ' ', LPAD(HOUR('2024-02-06 03:37:07.157'), 2, '0'), ':', LPAD(MINUTE('2024-02-06 03:37:07.157'), 2, '0'), ':', - LPAD(SECOND('2024-02-06 03:37:07.157'), 2, '0'), '.', "123456789" ) + LPAD(SECOND('2024-02-06 03:37:07.157'), 2, '0'), '.', "123456789") AS generated_string) select generated_string, cast(generated_string as DateTime(6)) from tmp """