Skip to content

Commit

Permalink
[fix](datetime) fix datetime round on BE (apache#31205) (apache#31229)
Browse files Browse the repository at this point in the history
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
before (incorrect round)

+-------------------------------+-----------------------------------------+
| generated_string              | cast(generated_string as DATETIMEV2(6)) |
+-------------------------------+-----------------------------------------+
| 2024-02-06 03:37:07.123456789 | 2024-02-06 03:37:07.123456              |
+-------------------------------+-----------------------------------------+
after (round up, keep consistent with mysql):

+-------------------------------+-----------------------------------------+
| generated_string              | cast(generated_string as DATETIMEV2(6)) |
+-------------------------------+-----------------------------------------+
| 2024-02-06 03:37:07.123456789 | 2024-02-06 03:37:07.123457              |
+-------------------------------+-----------------------------------------+
1 row in set (0.03 sec)
same work with apache#30744 but implemented on BE
  • Loading branch information
zhiqiang-hhhh authored Feb 21, 2024
1 parent b1719cc commit 37239d8
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 2 deletions.
4 changes: 2 additions & 2 deletions be/src/vec/runtime/vdatetime_value.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2037,8 +2037,8 @@ bool DateV2Value<T>::from_date_str_base(const char* date_str, int len, int scale
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 {
Expand Down
11 changes: 11 additions & 0 deletions regression-test/data/datatype_p0/datetimev2/test_round.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
-- This file is automatically generated. You should know what you did if you want to edit this
-- !cast --
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
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

58 changes: 58 additions & 0 deletions regression-test/suites/datatype_p0/datetimev2/test_round.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// 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
"""
sql """
DROP TABLE IF EXISTS test_time_round;
"""
sql """
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;
"""
}

0 comments on commit 37239d8

Please sign in to comment.