Coverage Report

Created: 2026-03-15 08:11

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/exprs/function/functions_multi_string_search.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
// This file is copied from
18
// https://github.com/ClickHouse/ClickHouse/blob/master/src/Functions/FunctionsMultiStringSearch.h
19
// and modified by Doris
20
21
#include <hs/hs_common.h>
22
#include <hs/hs_runtime.h>
23
24
#include <algorithm>
25
#include <boost/iterator/iterator_facade.hpp>
26
#include <cstddef>
27
#include <limits>
28
#include <memory>
29
#include <optional>
30
#include <utility>
31
#include <vector>
32
33
#include "common/status.h"
34
#include "core/block/block.h"
35
#include "core/block/column_numbers.h"
36
#include "core/block/column_with_type_and_name.h"
37
#include "core/column/column.h"
38
#include "core/column/column_array.h"
39
#include "core/column/column_const.h"
40
#include "core/column/column_nullable.h"
41
#include "core/column/column_string.h"
42
#include "core/column/column_vector.h"
43
#include "core/data_type/data_type.h"
44
#include "core/data_type/data_type_number.h" // IWYU pragma: keep
45
#include "core/field.h"
46
#include "core/pod_array_fwd.h"
47
#include "core/string_ref.h"
48
#include "core/types.h"
49
#include "exprs/aggregate/aggregate_function.h"
50
#include "exprs/function/function.h"
51
#include "exprs/function/function_helpers.h"
52
#include "exprs/function/regexps.h"
53
#include "exprs/function/simple_function_factory.h"
54
55
namespace doris {
56
class FunctionContext;
57
} // namespace doris
58
59
namespace doris {
60
61
template <typename Impl>
62
class FunctionsMultiStringSearch : public IFunction {
63
public:
64
    static constexpr auto name = Impl::name;
65
66
2
    static FunctionPtr create() { return std::make_shared<FunctionsMultiStringSearch>(); }
67
68
1
    String get_name() const override { return name; }
69
70
0
    size_t get_number_of_arguments() const override { return 2; }
71
72
0
    bool use_default_implementation_for_nulls() const override { return false; }
73
74
0
    DataTypePtr get_return_type_impl(const DataTypes& arguments) const override {
75
0
        return Impl::get_return_type();
76
0
    }
77
78
    Status execute_impl(FunctionContext* context, Block& block, const ColumnNumbers& arguments,
79
0
                        uint32_t result, size_t input_rows_count) const override {
80
0
        auto haystack_column = block.get_by_position(arguments[0]).column;
81
0
        auto needles_column = block.get_by_position(arguments[1]).column;
82
83
0
        auto haystack_ptr = remove_nullable(haystack_column);
84
0
        auto needles_ptr = remove_nullable(needles_column);
85
86
0
        const auto* col_haystack_vector = check_and_get_column<ColumnString>(&*haystack_ptr);
87
0
        const ColumnConst* col_haystack_const =
88
0
                check_and_get_column_const<ColumnString>(&*haystack_ptr);
89
90
0
        const auto* col_needles_vector = check_and_get_column<ColumnArray>(needles_ptr.get());
91
0
        const ColumnConst* col_needles_const =
92
0
                check_and_get_column_const<ColumnArray>(needles_ptr.get());
93
94
0
        if (!col_needles_const && !col_needles_vector) {
95
0
            return Status::InvalidArgument(
96
0
                    "function '{}' encountered unsupported needles column, found {}", name,
97
0
                    needles_column->get_name());
98
0
        }
99
100
0
        if (col_haystack_const && col_needles_vector) {
101
0
            return Status::InvalidArgument(
102
0
                    "function '{}' doesn't support search with non-constant needles "
103
0
                    "in constant haystack",
104
0
                    name);
105
0
        }
106
107
0
        auto col_res = ColumnVector<Impl::ResultPType>::create();
108
0
        auto col_offsets = ColumnArray::ColumnOffsets::create();
109
110
0
        auto& vec_res = col_res->get_data();
111
0
        auto& offsets_res = col_offsets->get_data();
112
113
0
        Status status;
114
0
        if (col_needles_const) {
115
0
            status = Impl::vector_constant(
116
0
                    col_haystack_vector->get_chars(), col_haystack_vector->get_offsets(),
117
0
                    col_needles_const->get_value<TYPE_ARRAY>(), vec_res, offsets_res,
118
0
                    allow_hyperscan_, max_hyperscan_regexp_length_,
119
0
                    max_hyperscan_regexp_total_length_);
120
0
        } else {
121
0
            status = Impl::vector_vector(
122
0
                    col_haystack_vector->get_chars(), col_haystack_vector->get_offsets(),
123
0
                    col_needles_vector->get_data(), col_needles_vector->get_offsets(), vec_res,
124
0
                    offsets_res, allow_hyperscan_, max_hyperscan_regexp_length_,
125
0
                    max_hyperscan_regexp_total_length_);
126
0
        }
127
128
0
        if (!status.ok()) {
129
0
            return status;
130
0
        }
131
132
0
        handle_nullable_column(haystack_column, vec_res, input_rows_count);
133
0
        handle_nullable_column(needles_column, vec_res, input_rows_count);
134
135
0
        block.replace_by_position(result, std::move(col_res));
136
137
0
        return status;
138
0
    }
139
140
private:
141
    using ResultType = typename Impl::ResultType;
142
143
    constexpr static bool allow_hyperscan_ = true;
144
    constexpr static size_t max_hyperscan_regexp_length_ = 0;       // not limited
145
    constexpr static size_t max_hyperscan_regexp_total_length_ = 0; // not limited
146
147
    /// Handles nullable column by setting result to 0 if the input is null
148
    void handle_nullable_column(const ColumnPtr& column, PaddedPODArray<ResultType>& vec_res,
149
0
                                size_t input_rows_count) const {
150
0
        if (column->is_nullable()) {
151
0
            const auto* column_nullable = assert_cast<const ColumnNullable*>(column.get());
152
0
            const auto& null_map = column_nullable->get_null_map_data();
153
0
            for (size_t i = 0; i != input_rows_count; ++i) {
154
0
                if (null_map[i] == 1) {
155
0
                    vec_res[i] = 0;
156
0
                }
157
0
            }
158
0
        }
159
0
    }
160
};
161
162
/// For more readable instantiations of MultiMatchAnyImpl<>
163
struct MultiMatchTraits {
164
    enum class Find { Any, AnyIndex };
165
};
166
167
template <PrimitiveType PType, MultiMatchTraits::Find Find, bool WithEditDistance>
168
struct FunctionMultiMatchAnyImpl {
169
    using ResultType = typename PrimitiveTypeTraits<PType>::CppType;
170
    static constexpr PrimitiveType ResultPType = PType;
171
172
    static constexpr bool FindAny = (Find == MultiMatchTraits::Find::Any);
173
    static constexpr bool FindAnyIndex = (Find == MultiMatchTraits::Find::AnyIndex);
174
175
    static constexpr auto name = "multi_match_any";
176
177
0
    static auto get_return_type() {
178
0
        return std::make_shared<typename PrimitiveTypeTraits<PType>::DataType>();
179
0
    }
180
181
    /**
182
     * Prepares the regular expressions and scratch space for Hyperscan.
183
     *
184
     * This function takes a vector of needles (substrings to search for) and initializes
185
     * the regular expressions and scratch space required for Hyperscan, a high-performance
186
     * regular expression matching library.
187
     *
188
     */
189
    static Status prepare_regexps_and_scratch(const std::vector<StringRef>& needles,
190
                                              multiregexps::Regexps*& regexps,
191
0
                                              multiregexps::ScratchPtr& smart_scratch) {
192
0
        multiregexps::DeferredConstructedRegexpsPtr deferred_constructed_regexps =
193
0
                multiregexps::getOrSet</*SaveIndices*/
194
0
                                       FindAnyIndex, WithEditDistance>(needles, std::nullopt);
195
0
        regexps = deferred_constructed_regexps->get();
196
197
0
        hs_scratch_t* scratch = nullptr;
198
0
        hs_error_t err = hs_clone_scratch(regexps->getScratch(), &scratch);
199
200
0
        if (err != HS_SUCCESS) {
201
0
            return Status::InternalError("could not clone scratch space for vectorscan");
202
0
        }
203
204
0
        smart_scratch.reset(scratch);
205
0
        return Status::OK();
206
0
    }
207
208
    /**
209
     * Static callback function to handle the match results of the hs_scan function.
210
     *
211
     * This function is called when a matching substring is found while scanning with
212
     * Hyperscan. It updates the result based on the match information.
213
     *
214
     */
215
    static int on_match([[maybe_unused]] unsigned int id, unsigned long long /* from */, // NOLINT
216
                        unsigned long long /* to */,                                     // NOLINT
217
0
                        unsigned int /* flags */, void* context) {
218
        if constexpr (FindAnyIndex) {
219
            *reinterpret_cast<ResultType*>(context) = id;
220
0
        } else if constexpr (FindAny) {
221
0
            *reinterpret_cast<ResultType*>(context) = 1;
222
0
        }
223
        /// Once we hit the callback, there is no need to search for others.
224
0
        return 1;
225
0
    }
226
227
    static Status vector_constant(const ColumnString::Chars& haystack_data,
228
                                  const ColumnString::Offsets& haystack_offsets,
229
                                  const Array& needles_arr, PaddedPODArray<ResultType>& res,
230
                                  PaddedPODArray<UInt64>& offsets, bool allow_hyperscan,
231
                                  size_t max_hyperscan_regexp_length,
232
0
                                  size_t max_hyperscan_regexp_total_length) {
233
0
        if (!allow_hyperscan) {
234
0
            return Status::InvalidArgument("Hyperscan functions are disabled");
235
0
        }
236
237
0
        std::vector<StringRef> needles;
238
0
        needles.reserve(needles_arr.size());
239
0
        for (const auto& needle : needles_arr) {
240
0
            const auto& tmp = needle.get<TYPE_STRING>();
241
0
            needles.emplace_back(StringRef {tmp.data(), tmp.size()});
242
0
        }
243
244
0
        res.resize(haystack_offsets.size());
245
246
0
        if (needles_arr.empty()) {
247
0
            std::fill(res.begin(), res.end(), 0);
248
0
            return Status::OK();
249
0
        }
250
251
0
        multiregexps::Regexps* regexps = nullptr;
252
0
        multiregexps::ScratchPtr smart_scratch;
253
0
        RETURN_IF_ERROR(prepare_regexps_and_scratch(needles, regexps, smart_scratch));
254
255
0
        const size_t haystack_offsets_size = haystack_offsets.size();
256
0
        UInt64 offset = 0;
257
0
        for (size_t i = 0; i < haystack_offsets_size; ++i) {
258
0
            UInt64 length = haystack_offsets[i] - offset;
259
            /// vectorscan restriction.
260
0
            if (length > std::numeric_limits<UInt32>::max()) {
261
0
                return Status::InternalError("too long string to search");
262
0
            }
263
            /// zero the result, scan, check, update the offset.
264
0
            res[i] = 0;
265
0
            hs_error_t err = hs_scan(
266
0
                    regexps->getDB(), reinterpret_cast<const char*>(haystack_data.data()) + offset,
267
0
                    static_cast<unsigned>(length), 0, smart_scratch.get(), on_match, &res[i]);
268
0
            if (err != HS_SUCCESS && err != HS_SCAN_TERMINATED) {
269
0
                return Status::InternalError("failed to scan with vectorscan");
270
0
            }
271
0
            offset = haystack_offsets[i];
272
0
        }
273
274
0
        return Status::OK();
275
0
    }
276
277
    static Status vector_vector(const ColumnString::Chars& haystack_data,
278
                                const ColumnString::Offsets& haystack_offsets,
279
                                const IColumn& needles_data,
280
                                const ColumnArray::Offsets64& needles_offsets,
281
                                PaddedPODArray<ResultType>& res, PaddedPODArray<UInt64>& offsets,
282
                                bool allow_hyperscan, size_t max_hyperscan_regexp_length,
283
0
                                size_t max_hyperscan_regexp_total_length) {
284
0
        if (!allow_hyperscan) {
285
0
            return Status::InvalidArgument("Hyperscan functions are disabled");
286
0
        }
287
288
0
        res.resize(haystack_offsets.size());
289
290
0
        size_t prev_haystack_offset = 0;
291
0
        size_t prev_needles_offset = 0;
292
293
0
        const auto& nested_column =
294
0
                check_and_get_column<ColumnNullable>(needles_data)->get_nested_column();
295
0
        const auto* needles_data_string = check_and_get_column<ColumnString>(nested_column);
296
297
0
        if (!needles_data_string) {
298
0
            return Status::InvalidArgument("needles should be string column");
299
0
        }
300
301
0
        std::vector<StringRef> needles;
302
0
        for (size_t i = 0; i < haystack_offsets.size(); ++i) {
303
0
            needles.reserve(needles_offsets[i] - prev_needles_offset);
304
305
0
            for (size_t j = prev_needles_offset; j < needles_offsets[i]; ++j) {
306
0
                needles.emplace_back(needles_data_string->get_data_at(j));
307
0
            }
308
0
            if (needles.empty()) {
309
0
                res[i] = 0;
310
0
                prev_haystack_offset = haystack_offsets[i];
311
0
                prev_needles_offset = needles_offsets[i];
312
0
                continue;
313
0
            }
314
315
0
            multiregexps::Regexps* regexps = nullptr;
316
0
            multiregexps::ScratchPtr smart_scratch;
317
0
            RETURN_IF_ERROR(prepare_regexps_and_scratch(needles, regexps, smart_scratch));
318
319
0
            const size_t cur_haystack_length = haystack_offsets[i] - prev_haystack_offset;
320
321
            /// vectorscan restriction.
322
0
            if (cur_haystack_length > std::numeric_limits<UInt32>::max()) {
323
0
                return Status::InternalError("too long string to search");
324
0
            }
325
326
            /// zero the result, scan, check, update the offset.
327
0
            res[i] = 0;
328
0
            hs_error_t err = hs_scan(
329
0
                    regexps->getDB(),
330
0
                    reinterpret_cast<const char*>(haystack_data.data()) + prev_haystack_offset,
331
0
                    static_cast<unsigned>(cur_haystack_length), 0, smart_scratch.get(), on_match,
332
0
                    &res[i]);
333
0
            if (err != HS_SUCCESS && err != HS_SCAN_TERMINATED) {
334
0
                return Status::InternalError("failed to scan with vectorscan");
335
0
            }
336
337
0
            prev_haystack_offset = haystack_offsets[i];
338
0
            prev_needles_offset = needles_offsets[i];
339
0
            needles.clear();
340
0
        }
341
342
0
        return Status::OK();
343
0
    }
344
};
345
346
using FunctionMultiMatchAny = FunctionsMultiStringSearch<FunctionMultiMatchAnyImpl<
347
        TYPE_TINYINT, MultiMatchTraits::Find::Any, /*WithEditDistance*/ false>>;
348
349
1
void register_function_multi_string_search(SimpleFunctionFactory& factory) {
350
1
    factory.register_function<FunctionMultiMatchAny>();
351
1
}
352
353
} // namespace doris