Coverage Report

Created: 2025-12-26 18:42

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