Coverage Report

Created: 2024-11-20 10:55

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