Coverage Report

Created: 2026-08-04 01:35

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
#include "storage/index/zone_map/zonemap_eval_context.h"
43
44
namespace doris {
45
ColumnPtr wrap_in_nullable(const ColumnPtr& src, const Block& block, const ColumnNumbers& args,
46
                           const NullableColumnInfos& nullable_column_infos,
47
70.9k
                           size_t input_rows_count) {
48
70.9k
    ColumnPtr result_null_map_column;
49
    /// If result is already nullable.
50
70.9k
    ColumnPtr src_not_nullable = src;
51
70.9k
    MutableColumnPtr mutable_result_null_map_column;
52
53
70.9k
    if (auto nullable = check_and_get_column_ptr<ColumnNullable>(src)) {
54
8.69k
        src_not_nullable = nullable->get_nested_column_ptr();
55
8.69k
        result_null_map_column = nullable->get_null_map_column_ptr();
56
8.69k
    }
57
58
97.5k
    for (const auto& arg : args) {
59
97.5k
        const auto& info = nullable_column_infos[arg];
60
97.5k
        if (!info.is_nullable || info.is_const) {
61
21.1k
            continue;
62
21.1k
        }
63
64
76.3k
        if (info.has_null) {
65
1.05k
            const auto& null_map_column = block.get_by_position(arg).get_nullable_null_map_column();
66
1.05k
            if (!result_null_map_column) { // NOLINT(bugprone-use-after-move)
67
937
                result_null_map_column = null_map_column;
68
937
                continue;
69
937
            }
70
71
117
            if (!mutable_result_null_map_column) {
72
92
                mutable_result_null_map_column = (*std::move(result_null_map_column)).mutate();
73
92
            }
74
75
117
            NullMap& result_null_map =
76
117
                    assert_cast<ColumnUInt8&>(*mutable_result_null_map_column).get_data();
77
117
            const NullMap& src_null_map = null_map_column->get_data();
78
79
117
            VectorizedUtils::update_null_map(result_null_map, src_null_map);
80
117
        }
81
76.3k
    }
82
83
    // Commit merged null map back: result_null_map_column was moved into
84
    // mutable_result_null_map_column when merging 2+ nullable args with nulls.
85
70.9k
    if (mutable_result_null_map_column) {
86
92
        result_null_map_column = std::move(mutable_result_null_map_column);
87
92
    }
88
89
70.9k
    if (!result_null_map_column) {
90
61.2k
        if (is_column_const(*src)) {
91
4
            return ColumnConst::create(
92
4
                    make_nullable(assert_cast<const ColumnConst&>(*src).get_data_column_ptr(),
93
4
                                  false),
94
4
                    input_rows_count);
95
4
        }
96
61.2k
        return ColumnNullable::create(src, ColumnUInt8::create(input_rows_count, 0));
97
61.2k
    }
98
99
9.63k
    return ColumnNullable::create(src_not_nullable, result_null_map_column);
100
70.9k
}
101
102
ColumnPtr wrap_in_nullable(const ColumnPtr& src, const Block& block, const ColumnNumbers& args,
103
14
                           size_t input_rows_count) {
104
14
    NullableColumnInfos nullable_column_infos(block.columns());
105
28
    for (const auto arg : args) {
106
28
        const auto& column = block.get_by_position(arg);
107
28
        if (column.type->is_nullable()) {
108
11
            nullable_column_infos[arg] = column.get_nullable_column_info();
109
11
        }
110
28
    }
111
14
    return wrap_in_nullable(src, block, args, nullable_column_infos, input_rows_count);
112
14
}
113
114
23.0k
bool have_null_column(const Block& block, const ColumnNumbers& args) {
115
38.3k
    return std::ranges::any_of(args, [&block](const auto& elem) {
116
38.3k
        return block.get_by_position(elem).type->is_nullable();
117
38.3k
    });
118
23.0k
}
119
120
13.7k
bool have_null_column(const ColumnsWithTypeAndName& args) {
121
13.8k
    return std::ranges::any_of(args, [](const auto& elem) { return elem.type->is_nullable(); });
122
13.7k
}
123
124
inline Status PreparedFunctionImpl::_execute_skipped_constant_deal(FunctionContext* context,
125
                                                                   Block& block,
126
                                                                   const ColumnNumbers& args,
127
                                                                   uint32_t result,
128
107k
                                                                   size_t input_rows_count) const {
129
107k
    bool executed = false;
130
107k
    RETURN_IF_ERROR(default_implementation_for_nulls(context, block, args, result, input_rows_count,
131
107k
                                                     &executed));
132
107k
    if (executed) {
133
13.4k
        return Status::OK();
134
13.4k
    }
135
94.4k
    return execute_impl(context, block, args, result, input_rows_count);
136
107k
}
137
138
Status PreparedFunctionImpl::default_implementation_for_constant_arguments(
139
        FunctionContext* context, Block& block, const ColumnNumbers& args, uint32_t result,
140
107k
        size_t input_rows_count, bool* executed) const {
141
107k
    *executed = false;
142
107k
    ColumnNumbers args_expect_const = get_arguments_that_are_always_constant();
143
144
    // Check that these arguments are really constant.
145
107k
    for (auto arg_num : args_expect_const) {
146
84.0k
        if (arg_num < args.size() &&
147
84.0k
            !is_column_const(*block.get_by_position(args[arg_num]).column)) {
148
0
            return Status::InvalidArgument("Argument at index {} for function {} must be constant",
149
0
                                           arg_num, get_name());
150
0
        }
151
84.0k
    }
152
153
107k
    if (args.empty() || !use_default_implementation_for_constants() ||
154
107k
        !VectorizedUtils::all_arguments_are_constant(block, args)) {
155
105k
        return Status::OK();
156
105k
    }
157
158
    // now all columns are const.
159
2.92k
    Block temporary_block;
160
161
2.92k
    int arguments_size = (int)args.size();
162
9.61k
    for (size_t arg_num = 0; arg_num < arguments_size; ++arg_num) {
163
6.69k
        const ColumnWithTypeAndName& column = block.get_by_position(args[arg_num]);
164
        // Columns in const_list --> column_const,    others --> nested_column
165
        // that's because some functions supposes some specific columns always constant.
166
        // If we unpack it, there will be unnecessary cost of virtual judge.
167
6.69k
        if (args_expect_const.end() !=
168
6.69k
            std::find(args_expect_const.begin(), args_expect_const.end(), arg_num)) {
169
0
            temporary_block.insert({column.column, column.type, column.name});
170
6.69k
        } else {
171
6.69k
            temporary_block.insert(
172
6.69k
                    {assert_cast<const ColumnConst*>(column.column.get())->get_data_column_ptr(),
173
6.69k
                     column.type, column.name});
174
6.69k
        }
175
6.69k
    }
176
177
2.92k
    temporary_block.insert(block.get_by_position(result));
178
179
2.92k
    ColumnNumbers temporary_argument_numbers(arguments_size);
180
9.61k
    for (int i = 0; i < arguments_size; ++i) {
181
6.69k
        temporary_argument_numbers[i] = i;
182
6.69k
    }
183
184
2.92k
    RETURN_IF_ERROR(_execute_skipped_constant_deal(context, temporary_block,
185
2.92k
                                                   temporary_argument_numbers, arguments_size,
186
2.92k
                                                   temporary_block.rows()));
187
188
2.92k
    ColumnPtr result_column;
189
    /// extremely rare case, when we have function with completely const arguments
190
    /// but some of them produced by non is_deterministic function
191
2.92k
    if (temporary_block.get_by_position(arguments_size).column->size() > 1) {
192
0
        result_column = temporary_block.get_by_position(arguments_size).column->clone_resized(1);
193
2.92k
    } else {
194
2.92k
        result_column = temporary_block.get_by_position(arguments_size).column;
195
2.92k
    }
196
    // We shuold handle the case where the result column is also a ColumnConst.
197
2.92k
    block.get_by_position(result).column = ColumnConst::create(result_column, input_rows_count);
198
2.92k
    *executed = true;
199
2.92k
    return Status::OK();
200
2.92k
}
201
202
Status PreparedFunctionImpl::default_implementation_for_nulls(
203
        FunctionContext* context, Block& block, const ColumnNumbers& args, uint32_t result,
204
107k
        size_t input_rows_count, bool* executed) const {
205
107k
    *executed = false;
206
107k
    if (args.empty() || !use_default_implementation_for_nulls()) {
207
84.9k
        return Status::OK();
208
84.9k
    }
209
210
22.9k
    if (have_null_column(block, args)) {
211
13.4k
        NullableColumnInfos nullable_column_infos(block.columns());
212
32.2k
        for (const auto arg : args) {
213
32.2k
            const auto& argument = block.get_by_position(arg);
214
32.2k
            if (!argument.type->is_nullable()) {
215
49
                continue;
216
49
            }
217
218
32.2k
            auto info = argument.get_nullable_column_info();
219
32.2k
            if (info.only_null) {
220
4.18k
                auto& result_column = block.get_by_position(result);
221
4.18k
                result_column.column =
222
4.18k
                        result_column.type->create_column_const(input_rows_count, Field());
223
4.18k
                *executed = true;
224
4.18k
                return Status::OK();
225
4.18k
            }
226
28.0k
            nullable_column_infos[arg] = info;
227
28.0k
        }
228
229
9.30k
        bool need_to_default = need_replace_null_data_to_default();
230
        // extract nested column from nulls
231
9.30k
        ColumnNumbers new_args;
232
9.30k
        Block new_block;
233
234
33.8k
        for (int i = 0; i < args.size(); ++i) {
235
24.5k
            uint32_t arg = args[i];
236
24.5k
            new_args.push_back(i);
237
24.5k
            new_block.insert(block.get_by_position(arg).unnest_nullable(nullable_column_infos[arg],
238
24.5k
                                                                        need_to_default));
239
24.5k
        }
240
9.30k
        new_block.insert(block.get_by_position(result));
241
9.30k
        int new_result = new_block.columns() - 1;
242
243
9.30k
        RETURN_IF_ERROR(default_execute(context, new_block, new_args, new_result, block.rows()));
244
        // After run with nested, wrap them in null. Before this, block.get_by_position(result).type
245
        // is not compatible with get_by_position(result).column
246
247
9.30k
        block.get_by_position(result).column =
248
9.30k
                wrap_in_nullable(new_block.get_by_position(new_result).column, block, args,
249
9.30k
                                 nullable_column_infos, input_rows_count);
250
251
9.30k
        *executed = true;
252
9.30k
        return Status::OK();
253
9.30k
    }
254
9.50k
    return Status::OK();
255
22.9k
}
256
257
Status PreparedFunctionImpl::default_execute(FunctionContext* context, Block& block,
258
                                             const ColumnNumbers& args, uint32_t result,
259
107k
                                             size_t input_rows_count) const {
260
107k
    bool executed = false;
261
262
107k
    RETURN_IF_ERROR(default_implementation_for_constant_arguments(context, block, args, result,
263
107k
                                                                  input_rows_count, &executed));
264
107k
    if (executed) {
265
2.92k
        return Status::OK();
266
2.92k
    }
267
268
105k
    return _execute_skipped_constant_deal(context, block, args, result, input_rows_count);
269
107k
}
270
271
Status PreparedFunctionImpl::execute(FunctionContext* context, Block& block,
272
                                     const ColumnNumbers& args, uint32_t result,
273
98.6k
                                     size_t input_rows_count) const {
274
98.6k
    return default_execute(context, block, args, result, input_rows_count);
275
98.6k
}
276
277
14.8k
void FunctionBuilderImpl::check_number_of_arguments(size_t number_of_arguments) const {
278
14.8k
    if (is_variadic()) {
279
4.10k
        return;
280
4.10k
    }
281
282
10.7k
    size_t expected_number_of_arguments = get_number_of_arguments();
283
284
10.7k
    DCHECK_EQ(number_of_arguments, expected_number_of_arguments) << fmt::format(
285
0
            "Number of arguments for function {} doesn't match: passed {} , should be {}",
286
0
            get_name(), number_of_arguments, expected_number_of_arguments);
287
10.7k
    if (number_of_arguments != expected_number_of_arguments) {
288
0
        throw Exception(
289
0
                ErrorCode::INVALID_ARGUMENT,
290
0
                "Number of arguments for function {} doesn't match: passed {} , should be {}",
291
0
                get_name(), number_of_arguments, expected_number_of_arguments);
292
0
    }
293
10.7k
}
294
295
14.8k
DataTypePtr FunctionBuilderImpl::get_return_type(const ColumnsWithTypeAndName& arguments) const {
296
14.8k
    check_number_of_arguments(arguments.size());
297
298
14.8k
    if (!arguments.empty() && use_default_implementation_for_nulls()) {
299
13.7k
        if (have_null_column(arguments)) {
300
13.5k
            ColumnNumbers numbers(arguments.size());
301
13.5k
            std::iota(numbers.begin(), numbers.end(), 0);
302
13.5k
            auto [nested_block, _] =
303
13.5k
                    create_block_with_nested_columns(Block(arguments), numbers, false);
304
13.5k
            auto return_type = get_return_type_impl(
305
13.5k
                    ColumnsWithTypeAndName(nested_block.begin(), nested_block.end()));
306
13.5k
            if (!return_type) {
307
0
                return nullptr;
308
0
            }
309
13.5k
            return make_nullable(return_type);
310
13.5k
        }
311
13.7k
    }
312
313
1.31k
    return get_return_type_impl(arguments);
314
14.8k
}
315
316
bool FunctionBuilderImpl::is_date_or_datetime_or_decimal(
317
65
        const DataTypePtr& return_type, const DataTypePtr& func_return_type) const {
318
65
    return (is_date_or_datetime(return_type->get_primitive_type()) &&
319
65
            is_date_or_datetime(func_return_type->get_primitive_type())) ||
320
65
           (is_date_v2_or_datetime_v2(return_type->get_primitive_type()) &&
321
65
            is_date_v2_or_datetime_v2(func_return_type->get_primitive_type())) ||
322
           // For some date functions such as str_to_date(string, string), return_type will
323
           // be datetimev2 if users enable datev2 but get_return_type(arguments) will still
324
           // return datetime. We need keep backward compatibility here.
325
65
           (is_date_v2_or_datetime_v2(return_type->get_primitive_type()) &&
326
34
            is_date_or_datetime(func_return_type->get_primitive_type())) ||
327
65
           (is_date_or_datetime(return_type->get_primitive_type()) &&
328
34
            is_date_v2_or_datetime_v2(func_return_type->get_primitive_type())) ||
329
65
           (is_decimal(return_type->get_primitive_type()) &&
330
34
            is_decimal(func_return_type->get_primitive_type())) ||
331
65
           (is_time_type(return_type->get_primitive_type()) &&
332
32
            is_time_type(func_return_type->get_primitive_type()));
333
65
}
334
335
28
bool contains_date_or_datetime_or_decimal(const DataTypePtr& type) {
336
28
    auto type_ptr = type->is_nullable() ? ((DataTypeNullable*)type.get())->get_nested_type() : type;
337
338
28
    switch (type_ptr->get_primitive_type()) {
339
0
    case TYPE_ARRAY: {
340
0
        const auto* array_type = assert_cast<const DataTypeArray*>(type_ptr.get());
341
0
        return contains_date_or_datetime_or_decimal(array_type->get_nested_type());
342
0
    }
343
0
    case TYPE_MAP: {
344
0
        const auto* map_type = assert_cast<const DataTypeMap*>(type_ptr.get());
345
0
        return contains_date_or_datetime_or_decimal(map_type->get_key_type()) ||
346
0
               contains_date_or_datetime_or_decimal(map_type->get_value_type());
347
0
    }
348
0
    case TYPE_STRUCT: {
349
0
        const auto* struct_type = assert_cast<const DataTypeStruct*>(type_ptr.get());
350
0
        const auto& elements = struct_type->get_elements();
351
0
        return std::ranges::any_of(elements, [](const DataTypePtr& element) {
352
0
            return contains_date_or_datetime_or_decimal(element);
353
0
        });
354
0
    }
355
28
    default:
356
        // For scalar types, check if it's date/datetime/decimal
357
28
        return is_date_or_datetime(type_ptr->get_primitive_type()) ||
358
28
               is_date_v2_or_datetime_v2(type_ptr->get_primitive_type()) ||
359
28
               is_decimal(type_ptr->get_primitive_type()) ||
360
28
               is_time_type(type_ptr->get_primitive_type());
361
28
    }
362
28
}
363
364
// make sure array/map/struct and nested  array/map/struct can be check
365
bool FunctionBuilderImpl::is_nested_type_date_or_datetime_or_decimal(
366
30
        const DataTypePtr& return_type, const DataTypePtr& func_return_type) const {
367
30
    auto return_type_ptr = return_type->is_nullable()
368
30
                                   ? ((DataTypeNullable*)return_type.get())->get_nested_type()
369
30
                                   : return_type;
370
30
    auto func_return_type_ptr =
371
30
            func_return_type->is_nullable()
372
30
                    ? ((DataTypeNullable*)func_return_type.get())->get_nested_type()
373
30
                    : func_return_type;
374
    // make sure that map/struct/array also need to check
375
30
    if (return_type_ptr->get_primitive_type() != func_return_type_ptr->get_primitive_type()) {
376
2
        return false;
377
2
    }
378
379
    // Check if this type contains date/datetime/decimal types
380
28
    if (!contains_date_or_datetime_or_decimal(return_type_ptr)) {
381
        // If no date/datetime/decimal types, just pass through
382
28
        return true;
383
28
    }
384
385
    // If contains date/datetime/decimal types, recursively check each element
386
0
    switch (return_type_ptr->get_primitive_type()) {
387
0
    case TYPE_ARRAY: {
388
0
        auto nested_return_type = remove_nullable(
389
0
                (assert_cast<const DataTypeArray*>(return_type_ptr.get()))->get_nested_type());
390
0
        auto nested_func_type = remove_nullable(
391
0
                (assert_cast<const DataTypeArray*>(func_return_type_ptr.get()))->get_nested_type());
392
0
        return is_nested_type_date_or_datetime_or_decimal(nested_return_type, nested_func_type);
393
0
    }
394
0
    case TYPE_MAP: {
395
0
        const auto* return_map = assert_cast<const DataTypeMap*>(return_type_ptr.get());
396
0
        const auto* func_map = assert_cast<const DataTypeMap*>(func_return_type_ptr.get());
397
398
0
        auto key_return = remove_nullable(return_map->get_key_type());
399
0
        auto key_func = remove_nullable(func_map->get_key_type());
400
0
        auto value_return = remove_nullable(return_map->get_value_type());
401
0
        auto value_func = remove_nullable(func_map->get_value_type());
402
403
0
        return is_nested_type_date_or_datetime_or_decimal(key_return, key_func) &&
404
0
               is_nested_type_date_or_datetime_or_decimal(value_return, value_func);
405
0
    }
406
0
    case TYPE_STRUCT: {
407
0
        const auto* return_struct = assert_cast<const DataTypeStruct*>(return_type_ptr.get());
408
0
        const auto* func_struct = assert_cast<const DataTypeStruct*>(func_return_type_ptr.get());
409
410
0
        auto return_elements = return_struct->get_elements();
411
0
        auto func_elements = func_struct->get_elements();
412
413
0
        if (return_elements.size() != func_elements.size()) {
414
0
            return false;
415
0
        }
416
417
0
        for (size_t i = 0; i < return_elements.size(); i++) {
418
0
            auto elem_return = remove_nullable(return_elements[i]);
419
0
            auto elem_func = remove_nullable(func_elements[i]);
420
421
0
            if (!is_nested_type_date_or_datetime_or_decimal(elem_return, elem_func)) {
422
0
                return false;
423
0
            }
424
0
        }
425
0
        return true;
426
0
    }
427
0
    default:
428
0
        return is_date_or_datetime_or_decimal(return_type_ptr, func_return_type_ptr);
429
0
    }
430
0
}
431
432
ZoneMapFilterResult IFunctionBase::evaluate_zonemap_filter(
433
0
        const ZoneMapEvalContext& ctx, const VExprSPtrs& function_arguments) const {
434
0
    return unsupported_zonemap_filter(ctx);
435
0
}
436
437
ZoneMapFilterResult IFunctionBase::evaluate_dictionary_filter(
438
0
        const DictionaryEvalContext& ctx, const VExprSPtrs& function_arguments) const {
439
0
    return ZoneMapFilterResult::kUnsupported;
440
0
}
441
442
ZoneMapFilterResult IFunctionBase::evaluate_bloom_filter(
443
0
        const BloomFilterEvalContext& ctx, const VExprSPtrs& function_arguments) const {
444
0
    return ZoneMapFilterResult::kUnsupported;
445
0
}
446
447
} // namespace doris