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