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

Mryange 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 79735311847 [fix](function) Consider microseconds in week floor and 
ceil (#67961)
79735311847 is described below

commit 797353118470b72add478c5560a03dc270ef2cb9
Author: Mryange <[email protected]>
AuthorDate: Fri Sep 18 10:39:38 2026 +0800

    [fix](function) Consider microseconds in week floor and ceil (#67961)
    
    `week_floor` and `week_ceil` compared the within-week remainder only to
    second precision for `DATETIME(6)` values. With a microsecond-bearing
    origin, `week_floor` could return a boundary later than its input. The
    fix compares the complete within-week microsecond remainder. Regression
    coverage includes literal and column expressions before, at, and after
    the boundary.
    
    
    ### Release note
    
    None
    
    ### Check List (For Author)
    
    - Test <!-- At least one of them must be included. -->
        - [ ] 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:
        - [ ] No.
        - [ ] Yes. <!-- Explain the behavior change -->
    
    - Does this need documentation?
        - [ ] 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 -->
---
 .../function/function_datetime_floor_ceil.cpp      | 14 ++++--
 .../test_week_floor_ceil_microsecond.out           |  8 ++++
 .../test_week_floor_ceil_microsecond.groovy        | 55 ++++++++++++++++++++++
 3 files changed, 72 insertions(+), 5 deletions(-)

diff --git a/be/src/exprs/function/function_datetime_floor_ceil.cpp 
b/be/src/exprs/function/function_datetime_floor_ceil.cpp
index 7daf7c412e5..6bf7e6c6799 100644
--- a/be/src/exprs/function/function_datetime_floor_ceil.cpp
+++ b/be/src/exprs/function/function_datetime_floor_ceil.cpp
@@ -730,12 +730,16 @@ struct DateTimeFloorCeilCore {
                         calc_origin.to_date_int_val() & 
MASK_YEAR_MONTH_FOR_DATETIMEV2;
             }
             if constexpr (Flag::Unit == WEEK) {
+                constexpr int64_t MICROSECONDS_PER_SECOND = 1'000'000;
+                const auto microseconds_since_week_start = [](const auto& 
value) {
+                    return (value.daynr() % 7 * 24 * 3600 + value.hour() * 
3600 +
+                            value.minute() * 60 + value.second()) *
+                                   MICROSECONDS_PER_SECOND +
+                           static_cast<int64_t>(value.microsecond());
+                };
                 diff = calc_arg.daynr() / 7 - calc_origin.daynr() / 7;
-                trivial_part_ts_arg = calc_arg.daynr() % 7 * 24 * 3600 + 
calc_arg.hour() * 3600 +
-                                      calc_arg.minute() * 60 + 
calc_arg.second();
-                trivial_part_ts_res = calc_origin.daynr() % 7 * 24 * 3600 +
-                                      calc_origin.hour() * 3600 + 
calc_origin.minute() * 60 +
-                                      calc_origin.second();
+                trivial_part_ts_arg = microseconds_since_week_start(calc_arg);
+                trivial_part_ts_res = 
microseconds_since_week_start(calc_origin);
             }
             if constexpr (Flag::Unit == DAY) {
                 diff = calc_arg.daynr() - calc_origin.daynr();
diff --git 
a/regression-test/data/query_p0/sql_functions/datetime_functions/test_week_floor_ceil_microsecond.out
 
b/regression-test/data/query_p0/sql_functions/datetime_functions/test_week_floor_ceil_microsecond.out
new file mode 100644
index 00000000000..44563fd0536
--- /dev/null
+++ 
b/regression-test/data/query_p0/sql_functions/datetime_functions/test_week_floor_ceil_microsecond.out
@@ -0,0 +1,8 @@
+-- This file is automatically generated. You should know what you did if you 
want to edit this
+-- !literal_boundary --
+2024-01-01T00:00:00.500        2024-01-15T00:00:00.500
+
+-- !column_boundaries --
+1      2024-01-08T00:00:00.400 2024-01-01T00:00:00.500 2024-01-08T00:00:00.500
+2      2024-01-08T00:00:00.500 2024-01-08T00:00:00.500 2024-01-08T00:00:00.500
+3      2024-01-08T00:00:00.600 2024-01-08T00:00:00.500 2024-01-15T00:00:00.500
diff --git 
a/regression-test/suites/query_p0/sql_functions/datetime_functions/test_week_floor_ceil_microsecond.groovy
 
b/regression-test/suites/query_p0/sql_functions/datetime_functions/test_week_floor_ceil_microsecond.groovy
new file mode 100644
index 00000000000..94716e60657
--- /dev/null
+++ 
b/regression-test/suites/query_p0/sql_functions/datetime_functions/test_week_floor_ceil_microsecond.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_week_floor_ceil_microsecond") {
+    sql """DROP TABLE IF EXISTS test_week_floor_ceil_microsecond"""
+    sql """
+        CREATE TABLE test_week_floor_ceil_microsecond (
+            id INT,
+            input_value DATETIME(6),
+            origin_value DATETIME(6)
+        ) DUPLICATE KEY(id)
+        DISTRIBUTED BY HASH(id) BUCKETS 1
+        PROPERTIES("replication_num" = "1")
+    """
+
+    sql """
+        INSERT INTO test_week_floor_ceil_microsecond VALUES
+            (1, '2024-01-08 00:00:00.400000', '2024-01-01 00:00:00.500000'),
+            (2, '2024-01-08 00:00:00.500000', '2024-01-01 00:00:00.500000'),
+            (3, '2024-01-08 00:00:00.600000', '2024-01-01 00:00:00.500000')
+    """
+
+    qt_literal_boundary """
+        SELECT /*+ SET_VAR(debug_skip_fold_constant = true) */
+               week_floor(CAST('2024-01-08 00:00:00.400000' AS DATETIME(6)),
+                          1,
+                          CAST('2024-01-01 00:00:00.500000' AS DATETIME(6))),
+               week_ceil(CAST('2024-01-08 00:00:00.600000' AS DATETIME(6)),
+                         1,
+                         CAST('2024-01-01 00:00:00.500000' AS DATETIME(6)))
+    """
+
+    order_qt_column_boundaries """
+        SELECT id,
+               input_value,
+               week_floor(input_value, 1, origin_value),
+               week_ceil(input_value, 1, origin_value)
+        FROM test_week_floor_ceil_microsecond
+        ORDER BY id
+    """
+}


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

Reply via email to