Coverage Report

Created: 2026-07-06 17:43

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/exprs/function/match.h
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
#pragma once
19
20
#include <stddef.h>
21
22
#include <algorithm>
23
#include <boost/iterator/iterator_facade.hpp>
24
#include <memory>
25
#include <ostream>
26
#include <string>
27
#include <utility>
28
29
#include "common/config.h"
30
#include "common/consts.h"
31
#include "common/logging.h"
32
#include "common/status.h"
33
#include "core/block/block.h"
34
#include "core/block/column_numbers.h"
35
#include "core/block/column_with_type_and_name.h"
36
#include "core/column/column.h"
37
#include "core/column/column_array.h"
38
#include "core/data_type/data_type_number.h"
39
#include "core/types.h"
40
#include "exprs/aggregate/aggregate_function.h"
41
#include "exprs/function/function.h"
42
#include "exprs/function/simple_function_factory.h"
43
#include "exprs/vmatch_predicate.h"
44
#include "storage/index/inverted/inverted_index_parser.h"
45
#include "storage/index/inverted/inverted_index_reader.h"
46
#include "storage/index/inverted/query/query_info.h"
47
48
namespace doris {
49
class FunctionContext;
50
} // namespace doris
51
52
namespace doris {
53
54
const std::string MATCH_ANY_FUNCTION = "match_any";
55
const std::string MATCH_ALL_FUNCTION = "match_all";
56
const std::string MATCH_PHRASE_FUNCTION = "match_phrase";
57
const std::string MATCH_PHRASE_PREFIX_FUNCTION = "match_phrase_prefix";
58
const std::string MATCH_PHRASE_REGEXP_FUNCTION = "match_regexp";
59
const std::string MATCH_PHRASE_EDGE_FUNCTION = "match_phrase_edge";
60
61
class FunctionMatchBase : public IFunction {
62
public:
63
0
    size_t get_number_of_arguments() const override { return 2; }
64
65
0
    String get_name() const override { return "match"; }
66
67
    /// Get result types by argument types. If the function does not apply to these arguments, throw an exception.
68
0
    DataTypePtr get_return_type_impl(const DataTypes& arguments) const override {
69
0
        return std::make_shared<DataTypeUInt8>();
70
0
    }
71
72
    Status execute_impl(FunctionContext* context, Block& block, const ColumnNumbers& arguments,
73
                        uint32_t result, size_t input_rows_count) const override;
74
75
    virtual Status execute_match(FunctionContext* context, const std::string& column_name,
76
                                 const std::string& match_query_str, size_t input_rows_count,
77
                                 const ColumnString* string_col,
78
                                 const InvertedIndexAnalyzerCtx* analyzer_ctx,
79
                                 const ColumnArray::Offsets64* array_offsets,
80
                                 ColumnUInt8::Container& result) const = 0;
81
82
    doris::segment_v2::InvertedIndexQueryType get_query_type_from_fn_name() const;
83
84
    std::vector<segment_v2::TermInfo> analyse_query_str_token(
85
            const InvertedIndexAnalyzerCtx* analyzer_ctx, const std::string& match_query_str,
86
            const std::string& field_name) const;
87
88
    std::vector<segment_v2::TermInfo> analyse_data_token(
89
            const std::string& column_name, const InvertedIndexAnalyzerCtx* analyzer_ctx,
90
            const ColumnString* string_col, int32_t current_block_row_idx,
91
            const ColumnArray::Offsets64* array_offsets, int32_t& current_src_array_offset) const;
92
93
    Status check(FunctionContext* context, const std::string& function_name) const;
94
95
    Status evaluate_inverted_index(
96
            const ColumnsWithTypeAndName& arguments,
97
            const std::vector<IndexFieldNameAndTypePair>& data_type_with_names,
98
            std::vector<segment_v2::IndexIterator*> iterators, uint32_t num_rows,
99
            const InvertedIndexAnalyzerCtx* analyzer_ctx,
100
            segment_v2::InvertedIndexResultBitmap& bitmap_result) const override;
101
};
102
103
class FunctionMatchAny : public FunctionMatchBase {
104
public:
105
    static constexpr auto name = "match_any";
106
2
    static FunctionPtr create() { return std::make_shared<FunctionMatchAny>(); }
107
108
4
    String get_name() const override { return name; }
109
110
    Status execute_match(FunctionContext* context, const std::string& column_name,
111
                         const std::string& match_query_str, size_t input_rows_count,
112
                         const ColumnString* string_col,
113
                         const InvertedIndexAnalyzerCtx* analyzer_ctx,
114
                         const ColumnArray::Offsets64* array_offsets,
115
                         ColumnUInt8::Container& result) const override;
116
};
117
118
class FunctionMatchAll : public FunctionMatchBase {
119
public:
120
    static constexpr auto name = "match_all";
121
2
    static FunctionPtr create() { return std::make_shared<FunctionMatchAll>(); }
122
123
4
    String get_name() const override { return name; }
124
125
    Status execute_match(FunctionContext* context, const std::string& column_name,
126
                         const std::string& match_query_str, size_t input_rows_count,
127
                         const ColumnString* string_col,
128
                         const InvertedIndexAnalyzerCtx* analyzer_ctx,
129
                         const ColumnArray::Offsets64* array_offsets,
130
                         ColumnUInt8::Container& result) const override;
131
};
132
133
class FunctionMatchPhrase : public FunctionMatchBase {
134
public:
135
    static constexpr auto name = "match_phrase";
136
2
    static FunctionPtr create() { return std::make_shared<FunctionMatchPhrase>(); }
137
138
5
    String get_name() const override { return name; }
139
140
    Status execute_match(FunctionContext* context, const std::string& column_name,
141
                         const std::string& match_query_str, size_t input_rows_count,
142
                         const ColumnString* string_col,
143
                         const InvertedIndexAnalyzerCtx* analyzer_ctx,
144
                         const ColumnArray::Offsets64* array_offsets,
145
                         ColumnUInt8::Container& result) const override;
146
};
147
148
class FunctionMatchPhrasePrefix : public FunctionMatchBase {
149
public:
150
    static constexpr auto name = "match_phrase_prefix";
151
2
    static FunctionPtr create() { return std::make_shared<FunctionMatchPhrasePrefix>(); }
152
153
5
    String get_name() const override { return name; }
154
155
    Status execute_match(FunctionContext* context, const std::string& column_name,
156
                         const std::string& match_query_str, size_t input_rows_count,
157
                         const ColumnString* string_col,
158
                         const InvertedIndexAnalyzerCtx* analyzer_ctx,
159
                         const ColumnArray::Offsets64* array_offsets,
160
                         ColumnUInt8::Container& result) const override;
161
};
162
163
class FunctionMatchRegexp : public FunctionMatchBase {
164
public:
165
    static constexpr auto name = "match_regexp";
166
2
    static FunctionPtr create() { return std::make_shared<FunctionMatchRegexp>(); }
167
168
4
    String get_name() const override { return name; }
169
170
    Status execute_match(FunctionContext* context, const std::string& column_name,
171
                         const std::string& match_query_str, size_t input_rows_count,
172
                         const ColumnString* string_col,
173
                         const InvertedIndexAnalyzerCtx* analyzer_ctx,
174
                         const ColumnArray::Offsets64* array_offsets,
175
                         ColumnUInt8::Container& result) const override;
176
};
177
178
class FunctionMatchPhraseEdge : public FunctionMatchBase {
179
public:
180
    static constexpr auto name = "match_phrase_edge";
181
2
    static FunctionPtr create() { return std::make_shared<FunctionMatchPhraseEdge>(); }
182
183
5
    String get_name() const override { return name; }
184
185
    Status execute_match(FunctionContext* context, const std::string& column_name,
186
                         const std::string& match_query_str, size_t input_rows_count,
187
                         const ColumnString* string_col,
188
                         const InvertedIndexAnalyzerCtx* analyzer_ctx,
189
                         const ColumnArray::Offsets64* array_offsets,
190
                         ColumnUInt8::Container& result) const override;
191
};
192
193
} // namespace doris