Coverage Report

Created: 2026-10-07 01:54

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(
76
            FunctionContext* context, const std::string& column_name,
77
            const std::string& match_query_str, size_t input_rows_count,
78
            const ColumnString* string_col, const InvertedIndexAnalyzerCtx* analyzer_ctx,
79
            const ColumnArray::Offsets64* array_offsets, ColumnUInt8::Container& result,
80
            const ColumnUInt8::Container* array_element_null_map = nullptr) 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, size_t current_block_row_idx,
91
            const ColumnArray::Offsets64* array_offsets, int32_t& current_src_array_offset,
92
            const ColumnUInt8::Container* array_element_null_map = nullptr) const;
93
94
    Status check(FunctionContext* context, const std::string& function_name) const;
95
96
    Status evaluate_inverted_index(
97
            const ColumnsWithTypeAndName& arguments,
98
            const std::vector<IndexFieldNameAndTypePair>& data_type_with_names,
99
            std::vector<segment_v2::IndexIterator*> iterators, uint32_t num_rows,
100
            const InvertedIndexAnalyzerCtx* analyzer_ctx,
101
            segment_v2::InvertedIndexResultBitmap& bitmap_result) const override;
102
};
103
104
class FunctionMatchAny : public FunctionMatchBase {
105
public:
106
    static constexpr auto name = "match_any";
107
2
    static FunctionPtr create() { return std::make_shared<FunctionMatchAny>(); }
108
109
9
    String get_name() const override { return name; }
110
111
    Status execute_match(
112
            FunctionContext* context, const std::string& column_name,
113
            const std::string& match_query_str, size_t input_rows_count,
114
            const ColumnString* string_col, const InvertedIndexAnalyzerCtx* analyzer_ctx,
115
            const ColumnArray::Offsets64* array_offsets, ColumnUInt8::Container& result,
116
            const ColumnUInt8::Container* array_element_null_map = nullptr) const override;
117
};
118
119
class FunctionMatchAll : public FunctionMatchBase {
120
public:
121
    static constexpr auto name = "match_all";
122
2
    static FunctionPtr create() { return std::make_shared<FunctionMatchAll>(); }
123
124
7
    String get_name() const override { return name; }
125
126
    Status execute_match(
127
            FunctionContext* context, const std::string& column_name,
128
            const std::string& match_query_str, size_t input_rows_count,
129
            const ColumnString* string_col, const InvertedIndexAnalyzerCtx* analyzer_ctx,
130
            const ColumnArray::Offsets64* array_offsets, ColumnUInt8::Container& result,
131
            const ColumnUInt8::Container* array_element_null_map = nullptr) const override;
132
};
133
134
class FunctionMatchPhrase : public FunctionMatchBase {
135
public:
136
    static constexpr auto name = "match_phrase";
137
2
    static FunctionPtr create() { return std::make_shared<FunctionMatchPhrase>(); }
138
139
29
    String get_name() const override { return name; }
140
141
    Status execute_match(
142
            FunctionContext* context, const std::string& column_name,
143
            const std::string& match_query_str, size_t input_rows_count,
144
            const ColumnString* string_col, const InvertedIndexAnalyzerCtx* analyzer_ctx,
145
            const ColumnArray::Offsets64* array_offsets, ColumnUInt8::Container& result,
146
            const ColumnUInt8::Container* array_element_null_map = nullptr) const override;
147
};
148
149
class FunctionMatchPhrasePrefix : public FunctionMatchBase {
150
public:
151
    static constexpr auto name = "match_phrase_prefix";
152
2
    static FunctionPtr create() { return std::make_shared<FunctionMatchPhrasePrefix>(); }
153
154
19
    String get_name() const override { return name; }
155
156
    Status execute_match(
157
            FunctionContext* context, const std::string& column_name,
158
            const std::string& match_query_str, size_t input_rows_count,
159
            const ColumnString* string_col, const InvertedIndexAnalyzerCtx* analyzer_ctx,
160
            const ColumnArray::Offsets64* array_offsets, ColumnUInt8::Container& result,
161
            const ColumnUInt8::Container* array_element_null_map = nullptr) const override;
162
};
163
164
class FunctionMatchRegexp : public FunctionMatchBase {
165
public:
166
    static constexpr auto name = "match_regexp";
167
2
    static FunctionPtr create() { return std::make_shared<FunctionMatchRegexp>(); }
168
169
6
    String get_name() const override { return name; }
170
171
    Status execute_match(
172
            FunctionContext* context, const std::string& column_name,
173
            const std::string& match_query_str, size_t input_rows_count,
174
            const ColumnString* string_col, const InvertedIndexAnalyzerCtx* analyzer_ctx,
175
            const ColumnArray::Offsets64* array_offsets, ColumnUInt8::Container& result,
176
            const ColumnUInt8::Container* array_element_null_map = nullptr) const override;
177
};
178
179
class FunctionMatchPhraseEdge : public FunctionMatchBase {
180
public:
181
    static constexpr auto name = "match_phrase_edge";
182
2
    static FunctionPtr create() { return std::make_shared<FunctionMatchPhraseEdge>(); }
183
184
10
    String get_name() const override { return name; }
185
186
    Status execute_match(
187
            FunctionContext* context, const std::string& column_name,
188
            const std::string& match_query_str, size_t input_rows_count,
189
            const ColumnString* string_col, const InvertedIndexAnalyzerCtx* analyzer_ctx,
190
            const ColumnArray::Offsets64* array_offsets, ColumnUInt8::Container& result,
191
            const ColumnUInt8::Container* array_element_null_map = nullptr) const override;
192
};
193
194
} // namespace doris