HappenLee commented on code in PR #50245: URL: https://github.com/apache/doris/pull/50245#discussion_r2052204103
########## be/src/vec/functions/function_regexp.cpp: ########## @@ -52,12 +51,129 @@ namespace doris::vectorized { #include "common/compile_check_begin.h" +struct ThreeParamTypes { + static DataTypes get_variadic_argument_types() { + return {std::make_shared<DataTypeString>(), std::make_shared<DataTypeString>(), + std::make_shared<DataTypeString>()}; + } +}; + +struct FourParamTypes { + static DataTypes get_variadic_argument_types() { + return {std::make_shared<DataTypeString>(), std::make_shared<DataTypeString>(), + std::make_shared<DataTypeString>(), std::make_shared<DataTypeString>()}; + } +}; + +// template FunctionRegexpFunctionality is used for regexp_replace/regexp_replace_one +template <typename Impl, typename ParamTypes> +class FunctionRegexpReplace : public IFunction { +public: + static constexpr auto name = Impl::name; + + static FunctionPtr create() { return std::make_shared<FunctionRegexpReplace>(); } + + String get_name() const override { return name; } + + size_t get_number_of_arguments() const override { + return get_variadic_argument_types_impl().size(); + } + + bool is_variadic() const override { return true; } + + DataTypePtr get_return_type_impl(const DataTypes& arguments) const override { + return make_nullable(std::make_shared<DataTypeString>()); + } + + DataTypes get_variadic_argument_types_impl() const override { + return ParamTypes::get_variadic_argument_types(); + } + + Status open(FunctionContext* context, FunctionContext::FunctionStateScope scope) override { + if (scope == FunctionContext::THREAD_LOCAL) { + if (context->is_col_constant(1)) { + DCHECK(!context->get_function_state(scope)); + const auto pattern_col = context->get_constant_col(1)->column_ptr; + const auto& pattern = pattern_col->get_data_at(0); + if (pattern.size == 0) { + return Status::OK(); + } + + std::string error_str; + std::unique_ptr<re2::RE2> scoped_re; + StringRef options_value; + if (context->get_num_args() == 4) { + DCHECK(context->is_col_constant(3)); + const auto options_col = context->get_constant_col(3)->column_ptr; + options_value = options_col->get_data_at(0); + } + + bool st = StringFunctions::compile_regex(pattern, &error_str, StringRef(), + options_value, scoped_re); + if (!st) { + context->set_error(error_str.c_str()); + return Status::InvalidArgument(error_str); + } + std::shared_ptr<re2::RE2> re(scoped_re.release()); + context->set_function_state(scope, re); + } + } + return Status::OK(); + } + + Status execute_impl(FunctionContext* context, Block& block, const ColumnNumbers& arguments, + uint32_t result, size_t input_rows_count) const override { + size_t argument_size = arguments.size(); + + auto result_null_map = ColumnUInt8::create(input_rows_count, 0); + auto result_data_column = ColumnString::create(); + auto& result_data = result_data_column->get_chars(); + auto& result_offset = result_data_column->get_offsets(); + result_offset.resize(input_rows_count); + + bool col_const[3]; + ColumnPtr argument_columns[3]; + for (int i = 0; i < argument_size; ++i) { + col_const[i] = is_column_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}, block, arguments); + + // the options have check in FE, so is always const, and get idx of 0 + StringRef options_value; + if (argument_size == 4) { Review Comment: the call seems only in 157 line, `if (col_const[1] && col_const[2])` is true. no need call get_data_at -- 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