Coverage Report

Created: 2025-12-27 02:48

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