zclllyybb commented on code in PR #53938:
URL: https://github.com/apache/doris/pull/53938#discussion_r2234825897


##########
be/src/vec/functions/cast/cast_to_date_or_datetime_impl.tpp:
##########
@@ -0,0 +1,770 @@
+// 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.
+
+#pragma once
+
+#include <sys/types.h>
+
+#include <type_traits>
+
+#include "cast_base.h"
+#include "common/status.h"
+#include "runtime/primitive_type.h"
+#include "vec/core/types.h"
+#include "vec/data_types/data_type_decimal.h" // IWYU pragma: keep
+#include "vec/data_types/serde/data_type_serde.h"
+#include "vec/data_types/serde/datelike_serde_common.hpp"
+#include "vec/runtime/vdatetime_value.h"
+
+namespace doris::vectorized {
+#include "common/compile_check_begin.h"
+// NOLINTBEGIN(readability-function-size)
+// NOLINTBEGIN(readability-function-cognitive-complexity)
+
+template <bool IsDatetime>
+inline static void cast_to_type(VecDateTimeValue& res) {
+    if constexpr (IsDatetime) {
+        res.to_datetime();
+    } else {
+        res.cast_to_date();
+    }
+}
+
+template <bool IsStrict>
+[[nodiscard]] inline static bool microsecond_carry_on(int64_t frac_part, 
uint32_t float_scale,
+                                                      VecDateTimeValue& val,
+                                                      CastParameters& params) {
+    if (val.type() == TimeType::TIME_DATE) {
+        // for date, we just ignore the fractional part
+        return true;
+    }
+    // normalize the fractional part to microseconds(6 digits)
+    if (float_scale > 0) {
+        if (float_scale > 6) {
+            int ms = int(frac_part / common::exp10_i64(float_scale - 6));
+            // if scale > 6, we need to round the fractional part
+            int digit7 = frac_part % common::exp10_i32(float_scale - 6) /
+                         common::exp10_i32(float_scale - 7);
+            if (digit7 >= 5) {
+                ms++;
+                DCHECK(ms <= 1000000);
+                if (ms == 1000000) {
+                    // overflow, round up to next second
+                    
SET_PARAMS_RET_FALSE_IFN(val.date_add_interval<TimeUnit::SECOND>(
+                                                     TimeInterval 
{TimeUnit::SECOND, 1, false}),
+                                             "datetime overflow when rounding 
up to next second");
+                    ms = 0;
+                }
+            }
+        }
+    }
+    return true;
+}
+
+/**
+ * For the functions:
+ * function name `strict_mode` or `non_strict_mode`: follow the RULES of which 
mode
+ * template parameter `IsStrict`: whether it is RUNNING IN strict mode or not.
+ * return value: whether the cast is successful or not.
+ * `params.status`: set error code ONLY IN STRICT MODE.
+ */
+// for datev1 or datetimev1
+struct CastToDateOrDatetime {
+    // same behaviour in both strict and non-strict mode
+    template <bool IsStrict, bool IsDatetime, typename T>
+    static inline bool from_integer(T int_val, VecDateTimeValue& val, 
CastParameters& params);
+
+    template <bool IsStrict, bool IsDatetime, typename T>
+        requires std::is_floating_point_v<T>
+    static inline bool from_float(T float_value, VecDateTimeValue& val, 
CastParameters& params) {
+        SET_PARAMS_RET_FALSE_IFN(float_value > 0 && !std::isnan(float_value) &&
+                                         !std::isinf(float_value) &&
+                                         float_value < 
(double)std::numeric_limits<int64_t>::max(),
+                                 "invalid float value for date/datetime: {}", 
float_value);
+
+        auto int_part = static_cast<int64_t>(float_value);
+        if (!from_integer<IsStrict, IsDatetime>(int_part, val, params)) {
+            // if IsStrict, error code has been set in from_integer
+            return false;
+        }
+
+        int ms_part_7 = (float_value - (double)int_part) * 
common::exp10_i32(7);
+        if (!microsecond_carry_on<IsStrict>(ms_part_7, 7, val, params)) 
[[unlikely]] {
+            return false; // status set in microsecond_carry_on
+        }
+        return true;
+    }
+
+    template <bool IsStrict, bool IsDatetime, typename T>
+    static inline bool from_decimal(const T& int_part, const T& frac_part,
+                                    const int64_t& decimal_scale, 
VecDateTimeValue& res,
+                                    CastParameters& params) {
+        SET_PARAMS_RET_FALSE_IFN(int_part <= 
std::numeric_limits<int64_t>::max() && int_part >= 1,
+                                 "invalid decimal value for date/datetime: 
{}.{}", int_part,
+                                 frac_part);
+
+        if (!from_integer<IsStrict, IsDatetime>(int_part, res, params)) {
+            // if IsStrict, error code has been set in from_integer
+            return false;
+        }
+
+        if (!microsecond_carry_on<IsStrict>((int64_t)frac_part, 
(uint32_t)decimal_scale, res,
+                                            params)) [[unlikely]] {
+            return false; // status set in microsecond_carry_on
+        }
+        return true;
+    }
+
+    // this code follow rules of strict mode, but whether it RUNNING IN strict 
mode or not depends on the `IsStrict`
+    // parameter. if it's false, we dont set error code for performance and we 
dont need.
+    template <bool IsStrict, bool IsDatetime>
+    static inline bool from_string_strict_mode(const StringRef& str, 
VecDateTimeValue& res,

Review Comment:
   对啊,就是根据CastParameters派发的



-- 
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: commits-unsubscr...@doris.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscr...@doris.apache.org
For additional commands, e-mail: commits-h...@doris.apache.org

Reply via email to