Coverage Report

Created: 2026-04-11 00:05

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