github-actions[bot] commented on code in PR #66761:
URL: https://github.com/apache/doris/pull/66761#discussion_r3828715711


##########
be/src/exprs/function/cast/variant_v2/cast_variant_to_scalar.cpp:
##########
@@ -203,14 +207,38 @@ void append_timestamp(ScalarGroups& groups, size_t row, 
int64_t micros, bool utc
     group.source_rows.push_back(row);
 }
 
+void append_timestamp_nanos(FunctionContext* context, ScalarGroups& groups, 
size_t row,
+                            int64_t nanos, bool utc_adjusted) {
+    if (utc_adjusted) {
+        DORIS_CHECK(context != nullptr);
+        DORIS_CHECK(context->state() != nullptr);
+        const auto seconds = TimeStampNsValue(nanos).epoch_seconds();
+        const auto instant = 
cctz::time_point<cctz::seconds>(cctz::seconds(seconds));
+        const auto offset = 
context->state()->timezone_obj().lookup(instant).offset;
+        const auto local_nanos = static_cast<__int128>(nanos) +

Review Comment:
   [P2] Preserve timezone-adjusted nanos when casting Variant to string
   
   This helper is also used when the target is `TYPE_STRING`. For an adjusted 
`TIMESTAMP_NANOS` at `INT64_MAX` in `Asia/Shanghai`, the correct civil text is 
`2262-04-12 07:47:16.854775807`, but adding `+08:00` makes `local_nanos` exceed 
Int64 and this path returns NULL. That signed range is required when 
materializing TIMESTAMP_NS, not when producing a string. Please keep the range 
check target-specific or format through a wider civil representation, and cover 
both endpoint/timezone directions.



##########
be/src/core/data_type_serde/data_type_timestamp_ns_serde.cpp:
##########
@@ -0,0 +1,270 @@
+// 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.
+
+#include "core/data_type_serde/data_type_timestamp_ns_serde.h"
+
+#include <cctz/time_zone.h>
+
+#include <algorithm>
+#include <cctype>
+#include <limits>
+#include <string>
+
+#include "common/exception.h"
+#include "core/assert_cast.h"
+#include "core/column/column_nullable.h"
+#include "core/column/column_string.h"
+#include "core/value/vdatetime_value.h"
+#include "exprs/function/cast/cast_to_datetimev2_impl.hpp"
+#include "util/mysql_row_buffer.h"
+#include "util/unaligned.h"
+
+namespace doris {
+Status parse_timestamp_ns(StringRef str, int64_t* epoch_nanos,
+                          const cctz::time_zone* local_time_zone) {
+    std::string input(str.data, str.size);
+    const size_t dot = input.rfind('.');
+    size_t fraction_begin = std::string::npos;
+    size_t fraction_end = std::string::npos;
+    if (dot != std::string::npos && dot + 1 < input.size() &&
+        std::isdigit(static_cast<unsigned char>(input[dot + 1]))) {
+        fraction_begin = dot + 1;
+        fraction_end = fraction_begin;
+        while (fraction_end < input.size() &&
+               std::isdigit(static_cast<unsigned char>(input[fraction_end]))) {
+            ++fraction_end;
+        }
+    }
+
+    uint32_t nanos = 0;
+    size_t fraction_length = 0;
+    if (fraction_begin != std::string::npos) {
+        fraction_length = fraction_end - fraction_begin;
+        const size_t copied_digits =
+                std::min<size_t>(fraction_length, 
TimeStampNsValue::FRACTIONAL_DIGITS);
+        for (size_t i = 0; i < copied_digits; ++i) {
+            nanos = nanos * 10 + static_cast<uint32_t>(input[fraction_begin + 
i] - '0');
+        }
+        for (size_t i = copied_digits; i < 
TimeStampNsValue::FRACTIONAL_DIGITS; ++i) {
+            nanos *= 10;
+        }
+    }
+
+    if (fraction_length > TimeStampNsValue::FRACTIONAL_DIGITS &&
+        input[fraction_begin + TimeStampNsValue::FRACTIONAL_DIGITS] >= '5') {
+        ++nanos;
+    }
+
+    // Keep the fractional token in place so that the legacy parser validates 
its position and all
+    // trailing syntax. Zeroing the digits prevents its scale-0 rounding from 
changing the second;
+    // the nanosecond rounding above remains the only source of fractional 
rounding.
+    if (fraction_begin != std::string::npos) {
+        std::fill(input.begin() + fraction_begin, input.begin() + 
fraction_end, '0');
+    }
+    const StringRef input_ref(input.data(), input.size());
+    DateV2Value<DateTimeV2ValueType> datetime;
+    CastParameters params {.status = Status::OK(), .is_strict = true};
+    CastToDatetimeV2::from_string_strict_mode<DatelikeParseMode::STRICT>(
+            input_ref, datetime, local_time_zone, 0, params);
+    if (!params.status.ok()) {
+        if (dot != std::string::npos) {
+            return Status::InvalidArgument("Invalid TIMESTAMP_NS value '{}'",
+                                           std::string(str.data, str.size));
+        }
+        return params.status;
+    }
+
+    if (nanos == TimeStampNsValue::NANOS_PER_SECOND) {

Review Comment:
   [P2] Round zoned input before converting to the session clock
   
   At a timezone transition this carries one civil second after the 
explicit-zone value has already been converted. With session zone 
`America/New_York`, `2024-03-10T06:59:59.9999999995Z` is first converted to 
`01:59:59 EST`, then this branch produces nonexistent `02:00:00`; rounding the 
instant first yields `07:00:00Z`, or `03:00:00 EDT`. Both FE literal parsers 
use the same ordering, so folding and runtime loads agree on the wrong value. 
Please round on the source timeline before zone conversion and add spring/fall 
transition cases with a tenth guard digit.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


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

Reply via email to