|
| 1 | +#include <Columns/ColumnArray.h> |
| 2 | +#include <Columns/ColumnFixedString.h> |
| 3 | +#include <Columns/ColumnString.h> |
| 4 | +#include <DataTypes/DataTypeArray.h> |
| 5 | +#include <DataTypes/DataTypeFixedString.h> |
| 6 | +#include <DataTypes/DataTypeString.h> |
| 7 | +#include <DataTypes/DataTypesNumber.h> |
| 8 | +#include <Functions/FunctionFactory.h> |
| 9 | +#include <Functions/FunctionHelpers.h> |
| 10 | +#include <Functions/IFunction.h> |
| 11 | +#include <Interpreters/Context_fwd.h> |
| 12 | +#include <Interpreters/GinFilter.h> |
| 13 | +#include <Interpreters/ITokenExtractor.h> |
| 14 | + |
| 15 | +#include <roaring.hh> |
| 16 | + |
| 17 | +namespace DB |
| 18 | +{ |
| 19 | + |
| 20 | +namespace ErrorCodes |
| 21 | +{ |
| 22 | + extern const int BAD_ARGUMENTS; |
| 23 | +} |
| 24 | + |
| 25 | +struct SearchAnyProps |
| 26 | +{ |
| 27 | + static constexpr String name = "searchAny"; |
| 28 | + static constexpr GinSearchMode search_mode = GinSearchMode::ANY; |
| 29 | +}; |
| 30 | + |
| 31 | +struct SearchAllProps |
| 32 | +{ |
| 33 | + static constexpr String name = "searchAll"; |
| 34 | + static constexpr GinSearchMode search_mode = GinSearchMode::ALL; |
| 35 | +}; |
| 36 | + |
| 37 | +namespace |
| 38 | +{ |
| 39 | +template <typename Type> |
| 40 | +std::optional<Type> getArgument(const ColumnsWithTypeAndName & arguments, size_t index) |
| 41 | +{ |
| 42 | + if (index < arguments.size()) |
| 43 | + { |
| 44 | + if constexpr (std::is_same_v<Type, UInt64>) |
| 45 | + return arguments[index].column->getUInt(0); |
| 46 | + else if constexpr (std::is_same_v<Type, String>) |
| 47 | + return arguments[index].column->getDataAt(0).toString(); |
| 48 | + else if constexpr (std::is_same_v<Type, std::string_view>) |
| 49 | + return arguments[index].column->getDataAt(0).toView(); |
| 50 | + else |
| 51 | + { |
| 52 | + throw Exception( |
| 53 | + ErrorCodes::BAD_ARGUMENTS, |
| 54 | + "Search function argument at index '{}' expected to be any of UInt64, String, or std::string_view", |
| 55 | + index); |
| 56 | + } |
| 57 | + } |
| 58 | + return std::nullopt; |
| 59 | +} |
| 60 | +} |
| 61 | + |
| 62 | +template <typename SearchProps> |
| 63 | +class FunctionSearchImpl : public IFunction |
| 64 | +{ |
| 65 | + static constexpr size_t arg_input = 0; |
| 66 | + static constexpr size_t arg_needle = 1; |
| 67 | + static constexpr size_t arg_tokenizer = 2; |
| 68 | + static constexpr size_t arg_ngrams = 3; |
| 69 | + |
| 70 | +public: |
| 71 | + static constexpr auto name = SearchProps::name; |
| 72 | + |
| 73 | + static FunctionPtr create(ContextPtr) |
| 74 | + { |
| 75 | + return std::make_shared<FunctionSearchImpl>(); |
| 76 | + } |
| 77 | + |
| 78 | + String getName() const override { return name; } |
| 79 | + size_t getNumberOfArguments() const override { return 0; } |
| 80 | + bool isVariadic() const override { return true; } |
| 81 | + bool useDefaultImplementationForConstants() const override { return true; } |
| 82 | + bool isSuitableForShortCircuitArgumentsExecution(const DataTypesWithConstInfo & /*arguments*/) const override { return true; } |
| 83 | + |
| 84 | + DataTypePtr getReturnTypeImpl(const ColumnsWithTypeAndName & arguments) const override |
| 85 | + { |
| 86 | + FunctionArgumentDescriptors mandatory_args{ |
| 87 | + {"input", static_cast<FunctionArgumentDescriptor::TypeValidator>(&isStringOrFixedString), nullptr, "String or FixedString"}, |
| 88 | + {"needle", static_cast<FunctionArgumentDescriptor::TypeValidator>(&isString), isColumnConst, "String"}}; |
| 89 | + |
| 90 | + FunctionArgumentDescriptors optional_args; |
| 91 | + |
| 92 | + if (arguments.size() > 2) |
| 93 | + { |
| 94 | + optional_args.emplace_back("tokenizer", static_cast<FunctionArgumentDescriptor::TypeValidator>(&isString), isColumnConst, "String"); |
| 95 | + |
| 96 | + validateFunctionArguments( |
| 97 | + *this, {arguments[arg_input], arguments[arg_needle], arguments[arg_tokenizer]}, mandatory_args, optional_args); |
| 98 | + |
| 99 | + if (arguments.size() == 4) |
| 100 | + { |
| 101 | + const auto tokenizer = arguments[arg_tokenizer].column->getDataAt(0).toString(); |
| 102 | + |
| 103 | + if (tokenizer == NgramTokenExtractor::getExternalName()) |
| 104 | + optional_args.emplace_back("ngrams", static_cast<FunctionArgumentDescriptor::TypeValidator>(&isUInt8), isColumnConst, "UInt8"); |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + validateFunctionArguments(*this, arguments, mandatory_args, optional_args); |
| 109 | + |
| 110 | + return std::make_shared<DataTypeNumber<UInt8>>(); |
| 111 | + } |
| 112 | + |
| 113 | + ColumnPtr executeImpl(const ColumnsWithTypeAndName & arguments, const DataTypePtr &, size_t input_rows_count) const override |
| 114 | + { |
| 115 | + auto col_input = arguments[arg_input].column; |
| 116 | + auto col_needle = arguments[arg_needle].column; |
| 117 | + auto col_result = ColumnVector<UInt8>::create(); |
| 118 | + |
| 119 | + if (input_rows_count == 0) |
| 120 | + return col_result; |
| 121 | + |
| 122 | + const auto tokenizer_arg = getArgument<std::string_view>(arguments, arg_tokenizer).value_or(SplitTokenExtractor::getExternalName()); |
| 123 | + |
| 124 | + std::unique_ptr<ITokenExtractor> token_extractor; |
| 125 | + if (tokenizer_arg == SplitTokenExtractor::getExternalName()) |
| 126 | + token_extractor = std::make_unique<SplitTokenExtractor>(); |
| 127 | + else if (tokenizer_arg == NoOpTokenExtractor::getExternalName()) |
| 128 | + token_extractor = std::make_unique<NoOpTokenExtractor>(); |
| 129 | + else if (tokenizer_arg == NgramTokenExtractor::getExternalName()) |
| 130 | + { |
| 131 | + auto ngrams = getArgument<UInt64>(arguments, arg_ngrams).value_or(3); |
| 132 | + if (ngrams < 2 || ngrams > 8) |
| 133 | + throw Exception(ErrorCodes::BAD_ARGUMENTS, "Ngrams argument of function {} should be between 2 and 8, got: {}", name, ngrams); |
| 134 | + token_extractor = std::make_unique<NgramTokenExtractor>(ngrams); |
| 135 | + } |
| 136 | + else |
| 137 | + { |
| 138 | + throw Exception( |
| 139 | + ErrorCodes::BAD_ARGUMENTS, |
| 140 | + "Function '{}' supports only tokenizers 'default', 'ngram', and 'noop'", name); |
| 141 | + } |
| 142 | + |
| 143 | + const auto & col_needle_tokens = col_needle->getDataAt(0); |
| 144 | + std::vector<String> needle_tokens = SplitTokenExtractor().getTokens(col_needle_tokens.data, col_needle_tokens.size); |
| 145 | + |
| 146 | + if (const auto * column_string = checkAndGetColumn<ColumnString>(col_input.get())) |
| 147 | + executeImpl(std::move(token_extractor), *column_string, input_rows_count, needle_tokens, col_result->getData()); |
| 148 | + else if (const auto * column_fixed_string = checkAndGetColumn<ColumnFixedString>(col_input.get())) |
| 149 | + executeImpl(std::move(token_extractor), *column_fixed_string, input_rows_count, needle_tokens, col_result->getData()); |
| 150 | + |
| 151 | + return col_result; |
| 152 | + } |
| 153 | + |
| 154 | +private: |
| 155 | + template <typename StringColumnType> |
| 156 | + void executeImpl( |
| 157 | + std::unique_ptr<ITokenExtractor> token_extractor, |
| 158 | + StringColumnType & col_input, |
| 159 | + size_t rows_count_input, |
| 160 | + const std::vector<String>& needle_tokens, |
| 161 | + PaddedPODArray<UInt8> & col_result) const |
| 162 | + { |
| 163 | + col_result.resize(rows_count_input); |
| 164 | + |
| 165 | + for (size_t i = 0; i < rows_count_input; ++i) |
| 166 | + { |
| 167 | + const auto value{col_input.getDataAt(i)}; |
| 168 | + |
| 169 | + col_result[i] = false; |
| 170 | + |
| 171 | + [[maybe_unused]] roaring::Roaring mask; |
| 172 | + for (const auto& token : token_extractor->getTokens(value.data, value.size)) |
| 173 | + { |
| 174 | + for (size_t pos = 0; pos < needle_tokens.size(); ++pos) { |
| 175 | + if (token == needle_tokens[pos]) { |
| 176 | + if constexpr (SearchProps::search_mode == GinSearchMode::ALL) |
| 177 | + { |
| 178 | + mask.add(pos); |
| 179 | + } |
| 180 | + else |
| 181 | + { |
| 182 | + col_result[i] = true; |
| 183 | + break; |
| 184 | + } |
| 185 | + } |
| 186 | + } |
| 187 | + } |
| 188 | + if constexpr (SearchProps::search_mode == GinSearchMode::ALL) |
| 189 | + col_result[i] = mask.cardinality() == needle_tokens.size(); |
| 190 | + } |
| 191 | + } |
| 192 | +}; |
| 193 | + |
| 194 | +REGISTER_FUNCTION(SearchAny) |
| 195 | +{ |
| 196 | + factory.registerFunction<FunctionSearchImpl<SearchAnyProps>>(FunctionDocumentation{ |
| 197 | + .description = "Searches the needle tokens in the generated tokens from the text by a given tokenizer. Returns true if any needle " |
| 198 | + "tokens exists in the text, otherwise false.", |
| 199 | + .category = FunctionDocumentation::Category::StringSearch}); |
| 200 | +} |
| 201 | + |
| 202 | +REGISTER_FUNCTION(SearchAll) |
| 203 | +{ |
| 204 | + factory.registerFunction<FunctionSearchImpl<SearchAllProps>>(FunctionDocumentation{ |
| 205 | + .description = "Searches the needle tokens in the generated tokens from the text by a given tokenizer. Returns true if all needle " |
| 206 | + "tokens exists in the text, otherwise false.", |
| 207 | + .category = FunctionDocumentation::Category::StringSearch}); |
| 208 | +} |
| 209 | +} |
0 commit comments