Coverage Report

Created: 2025-07-23 20:08

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