Coverage Report

Created: 2024-11-21 10:56

/root/doris/be/src/vec/functions/match.h
Line
Count
Source (jump to first uncovered line)
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 "olap/inverted_index_parser.h"
34
#include "olap/rowset/segment_v2/inverted_index_reader.h"
35
#include "vec/aggregate_functions/aggregate_function.h"
36
#include "vec/columns/column.h"
37
#include "vec/columns/column_array.h"
38
#include "vec/core/block.h"
39
#include "vec/core/column_numbers.h"
40
#include "vec/core/column_with_type_and_name.h"
41
#include "vec/core/types.h"
42
#include "vec/data_types/data_type_number.h"
43
#include "vec/exprs/vmatch_predicate.h"
44
#include "vec/functions/function.h"
45
#include "vec/functions/simple_function_factory.h"
46
47
namespace doris {
48
class FunctionContext;
49
} // namespace doris
50
51
namespace doris::vectorized {
52
53
const std::string MATCH_ANY_FUNCTION = "match_any";
54
const std::string MATCH_ALL_FUNCTION = "match_all";
55
const std::string MATCH_PHRASE_FUNCTION = "match_phrase";
56
const std::string MATCH_PHRASE_PREFIX_FUNCTION = "match_phrase_prefix";
57
const std::string MATCH_PHRASE_REGEXP_FUNCTION = "match_regexp";
58
const std::string MATCH_PHRASE_EDGE_FUNCTION = "match_phrase_edge";
59
60
class FunctionMatchBase : public IFunction {
61
public:
62
0
    size_t get_number_of_arguments() const override { return 2; }
63
64
0
    String get_name() const override { return "match"; }
65
66
    /// Get result types by argument types. If the function does not apply to these arguments, throw an exception.
67
0
    DataTypePtr get_return_type_impl(const DataTypes& arguments) const override {
68
0
        return std::make_shared<DataTypeUInt8>();
69
0
    }
70
71
    Status execute_impl(FunctionContext* context, Block& block, const ColumnNumbers& arguments,
72
                        uint32_t result, size_t input_rows_count) const override;
73
74
    virtual Status execute_match(FunctionContext* context, const std::string& column_name,
75
                                 const std::string& match_query_str, size_t input_rows_count,
76
                                 const ColumnString* string_col,
77
                                 InvertedIndexCtx* inverted_index_ctx,
78
                                 const ColumnArray::Offsets64* array_offsets,
79
                                 ColumnUInt8::Container& result) const = 0;
80
81
    doris::segment_v2::InvertedIndexQueryType get_query_type_from_fn_name() const;
82
83
    std::vector<std::string> analyse_query_str_token(InvertedIndexCtx* inverted_index_ctx,
84
                                                     const std::string& match_query_str,
85
                                                     const std::string& field_name) const;
86
87
    std::vector<std::string> analyse_data_token(const std::string& column_name,
88
                                                InvertedIndexCtx* inverted_index_ctx,
89
                                                const ColumnString* string_col,
90
                                                int32_t current_block_row_idx,
91
                                                const ColumnArray::Offsets64* array_offsets,
92
                                                int32_t& current_src_array_offset) const;
93
94
    Status check(FunctionContext* context, const std::string& function_name) const;
95
    Status evaluate_inverted_index(
96
            const ColumnsWithTypeAndName& arguments,
97
            const std::vector<vectorized::IndexFieldNameAndTypePair>& data_type_with_names,
98
            std::vector<segment_v2::InvertedIndexIterator*> iterators, uint32_t num_rows,
99
            segment_v2::InvertedIndexResultBitmap& bitmap_result) const override;
100
};
101
102
class FunctionMatchAny : public FunctionMatchBase {
103
public:
104
    static constexpr auto name = "match_any";
105
1
    static FunctionPtr create() { return std::make_shared<FunctionMatchAny>(); }
106
107
0
    String get_name() const override { return name; }
108
109
    Status execute_match(FunctionContext* context, const std::string& column_name,
110
                         const std::string& match_query_str, size_t input_rows_count,
111
                         const ColumnString* string_col, InvertedIndexCtx* inverted_index_ctx,
112
                         const ColumnArray::Offsets64* array_offsets,
113
                         ColumnUInt8::Container& result) const override;
114
};
115
116
class FunctionMatchAll : public FunctionMatchBase {
117
public:
118
    static constexpr auto name = "match_all";
119
1
    static FunctionPtr create() { return std::make_shared<FunctionMatchAll>(); }
120
121
0
    String get_name() const override { return name; }
122
123
    Status execute_match(FunctionContext* context, const std::string& column_name,
124
                         const std::string& match_query_str, size_t input_rows_count,
125
                         const ColumnString* string_col, InvertedIndexCtx* inverted_index_ctx,
126
                         const ColumnArray::Offsets64* array_offsets,
127
                         ColumnUInt8::Container& result) const override;
128
};
129
130
class FunctionMatchPhrase : public FunctionMatchBase {
131
public:
132
    static constexpr auto name = "match_phrase";
133
1
    static FunctionPtr create() { return std::make_shared<FunctionMatchPhrase>(); }
134
135
0
    String get_name() const override { return name; }
136
137
    Status execute_match(FunctionContext* context, const std::string& column_name,
138
                         const std::string& match_query_str, size_t input_rows_count,
139
                         const ColumnString* string_col, InvertedIndexCtx* inverted_index_ctx,
140
                         const ColumnArray::Offsets64* array_offsets,
141
                         ColumnUInt8::Container& result) const override;
142
};
143
144
class FunctionMatchPhrasePrefix : public FunctionMatchBase {
145
public:
146
    static constexpr auto name = "match_phrase_prefix";
147
1
    static FunctionPtr create() { return std::make_shared<FunctionMatchPhrasePrefix>(); }
148
149
0
    String get_name() const override { return name; }
150
151
    Status execute_match(FunctionContext* context, const std::string& column_name,
152
                         const std::string& match_query_str, size_t input_rows_count,
153
                         const ColumnString* string_col, InvertedIndexCtx* inverted_index_ctx,
154
                         const ColumnArray::Offsets64* array_offsets,
155
                         ColumnUInt8::Container& result) const override;
156
};
157
158
class FunctionMatchRegexp : public FunctionMatchBase {
159
public:
160
    static constexpr auto name = "match_regexp";
161
1
    static FunctionPtr create() { return std::make_shared<FunctionMatchRegexp>(); }
162
163
0
    String get_name() const override { return name; }
164
165
    Status execute_match(FunctionContext* context, const std::string& column_name,
166
                         const std::string& match_query_str, size_t input_rows_count,
167
                         const ColumnString* string_col, InvertedIndexCtx* inverted_index_ctx,
168
                         const ColumnArray::Offsets64* array_offsets,
169
                         ColumnUInt8::Container& result) const override;
170
};
171
172
class FunctionMatchPhraseEdge : public FunctionMatchBase {
173
public:
174
    static constexpr auto name = "match_phrase_edge";
175
1
    static FunctionPtr create() { return std::make_shared<FunctionMatchPhraseEdge>(); }
176
177
0
    String get_name() const override { return name; }
178
179
    Status execute_match(FunctionContext* context, const std::string& column_name,
180
                         const std::string& match_query_str, size_t input_rows_count,
181
                         const ColumnString* string_col, InvertedIndexCtx* inverted_index_ctx,
182
                         const ColumnArray::Offsets64* array_offsets,
183
                         ColumnUInt8::Container& result) const override;
184
};
185
186
} // namespace doris::vectorized