Coverage Report

Created: 2026-03-17 00:16

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