/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, uint32_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 (is_column<vectorized::ColumnArray>(source_col.get())) { |
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(fmt::format( |
113 | 0 | "unsupported nested array of type {} for function {}", |
114 | 0 | is_column_nullable(array_col->get_data()) ? array_col->get_data().get_name() |
115 | 0 | : array_col->get_data().get_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 | 1 | const { |
151 | 1 | std::string fn_name = get_name(); |
152 | 1 | if (fn_name == MATCH_ANY_FUNCTION) { |
153 | 0 | return doris::segment_v2::InvertedIndexQueryType::MATCH_ANY_QUERY; |
154 | 1 | } else if (fn_name == MATCH_ALL_FUNCTION) { |
155 | 0 | return doris::segment_v2::InvertedIndexQueryType::MATCH_ALL_QUERY; |
156 | 1 | } else if (fn_name == MATCH_PHRASE_FUNCTION) { |
157 | 1 | return doris::segment_v2::InvertedIndexQueryType::MATCH_PHRASE_QUERY; |
158 | 1 | } 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 | 1 | } |
167 | | |
168 | | std::vector<std::string> FunctionMatchBase::analyse_query_str_token( |
169 | | InvertedIndexCtx* inverted_index_ctx, const std::string& match_query_str, |
170 | 3 | const std::string& column_name) const { |
171 | 3 | VLOG_DEBUG << "begin to run " << get_name() << ", parser_type: " |
172 | 0 | << inverted_index_parser_type_to_string(inverted_index_ctx->parser_type); |
173 | 3 | std::vector<std::string> query_tokens; |
174 | 3 | if (inverted_index_ctx == nullptr) { |
175 | 1 | return query_tokens; |
176 | 1 | } |
177 | 2 | if (inverted_index_ctx->parser_type == InvertedIndexParserType::PARSER_NONE) { |
178 | 1 | query_tokens.emplace_back(match_query_str); |
179 | 1 | return query_tokens; |
180 | 1 | } |
181 | 1 | auto reader = doris::segment_v2::inverted_index::InvertedIndexAnalyzer::create_reader( |
182 | 1 | inverted_index_ctx->char_filter_map); |
183 | 1 | reader->init(match_query_str.data(), match_query_str.size(), true); |
184 | 1 | query_tokens = doris::segment_v2::inverted_index::InvertedIndexAnalyzer::get_analyse_result( |
185 | 1 | reader.get(), inverted_index_ctx->analyzer, column_name, get_query_type_from_fn_name()); |
186 | 1 | return query_tokens; |
187 | 2 | } |
188 | | |
189 | | inline std::vector<std::string> FunctionMatchBase::analyse_data_token( |
190 | | const std::string& column_name, InvertedIndexCtx* inverted_index_ctx, |
191 | | const ColumnString* string_col, int32_t current_block_row_idx, |
192 | 0 | const ColumnArray::Offsets64* array_offsets, int32_t& current_src_array_offset) const { |
193 | 0 | std::vector<std::string> data_tokens; |
194 | 0 | auto query_type = get_query_type_from_fn_name(); |
195 | 0 | if (array_offsets) { |
196 | 0 | for (auto next_src_array_offset = (*array_offsets)[current_block_row_idx]; |
197 | 0 | current_src_array_offset < next_src_array_offset; ++current_src_array_offset) { |
198 | 0 | const auto& str_ref = string_col->get_data_at(current_src_array_offset); |
199 | 0 | if (inverted_index_ctx->parser_type == InvertedIndexParserType::PARSER_NONE) { |
200 | 0 | data_tokens.emplace_back(str_ref.to_string()); |
201 | 0 | continue; |
202 | 0 | } |
203 | 0 | auto reader = doris::segment_v2::inverted_index::InvertedIndexAnalyzer::create_reader( |
204 | 0 | inverted_index_ctx->char_filter_map); |
205 | 0 | reader->init(str_ref.data, str_ref.size, true); |
206 | |
|
207 | 0 | std::vector<std::string> element_tokens = |
208 | 0 | doris::segment_v2::inverted_index::InvertedIndexAnalyzer::get_analyse_result( |
209 | 0 | reader.get(), inverted_index_ctx->analyzer, column_name, query_type, |
210 | 0 | false); |
211 | 0 | data_tokens.insert(data_tokens.end(), element_tokens.begin(), element_tokens.end()); |
212 | 0 | } |
213 | 0 | } else { |
214 | 0 | const auto& str_ref = string_col->get_data_at(current_block_row_idx); |
215 | 0 | if (inverted_index_ctx->parser_type == InvertedIndexParserType::PARSER_NONE) { |
216 | 0 | data_tokens.emplace_back(str_ref.to_string()); |
217 | 0 | } else { |
218 | 0 | auto reader = doris::segment_v2::inverted_index::InvertedIndexAnalyzer::create_reader( |
219 | 0 | inverted_index_ctx->char_filter_map); |
220 | 0 | reader->init(str_ref.data, str_ref.size, true); |
221 | 0 | data_tokens = |
222 | 0 | doris::segment_v2::inverted_index::InvertedIndexAnalyzer::get_analyse_result( |
223 | 0 | reader.get(), inverted_index_ctx->analyzer, column_name, query_type, |
224 | 0 | false); |
225 | 0 | } |
226 | 0 | } |
227 | 0 | return data_tokens; |
228 | 0 | } |
229 | | |
230 | 0 | Status FunctionMatchBase::check(FunctionContext* context, const std::string& function_name) const { |
231 | 0 | if (!context->state()->query_options().enable_match_without_inverted_index) { |
232 | 0 | return Status::Error<ErrorCode::INVERTED_INDEX_NOT_SUPPORTED>( |
233 | 0 | "{} not support execute_match", function_name); |
234 | 0 | } |
235 | | |
236 | 0 | DBUG_EXECUTE_IF("match.invert_index_not_support_execute_match", { |
237 | 0 | return Status::Error<ErrorCode::INVERTED_INDEX_NOT_SUPPORTED>( |
238 | 0 | "{} not support execute_match", function_name); |
239 | 0 | }); |
240 | |
|
241 | 0 | return Status::OK(); |
242 | 0 | } |
243 | | |
244 | | Status FunctionMatchAny::execute_match(FunctionContext* context, const std::string& column_name, |
245 | | const std::string& match_query_str, size_t input_rows_count, |
246 | | const ColumnString* string_col, |
247 | | InvertedIndexCtx* inverted_index_ctx, |
248 | | const ColumnArray::Offsets64* array_offsets, |
249 | 0 | ColumnUInt8::Container& result) const { |
250 | 0 | RETURN_IF_ERROR(check(context, name)); |
251 | | |
252 | 0 | std::vector<std::string> query_tokens = |
253 | 0 | analyse_query_str_token(inverted_index_ctx, match_query_str, column_name); |
254 | 0 | if (query_tokens.empty()) { |
255 | 0 | VLOG_DEBUG << fmt::format( |
256 | 0 | "token parser result is empty for query, " |
257 | 0 | "please check your query: '{}' and index parser: '{}'", |
258 | 0 | match_query_str, |
259 | 0 | inverted_index_parser_type_to_string(inverted_index_ctx->parser_type)); |
260 | 0 | return Status::OK(); |
261 | 0 | } |
262 | | |
263 | 0 | auto current_src_array_offset = 0; |
264 | 0 | for (int i = 0; i < input_rows_count; i++) { |
265 | 0 | std::vector<std::string> data_tokens = |
266 | 0 | analyse_data_token(column_name, inverted_index_ctx, string_col, i, array_offsets, |
267 | 0 | current_src_array_offset); |
268 | | |
269 | | // TODO: more efficient impl |
270 | 0 | for (auto& token : query_tokens) { |
271 | 0 | auto it = std::find(data_tokens.begin(), data_tokens.end(), token); |
272 | 0 | if (it != data_tokens.end()) { |
273 | 0 | result[i] = true; |
274 | 0 | break; |
275 | 0 | } |
276 | 0 | } |
277 | 0 | } |
278 | |
|
279 | 0 | return Status::OK(); |
280 | 0 | } |
281 | | |
282 | | Status FunctionMatchAll::execute_match(FunctionContext* context, const std::string& column_name, |
283 | | const std::string& match_query_str, size_t input_rows_count, |
284 | | const ColumnString* string_col, |
285 | | InvertedIndexCtx* inverted_index_ctx, |
286 | | const ColumnArray::Offsets64* array_offsets, |
287 | 0 | ColumnUInt8::Container& result) const { |
288 | 0 | RETURN_IF_ERROR(check(context, name)); |
289 | | |
290 | 0 | std::vector<std::string> query_tokens = |
291 | 0 | analyse_query_str_token(inverted_index_ctx, match_query_str, column_name); |
292 | 0 | if (query_tokens.empty()) { |
293 | 0 | VLOG_DEBUG << fmt::format( |
294 | 0 | "token parser result is empty for query, " |
295 | 0 | "please check your query: '{}' and index parser: '{}'", |
296 | 0 | match_query_str, |
297 | 0 | inverted_index_parser_type_to_string(inverted_index_ctx->parser_type)); |
298 | 0 | return Status::OK(); |
299 | 0 | } |
300 | | |
301 | 0 | auto current_src_array_offset = 0; |
302 | 0 | for (int i = 0; i < input_rows_count; i++) { |
303 | 0 | std::vector<std::string> data_tokens = |
304 | 0 | analyse_data_token(column_name, inverted_index_ctx, string_col, i, array_offsets, |
305 | 0 | current_src_array_offset); |
306 | | |
307 | | // TODO: more efficient impl |
308 | 0 | auto find_count = 0; |
309 | 0 | for (auto& token : query_tokens) { |
310 | 0 | auto it = std::find(data_tokens.begin(), data_tokens.end(), token); |
311 | 0 | if (it != data_tokens.end()) { |
312 | 0 | ++find_count; |
313 | 0 | } else { |
314 | 0 | break; |
315 | 0 | } |
316 | 0 | } |
317 | |
|
318 | 0 | if (find_count == query_tokens.size()) { |
319 | 0 | result[i] = true; |
320 | 0 | } |
321 | 0 | } |
322 | |
|
323 | 0 | return Status::OK(); |
324 | 0 | } |
325 | | |
326 | | Status FunctionMatchPhrase::execute_match(FunctionContext* context, const std::string& column_name, |
327 | | const std::string& match_query_str, |
328 | | size_t input_rows_count, const ColumnString* string_col, |
329 | | InvertedIndexCtx* inverted_index_ctx, |
330 | | const ColumnArray::Offsets64* array_offsets, |
331 | 0 | ColumnUInt8::Container& result) const { |
332 | 0 | RETURN_IF_ERROR(check(context, name)); |
333 | | |
334 | 0 | std::vector<std::string> query_tokens = |
335 | 0 | analyse_query_str_token(inverted_index_ctx, match_query_str, column_name); |
336 | 0 | if (query_tokens.empty()) { |
337 | 0 | VLOG_DEBUG << fmt::format( |
338 | 0 | "token parser result is empty for query, " |
339 | 0 | "please check your query: '{}' and index parser: '{}'", |
340 | 0 | match_query_str, |
341 | 0 | inverted_index_parser_type_to_string(inverted_index_ctx->parser_type)); |
342 | 0 | return Status::OK(); |
343 | 0 | } |
344 | | |
345 | 0 | auto current_src_array_offset = 0; |
346 | 0 | for (int i = 0; i < input_rows_count; i++) { |
347 | 0 | std::vector<std::string> data_tokens = |
348 | 0 | analyse_data_token(column_name, inverted_index_ctx, string_col, i, array_offsets, |
349 | 0 | current_src_array_offset); |
350 | | |
351 | | // TODO: more efficient impl |
352 | 0 | bool matched = false; |
353 | 0 | auto data_it = data_tokens.begin(); |
354 | 0 | while (data_it != data_tokens.end()) { |
355 | | // find position of first token |
356 | 0 | data_it = std::find(data_it, data_tokens.end(), query_tokens[0]); |
357 | 0 | if (data_it != data_tokens.end()) { |
358 | 0 | matched = true; |
359 | 0 | auto data_it_next = ++data_it; |
360 | 0 | auto query_it = query_tokens.begin() + 1; |
361 | | // compare query_tokens after the first to data_tokens one by one |
362 | 0 | while (query_it != query_tokens.end()) { |
363 | 0 | if (data_it_next == data_tokens.end() || *data_it_next != *query_it) { |
364 | 0 | matched = false; |
365 | 0 | break; |
366 | 0 | } |
367 | 0 | query_it++; |
368 | 0 | data_it_next++; |
369 | 0 | } |
370 | |
|
371 | 0 | if (matched) { |
372 | 0 | break; |
373 | 0 | } |
374 | 0 | } |
375 | 0 | } |
376 | | |
377 | | // check matched |
378 | 0 | if (matched) { |
379 | 0 | result[i] = true; |
380 | 0 | } |
381 | 0 | } |
382 | |
|
383 | 0 | return Status::OK(); |
384 | 0 | } |
385 | | |
386 | | Status FunctionMatchPhrasePrefix::execute_match( |
387 | | FunctionContext* context, const std::string& column_name, |
388 | | const std::string& match_query_str, size_t input_rows_count, const ColumnString* string_col, |
389 | | InvertedIndexCtx* inverted_index_ctx, const ColumnArray::Offsets64* array_offsets, |
390 | 0 | ColumnUInt8::Container& result) const { |
391 | 0 | RETURN_IF_ERROR(check(context, name)); |
392 | | |
393 | 0 | std::vector<std::string> query_tokens = |
394 | 0 | analyse_query_str_token(inverted_index_ctx, match_query_str, column_name); |
395 | 0 | if (query_tokens.empty()) { |
396 | 0 | VLOG_DEBUG << fmt::format( |
397 | 0 | "token parser result is empty for query, " |
398 | 0 | "please check your query: '{}' and index parser: '{}'", |
399 | 0 | match_query_str, |
400 | 0 | inverted_index_parser_type_to_string(inverted_index_ctx->parser_type)); |
401 | 0 | return Status::OK(); |
402 | 0 | } |
403 | | |
404 | 0 | int32_t current_src_array_offset = 0; |
405 | 0 | for (size_t i = 0; i < input_rows_count; i++) { |
406 | 0 | auto data_tokens = analyse_data_token(column_name, inverted_index_ctx, string_col, i, |
407 | 0 | array_offsets, current_src_array_offset); |
408 | |
|
409 | 0 | int32_t dis_count = data_tokens.size() - query_tokens.size(); |
410 | 0 | if (dis_count < 0) { |
411 | 0 | continue; |
412 | 0 | } |
413 | | |
414 | 0 | for (size_t j = 0; j < dis_count + 1; j++) { |
415 | 0 | if (data_tokens[j] == query_tokens[0] || query_tokens.size() == 1) { |
416 | 0 | bool match = true; |
417 | 0 | for (size_t k = 0; k < query_tokens.size(); k++) { |
418 | 0 | const std::string& data_token = data_tokens[j + k]; |
419 | 0 | const std::string& query_token = query_tokens[k]; |
420 | 0 | if (k == query_tokens.size() - 1) { |
421 | 0 | if (data_token.compare(0, query_token.size(), query_token) != 0) { |
422 | 0 | match = false; |
423 | 0 | break; |
424 | 0 | } |
425 | 0 | } else { |
426 | 0 | if (data_token != query_token) { |
427 | 0 | match = false; |
428 | 0 | break; |
429 | 0 | } |
430 | 0 | } |
431 | 0 | } |
432 | 0 | if (match) { |
433 | 0 | result[i] = true; |
434 | 0 | break; |
435 | 0 | } |
436 | 0 | } |
437 | 0 | } |
438 | 0 | } |
439 | |
|
440 | 0 | return Status::OK(); |
441 | 0 | } |
442 | | |
443 | | Status FunctionMatchRegexp::execute_match(FunctionContext* context, const std::string& column_name, |
444 | | const std::string& match_query_str, |
445 | | size_t input_rows_count, const ColumnString* string_col, |
446 | | InvertedIndexCtx* inverted_index_ctx, |
447 | | const ColumnArray::Offsets64* array_offsets, |
448 | 0 | ColumnUInt8::Container& result) const { |
449 | 0 | RETURN_IF_ERROR(check(context, name)); |
450 | | |
451 | 0 | VLOG_DEBUG << "begin to run FunctionMatchRegexp::execute_match, parser_type: " |
452 | 0 | << inverted_index_parser_type_to_string(inverted_index_ctx->parser_type); |
453 | |
|
454 | 0 | const std::string& pattern = match_query_str; |
455 | |
|
456 | 0 | hs_database_t* database = nullptr; |
457 | 0 | hs_compile_error_t* compile_err = nullptr; |
458 | 0 | hs_scratch_t* scratch = nullptr; |
459 | |
|
460 | 0 | if (hs_compile(pattern.data(), HS_FLAG_DOTALL | HS_FLAG_ALLOWEMPTY | HS_FLAG_UTF8, |
461 | 0 | HS_MODE_BLOCK, nullptr, &database, &compile_err) != HS_SUCCESS) { |
462 | 0 | std::string err_message = "hyperscan compilation failed: "; |
463 | 0 | err_message.append(compile_err->message); |
464 | 0 | LOG(ERROR) << err_message; |
465 | 0 | hs_free_compile_error(compile_err); |
466 | 0 | return Status::Error<ErrorCode::INVERTED_INDEX_INVALID_PARAMETERS>(err_message); |
467 | 0 | } |
468 | | |
469 | 0 | if (hs_alloc_scratch(database, &scratch) != HS_SUCCESS) { |
470 | 0 | LOG(ERROR) << "hyperscan could not allocate scratch space."; |
471 | 0 | hs_free_database(database); |
472 | 0 | return Status::Error<ErrorCode::INVERTED_INDEX_INVALID_PARAMETERS>( |
473 | 0 | "hyperscan could not allocate scratch space."); |
474 | 0 | } |
475 | | |
476 | 0 | auto on_match = [](unsigned int id, unsigned long long from, unsigned long long to, |
477 | 0 | unsigned int flags, void* context) -> int { |
478 | 0 | *((bool*)context) = true; |
479 | 0 | return 0; |
480 | 0 | }; |
481 | |
|
482 | 0 | try { |
483 | 0 | auto current_src_array_offset = 0; |
484 | 0 | for (int i = 0; i < input_rows_count; i++) { |
485 | 0 | std::vector<std::string> data_tokens = |
486 | 0 | analyse_data_token(column_name, inverted_index_ctx, string_col, i, |
487 | 0 | array_offsets, current_src_array_offset); |
488 | |
|
489 | 0 | for (auto& input : data_tokens) { |
490 | 0 | bool is_match = false; |
491 | 0 | if (hs_scan(database, input.data(), input.size(), 0, scratch, on_match, |
492 | 0 | (void*)&is_match) != HS_SUCCESS) { |
493 | 0 | LOG(ERROR) << "hyperscan match failed: " << input; |
494 | 0 | break; |
495 | 0 | } |
496 | | |
497 | 0 | if (is_match) { |
498 | 0 | result[i] = true; |
499 | 0 | break; |
500 | 0 | } |
501 | 0 | } |
502 | 0 | } |
503 | 0 | } |
504 | 0 | _CLFINALLY({ |
505 | 0 | hs_free_scratch(scratch); |
506 | 0 | hs_free_database(database); |
507 | 0 | }) |
508 | |
|
509 | 0 | return Status::OK(); |
510 | 0 | } |
511 | | |
512 | | Status FunctionMatchPhraseEdge::execute_match( |
513 | | FunctionContext* context, const std::string& column_name, |
514 | | const std::string& match_query_str, size_t input_rows_count, const ColumnString* string_col, |
515 | | InvertedIndexCtx* inverted_index_ctx, const ColumnArray::Offsets64* array_offsets, |
516 | 0 | ColumnUInt8::Container& result) const { |
517 | 0 | RETURN_IF_ERROR(check(context, name)); |
518 | | |
519 | 0 | std::vector<std::string> query_tokens = |
520 | 0 | analyse_query_str_token(inverted_index_ctx, match_query_str, column_name); |
521 | 0 | if (query_tokens.empty()) { |
522 | 0 | VLOG_DEBUG << fmt::format( |
523 | 0 | "token parser result is empty for query, " |
524 | 0 | "please check your query: '{}' and index parser: '{}'", |
525 | 0 | match_query_str, |
526 | 0 | inverted_index_parser_type_to_string(inverted_index_ctx->parser_type)); |
527 | 0 | return Status::OK(); |
528 | 0 | } |
529 | | |
530 | 0 | int32_t current_src_array_offset = 0; |
531 | 0 | for (size_t i = 0; i < input_rows_count; i++) { |
532 | 0 | auto data_tokens = analyse_data_token(column_name, inverted_index_ctx, string_col, i, |
533 | 0 | array_offsets, current_src_array_offset); |
534 | |
|
535 | 0 | int32_t dis_count = data_tokens.size() - query_tokens.size(); |
536 | 0 | if (dis_count < 0) { |
537 | 0 | continue; |
538 | 0 | } |
539 | | |
540 | 0 | for (size_t j = 0; j < dis_count + 1; j++) { |
541 | 0 | bool match = true; |
542 | 0 | if (query_tokens.size() == 1) { |
543 | 0 | if (data_tokens[j].find(query_tokens[0]) == std::string::npos) { |
544 | 0 | match = false; |
545 | 0 | } |
546 | 0 | } else { |
547 | 0 | for (size_t k = 0; k < query_tokens.size(); k++) { |
548 | 0 | const std::string& data_token = data_tokens[j + k]; |
549 | 0 | const std::string& query_token = query_tokens[k]; |
550 | 0 | if (k == 0) { |
551 | 0 | if (!data_token.ends_with(query_token)) { |
552 | 0 | match = false; |
553 | 0 | break; |
554 | 0 | } |
555 | 0 | } else if (k == query_tokens.size() - 1) { |
556 | 0 | if (!data_token.starts_with(query_token)) { |
557 | 0 | match = false; |
558 | 0 | break; |
559 | 0 | } |
560 | 0 | } else { |
561 | 0 | if (data_token != query_token) { |
562 | 0 | match = false; |
563 | 0 | break; |
564 | 0 | } |
565 | 0 | } |
566 | 0 | } |
567 | 0 | } |
568 | 0 | if (match) { |
569 | 0 | result[i] = true; |
570 | 0 | break; |
571 | 0 | } |
572 | 0 | } |
573 | 0 | } |
574 | |
|
575 | 0 | return Status::OK(); |
576 | 0 | } |
577 | | |
578 | 1 | void register_function_match(SimpleFunctionFactory& factory) { |
579 | 1 | factory.register_function<FunctionMatchAny>(); |
580 | 1 | factory.register_function<FunctionMatchAll>(); |
581 | 1 | factory.register_function<FunctionMatchPhrase>(); |
582 | 1 | factory.register_function<FunctionMatchPhrasePrefix>(); |
583 | 1 | factory.register_function<FunctionMatchRegexp>(); |
584 | 1 | factory.register_function<FunctionMatchPhraseEdge>(); |
585 | 1 | } |
586 | | |
587 | | } // namespace doris::vectorized |