Coverage Report

Created: 2026-04-14 13:42

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/exprs/function/functions_multi_string_position.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/FunctionsMultiStringPosition.h
19
// and modified by Doris
20
21
#include <stddef.h>
22
23
#include <algorithm>
24
#include <boost/iterator/iterator_facade.hpp>
25
#include <cstdint>
26
#include <iterator>
27
#include <limits>
28
#include <memory>
29
#include <utility>
30
#include <vector>
31
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/column/column_const.h"
39
#include "core/column/column_nullable.h"
40
#include "core/column/column_string.h"
41
#include "core/column/column_vector.h"
42
#include "core/data_type/data_type.h"
43
#include "core/data_type/data_type_array.h"
44
#include "core/data_type/data_type_nullable.h"
45
#include "core/data_type/data_type_number.h"
46
#include "core/field.h"
47
#include "core/pod_array_fwd.h"
48
#include "core/string_ref.h"
49
#include "core/types.h"
50
#include "exec/common/string_searcher.h"
51
#include "exprs/aggregate/aggregate_function.h"
52
#include "exprs/function/function.h"
53
#include "exprs/function/function_helpers.h"
54
#include "exprs/function/simple_function_factory.h"
55
56
namespace doris {
57
class FunctionContext;
58
} // namespace doris
59
60
namespace doris {
61
62
template <typename Impl>
63
class FunctionMultiStringPosition : public IFunction {
64
public:
65
    static constexpr auto name = Impl::name;
66
67
2
    static FunctionPtr create() { return std::make_shared<FunctionMultiStringPosition>(); }
68
69
1
    String get_name() const override { return name; }
70
71
0
    size_t get_number_of_arguments() const override { return 2; }
72
73
0
    bool use_default_implementation_for_nulls() const override { return false; }
74
75
0
    DataTypePtr get_return_type_impl(const DataTypes& arguments) const override {
76
0
        return std::make_shared<DataTypeArray>(make_nullable(std::make_shared<DataTypeInt32>()));
77
0
    }
78
79
    Status execute_impl(FunctionContext* context, Block& block, const ColumnNumbers& arguments,
80
0
                        uint32_t result, size_t input_rows_count) const override {
81
0
        auto haystack_column = block.get_by_position(arguments[0]).column;
82
0
        auto needles_column = block.get_by_position(arguments[1]).column;
83
84
0
        bool haystack_nullable = false;
85
0
        bool needles_nullable = false;
86
87
0
        if (haystack_column->is_nullable()) {
88
0
            haystack_nullable = true;
89
0
        }
90
91
0
        if (needles_column->is_nullable()) {
92
0
            needles_nullable = true;
93
0
        }
94
95
0
        auto haystack_ptr = remove_nullable(haystack_column);
96
0
        auto needles_ptr = remove_nullable(needles_column);
97
98
0
        const ColumnString* col_haystack_vector =
99
0
                check_and_get_column<ColumnString>(&*haystack_ptr);
100
0
        const ColumnConst* col_haystack_const =
101
0
                check_and_get_column_const<ColumnString>(&*haystack_ptr);
102
103
0
        const ColumnArray* col_needles_vector =
104
0
                check_and_get_column<ColumnArray>(needles_ptr.get());
105
0
        const ColumnConst* col_needles_const =
106
0
                check_and_get_column_const<ColumnArray>(needles_ptr.get());
107
108
0
        if (!col_needles_const && !col_needles_vector) {
109
0
            return Status::InvalidArgument(
110
0
                    "function '{}' encountered unsupported needles column, found {}", name,
111
0
                    needles_column->get_name());
112
0
        }
113
114
0
        if (col_haystack_const && col_needles_vector) {
115
0
            return Status::InvalidArgument(
116
0
                    "function '{}' doesn't support search with non-constant needles "
117
0
                    "in constant haystack",
118
0
                    name);
119
0
        }
120
121
0
        auto col_res = ColumnVector<Impl::ResultType>::create();
122
0
        auto col_offsets = ColumnArray::ColumnOffsets::create();
123
124
0
        auto& vec_res = col_res->get_data();
125
0
        auto& offsets_res = col_offsets->get_data();
126
127
0
        Status status;
128
0
        if (col_needles_const) {
129
0
            status = Impl::vector_constant(
130
0
                    col_haystack_vector->get_chars(), col_haystack_vector->get_offsets(),
131
0
                    col_needles_const->get_value<TYPE_ARRAY>(), vec_res, offsets_res);
132
0
        } else {
133
0
            status = Impl::vector_vector(col_haystack_vector->get_chars(),
134
0
                                         col_haystack_vector->get_offsets(),
135
0
                                         col_needles_vector->get_data(),
136
0
                                         col_needles_vector->get_offsets(), vec_res, offsets_res);
137
0
        }
138
139
0
        if (!status.ok()) {
140
0
            return status;
141
0
        }
142
143
0
        if (haystack_nullable) {
144
0
            auto column_nullable = check_and_get_column<ColumnNullable>(haystack_column.get());
145
0
            auto& null_map = column_nullable->get_null_map_data();
146
0
            for (size_t i = 0; i != input_rows_count; ++i) {
147
0
                if (null_map[i] == 1) {
148
0
                    for (size_t offset = offsets_res[i - 1]; offset != offsets_res[i]; ++offset) {
149
0
                        vec_res[offset] = 0;
150
0
                    }
151
0
                }
152
0
            }
153
0
        }
154
155
0
        if (needles_nullable) {
156
0
            auto column_nullable = check_and_get_column<ColumnNullable>(needles_column.get());
157
0
            auto& null_map = column_nullable->get_null_map_data();
158
0
            for (size_t i = 0; i != input_rows_count; ++i) {
159
0
                if (null_map[i] == 1) {
160
0
                    for (size_t offset = offsets_res[i - 1]; offset != offsets_res[i]; ++offset) {
161
0
                        vec_res[offset] = 0;
162
0
                    }
163
0
                }
164
0
            }
165
0
        }
166
167
0
        auto nullable_col =
168
0
                ColumnNullable::create(std::move(col_res), ColumnUInt8::create(col_res->size(), 0));
169
0
        block.get_by_position(result).column =
170
0
                ColumnArray::create(std::move(nullable_col), std::move(col_offsets));
171
0
        return status;
172
0
    }
173
};
174
175
struct FunctionMultiSearchAllPositionsImpl {
176
public:
177
    static constexpr PrimitiveType ResultType = TYPE_INT;
178
    using SingleSearcher = ASCIICaseSensitiveStringSearcher;
179
    static constexpr auto name = "multi_search_all_positions";
180
181
    static Status vector_constant(const ColumnString::Chars& haystack_data,
182
                                  const ColumnString::Offsets& haystack_offsets,
183
                                  const Array& needles_arr, PaddedPODArray<Int32>& vec_res,
184
0
                                  PaddedPODArray<UInt64>& offsets_res) {
185
0
        if (needles_arr.size() > std::numeric_limits<UInt8>::max()) {
186
0
            return Status::InvalidArgument(
187
0
                    "number of arguments for function {} doesn't match: "
188
0
                    "passed {}, should be at most 255",
189
0
                    name, needles_arr.size());
190
0
        }
191
192
0
        const size_t needles_size = needles_arr.size();
193
0
        std::vector<SingleSearcher> searchers;
194
0
        searchers.reserve(needles_size);
195
0
        for (const auto& needle : needles_arr) {
196
0
            if (!is_string_type(needle.get_type())) {
197
0
                return Status::InvalidArgument("invalid type of needle {}", needle.get_type_name());
198
0
            }
199
0
            searchers.emplace_back(needle.get<TYPE_STRING>().data(),
200
0
                                   needle.get<TYPE_STRING>().size());
201
0
        }
202
203
0
        const size_t haystack_size = haystack_offsets.size();
204
0
        vec_res.resize(haystack_size * needles_size);
205
0
        offsets_res.resize(haystack_size);
206
207
0
        std::fill(vec_res.begin(), vec_res.end(), 0);
208
209
        // we traverse to generator answer by Vector's slot of ColumnVector, not by Vector.
210
        // TODO: check if the order of loop is best. The large data may make us writing across the line which size out of L2 cache.
211
0
        for (size_t ans_slot_in_row = 0; ans_slot_in_row < searchers.size(); ans_slot_in_row++) {
212
            //  is i.e. answer slot index in one Vector(row) of answer
213
0
            auto& searcher = searchers[ans_slot_in_row];
214
0
            size_t prev_haystack_offset = 0;
215
216
0
            for (size_t haystack_index = 0, res_index = ans_slot_in_row;
217
0
                 haystack_index < haystack_size; ++haystack_index, res_index += needles_size) {
218
0
                const auto* haystack = &haystack_data[prev_haystack_offset];
219
0
                const auto* haystack_end =
220
0
                        haystack - prev_haystack_offset + haystack_offsets[haystack_index];
221
222
0
                const auto* ans_now = searcher.search(haystack, haystack_end);
223
0
                vec_res[res_index] =
224
0
                        ans_now >= haystack_end ? 0 : (Int32)std::distance(haystack, ans_now) + 1;
225
0
                prev_haystack_offset = haystack_offsets[haystack_index];
226
0
            }
227
0
        }
228
229
0
        size_t accum = needles_size;
230
0
        for (size_t i = 0; i < haystack_size; ++i) {
231
0
            offsets_res[i] = accum;
232
0
            accum += needles_size;
233
0
        }
234
235
0
        return Status::OK();
236
0
    }
237
238
    static Status vector_vector(const ColumnString::Chars& haystack_data,
239
                                const ColumnString::Offsets& haystack_offsets,
240
                                const IColumn& needles_data,
241
                                const ColumnArray::Offsets64& needles_offsets,
242
                                PaddedPODArray<Int32>& vec_res,
243
0
                                PaddedPODArray<UInt64>& offsets_res) {
244
0
        size_t prev_haystack_offset = 0;
245
0
        size_t prev_needles_offset = 0;
246
247
0
        offsets_res.reserve(haystack_data.size());
248
0
        uint64_t offset_now = 0;
249
250
0
        auto& nested_column =
251
0
                check_and_get_column<ColumnNullable>(needles_data)->get_nested_column();
252
0
        const ColumnString* needles_data_string = check_and_get_column<ColumnString>(nested_column);
253
254
0
        std::vector<StringRef> needles_for_row;
255
        // haystack first, row by row.
256
0
        for (size_t haystack_index = 0; haystack_index < haystack_offsets.size();
257
0
             ++haystack_index) {
258
            // get haystack for this row.
259
0
            const auto* haystack = &haystack_data[prev_haystack_offset];
260
0
            const auto* haystack_end =
261
0
                    haystack - prev_haystack_offset + haystack_offsets[haystack_index];
262
263
            // build needles for this row.
264
0
            needles_for_row.reserve(needles_offsets[haystack_index] - prev_needles_offset);
265
0
            for (size_t j = prev_needles_offset; j < needles_offsets[haystack_index]; ++j) {
266
0
                needles_for_row.emplace_back(needles_data_string->get_data_at(j));
267
0
            }
268
0
            const size_t needles_row_size = needles_for_row.size();
269
0
            if (needles_row_size > std::numeric_limits<UInt8>::max()) {
270
0
                return Status::InvalidArgument(
271
0
                        "number of arguments for function {} doesn't match: "
272
0
                        "passed {}, should be at most 255",
273
0
                        name, needles_row_size);
274
0
            }
275
276
            // each searcher search for one needle.
277
0
            std::vector<SingleSearcher> searchers;
278
0
            searchers.clear();
279
0
            searchers.reserve(needles_row_size);
280
0
            for (auto needle : needles_for_row) {
281
0
                searchers.emplace_back(needle.data, needle.size);
282
0
            }
283
284
            // search for first so that the ans's size is constant for each row.
285
0
            auto ans_row_begin = vec_res.size();
286
0
            vec_res.resize(vec_res.size() + needles_row_size);
287
0
            offset_now += searchers.size();
288
0
            offsets_res.emplace_back(offset_now);
289
290
            //for now haystack, apply needle to search, generator answer by order.
291
0
            for (size_t ans_slot_in_row = 0; ans_slot_in_row < searchers.size();
292
0
                 ans_slot_in_row++) {
293
                //  is i.e. answer slot index in one Vector(row) of answer
294
0
                auto& searcher = searchers[ans_slot_in_row];
295
296
0
                auto ans_now = searcher.search(haystack, haystack_end);
297
0
                vec_res[ans_row_begin + ans_slot_in_row] =
298
0
                        ans_now >= haystack_end ? 0 : (Int32)std::distance(haystack, ans_now) + 1;
299
0
            }
300
301
0
            prev_haystack_offset = haystack_offsets[haystack_index];
302
0
            prev_needles_offset = needles_offsets[haystack_index];
303
0
            needles_for_row.clear();
304
0
        }
305
306
0
        return Status::OK();
307
0
    }
308
};
309
310
using FunctionMultiSearchAllPositions =
311
        FunctionMultiStringPosition<FunctionMultiSearchAllPositionsImpl>;
312
313
1
void register_function_multi_string_position(SimpleFunctionFactory& factory) {
314
1
    factory.register_function<FunctionMultiSearchAllPositions>();
315
1
}
316
317
} // namespace doris