HappenLee commented on code in PR #33243: URL: https://github.com/apache/doris/pull/33243#discussion_r1567000411
########## be/src/vec/functions/function_string.h: ########## @@ -3914,4 +3914,129 @@ class FunctionIntToChar : public IFunction { #endif } }; + +class FunctionStrInsert : public IFunction { +public: + static constexpr auto name = "str_insert"; + static FunctionPtr create() { return std::make_shared<FunctionStrInsert>(); } + String get_name() const override { return name; } + size_t get_number_of_arguments() const override { return 4; } + + DataTypePtr get_return_type_impl(const DataTypes& arguments) const override { + return std::make_shared<DataTypeString>(); + } + + Status execute_impl(FunctionContext* context, Block& block, const ColumnNumbers& arguments, + size_t result, size_t input_rows_count) const override { + DCHECK_EQ(arguments.size(), 4); + + bool col_const[4]; + ColumnPtr argument_columns[4]; + for (int i = 0; i < 4; ++i) { + std::tie(argument_columns[i], col_const[i]) = + unpack_if_const(block.get_by_position(arguments[i]).column); + } + argument_columns[0] = col_const[0] ? static_cast<const ColumnConst&>( + *block.get_by_position(arguments[0]).column) + .convert_to_full_column() + : block.get_by_position(arguments[0]).column; + + default_preprocess_parameter_columns(argument_columns, col_const, {1, 2, 3}, block, + arguments); + + const auto* col_origin = assert_cast<const ColumnString*>(argument_columns[0].get()); + + const auto* col_pos = assert_cast<const ColumnVector<Int32>*>(argument_columns[1].get()) + ->get_data() + .data(); + const auto* col_len = assert_cast<const ColumnVector<Int32>*>(argument_columns[2].get()) + ->get_data() + .data(); + const auto* col_insert = assert_cast<const ColumnString*>(argument_columns[3].get()); + + ColumnString::MutablePtr col_res = ColumnString::create(); + + if (col_const[1] && col_const[2] && col_const[3]) { + vector_const(col_origin, col_pos, col_len, col_insert, col_res, input_rows_count); + } else { + vector(col_origin, col_pos, col_len, col_insert, col_res, input_rows_count); + } + + block.replace_by_position(result, std::move(col_res)); + return Status::OK(); + } + +private: + // get the new str size + static std::pair<bool, size_t> get_size(size_t& str_size, int& pos, int& len, + size_t& ins_size) { + if (pos > str_size || pos < 1) { + return {true, str_size}; + } + if (len < 0 || pos + len - 1 >= str_size) { + len = str_size - pos + 1; + return {false, pos + ins_size - 1}; + } + return {false, str_size - len + ins_size}; + } + + static void vector_const(const ColumnString* col_origin, int const* col_pos, int const* col_len, + const ColumnString* col_insert, ColumnString::MutablePtr& col_res, + size_t input_rows_count) { + auto& col_res_chars = col_res->get_chars(); + auto& col_res_offsets = col_res->get_offsets(); + StringRef origin_str; + StringRef insert_str = col_insert->get_data_at(0); + auto pos = col_pos[0]; + for (size_t i = 0; i < input_rows_count; i++) { + origin_str = col_origin->get_data_at(i); + auto len = col_len[0]; Review Comment: the len is const, why get the value in 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