/root/doris/be/src/vec/functions/match.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | // Licensed to the Apache Software Foundation (ASF) under one |
2 | | // or more contributor license agreements. See the NOTICE file |
3 | | // distributed with this work for additional information |
4 | | // regarding copyright ownership. The ASF licenses this file |
5 | | // to you under the Apache License, Version 2.0 (the |
6 | | // "License"); you may not use this file except in compliance |
7 | | // with the License. You may obtain a copy of the License at |
8 | | // |
9 | | // http://www.apache.org/licenses/LICENSE-2.0 |
10 | | // |
11 | | // Unless required by applicable law or agreed to in writing, |
12 | | // software distributed under the License is distributed on an |
13 | | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
14 | | // KIND, either express or implied. See the License for the |
15 | | // specific language governing permissions and limitations |
16 | | // under the License. |
17 | | |
18 | | #include "vec/functions/match.h" |
19 | | |
20 | | #include <hs/hs.h> |
21 | | |
22 | | #include "runtime/query_context.h" |
23 | | #include "runtime/runtime_state.h" |
24 | | #include "util/debug_points.h" |
25 | | |
26 | | namespace doris::vectorized { |
27 | | Status FunctionMatchBase::evaluate_inverted_index( |
28 | | const ColumnsWithTypeAndName& arguments, |
29 | | const std::vector<vectorized::IndexFieldNameAndTypePair>& data_type_with_names, |
30 | | std::vector<segment_v2::InvertedIndexIterator*> iterators, uint32_t num_rows, |
31 | 0 | segment_v2::InvertedIndexResultBitmap& bitmap_result) const { |
32 | 0 | DCHECK(arguments.size() == 1); |
33 | 0 | DCHECK(data_type_with_names.size() == 1); |
34 | 0 | DCHECK(iterators.size() == 1); |
35 | 0 | auto* iter = iterators[0]; |
36 | 0 | auto data_type_with_name = data_type_with_names[0]; |
37 | 0 | if (iter == nullptr) { |
38 | 0 | return Status::OK(); |
39 | 0 | } |
40 | 0 | const std::string& function_name = get_name(); |
41 | |
|
42 | 0 | if (function_name == MATCH_PHRASE_FUNCTION || function_name == MATCH_PHRASE_PREFIX_FUNCTION || |
43 | 0 | function_name == MATCH_PHRASE_EDGE_FUNCTION) { |
44 | 0 | if (iter->get_inverted_index_reader_type() == InvertedIndexReaderType::FULLTEXT && |
45 | 0 | get_parser_phrase_support_string_from_properties(iter->get_index_properties()) == |
46 | 0 | INVERTED_INDEX_PARSER_PHRASE_SUPPORT_NO) { |
47 | 0 | return Status::Error<ErrorCode::INVERTED_INDEX_INVALID_PARAMETERS>( |
48 | 0 | "phrase queries require setting support_phrase = true"); |
49 | 0 | } |
50 | 0 | } |
51 | 0 | std::shared_ptr<roaring::Roaring> roaring = std::make_shared<roaring::Roaring>(); |
52 | 0 | Field param_value; |
53 | 0 | arguments[0].column->get(0, param_value); |
54 | 0 | if (param_value.is_null()) { |
55 | | // if query value is null, skip evaluate inverted index |
56 | 0 | return Status::OK(); |
57 | 0 | } |
58 | 0 | auto param_type = arguments[0].type->get_type_as_type_descriptor().type; |
59 | 0 | if (!is_string_type(param_type)) { |
60 | 0 | return Status::Error<ErrorCode::INVERTED_INDEX_INVALID_PARAMETERS>( |
61 | 0 | "arguments for match must be string"); |
62 | 0 | } |
63 | 0 | std::unique_ptr<segment_v2::InvertedIndexQueryParamFactory> query_param = nullptr; |
64 | 0 | RETURN_IF_ERROR(segment_v2::InvertedIndexQueryParamFactory::create_query_value( |
65 | 0 | param_type, ¶m_value, query_param)); |
66 | 0 | if (is_string_type(param_type)) { |
67 | 0 | auto inverted_index_query_type = get_query_type_from_fn_name(); |
68 | 0 | RETURN_IF_ERROR( |
69 | 0 | iter->read_from_inverted_index(data_type_with_name.first, query_param->get_value(), |
70 | 0 | inverted_index_query_type, num_rows, roaring)); |
71 | 0 | } else { |
72 | 0 | return Status::Error<ErrorCode::INVERTED_INDEX_INVALID_PARAMETERS>( |
73 | 0 | "invalid params type for FunctionMatchBase::evaluate_inverted_index {}", |
74 | 0 | param_type); |
75 | 0 | } |
76 | 0 | std::shared_ptr<roaring::Roaring> null_bitmap = std::make_shared<roaring::Roaring>(); |
77 | 0 | if (iter->has_null()) { |
78 | 0 | segment_v2::InvertedIndexQueryCacheHandle null_bitmap_cache_handle; |
79 | 0 | RETURN_IF_ERROR(iter->read_null_bitmap(&null_bitmap_cache_handle)); |
80 | 0 | null_bitmap = null_bitmap_cache_handle.get_bitmap(); |
81 | 0 | } |
82 | 0 | segment_v2::InvertedIndexResultBitmap result(roaring, null_bitmap); |
83 | 0 | bitmap_result = result; |
84 | 0 | bitmap_result.mask_out_null(); |
85 | |
|
86 | 0 | return Status::OK(); |
87 | 0 | } |
88 | | Status FunctionMatchBase::execute_impl(FunctionContext* context, Block& block, |
89 | | const ColumnNumbers& arguments, size_t result, |
90 | 0 | size_t input_rows_count) const { |
91 | 0 | ColumnPtr& column_ptr = block.get_by_position(arguments[1]).column; |
92 | 0 | DataTypePtr& type_ptr = block.get_by_position(arguments[1]).type; |
93 | 0 | auto match_query_str = type_ptr->to_string(*column_ptr, 0); |
94 | 0 | std::string column_name = block.get_by_position(arguments[0]).name; |
95 | 0 | VLOG_DEBUG << "begin to execute match directly, column_name=" << column_name |
96 | 0 | << ", match_query_str=" << match_query_str; |
97 | 0 | InvertedIndexCtx* inverted_index_ctx = reinterpret_cast<InvertedIndexCtx*>( |
98 | 0 | context->get_function_state(FunctionContext::THREAD_LOCAL)); |
99 | 0 | if (inverted_index_ctx == nullptr) { |
100 | 0 | inverted_index_ctx = reinterpret_cast<InvertedIndexCtx*>( |
101 | 0 | context->get_function_state(FunctionContext::FRAGMENT_LOCAL)); |
102 | 0 | } |
103 | |
|
104 | 0 | const ColumnPtr source_col = |
105 | 0 | block.get_by_position(arguments[0]).column->convert_to_full_column_if_const(); |
106 | 0 | const auto* values = check_and_get_column<ColumnString>(source_col.get()); |
107 | 0 | const ColumnArray* array_col = nullptr; |
108 | 0 | if (source_col->is_column_array()) { |
109 | 0 | array_col = check_and_get_column<ColumnArray>(source_col.get()); |
110 | 0 | if (array_col && !array_col->get_data().is_column_string()) { |
111 | 0 | return Status::NotSupported( |
112 | 0 | fmt::format("unsupported nested array of type {} for function {}", |
113 | 0 | is_column_nullable(array_col->get_data()) |
114 | 0 | ? array_col->get_data().get_name() |
115 | 0 | : array_col->get_data().get_family_name(), |
116 | 0 | get_name())); |
117 | 0 | } |
118 | | |
119 | 0 | if (is_column_nullable(array_col->get_data())) { |
120 | 0 | const auto& array_nested_null_column = |
121 | 0 | reinterpret_cast<const ColumnNullable&>(array_col->get_data()); |
122 | 0 | values = check_and_get_column<ColumnString>( |
123 | 0 | *(array_nested_null_column.get_nested_column_ptr())); |
124 | 0 | } else { |
125 | | // array column element is always set Nullable for now. |
126 | 0 | values = check_and_get_column<ColumnString>(*(array_col->get_data_ptr())); |
127 | 0 | } |
128 | 0 | } else if (auto* nullable = check_and_get_column<ColumnNullable>(source_col.get())) { |
129 | 0 | values = check_and_get_column<ColumnString>(*nullable->get_nested_column_ptr()); |
130 | 0 | } |
131 | | |
132 | 0 | if (!values) { |
133 | 0 | LOG(WARNING) << "Illegal column " << source_col->get_name(); |
134 | 0 | return Status::InternalError("Not supported input column types"); |
135 | 0 | } |
136 | | // result column |
137 | 0 | auto res = ColumnUInt8::create(); |
138 | 0 | ColumnUInt8::Container& vec_res = res->get_data(); |
139 | | // set default value to 0, and match functions only need to set 1/true |
140 | 0 | vec_res.resize_fill(input_rows_count); |
141 | 0 | RETURN_IF_ERROR(execute_match(context, column_name, match_query_str, input_rows_count, values, |
142 | 0 | inverted_index_ctx, |
143 | 0 | (array_col ? &(array_col->get_offsets()) : nullptr), vec_res)); |
144 | 0 | block.replace_by_position(result, std::move(res)); |
145 | |
|
146 | 0 | return Status::OK(); |
147 | 0 | } |
148 | | |
149 | | inline doris::segment_v2::InvertedIndexQueryType FunctionMatchBase::get_query_type_from_fn_name() |
150 | 0 | const { |
151 | 0 | std::string fn_name = get_name(); |
152 | 0 | if (fn_name == MATCH_ANY_FUNCTION) { |
153 | 0 | return doris::segment_v2::InvertedIndexQueryType::MATCH_ANY_QUERY; |
154 | 0 | } else if (fn_name == MATCH_ALL_FUNCTION) { |
155 | 0 | return doris::segment_v2::InvertedIndexQueryType::MATCH_ALL_QUERY; |
156 | 0 | } else if (fn_name == MATCH_PHRASE_FUNCTION) { |
157 | 0 | return doris::segment_v2::InvertedIndexQueryType::MATCH_PHRASE_QUERY; |
158 | 0 | } else if (fn_name == MATCH_PHRASE_PREFIX_FUNCTION) { |
159 | 0 | return doris::segment_v2::InvertedIndexQueryType::MATCH_PHRASE_PREFIX_QUERY; |
160 | 0 | } else if (fn_name == MATCH_PHRASE_REGEXP_FUNCTION) { |
161 | 0 | return doris::segment_v2::InvertedIndexQueryType::MATCH_REGEXP_QUERY; |
162 | 0 | } else if (fn_name == MATCH_PHRASE_EDGE_FUNCTION) { |
163 | 0 | return doris::segment_v2::InvertedIndexQueryType::MATCH_PHRASE_EDGE_QUERY; |
164 | 0 | } |
165 | 0 | return doris::segment_v2::InvertedIndexQueryType::UNKNOWN_QUERY; |
166 | 0 | } |
167 | | |
168 | | void FunctionMatchBase::analyse_query_str_token(std::vector<std::string>* query_tokens, |
169 | | InvertedIndexCtx* inverted_index_ctx, |
170 | | const std::string& match_query_str, |
171 | 0 | const std::string& column_name) const { |
172 | 0 | VLOG_DEBUG << "begin to run " << get_name() << ", parser_type: " |
173 | 0 | << inverted_index_parser_type_to_string(inverted_index_ctx->parser_type); |
174 | 0 | if (inverted_index_ctx->parser_type == InvertedIndexParserType::PARSER_NONE) { |
175 | 0 | query_tokens->emplace_back(match_query_str); |
176 | 0 | return; |
177 | 0 | } |
178 | 0 | auto reader = doris::segment_v2::InvertedIndexReader::create_reader(inverted_index_ctx, |
179 | 0 | match_query_str); |
180 | 0 | doris::segment_v2::InvertedIndexReader::get_analyse_result( |
181 | 0 | *query_tokens, reader.get(), inverted_index_ctx->analyzer, column_name, |
182 | 0 | get_query_type_from_fn_name()); |
183 | 0 | } |
184 | | |
185 | | inline std::vector<std::string> FunctionMatchBase::analyse_data_token( |
186 | | const std::string& column_name, InvertedIndexCtx* inverted_index_ctx, |
187 | | const ColumnString* string_col, int32_t current_block_row_idx, |
188 | 0 | const ColumnArray::Offsets64* array_offsets, int32_t& current_src_array_offset) const { |
189 | 0 | std::vector<std::string> data_tokens; |
190 | 0 | auto query_type = get_query_type_from_fn_name(); |
191 | 0 | if (array_offsets) { |
192 | 0 | for (auto next_src_array_offset = (*array_offsets)[current_block_row_idx]; |
193 | 0 | current_src_array_offset < next_src_array_offset; ++current_src_array_offset) { |
194 | 0 | const auto& str_ref = string_col->get_data_at(current_src_array_offset); |
195 | 0 | if (inverted_index_ctx->parser_type == InvertedIndexParserType::PARSER_NONE) { |
196 | 0 | data_tokens.emplace_back(str_ref.to_string()); |
197 | 0 | continue; |
198 | 0 | } |
199 | 0 | auto reader = doris::segment_v2::InvertedIndexReader::create_reader( |
200 | 0 | inverted_index_ctx, str_ref.to_string()); |
201 | |
|
202 | 0 | std::vector<std::string> element_tokens; |
203 | |
|
204 | 0 | doris::segment_v2::InvertedIndexReader::get_analyse_result( |
205 | 0 | element_tokens, reader.get(), inverted_index_ctx->analyzer, column_name, |
206 | 0 | query_type, false); |
207 | 0 | data_tokens.insert(data_tokens.end(), element_tokens.begin(), element_tokens.end()); |
208 | 0 | } |
209 | 0 | } else { |
210 | 0 | const auto& str_ref = string_col->get_data_at(current_block_row_idx); |
211 | 0 | if (inverted_index_ctx->parser_type == InvertedIndexParserType::PARSER_NONE) { |
212 | 0 | data_tokens.emplace_back(str_ref.to_string()); |
213 | 0 | } else { |
214 | 0 | auto reader = doris::segment_v2::InvertedIndexReader::create_reader( |
215 | 0 | inverted_index_ctx, str_ref.to_string()); |
216 | 0 | doris::segment_v2::InvertedIndexReader::get_analyse_result( |
217 | 0 | data_tokens, reader.get(), inverted_index_ctx->analyzer, column_name, |
218 | 0 | query_type, false); |
219 | 0 | } |
220 | 0 | } |
221 | 0 | return data_tokens; |
222 | 0 | } |
223 | | |
224 | 0 | Status FunctionMatchBase::check(FunctionContext* context, const std::string& function_name) const { |
225 | 0 | if (!context->state()->query_options().enable_match_without_inverted_index) { |
226 | 0 | return Status::Error<ErrorCode::INVERTED_INDEX_NOT_SUPPORTED>( |
227 | 0 | "{} not support execute_match", function_name); |
228 | 0 | } |
229 | | |
230 | 0 | DBUG_EXECUTE_IF("match.invert_index_not_support_execute_match", { |
231 | 0 | return Status::Error<ErrorCode::INVERTED_INDEX_NOT_SUPPORTED>( |
232 | 0 | "{} not support execute_match", function_name); |
233 | 0 | }); |
234 | |
|
235 | 0 | return Status::OK(); |
236 | 0 | } |
237 | | |
238 | | Status FunctionMatchAny::execute_match(FunctionContext* context, const std::string& column_name, |
239 | | const std::string& match_query_str, size_t input_rows_count, |
240 | | const ColumnString* string_col, |
241 | | InvertedIndexCtx* inverted_index_ctx, |
242 | | const ColumnArray::Offsets64* array_offsets, |
243 | 0 | ColumnUInt8::Container& result) const { |
244 | 0 | RETURN_IF_ERROR(check(context, name)); |
245 | | |
246 | 0 | std::vector<std::string> query_tokens; |
247 | 0 | analyse_query_str_token(&query_tokens, inverted_index_ctx, match_query_str, column_name); |
248 | 0 | if (query_tokens.empty()) { |
249 | 0 | VLOG_DEBUG << fmt::format( |
250 | 0 | "token parser result is empty for query, " |
251 | 0 | "please check your query: '{}' and index parser: '{}'", |
252 | 0 | match_query_str, |
253 | 0 | inverted_index_parser_type_to_string(inverted_index_ctx->parser_type)); |
254 | 0 | return Status::OK(); |
255 | 0 | } |
256 | | |
257 | 0 | auto current_src_array_offset = 0; |
258 | 0 | for (int i = 0; i < input_rows_count; i++) { |
259 | 0 | std::vector<std::string> data_tokens = |
260 | 0 | analyse_data_token(column_name, inverted_index_ctx, string_col, i, array_offsets, |
261 | 0 | current_src_array_offset); |
262 | | |
263 | | // TODO: more efficient impl |
264 | 0 | for (auto& token : query_tokens) { |
265 | 0 | auto it = std::find(data_tokens.begin(), data_tokens.end(), token); |
266 | 0 | if (it != data_tokens.end()) { |
267 | 0 | result[i] = true; |
268 | 0 | break; |
269 | 0 | } |
270 | 0 | } |
271 | 0 | } |
272 | |
|
273 | 0 | return Status::OK(); |
274 | 0 | } |
275 | | |
276 | | Status FunctionMatchAll::execute_match(FunctionContext* context, const std::string& column_name, |
277 | | const std::string& match_query_str, size_t input_rows_count, |
278 | | const ColumnString* string_col, |
279 | | InvertedIndexCtx* inverted_index_ctx, |
280 | | const ColumnArray::Offsets64* array_offsets, |
281 | 0 | ColumnUInt8::Container& result) const { |
282 | 0 | RETURN_IF_ERROR(check(context, name)); |
283 | | |
284 | 0 | std::vector<std::string> query_tokens; |
285 | 0 | analyse_query_str_token(&query_tokens, inverted_index_ctx, match_query_str, column_name); |
286 | 0 | if (query_tokens.empty()) { |
287 | 0 | VLOG_DEBUG << fmt::format( |
288 | 0 | "token parser result is empty for query, " |
289 | 0 | "please check your query: '{}' and index parser: '{}'", |
290 | 0 | match_query_str, |
291 | 0 | inverted_index_parser_type_to_string(inverted_index_ctx->parser_type)); |
292 | 0 | return Status::OK(); |
293 | 0 | } |
294 | | |
295 | 0 | auto current_src_array_offset = 0; |
296 | 0 | for (int i = 0; i < input_rows_count; i++) { |
297 | 0 | std::vector<std::string> data_tokens = |
298 | 0 | analyse_data_token(column_name, inverted_index_ctx, string_col, i, array_offsets, |
299 | 0 | current_src_array_offset); |
300 | | |
301 | | // TODO: more efficient impl |
302 | 0 | auto find_count = 0; |
303 | 0 | for (auto& token : query_tokens) { |
304 | 0 | auto it = std::find(data_tokens.begin(), data_tokens.end(), token); |
305 | 0 | if (it != data_tokens.end()) { |
306 | 0 | ++find_count; |
307 | 0 | } else { |
308 | 0 | break; |
309 | 0 | } |
310 | 0 | } |
311 | |
|
312 | 0 | if (find_count == query_tokens.size()) { |
313 | 0 | result[i] = true; |
314 | 0 | } |
315 | 0 | } |
316 | |
|
317 | 0 | return Status::OK(); |
318 | 0 | } |
319 | | |
320 | | Status FunctionMatchPhrase::execute_match(FunctionContext* context, const std::string& column_name, |
321 | | const std::string& match_query_str, |
322 | | size_t input_rows_count, const ColumnString* string_col, |
323 | | InvertedIndexCtx* inverted_index_ctx, |
324 | | const ColumnArray::Offsets64* array_offsets, |
325 | 0 | ColumnUInt8::Container& result) const { |
326 | 0 | RETURN_IF_ERROR(check(context, name)); |
327 | | |
328 | 0 | std::vector<std::string> query_tokens; |
329 | 0 | analyse_query_str_token(&query_tokens, inverted_index_ctx, match_query_str, column_name); |
330 | 0 | if (query_tokens.empty()) { |
331 | 0 | VLOG_DEBUG << fmt::format( |
332 | 0 | "token parser result is empty for query, " |
333 | 0 | "please check your query: '{}' and index parser: '{}'", |
334 | 0 | match_query_str, |
335 | 0 | inverted_index_parser_type_to_string(inverted_index_ctx->parser_type)); |
336 | 0 | return Status::OK(); |
337 | 0 | } |
338 | | |
339 | 0 | auto current_src_array_offset = 0; |
340 | 0 | for (int i = 0; i < input_rows_count; i++) { |
341 | 0 | std::vector<std::string> data_tokens = |
342 | 0 | analyse_data_token(column_name, inverted_index_ctx, string_col, i, array_offsets, |
343 | 0 | current_src_array_offset); |
344 | | |
345 | | // TODO: more efficient impl |
346 | 0 | bool matched = false; |
347 | 0 | auto data_it = data_tokens.begin(); |
348 | 0 | while (data_it != data_tokens.end()) { |
349 | | // find position of first token |
350 | 0 | data_it = std::find(data_it, data_tokens.end(), query_tokens[0]); |
351 | 0 | if (data_it != data_tokens.end()) { |
352 | 0 | matched = true; |
353 | 0 | auto data_it_next = ++data_it; |
354 | 0 | auto query_it = query_tokens.begin() + 1; |
355 | | // compare query_tokens after the first to data_tokens one by one |
356 | 0 | while (query_it != query_tokens.end()) { |
357 | 0 | if (data_it_next == data_tokens.end() || *data_it_next != *query_it) { |
358 | 0 | matched = false; |
359 | 0 | break; |
360 | 0 | } |
361 | 0 | query_it++; |
362 | 0 | data_it_next++; |
363 | 0 | } |
364 | |
|
365 | 0 | if (matched) { |
366 | 0 | break; |
367 | 0 | } |
368 | 0 | } |
369 | 0 | } |
370 | | |
371 | | // check matched |
372 | 0 | if (matched) { |
373 | 0 | result[i] = true; |
374 | 0 | } |
375 | 0 | } |
376 | |
|
377 | 0 | return Status::OK(); |
378 | 0 | } |
379 | | |
380 | | Status FunctionMatchPhrasePrefix::execute_match( |
381 | | FunctionContext* context, const std::string& column_name, |
382 | | const std::string& match_query_str, size_t input_rows_count, const ColumnString* string_col, |
383 | | InvertedIndexCtx* inverted_index_ctx, const ColumnArray::Offsets64* array_offsets, |
384 | 0 | ColumnUInt8::Container& result) const { |
385 | 0 | RETURN_IF_ERROR(check(context, name)); |
386 | | |
387 | 0 | std::vector<std::string> query_tokens; |
388 | 0 | analyse_query_str_token(&query_tokens, inverted_index_ctx, match_query_str, column_name); |
389 | 0 | if (query_tokens.empty()) { |
390 | 0 | VLOG_DEBUG << fmt::format( |
391 | 0 | "token parser result is empty for query, " |
392 | 0 | "please check your query: '{}' and index parser: '{}'", |
393 | 0 | match_query_str, |
394 | 0 | inverted_index_parser_type_to_string(inverted_index_ctx->parser_type)); |
395 | 0 | return Status::OK(); |
396 | 0 | } |
397 | | |
398 | 0 | int32_t current_src_array_offset = 0; |
399 | 0 | for (size_t i = 0; i < input_rows_count; i++) { |
400 | 0 | std::vector<std::string> data_tokens = |
401 | 0 | analyse_data_token(column_name, inverted_index_ctx, string_col, i, array_offsets, |
402 | 0 | current_src_array_offset); |
403 | |
|
404 | 0 | int32_t dis_count = data_tokens.size() - query_tokens.size(); |
405 | 0 | if (dis_count < 0) { |
406 | 0 | continue; |
407 | 0 | } |
408 | | |
409 | 0 | for (size_t j = 0; j < dis_count + 1; j++) { |
410 | 0 | if (data_tokens[j] == query_tokens[0] || query_tokens.size() == 1) { |
411 | 0 | bool match = true; |
412 | 0 | for (size_t k = 0; k < query_tokens.size(); k++) { |
413 | 0 | const std::string& data_token = data_tokens[j + k]; |
414 | 0 | const std::string& query_token = query_tokens[k]; |
415 | 0 | if (k == query_tokens.size() - 1) { |
416 | 0 | if (data_token.compare(0, query_token.size(), query_token) != 0) { |
417 | 0 | match = false; |
418 | 0 | break; |
419 | 0 | } |
420 | 0 | } else { |
421 | 0 | if (data_token != query_token) { |
422 | 0 | match = false; |
423 | 0 | break; |
424 | 0 | } |
425 | 0 | } |
426 | 0 | } |
427 | 0 | if (match) { |
428 | 0 | result[i] = true; |
429 | 0 | break; |
430 | 0 | } |
431 | 0 | } |
432 | 0 | } |
433 | 0 | } |
434 | |
|
435 | 0 | return Status::OK(); |
436 | 0 | } |
437 | | |
438 | | Status FunctionMatchRegexp::execute_match(FunctionContext* context, const std::string& column_name, |
439 | | const std::string& match_query_str, |
440 | | size_t input_rows_count, const ColumnString* string_col, |
441 | | InvertedIndexCtx* inverted_index_ctx, |
442 | | const ColumnArray::Offsets64* array_offsets, |
443 | 0 | ColumnUInt8::Container& result) const { |
444 | 0 | RETURN_IF_ERROR(check(context, name)); |
445 | | |
446 | 0 | VLOG_DEBUG << "begin to run FunctionMatchRegexp::execute_match, parser_type: " |
447 | 0 | << inverted_index_parser_type_to_string(inverted_index_ctx->parser_type); |
448 | |
|
449 | 0 | const std::string& pattern = match_query_str; |
450 | |
|
451 | 0 | hs_database_t* database = nullptr; |
452 | 0 | hs_compile_error_t* compile_err = nullptr; |
453 | 0 | hs_scratch_t* scratch = nullptr; |
454 | |
|
455 | 0 | if (hs_compile(pattern.data(), HS_FLAG_DOTALL | HS_FLAG_ALLOWEMPTY | HS_FLAG_UTF8, |
456 | 0 | HS_MODE_BLOCK, nullptr, &database, &compile_err) != HS_SUCCESS) { |
457 | 0 | std::string err_message = "hyperscan compilation failed: "; |
458 | 0 | err_message.append(compile_err->message); |
459 | 0 | LOG(ERROR) << err_message; |
460 | 0 | hs_free_compile_error(compile_err); |
461 | 0 | return Status::Error<ErrorCode::INVERTED_INDEX_INVALID_PARAMETERS>(err_message); |
462 | 0 | } |
463 | | |
464 | 0 | if (hs_alloc_scratch(database, &scratch) != HS_SUCCESS) { |
465 | 0 | LOG(ERROR) << "hyperscan could not allocate scratch space."; |
466 | 0 | hs_free_database(database); |
467 | 0 | return Status::Error<ErrorCode::INVERTED_INDEX_INVALID_PARAMETERS>( |
468 | 0 | "hyperscan could not allocate scratch space."); |
469 | 0 | } |
470 | | |
471 | 0 | auto on_match = [](unsigned int id, unsigned long long from, unsigned long long to, |
472 | 0 | unsigned int flags, void* context) -> int { |
473 | 0 | *((bool*)context) = true; |
474 | 0 | return 0; |
475 | 0 | }; |
476 | |
|
477 | 0 | try { |
478 | 0 | auto current_src_array_offset = 0; |
479 | 0 | for (int i = 0; i < input_rows_count; i++) { |
480 | 0 | std::vector<std::string> data_tokens = |
481 | 0 | analyse_data_token(column_name, inverted_index_ctx, string_col, i, |
482 | 0 | array_offsets, current_src_array_offset); |
483 | |
|
484 | 0 | for (auto& input : data_tokens) { |
485 | 0 | bool is_match = false; |
486 | 0 | if (hs_scan(database, input.data(), input.size(), 0, scratch, on_match, |
487 | 0 | (void*)&is_match) != HS_SUCCESS) { |
488 | 0 | LOG(ERROR) << "hyperscan match failed: " << input; |
489 | 0 | break; |
490 | 0 | } |
491 | | |
492 | 0 | if (is_match) { |
493 | 0 | result[i] = true; |
494 | 0 | break; |
495 | 0 | } |
496 | 0 | } |
497 | 0 | } |
498 | 0 | } |
499 | 0 | _CLFINALLY({ |
500 | 0 | hs_free_scratch(scratch); |
501 | 0 | hs_free_database(database); |
502 | 0 | }) |
503 | |
|
504 | 0 | return Status::OK(); |
505 | 0 | } |
506 | | |
507 | | Status FunctionMatchPhraseEdge::execute_match( |
508 | | FunctionContext* context, const std::string& column_name, |
509 | | const std::string& match_query_str, size_t input_rows_count, const ColumnString* string_col, |
510 | | InvertedIndexCtx* inverted_index_ctx, const ColumnArray::Offsets64* array_offsets, |
511 | 0 | ColumnUInt8::Container& result) const { |
512 | 0 | RETURN_IF_ERROR(check(context, name)); |
513 | | |
514 | 0 | std::vector<std::string> query_tokens; |
515 | 0 | analyse_query_str_token(&query_tokens, inverted_index_ctx, match_query_str, column_name); |
516 | 0 | if (query_tokens.empty()) { |
517 | 0 | VLOG_DEBUG << fmt::format( |
518 | 0 | "token parser result is empty for query, " |
519 | 0 | "please check your query: '{}' and index parser: '{}'", |
520 | 0 | match_query_str, |
521 | 0 | inverted_index_parser_type_to_string(inverted_index_ctx->parser_type)); |
522 | 0 | return Status::OK(); |
523 | 0 | } |
524 | | |
525 | 0 | int32_t current_src_array_offset = 0; |
526 | 0 | for (size_t i = 0; i < input_rows_count; i++) { |
527 | 0 | auto data_tokens = analyse_data_token(column_name, inverted_index_ctx, string_col, i, |
528 | 0 | array_offsets, current_src_array_offset); |
529 | |
|
530 | 0 | int32_t dis_count = data_tokens.size() - query_tokens.size(); |
531 | 0 | if (dis_count < 0) { |
532 | 0 | continue; |
533 | 0 | } |
534 | | |
535 | 0 | for (size_t j = 0; j < dis_count + 1; j++) { |
536 | 0 | bool match = true; |
537 | 0 | if (query_tokens.size() == 1) { |
538 | 0 | if (data_tokens[j].find(query_tokens[0]) == std::string::npos) { |
539 | 0 | match = false; |
540 | 0 | } |
541 | 0 | } else { |
542 | 0 | for (size_t k = 0; k < query_tokens.size(); k++) { |
543 | 0 | const std::string& data_token = data_tokens[j + k]; |
544 | 0 | const std::string& query_token = query_tokens[k]; |
545 | 0 | if (k == 0) { |
546 | 0 | if (!data_token.ends_with(query_token)) { |
547 | 0 | match = false; |
548 | 0 | break; |
549 | 0 | } |
550 | 0 | } else if (k == query_tokens.size() - 1) { |
551 | 0 | if (!data_token.starts_with(query_token)) { |
552 | 0 | match = false; |
553 | 0 | break; |
554 | 0 | } |
555 | 0 | } else { |
556 | 0 | if (data_token != query_token) { |
557 | 0 | match = false; |
558 | 0 | break; |
559 | 0 | } |
560 | 0 | } |
561 | 0 | } |
562 | 0 | } |
563 | 0 | if (match) { |
564 | 0 | result[i] = true; |
565 | 0 | break; |
566 | 0 | } |
567 | 0 | } |
568 | 0 | } |
569 | |
|
570 | 0 | return Status::OK(); |
571 | 0 | } |
572 | | |
573 | 1 | void register_function_match(SimpleFunctionFactory& factory) { |
574 | 1 | factory.register_function<FunctionMatchAny>(); |
575 | 1 | factory.register_function<FunctionMatchAll>(); |
576 | 1 | factory.register_function<FunctionMatchPhrase>(); |
577 | 1 | factory.register_function<FunctionMatchPhrasePrefix>(); |
578 | 1 | factory.register_function<FunctionMatchRegexp>(); |
579 | 1 | factory.register_function<FunctionMatchPhraseEdge>(); |
580 | 1 | } |
581 | | |
582 | | } // namespace doris::vectorized |