This is an automated email from the ASF dual-hosted git repository.

yiguolei pushed a commit to branch branch-2.1
in repository https://gitbox.apache.org/repos/asf/doris.git

commit b96148c9cdeb7f98d402ab2019c9a5591a91b833
Author: zclllyybb <[email protected]>
AuthorDate: Tue May 21 16:36:45 2024 +0800

    [Fix](function) fix days/weeks_diff result wrong on BE #35104
    
    select days_diff('2024-01-01 00:00:00', '2023-12-31 23:59:59');
    should be 0 but got 1 on BE.
---
 be/src/vec/runtime/vdatetime_value.h               | 13 +++++++++
 .../data/correctness_p0/test_date_diff.out         | 21 ++++++++++++++
 .../suites/correctness_p0/test_date_diff.groovy    | 32 ++++++++++++++++++++++
 3 files changed, 66 insertions(+)

diff --git a/be/src/vec/runtime/vdatetime_value.h 
b/be/src/vec/runtime/vdatetime_value.h
index 2031c782267..b1197b611ed 100644
--- a/be/src/vec/runtime/vdatetime_value.h
+++ b/be/src/vec/runtime/vdatetime_value.h
@@ -24,6 +24,7 @@
 
 #include <algorithm>
 #include <cstddef>
+#include <cstdint>
 #include <iostream>
 #include <iterator>
 #include <shared_mutex>
@@ -1405,10 +1406,22 @@ int64_t datetime_diff(const DateV2Value<T0>& ts_value1, 
const DateV2Value<T1>& t
     }
     case WEEK: {
         int day = ts_value2.daynr() - ts_value1.daynr();
+        int64_t ms_diff = ts_value2.time_part_diff_microsecond(ts_value1);
+        if (day > 0 && ms_diff < 0) {
+            day--;
+        } else if (day < 0 && ms_diff > 0) {
+            day++;
+        }
         return day / 7;
     }
     case DAY: {
         int day = ts_value2.daynr() - ts_value1.daynr();
+        int64_t ms_diff = ts_value2.time_part_diff_microsecond(ts_value1);
+        if (day > 0 && ms_diff < 0) {
+            day--;
+        } else if (day < 0 && ms_diff > 0) {
+            day++;
+        }
         return day;
     }
     case HOUR: {
diff --git a/regression-test/data/correctness_p0/test_date_diff.out 
b/regression-test/data/correctness_p0/test_date_diff.out
new file mode 100644
index 00000000000..4f2470fe4ee
--- /dev/null
+++ b/regression-test/data/correctness_p0/test_date_diff.out
@@ -0,0 +1,21 @@
+-- This file is automatically generated. You should know what you did if you 
want to edit this
+-- !days --
+0
+0
+1
+2
+3
+4
+5
+6
+
+-- !weeks --
+0
+0
+0
+0
+0
+0
+0
+0
+
diff --git a/regression-test/suites/correctness_p0/test_date_diff.groovy 
b/regression-test/suites/correctness_p0/test_date_diff.groovy
new file mode 100644
index 00000000000..91e9cef6eb3
--- /dev/null
+++ b/regression-test/suites/correctness_p0/test_date_diff.groovy
@@ -0,0 +1,32 @@
+// 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_date_diff") {
+    sql """
+        create table dt6(
+            k0 datetime(6) null
+        )
+        DISTRIBUTED BY HASH(`k0`) BUCKETS auto
+        properties("replication_num" = "1");
+    """
+    sql """
+    insert into dt6 values ("0000-01-01 12:00:00"), ("0000-01-02 12:00:00"), 
("0000-01-03 12:00:00"), ("0000-01-04 12:00:00"),
+        ("0000-01-05 12:00:00"), ("0000-01-06 12:00:00"), ("0000-01-07 
12:00:00"), ("0000-01-08 12:00:00");
+    """
+    qt_days """ select days_diff(k0, '0000-01-01 13:00:00') from dt6 order by 
k0; """
+    qt_weeks """ select weeks_diff(k0, '0000-01-01 13:00:00') from dt6 order 
by k0; """
+}
\ No newline at end of file


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to