HappenLee commented on a change in pull request #7988: URL: https://github.com/apache/incubator-doris/pull/7988#discussion_r801532798
########## File path: be/src/vec/functions/function_string.h ########## @@ -1142,4 +1154,88 @@ struct MoneyFormatDecimalImpl { } }; +class FunctionStringLocatePos : public IFunction { +public: + static constexpr auto name = "locate"; + static FunctionPtr create() { return std::make_shared<FunctionStringLocatePos>(); } + String get_name() const override { return name; } + size_t get_number_of_arguments() const override { return 3; } + + DataTypePtr get_return_type_impl(const DataTypes& arguments) const override { + return std::make_shared<DataTypeInt32>(); + } + + DataTypes get_variadic_argument_types() { + return {std::make_shared<DataTypeString>(), std::make_shared<DataTypeString>(), + std::make_shared<DataTypeInt32>()}; + } + + bool use_default_implementation_for_constants() const override { return true; } + + Status execute_impl(FunctionContext* context, Block& block, const ColumnNumbers& arguments, + size_t result, size_t input_rows_count) override { + auto col_substr = + block.get_by_position(arguments[0]).column->convert_to_full_column_if_const(); + auto col_str = + block.get_by_position(arguments[1]).column->convert_to_full_column_if_const(); + auto col_pos = + block.get_by_position(arguments[2]).column->convert_to_full_column_if_const(); + + ColumnInt32::MutablePtr col_res = ColumnInt32::create(); + + auto& vec_pos = reinterpret_cast<const ColumnInt32*>(col_pos.get())->get_data(); + auto& vec_res = col_res->get_data(); + vec_res.resize(input_rows_count); + + for (int i = 0; i < input_rows_count; ++i) { + vec_res[i] = locate_pos(col_substr->get_data_at(i).to_string_val(), + col_str->get_data_at(i).to_string_val(), vec_pos[i]); + } + + block.replace_by_position(result, std::move(col_res)); + return Status::OK(); + } + +private: + int locate_pos(StringVal substr, StringVal str, int start_pos) { + if (substr.len == 0) { + if (start_pos <= 0) { + return 0; + } else if (start_pos == 1) { + return 1; + } else if (start_pos > str.len) { + return 0; + } else { + return start_pos; + } + } + // Hive returns 0 for *start_pos <= 0, + // but throws an exception for *start_pos > str->len. + // Since returning 0 seems to be Hive's error condition, return 0. + std::vector<size_t> index; + size_t char_len = get_char_len(str, &index); + if (start_pos > char_len) { + return 0; + } + StringValue substr_sv = StringValue::from_string_val(substr); + StringSearch search(&substr_sv); + // Input start_pos starts from 1. + StringValue adjusted_str(reinterpret_cast<char*>(str.ptr) + index[start_pos - 1], + str.len - index[start_pos - 1]); + int32_t match_pos = search.search(&adjusted_str); + if (match_pos >= 0) { + // Hive returns the position in the original string starting from 1. + size_t char_len = 0; Review comment: abtruct the function -- 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