zhiqiang-hhhh commented on code in PR #35681:
URL: https://github.com/apache/doris/pull/35681#discussion_r1629323142


##########
be/src/vec/functions/function_from_unixtime.cpp:
##########
@@ -0,0 +1,301 @@
+// 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 <cctz/time_zone.h>
+
+#include <type_traits>
+
+#include "common/exception.h"
+#include "common/status.h"
+#include "olap/olap_common.h"
+#include "runtime/runtime_state.h"
+#include "vec/columns/column_nullable.h"
+#include "vec/columns/columns_number.h"
+#include "vec/common/assert_cast.h"
+#include "vec/common/int_exp.h"
+#include "vec/common/string_ref.h"
+#include "vec/core/column_numbers.h"
+#include "vec/core/types.h"
+#include "vec/data_types/data_type.h"
+#include "vec/data_types/data_type_decimal.h"
+#include "vec/data_types/data_type_number.h"
+#include "vec/functions/function.h"
+#include "vec/functions/simple_function_factory.h"
+#include "vec/runtime/vdatetime_value.h"
+
+namespace doris::vectorized {
+
+enum class ArgumentNumber { One, Two };
+
+template <typename T, ArgumentNumber argument_num>
+struct FromDecimal {
+    static void apply(const ColumnDecimal<T>* col_dec, Int64 scale, StringRef 
formatter,
+                      const cctz::time_zone& time_zone, 
ColumnString::MutablePtr& col_res,
+                      ColumnUInt8::MutablePtr& col_null_map) {
+        const size_t input_rows_count = col_dec->size();
+        for (size_t i = 0; i < input_rows_count; ++i) {
+            // For from_unixtime(x.10) we will get 10
+            Int64 fraction = col_dec->get_fractional_part(i);
+            if (fraction < 0) {
+                col_res->insert_default();
+                col_null_map->insert_value(1);
+                continue;
+            }
+
+            DateV2Value<DateTimeV2ValueType> datetime(0);
+
+            if (scale > 9) {
+                fraction /= common::exp10_i32(scale - 9);
+            } else {
+                fraction *= common::exp10_i32(9 - scale);
+            }
+            datetime.from_unixtime(col_dec->get_whole_part(i), fraction, 
time_zone,
+                                   scale > 6 ? 6 : scale);
+
+            char buf[128];
+
+            if constexpr (argument_num == ArgumentNumber::One) {
+                if (datetime.to_string(buf, scale) != buf) {
+                    col_res->insert_data(buf, strlen(buf));
+                    col_null_map->insert_value(0);
+                } else {
+                    col_res->insert_default();
+                    col_null_map->insert_value(1);
+                }
+            } else {
+                if (datetime.to_format_string(formatter.data, formatter.size, 
buf)) {
+                    col_res->insert_data(buf, strlen(buf));
+                    col_null_map->insert_value(0);
+                } else {
+                    col_res->insert_default();
+                    col_null_map->insert_value(1);
+                }
+            }
+        }
+    }
+};
+
+struct FromInt64 {
+    // 
https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_from-unixtime
+    // Keep consistent with MySQL
+    static const int64_t TIMESTAMP_VALID_MAX = 32536771199;
+    static void apply(const ColumnInt64* col_int64, Int64 scale, StringRef 
formatter,
+                      const cctz::time_zone& time_zone, 
ColumnString::MutablePtr& col_res,
+                      ColumnUInt8::MutablePtr& col_null_map) {
+        const size_t input_rows_count = col_int64->size();
+        for (size_t i = 0; i < input_rows_count; ++i) {
+            const auto& data = col_int64->get_data()[i];
+            if (formatter.size > 128 || data < 0 || data > 
TIMESTAMP_VALID_MAX) {
+                col_res->insert_default();
+                col_null_map->insert_value(1);
+                continue;
+            }
+
+            DateV2Value<DateTimeV2ValueType> datetime;
+            datetime.from_unixtime(static_cast<Int64>(data), 0, time_zone,
+                                   static_cast<Int32>(scale));
+
+            char buf[128];
+            if (datetime.to_format_string(formatter.data, formatter.size, 
buf)) {
+                col_res->insert_data(buf, strlen(buf));
+                col_null_map->insert_value(0);
+            } else {
+                col_res->insert_default();
+                col_null_map->insert_value(1);
+            }
+        }
+    }
+};
+
+template <typename from_type, ArgumentNumber argument_num>
+class FunctionFromUnixTime : public IFunction {
+    static_assert(std::is_same_v<from_type, Int64> || 
std::is_same_v<from_type, Float64> ||
+                          IsDecimalNumber<from_type>,
+                  "from_unixtime only support using Int64, Decimal or Float64 
as first arugment.");
+
+public:
+    static constexpr auto name = "from_unixtime";
+    String get_name() const override { return name; }
+
+    static FunctionPtr create() { return 
std::make_shared<FunctionFromUnixTime>(); }
+
+    size_t get_number_of_arguments() const override {
+        if constexpr (argument_num == ArgumentNumber::Two) {
+            return 2;
+        } else {
+            return 1;
+        }
+    }
+
+    ColumnNumbers get_arguments_that_are_always_constant() const override {
+        if constexpr (argument_num == ArgumentNumber::Two) {
+            return {1};
+        } else {
+            return {};
+        }
+    }
+
+    DataTypes get_variadic_argument_types_impl() const override {
+        if constexpr (IsDecimalNumber<from_type>) {
+            if constexpr (argument_num == ArgumentNumber::Two) {
+                return 
{std::make_shared<vectorized::DataTypeDecimal<Decimal32>>(9, 0),

Review Comment:
   its not matter, pass 0 because of the constructor



-- 
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