Coverage Report

Created: 2024-11-21 13:02

/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
                        size_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
    void analyse_query_str_token(std::vector<std::string>* query_tokens,
84
                                 InvertedIndexCtx* inverted_index_ctx,
85
                                 const std::string& match_query_str,
86
                                 const std::string& field_name) const;
87
88
    std::vector<std::string> analyse_data_token(const std::string& column_name,
89
                                                InvertedIndexCtx* inverted_index_ctx,
90
                                                const ColumnString* string_col,
91
                                                int32_t current_block_row_idx,
92
                                                const ColumnArray::Offsets64* array_offsets,
93
                                                int32_t& current_src_array_offset) const;
94
95
    Status check(FunctionContext* context, const std::string& function_name) const;
96
    Status evaluate_inverted_index(
97
            const ColumnsWithTypeAndName& arguments,
98
            const std::vector<vectorized::IndexFieldNameAndTypePair>& data_type_with_names,
99
            std::vector<segment_v2::InvertedIndexIterator*> iterators, uint32_t num_rows,
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
1
    static FunctionPtr create() { return std::make_shared<FunctionMatchAny>(); }
107
108
0
    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, InvertedIndexCtx* inverted_index_ctx,
113
                         const ColumnArray::Offsets64* array_offsets,
114
                         ColumnUInt8::Container& result) const override;
115
};
116
117
class FunctionMatchAll : public FunctionMatchBase {
118
public:
119
    static constexpr auto name = "match_all";
120
1
    static FunctionPtr create() { return std::make_shared<FunctionMatchAll>(); }
121
122
0
    String get_name() const override { return name; }
123
124
    Status execute_match(FunctionContext* context, const std::string& column_name,
125
                         const std::string& match_query_str, size_t input_rows_count,
126
                         const ColumnString* string_col, InvertedIndexCtx* inverted_index_ctx,
127
                         const ColumnArray::Offsets64* array_offsets,
128
                         ColumnUInt8::Container& result) const override;
129
};
130
131
class FunctionMatchPhrase : public FunctionMatchBase {
132
public:
133
    static constexpr auto name = "match_phrase";
134
1
    static FunctionPtr create() { return std::make_shared<FunctionMatchPhrase>(); }
135
136
0
    String get_name() const override { return name; }
137
138
    Status execute_match(FunctionContext* context, const std::string& column_name,
139
                         const std::string& match_query_str, size_t input_rows_count,
140
                         const ColumnString* string_col, InvertedIndexCtx* inverted_index_ctx,
141
                         const ColumnArray::Offsets64* array_offsets,
142
                         ColumnUInt8::Container& result) const override;
143
};
144
145
class FunctionMatchPhrasePrefix : public FunctionMatchBase {
146
public:
147
    static constexpr auto name = "match_phrase_prefix";
148
1
    static FunctionPtr create() { return std::make_shared<FunctionMatchPhrasePrefix>(); }
149
150
0
    String get_name() const override { return name; }
151
152
    Status execute_match(FunctionContext* context, const std::string& column_name,
153
                         const std::string& match_query_str, size_t input_rows_count,
154
                         const ColumnString* string_col, InvertedIndexCtx* inverted_index_ctx,
155
                         const ColumnArray::Offsets64* array_offsets,
156
                         ColumnUInt8::Container& result) const override;
157
};
158
159
class FunctionMatchRegexp : public FunctionMatchBase {
160
public:
161
    static constexpr auto name = "match_regexp";
162
1
    static FunctionPtr create() { return std::make_shared<FunctionMatchRegexp>(); }
163
164
0
    String get_name() const override { return name; }
165
166
    Status execute_match(FunctionContext* context, const std::string& column_name,
167
                         const std::string& match_query_str, size_t input_rows_count,
168
                         const ColumnString* string_col, InvertedIndexCtx* inverted_index_ctx,
169
                         const ColumnArray::Offsets64* array_offsets,
170
                         ColumnUInt8::Container& result) const override;
171
};
172
173
class FunctionMatchPhraseEdge : public FunctionMatchBase {
174
public:
175
    static constexpr auto name = "match_phrase_edge";
176
1
    static FunctionPtr create() { return std::make_shared<FunctionMatchPhraseEdge>(); }
177
178
0
    String get_name() const override { return name; }
179
180
    Status execute_match(FunctionContext* context, const std::string& column_name,
181
                         const std::string& match_query_str, size_t input_rows_count,
182
                         const ColumnString* string_col, InvertedIndexCtx* inverted_index_ctx,
183
                         const ColumnArray::Offsets64* array_offsets,
184
                         ColumnUInt8::Container& result) const override;
185
};
186
187
} // namespace doris::vectorized