Coverage Report

Created: 2025-12-30 21:03

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