Coverage Report

Created: 2025-11-13 20:23

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/root/doris/be/src/vec/functions/function.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/IFunction.cpp
19
// and modified by Doris
20
21
#include "vec/functions/function.h"
22
23
#include <algorithm>
24
#include <memory>
25
#include <numeric>
26
27
#include "common/status.h"
28
#include "runtime/define_primitive_type.h"
29
#include "runtime/primitive_type.h"
30
#include "vec/aggregate_functions/aggregate_function.h"
31
#include "vec/columns/column.h"
32
#include "vec/columns/column_const.h"
33
#include "vec/columns/column_nullable.h"
34
#include "vec/columns/column_vector.h"
35
#include "vec/common/assert_cast.h"
36
#include "vec/core/field.h"
37
#include "vec/data_types/data_type_array.h"
38
#include "vec/data_types/data_type_nothing.h"
39
#include "vec/data_types/data_type_nullable.h"
40
#include "vec/functions/function_helpers.h"
41
#include "vec/utils/util.hpp"
42
43
namespace doris::vectorized {
44
#include "common/compile_check_begin.h"
45
ColumnPtr wrap_in_nullable(const ColumnPtr& src, const Block& block, const ColumnNumbers& args,
46
70.0k
                           size_t input_rows_count) {
47
70.0k
    ColumnPtr result_null_map_column;
48
    /// If result is already nullable.
49
70.0k
    ColumnPtr src_not_nullable = src;
50
70.0k
    MutableColumnPtr mutable_result_null_map_column;
51
52
70.0k
    if (const auto* nullable = check_and_get_column<ColumnNullable>(*src)) {
53
8.47k
        src_not_nullable = nullable->get_nested_column_ptr();
54
8.47k
        result_null_map_column = nullable->get_null_map_column_ptr();
55
8.47k
    }
56
57
96.4k
    for (const auto& arg : args) {
58
96.4k
        const ColumnWithTypeAndName& elem = block.get_by_position(arg);
59
96.4k
        if (!elem.type->is_nullable() || is_column_const(*elem.column)) {
60
21.0k
            continue;
61
21.0k
        }
62
63
75.3k
        if (const auto* nullable = assert_cast<const ColumnNullable*>(elem.column.get());
64
75.3k
            nullable->has_null()) {
65
1.02k
            const ColumnPtr& null_map_column = nullable->get_null_map_column_ptr();
66
1.02k
            if (!result_null_map_column) { // NOLINT(bugprone-use-after-move)
67
924
                result_null_map_column = null_map_column->clone_resized(input_rows_count);
68
924
                continue;
69
924
            }
70
71
99
            if (!mutable_result_null_map_column) {
72
74
                mutable_result_null_map_column =
73
74
                        std::move(result_null_map_column)->assume_mutable();
74
74
            }
75
76
99
            NullMap& result_null_map =
77
99
                    assert_cast<ColumnUInt8&>(*mutable_result_null_map_column).get_data();
78
99
            const NullMap& src_null_map =
79
99
                    assert_cast<const ColumnUInt8&>(*null_map_column).get_data();
80
81
99
            VectorizedUtils::update_null_map(result_null_map, src_null_map);
82
99
        }
83
75.3k
    }
84
85
70.0k
    if (!result_null_map_column) {
86
60.6k
        if (is_column_const(*src)) {
87
2
            return ColumnConst::create(
88
2
                    make_nullable(assert_cast<const ColumnConst&>(*src).get_data_column_ptr(),
89
2
                                  false),
90
2
                    input_rows_count);
91
2
        }
92
60.5k
        return ColumnNullable::create(src, ColumnUInt8::create(input_rows_count, 0));
93
60.6k
    }
94
95
9.40k
    return ColumnNullable::create(src_not_nullable, result_null_map_column);
96
70.0k
}
97
98
18.2k
bool have_null_column(const Block& block, const ColumnNumbers& args) {
99
33.3k
    return std::ranges::any_of(args, [&block](const auto& elem) {
100
33.3k
        return block.get_by_position(elem).type->is_nullable();
101
33.3k
    });
102
18.2k
}
103
104
13.3k
bool have_null_column(const ColumnsWithTypeAndName& args) {
105
13.3k
    return std::ranges::any_of(args, [](const auto& elem) { return elem.type->is_nullable(); });
106
13.3k
}
107
108
inline Status PreparedFunctionImpl::_execute_skipped_constant_deal(FunctionContext* context,
109
                                                                   Block& block,
110
                                                                   const ColumnNumbers& args,
111
                                                                   uint32_t result,
112
108k
                                                                   size_t input_rows_count) const {
113
108k
    bool executed = false;
114
108k
    RETURN_IF_ERROR(default_implementation_for_nulls(context, block, args, result, input_rows_count,
115
108k
                                                     &executed));
116
108k
    if (executed) {
117
13.1k
        return Status::OK();
118
13.1k
    }
119
95.7k
    return execute_impl(context, block, args, result, input_rows_count);
120
108k
}
121
122
Status PreparedFunctionImpl::default_implementation_for_constant_arguments(
123
        FunctionContext* context, Block& block, const ColumnNumbers& args, uint32_t result,
124
108k
        size_t input_rows_count, bool* executed) const {
125
108k
    *executed = false;
126
108k
    ColumnNumbers args_expect_const = get_arguments_that_are_always_constant();
127
128
    // Check that these arguments are really constant.
129
108k
    for (auto arg_num : args_expect_const) {
130
85.4k
        if (arg_num < args.size() &&
131
85.4k
            !is_column_const(*block.get_by_position(args[arg_num]).column)) {
132
0
            return Status::InvalidArgument("Argument at index {} for function {} must be constant",
133
0
                                           arg_num, get_name());
134
0
        }
135
85.4k
    }
136
137
108k
    if (args.empty() || !use_default_implementation_for_constants() ||
138
108k
        !VectorizedUtils::all_arguments_are_constant(block, args)) {
139
106k
        return Status::OK();
140
106k
    }
141
142
    // now all columns are const.
143
2.80k
    Block temporary_block;
144
145
2.80k
    int arguments_size = (int)args.size();
146
9.37k
    for (size_t arg_num = 0; arg_num < arguments_size; ++arg_num) {
147
6.57k
        const ColumnWithTypeAndName& column = block.get_by_position(args[arg_num]);
148
        // Columns in const_list --> column_const,    others --> nested_column
149
        // that's because some functions supposes some specific columns always constant.
150
        // If we unpack it, there will be unnecessary cost of virtual judge.
151
6.57k
        if (args_expect_const.end() !=
152
6.57k
            std::find(args_expect_const.begin(), args_expect_const.end(), arg_num)) {
153
0
            temporary_block.simple_insert({column.column, column.type, column.name});
154
6.57k
        } else {
155
6.57k
            temporary_block.simple_insert(
156
6.57k
                    {assert_cast<const ColumnConst*>(column.column.get())->get_data_column_ptr(),
157
6.57k
                     column.type, column.name});
158
6.57k
        }
159
6.57k
    }
160
161
2.80k
    temporary_block.simple_insert(block.get_by_position(result));
162
163
2.80k
    ColumnNumbers temporary_argument_numbers(arguments_size);
164
9.37k
    for (int i = 0; i < arguments_size; ++i) {
165
6.57k
        temporary_argument_numbers[i] = i;
166
6.57k
    }
167
168
2.80k
    RETURN_IF_ERROR(_execute_skipped_constant_deal(context, temporary_block,
169
2.80k
                                                   temporary_argument_numbers, arguments_size,
170
2.80k
                                                   temporary_block.rows()));
171
172
2.80k
    ColumnPtr result_column;
173
    /// extremely rare case, when we have function with completely const arguments
174
    /// but some of them produced by non is_deterministic function
175
2.80k
    if (temporary_block.get_by_position(arguments_size).column->size() > 1) {
176
0
        result_column = temporary_block.get_by_position(arguments_size).column->clone_resized(1);
177
2.80k
    } else {
178
2.80k
        result_column = temporary_block.get_by_position(arguments_size).column;
179
2.80k
    }
180
    // We shuold handle the case where the result column is also a ColumnConst.
181
2.80k
    block.get_by_position(result).column = ColumnConst::create(result_column, input_rows_count);
182
2.80k
    *executed = true;
183
2.80k
    return Status::OK();
184
2.80k
}
185
186
Status PreparedFunctionImpl::default_implementation_for_nulls(
187
        FunctionContext* context, Block& block, const ColumnNumbers& args, uint32_t result,
188
108k
        size_t input_rows_count, bool* executed) const {
189
108k
    *executed = false;
190
108k
    if (args.empty() || !use_default_implementation_for_nulls()) {
191
86.5k
        return Status::OK();
192
86.5k
    }
193
194
56.0k
    if (std::ranges::any_of(args, [&block](const auto& elem) {
195
56.0k
            return block.get_by_position(elem).column->only_null();
196
56.0k
        })) {
197
4.15k
        block.get_by_position(result).column =
198
4.15k
                block.get_by_position(result).type->create_column_const(input_rows_count, Field());
199
4.15k
        *executed = true;
200
4.15k
        return Status::OK();
201
4.15k
    }
202
203
18.2k
    if (have_null_column(block, args)) {
204
9.04k
        bool need_to_default = need_replace_null_data_to_default();
205
        // extract nested column from nulls
206
9.04k
        ColumnNumbers new_args;
207
9.04k
        Block new_block;
208
209
33.1k
        for (int i = 0; i < args.size(); ++i) {
210
24.0k
            uint32_t arg = args[i];
211
24.0k
            new_args.push_back(i);
212
24.0k
            new_block.simple_insert(block.get_by_position(arg).unnest_nullable(need_to_default));
213
24.0k
        }
214
9.04k
        new_block.simple_insert(block.get_by_position(result));
215
9.04k
        int new_result = new_block.columns() - 1;
216
217
9.04k
        RETURN_IF_ERROR(default_execute(context, new_block, new_args, new_result, block.rows()));
218
        // After run with nested, wrap them in null. Before this, block.get_by_position(result).type
219
        // is not compatible with get_by_position(result).column
220
221
9.04k
        block.get_by_position(result).column = wrap_in_nullable(
222
9.04k
                new_block.get_by_position(new_result).column, block, args, input_rows_count);
223
224
9.04k
        *executed = true;
225
9.04k
        return Status::OK();
226
9.04k
    }
227
9.19k
    return Status::OK();
228
18.2k
}
229
230
Status PreparedFunctionImpl::default_execute(FunctionContext* context, Block& block,
231
                                             const ColumnNumbers& args, uint32_t result,
232
108k
                                             size_t input_rows_count) const {
233
108k
    bool executed = false;
234
235
108k
    RETURN_IF_ERROR(default_implementation_for_constant_arguments(context, block, args, result,
236
108k
                                                                  input_rows_count, &executed));
237
108k
    if (executed) {
238
2.80k
        return Status::OK();
239
2.80k
    }
240
241
106k
    return _execute_skipped_constant_deal(context, block, args, result, input_rows_count);
242
108k
}
243
244
Status PreparedFunctionImpl::execute(FunctionContext* context, Block& block,
245
                                     const ColumnNumbers& args, uint32_t result,
246
99.9k
                                     size_t input_rows_count) const {
247
99.9k
    return default_execute(context, block, args, result, input_rows_count);
248
99.9k
}
249
250
14.5k
void FunctionBuilderImpl::check_number_of_arguments(size_t number_of_arguments) const {
251
14.5k
    if (is_variadic()) {
252
3.96k
        return;
253
3.96k
    }
254
255
10.5k
    size_t expected_number_of_arguments = get_number_of_arguments();
256
257
10.5k
    DCHECK_EQ(number_of_arguments, expected_number_of_arguments) << fmt::format(
258
0
            "Number of arguments for function {} doesn't match: passed {} , should be {}",
259
0
            get_name(), number_of_arguments, expected_number_of_arguments);
260
10.5k
    if (number_of_arguments != expected_number_of_arguments) {
261
0
        throw Exception(
262
0
                ErrorCode::INVALID_ARGUMENT,
263
0
                "Number of arguments for function {} doesn't match: passed {} , should be {}",
264
0
                get_name(), number_of_arguments, expected_number_of_arguments);
265
0
    }
266
10.5k
}
267
268
14.5k
DataTypePtr FunctionBuilderImpl::get_return_type(const ColumnsWithTypeAndName& arguments) const {
269
14.5k
    check_number_of_arguments(arguments.size());
270
271
14.5k
    if (!arguments.empty() && use_default_implementation_for_nulls()) {
272
13.3k
        if (have_null_column(arguments)) {
273
13.2k
            ColumnNumbers numbers(arguments.size());
274
13.2k
            std::iota(numbers.begin(), numbers.end(), 0);
275
13.2k
            auto [nested_block, _] =
276
13.2k
                    create_block_with_nested_columns(Block(arguments), numbers, false);
277
13.2k
            auto return_type = get_return_type_impl(
278
13.2k
                    ColumnsWithTypeAndName(nested_block.begin(), nested_block.end()));
279
13.2k
            return make_nullable(return_type);
280
13.2k
        }
281
13.3k
    }
282
283
1.29k
    return get_return_type_impl(arguments);
284
14.5k
}
285
286
bool FunctionBuilderImpl::is_date_or_datetime_or_decimal(
287
11
        const DataTypePtr& return_type, const DataTypePtr& func_return_type) const {
288
11
    return (is_date_or_datetime(return_type->get_primitive_type()) &&
289
11
            is_date_or_datetime(func_return_type->get_primitive_type())) ||
290
11
           (is_date_v2_or_datetime_v2(return_type->get_primitive_type()) &&
291
11
            is_date_v2_or_datetime_v2(func_return_type->get_primitive_type())) ||
292
           // For some date functions such as str_to_date(string, string), return_type will
293
           // be datetimev2 if users enable datev2 but get_return_type(arguments) will still
294
           // return datetime. We need keep backward compatibility here.
295
11
           (is_date_v2_or_datetime_v2(return_type->get_primitive_type()) &&
296
6
            is_date_or_datetime(func_return_type->get_primitive_type())) ||
297
11
           (is_date_or_datetime(return_type->get_primitive_type()) &&
298
6
            is_date_v2_or_datetime_v2(func_return_type->get_primitive_type())) ||
299
11
           (is_decimal(return_type->get_primitive_type()) &&
300
6
            is_decimal(func_return_type->get_primitive_type())) ||
301
11
           (is_time_type(return_type->get_primitive_type()) &&
302
4
            is_time_type(func_return_type->get_primitive_type()));
303
11
}
304
305
0
bool contains_date_or_datetime_or_decimal(const DataTypePtr& type) {
306
0
    auto type_ptr = type->is_nullable() ? ((DataTypeNullable*)type.get())->get_nested_type() : type;
307
308
0
    switch (type_ptr->get_primitive_type()) {
309
0
    case TYPE_ARRAY: {
310
0
        const auto* array_type = assert_cast<const DataTypeArray*>(type_ptr.get());
311
0
        return contains_date_or_datetime_or_decimal(array_type->get_nested_type());
312
0
    }
313
0
    case TYPE_MAP: {
314
0
        const auto* map_type = assert_cast<const DataTypeMap*>(type_ptr.get());
315
0
        return contains_date_or_datetime_or_decimal(map_type->get_key_type()) ||
316
0
               contains_date_or_datetime_or_decimal(map_type->get_value_type());
317
0
    }
318
0
    case TYPE_STRUCT: {
319
0
        const auto* struct_type = assert_cast<const DataTypeStruct*>(type_ptr.get());
320
0
        const auto& elements = struct_type->get_elements();
321
0
        return std::ranges::any_of(elements, [](const DataTypePtr& element) {
322
0
            return contains_date_or_datetime_or_decimal(element);
323
0
        });
324
0
    }
325
0
    default:
326
        // For scalar types, check if it's date/datetime/decimal
327
0
        return is_date_or_datetime(type_ptr->get_primitive_type()) ||
328
0
               is_date_v2_or_datetime_v2(type_ptr->get_primitive_type()) ||
329
0
               is_decimal(type_ptr->get_primitive_type()) ||
330
0
               is_time_type(type_ptr->get_primitive_type());
331
0
    }
332
0
}
333
334
// make sure array/map/struct and nested  array/map/struct can be check
335
bool FunctionBuilderImpl::is_nested_type_date_or_datetime_or_decimal(
336
2
        const DataTypePtr& return_type, const DataTypePtr& func_return_type) const {
337
2
    auto return_type_ptr = return_type->is_nullable()
338
2
                                   ? ((DataTypeNullable*)return_type.get())->get_nested_type()
339
2
                                   : return_type;
340
2
    auto func_return_type_ptr =
341
2
            func_return_type->is_nullable()
342
2
                    ? ((DataTypeNullable*)func_return_type.get())->get_nested_type()
343
2
                    : func_return_type;
344
    // make sure that map/struct/array also need to check
345
2
    if (return_type_ptr->get_primitive_type() != func_return_type_ptr->get_primitive_type()) {
346
2
        return false;
347
2
    }
348
349
    // Check if this type contains date/datetime/decimal types
350
0
    if (!contains_date_or_datetime_or_decimal(return_type_ptr)) {
351
        // If no date/datetime/decimal types, just pass through
352
0
        return true;
353
0
    }
354
355
    // If contains date/datetime/decimal types, recursively check each element
356
0
    switch (return_type_ptr->get_primitive_type()) {
357
0
    case TYPE_ARRAY: {
358
0
        auto nested_return_type = remove_nullable(
359
0
                (assert_cast<const DataTypeArray*>(return_type_ptr.get()))->get_nested_type());
360
0
        auto nested_func_type = remove_nullable(
361
0
                (assert_cast<const DataTypeArray*>(func_return_type_ptr.get()))->get_nested_type());
362
0
        return is_nested_type_date_or_datetime_or_decimal(nested_return_type, nested_func_type);
363
0
    }
364
0
    case TYPE_MAP: {
365
0
        const auto* return_map = assert_cast<const DataTypeMap*>(return_type_ptr.get());
366
0
        const auto* func_map = assert_cast<const DataTypeMap*>(func_return_type_ptr.get());
367
368
0
        auto key_return = remove_nullable(return_map->get_key_type());
369
0
        auto key_func = remove_nullable(func_map->get_key_type());
370
0
        auto value_return = remove_nullable(return_map->get_value_type());
371
0
        auto value_func = remove_nullable(func_map->get_value_type());
372
373
0
        return is_nested_type_date_or_datetime_or_decimal(key_return, key_func) &&
374
0
               is_nested_type_date_or_datetime_or_decimal(value_return, value_func);
375
0
    }
376
0
    case TYPE_STRUCT: {
377
0
        const auto* return_struct = assert_cast<const DataTypeStruct*>(return_type_ptr.get());
378
0
        const auto* func_struct = assert_cast<const DataTypeStruct*>(func_return_type_ptr.get());
379
380
0
        auto return_elements = return_struct->get_elements();
381
0
        auto func_elements = func_struct->get_elements();
382
383
0
        if (return_elements.size() != func_elements.size()) {
384
0
            return false;
385
0
        }
386
387
0
        for (size_t i = 0; i < return_elements.size(); i++) {
388
0
            auto elem_return = remove_nullable(return_elements[i]);
389
0
            auto elem_func = remove_nullable(func_elements[i]);
390
391
0
            if (!is_nested_type_date_or_datetime_or_decimal(elem_return, elem_func)) {
392
0
                return false;
393
0
            }
394
0
        }
395
0
        return true;
396
0
    }
397
0
    default:
398
0
        return is_date_or_datetime_or_decimal(return_type_ptr, func_return_type_ptr);
399
0
    }
400
0
}
401
402
#include "common/compile_check_end.h"
403
} // namespace doris::vectorized