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


##########
be/src/vec/functions/date_time_transforms.h:
##########
@@ -427,6 +429,86 @@ struct FromUnixTimeDecimalImpl {
     }
 };
 
+template <PrimitiveType ArgPType>
+class FunctionTimeFormat : public IFunction {
+public:
+    using ArgColType = typename PrimitiveTypeTraits<ArgPType>::ColumnType;
+    using ArgCppType = typename PrimitiveTypeTraits<ArgPType>::CppType;
+
+    static constexpr auto name = "time_format";
+    String get_name() const override { return name; }
+    static FunctionPtr create() { return 
std::make_shared<FunctionTimeFormat>(); }
+    DataTypes get_variadic_argument_types_impl() const override {
+        return {std::make_shared<typename 
PrimitiveTypeTraits<ArgPType>::DataType>(),
+                std::make_shared<vectorized::DataTypeString>()};
+    }
+    DataTypePtr get_return_type_impl(const ColumnsWithTypeAndName& arguments) 
const override {
+        return make_nullable(std::make_shared<DataTypeString>());
+    }
+    size_t get_number_of_arguments() const override { return 2; }
+
+    Status execute_impl(FunctionContext* context, Block& block, const 
ColumnNumbers& arguments,
+                        uint32_t result, size_t input_rows_count) const 
override {
+        auto res_col = ColumnString::create();
+        ColumnString::Chars& res_chars = res_col->get_chars();
+        ColumnString::Offsets& res_offsets = res_col->get_offsets();
+
+        auto null_map = ColumnUInt8::create();
+        auto& null_map_data = null_map->get_data();
+        null_map_data.resize_fill(input_rows_count, 0);
+
+        res_offsets.reserve(input_rows_count);
+
+        ColumnPtr arg_col[2];
+        bool is_const[2];
+        for (size_t i = 0; i < 2; ++i) {
+            const ColumnPtr& col = block.get_by_position(arguments[i]).column;
+            std::tie(arg_col[i], is_const[i]) = unpack_if_const(col);
+            const NullMap* arg_null_map = VectorizedUtils::get_null_map(col);
+            if (arg_null_map) {

Review Comment:
   这个null不是默认处理了么?



##########
be/src/vec/functions/date_time_transforms.h:
##########
@@ -427,6 +429,86 @@ struct FromUnixTimeDecimalImpl {
     }
 };
 
+template <PrimitiveType ArgPType>
+class FunctionTimeFormat : public IFunction {
+public:
+    using ArgColType = typename PrimitiveTypeTraits<ArgPType>::ColumnType;
+    using ArgCppType = typename PrimitiveTypeTraits<ArgPType>::CppType;
+
+    static constexpr auto name = "time_format";
+    String get_name() const override { return name; }
+    static FunctionPtr create() { return 
std::make_shared<FunctionTimeFormat>(); }
+    DataTypes get_variadic_argument_types_impl() const override {
+        return {std::make_shared<typename 
PrimitiveTypeTraits<ArgPType>::DataType>(),
+                std::make_shared<vectorized::DataTypeString>()};
+    }
+    DataTypePtr get_return_type_impl(const ColumnsWithTypeAndName& arguments) 
const override {
+        return make_nullable(std::make_shared<DataTypeString>());
+    }
+    size_t get_number_of_arguments() const override { return 2; }
+
+    Status execute_impl(FunctionContext* context, Block& block, const 
ColumnNumbers& arguments,
+                        uint32_t result, size_t input_rows_count) const 
override {
+        auto res_col = ColumnString::create();
+        ColumnString::Chars& res_chars = res_col->get_chars();
+        ColumnString::Offsets& res_offsets = res_col->get_offsets();
+
+        auto null_map = ColumnUInt8::create();
+        auto& null_map_data = null_map->get_data();
+        null_map_data.resize_fill(input_rows_count, 0);
+
+        res_offsets.reserve(input_rows_count);
+
+        ColumnPtr arg_col[2];
+        bool is_const[2];
+        for (size_t i = 0; i < 2; ++i) {
+            const ColumnPtr& col = block.get_by_position(arguments[i]).column;
+            std::tie(arg_col[i], is_const[i]) = unpack_if_const(col);
+            const NullMap* arg_null_map = VectorizedUtils::get_null_map(col);
+            if (arg_null_map) {
+                VectorizedUtils::update_null_map(null_map_data, *arg_null_map, 
is_const[i]);
+            }
+            arg_col[i] = remove_nullable(arg_col[i]);
+        }
+
+        const auto* datetime_col = assert_cast<const 
ArgColType*>(arg_col[0].get());
+        const auto* format_col = assert_cast<const 
ColumnString*>(arg_col[1].get());
+        for (size_t i = 0; i < input_rows_count; ++i) {
+            if (null_map_data[i]) {

Review Comment:
   这里是不是要不走默认null处理逻辑才能触发这个?



##########
fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/scalar/TimeFormat.java:
##########
@@ -0,0 +1,109 @@
+// 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.
+
+package org.apache.doris.nereids.trees.expressions.functions.scalar;
+
+import org.apache.doris.catalog.FunctionSignature;
+import org.apache.doris.nereids.trees.expressions.Expression;
+import org.apache.doris.nereids.trees.expressions.functions.AlwaysNullable;
+import 
org.apache.doris.nereids.trees.expressions.functions.ExplicitlyCastableSignature;
+import org.apache.doris.nereids.trees.expressions.functions.Monotonic;
+import org.apache.doris.nereids.trees.expressions.literal.Literal;
+import org.apache.doris.nereids.trees.expressions.literal.StringLikeLiteral;
+import org.apache.doris.nereids.trees.expressions.shape.BinaryExpression;
+import org.apache.doris.nereids.trees.expressions.visitor.ExpressionVisitor;
+import org.apache.doris.nereids.types.DateTimeV2Type;
+import org.apache.doris.nereids.types.DateV2Type;
+import org.apache.doris.nereids.types.TimeV2Type;
+import org.apache.doris.nereids.types.VarcharType;
+import org.apache.doris.nereids.util.DateUtils;
+
+import com.google.common.base.Preconditions;
+import com.google.common.collect.ImmutableList;
+
+import java.util.List;
+
+/**
+ * ScalarFunction 'time_format'.
+ */
+public class TimeFormat extends ScalarFunction
+        implements BinaryExpression, ExplicitlyCastableSignature, 
AlwaysNullable, Monotonic {
+
+    public static final List<FunctionSignature> SIGNATURES = ImmutableList.of(
+            FunctionSignature.ret(VarcharType.SYSTEM_DEFAULT)

Review Comment:
   是不是可以只留一个这个?



##########
be/src/vec/runtime/vdatetime_value.cpp:
##########
@@ -2977,11 +2977,10 @@ void DateV2Value<T>::set_microsecond(uint64_t 
microsecond) {
 }
 
 template <typename T>
-bool DateV2Value<T>::to_format_string_conservative(const char* format, size_t 
len, char* to,
-                                                   size_t max_valid_length) 
const {
-    if (is_invalid(year(), month(), day(), hour(), minute(), second(), 
microsecond())) {
-        return false;
-    }
+bool DateV2Value<T>::to_format_string_without_check(const char* format, size_t 
len, char* to,

Review Comment:
   不应该是成员函数了



##########
be/src/vec/runtime/vdatetime_value.cpp:
##########
@@ -3093,135 +3103,164 @@ bool 
DateV2Value<T>::to_format_string_conservative(const char* format, size_t le
             break;
         case 'e':
             // Day of the month, numeric (0..31)
-            pos = int_to_str(this->day(), cursor);
+            pos = int_to_str(day, cursor);
             to = append_with_prefix(cursor, pos - cursor, '0', 1, to);
             break;
         case 'f':
             // Microseconds (000000..999999)
-            pos = int_to_str(this->microsecond(), cursor);
+            pos = int_to_str(ms, cursor);
             to = append_with_prefix(cursor, pos - cursor, '0', 6, to);
             break;
         case 'j':
             // Day of year (001..366)
-            pos = int_to_str(daynr() - doris::calc_daynr(this->year(), 1, 1) + 
1, cursor);
+            if (month == 0 || day == 0) {

Review Comment:
   加个模板参数,从date/datetime调用不进行这些check



##########
fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/executable/DateTimeExtractAndTransform.java:
##########
@@ -281,10 +282,8 @@ public static Expression dateFormat(DateV2Literal date, 
StringLikeLiteral format
             throw new AnalysisException("The length of format string in 
date_format() function should not be greater"
                     + " than 128.");
         }
-        DateTimeV2Literal datetime = new DateTimeV2Literal(date.getYear(), 
date.getMonth(), date.getDay(), 0, 0, 0, 0);
         format = (StringLikeLiteral) 
SupportJavaDateFormatter.translateJavaFormatter(format);
-        return new 
VarcharLiteral(DateUtils.dateTimeFormatterChecklength(format.getValue(), 
datetime).format(
-                java.time.LocalDate.of(((int) date.getYear()), ((int) 
date.getMonth()), ((int) date.getDay()))));
+        return new 
VarcharLiteral(DateTimeFormatterUtils.toFormatStringConservative(date, format, 
false));

Review Comment:
   如果只需要time的签名,这里一并改回去



##########
be/src/vec/runtime/vdatetime_value.cpp:
##########
@@ -3345,6 +3396,11 @@ uint16_t DateV2Value<T>::year_of_week() const {
     }
     return date_v2_value_.year_;
 }
+template <typename T>
+uint8_t DateV2Value<T>::week(int16_t year, int8_t month, int8_t day, uint8_t 
mode) {

Review Comment:
   double check下,如果确实需要,加个注释说明



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