Gabriel39 commented on code in PR #13039: URL: https://github.com/apache/doris/pull/13039#discussion_r984361760
########## be/src/vec/functions/function_timestamp.cpp: ########## @@ -33,6 +33,16 @@ namespace doris::vectorized { struct StrToDate { static constexpr auto name = "str_to_date"; + static bool is_variadic() { return false; } + + static DataTypes get_variadic_argument_types() { Review Comment: Since the function with variadic arguments are less efficient when we search it, I think you should keep these functions with no variadic arguments ########## be/src/vec/functions/function_timestamp.cpp: ########## @@ -214,6 +234,90 @@ struct MakeDateImpl { } }; +template <typename DateValueType, typename ArgType> +struct DateTrunc { + static constexpr auto name = "date_trunc"; + + static bool is_variadic() { return true; } + + static DataTypes get_variadic_argument_types() { + if constexpr (std::is_same_v<DateValueType, VecDateTimeValue>) { + return {std::make_shared<DataTypeDateTime>(), std::make_shared<DataTypeString>()}; + } else { + return {std::make_shared<DataTypeDateTimeV2>(), std::make_shared<DataTypeString>()}; + } + } + + static DataTypePtr get_return_type_impl(const DataTypes& arguments) { + if constexpr (std::is_same_v<DateValueType, VecDateTimeValue>) { + return make_nullable(std::make_shared<DataTypeDateTime>()); + } else { + return make_nullable(std::make_shared<DataTypeDateTimeV2>()); + } + } + + static Status execute(FunctionContext* context, Block& block, const ColumnNumbers& arguments, + size_t result, size_t input_rows_count) { + DCHECK_EQ(arguments.size(), 2); + ColumnPtr argument_columns[2]; + auto null_map = ColumnUInt8::create(input_rows_count, 0); + argument_columns[0] = + block.get_by_position(arguments[0]).column->convert_to_full_column_if_const(); + argument_columns[1] = + block.get_by_position(arguments[1]).column->convert_to_full_column_if_const(); + auto datetime_column = static_cast<const ColumnVector<ArgType>*>(argument_columns[0].get()); + auto str_column = static_cast<const ColumnString*>(argument_columns[1].get()); + auto& rdata = str_column->get_chars(); + auto& roffsets = str_column->get_offsets(); + + ColumnPtr res = nullptr; + + if constexpr (std::is_same_v<DateValueType, VecDateTimeValue>) { + res = ColumnVector<Int64>::create(); + executeImpl(datetime_column->get_data(), rdata, roffsets, + static_cast<ColumnVector<Int64>*>(res->assume_mutable().get())->get_data(), + null_map->get_data(), input_rows_count); + } else { + res = ColumnVector<UInt64>::create(); + executeImpl(datetime_column->get_data(), rdata, roffsets, + static_cast<ColumnVector<UInt64>*>(res->assume_mutable().get())->get_data(), + null_map->get_data(), input_rows_count); + } + + block.get_by_position(result).column = + ColumnNullable::create(std::move(res), std::move(null_map)); + return Status::OK(); + } + + static void executeImpl(const PaddedPODArray<ArgType>& ldata, const ColumnString::Chars& rdata, + const ColumnString::Offsets& roffsets, PaddedPODArray<ArgType>& res, + NullMap& null_map, size_t input_rows_count) { + res.resize(input_rows_count); + for (size_t i = 0; i < input_rows_count; ++i) { + auto& dt = (DateValueType&)ldata[i]; + const char* str_data = reinterpret_cast<const char*>(&rdata[roffsets[i - 1]]); + if (std::strncmp("year", str_data, 4) == 0) { Review Comment: Could you move this heavy `if-else` out of the for-loop? -- 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