Coverage Report

Created: 2026-02-02 19:44

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