Coverage Report

Created: 2025-07-23 18:49

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 "vec/aggregate_functions/aggregate_function.h"
28
#include "vec/columns/column.h"
29
#include "vec/columns/column_const.h"
30
#include "vec/columns/column_nullable.h"
31
#include "vec/columns/column_vector.h"
32
#include "vec/common/assert_cast.h"
33
#include "vec/core/field.h"
34
#include "vec/data_types/data_type_array.h"
35
#include "vec/data_types/data_type_nothing.h"
36
#include "vec/data_types/data_type_nullable.h"
37
#include "vec/functions/function_helpers.h"
38
#include "vec/utils/util.hpp"
39
40
namespace doris::vectorized {
41
#include "common/compile_check_begin.h"
42
ColumnPtr wrap_in_nullable(const ColumnPtr& src, const Block& block, const ColumnNumbers& args,
43
232k
                           uint32_t result, size_t input_rows_count) {
44
232k
    ColumnPtr result_null_map_column;
45
    /// If result is already nullable.
46
232k
    ColumnPtr src_not_nullable = src;
47
232k
    MutableColumnPtr mutable_result_null_map_column;
48
49
232k
    if (const auto* nullable = check_and_get_column<ColumnNullable>(*src)) {
50
81.4k
        src_not_nullable = nullable->get_nested_column_ptr();
51
81.4k
        result_null_map_column = nullable->get_null_map_column_ptr();
52
81.4k
    }
53
54
468k
    for (const auto& arg : args) {
55
468k
        const ColumnWithTypeAndName& elem = block.get_by_position(arg);
56
468k
        if (!elem.type->is_nullable() || is_column_const(*elem.column)) {
57
232k
            continue;
58
232k
        }
59
60
236k
        if (const auto* nullable = assert_cast<const ColumnNullable*>(elem.column.get());
61
236k
            nullable->has_null()) {
62
206
            const ColumnPtr& null_map_column = nullable->get_null_map_column_ptr();
63
206
            if (!result_null_map_column) { // NOLINT(bugprone-use-after-move)
64
115
                result_null_map_column = null_map_column->clone_resized(input_rows_count);
65
115
                continue;
66
115
            }
67
68
91
            if (!mutable_result_null_map_column) {
69
68
                mutable_result_null_map_column =
70
68
                        std::move(result_null_map_column)->assume_mutable();
71
68
            }
72
73
91
            NullMap& result_null_map =
74
91
                    assert_cast<ColumnUInt8&>(*mutable_result_null_map_column).get_data();
75
91
            const NullMap& src_null_map =
76
91
                    assert_cast<const ColumnUInt8&>(*null_map_column).get_data();
77
78
91
            VectorizedUtils::update_null_map(result_null_map, src_null_map);
79
91
        }
80
236k
    }
81
82
232k
    if (!result_null_map_column) {
83
150k
        if (is_column_const(*src)) {
84
0
            return ColumnConst::create(
85
0
                    make_nullable(assert_cast<const ColumnConst&>(*src).get_data_column_ptr(),
86
0
                                  false),
87
0
                    input_rows_count);
88
0
        }
89
150k
        return ColumnNullable::create(src, ColumnUInt8::create(input_rows_count, 0));
90
150k
    }
91
92
81.5k
    return ColumnNullable::create(src_not_nullable, result_null_map_column);
93
232k
}
94
95
17.0k
bool have_null_column(const Block& block, const ColumnNumbers& args) {
96
28.8k
    return std::ranges::any_of(args, [&block](const auto& elem) {
97
28.8k
        return block.get_by_position(elem).type->is_nullable();
98
28.8k
    });
99
17.0k
}
100
101
13.3k
bool have_null_column(const ColumnsWithTypeAndName& args) {
102
13.3k
    return std::ranges::any_of(args, [](const auto& elem) { return elem.type->is_nullable(); });
103
13.3k
}
104
105
inline Status PreparedFunctionImpl::_execute_skipped_constant_deal(
106
        FunctionContext* context, Block& block, const ColumnNumbers& args, uint32_t result,
107
275k
        size_t input_rows_count, bool dry_run) const {
108
275k
    bool executed = false;
109
275k
    RETURN_IF_ERROR(default_implementation_for_nulls(context, block, args, result, input_rows_count,
110
275k
                                                     dry_run, &executed));
111
275k
    if (executed) {
112
10.3k
        return Status::OK();
113
10.3k
    }
114
115
265k
    if (dry_run) {
116
0
        return execute_impl_dry_run(context, block, args, result, input_rows_count);
117
265k
    } else {
118
265k
        return execute_impl(context, block, args, result, input_rows_count);
119
265k
    }
120
265k
}
121
122
Status PreparedFunctionImpl::default_implementation_for_constant_arguments(
123
        FunctionContext* context, Block& block, const ColumnNumbers& args, uint32_t result,
124
275k
        size_t input_rows_count, bool dry_run, bool* executed) const {
125
275k
    *executed = false;
126
275k
    ColumnNumbers args_expect_const = get_arguments_that_are_always_constant();
127
128
    // Check that these arguments are really constant.
129
275k
    for (auto arg_num : args_expect_const) {
130
254k
        if (arg_num < args.size() &&
131
254k
            !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
254k
    }
136
137
275k
    if (args.empty() || !use_default_implementation_for_constants() ||
138
275k
        !VectorizedUtils::all_arguments_are_constant(block, args)) {
139
273k
        return Status::OK();
140
273k
    }
141
142
    // now all columns are const.
143
2.21k
    Block temporary_block;
144
145
2.21k
    int arguments_size = (int)args.size();
146
7.43k
    for (size_t arg_num = 0; arg_num < arguments_size; ++arg_num) {
147
5.21k
        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
5.21k
        if (args_expect_const.end() !=
152
5.21k
            std::find(args_expect_const.begin(), args_expect_const.end(), arg_num)) {
153
0
            temporary_block.insert({column.column, column.type, column.name});
154
5.21k
        } else {
155
5.21k
            temporary_block.insert(
156
5.21k
                    {assert_cast<const ColumnConst*>(column.column.get())->get_data_column_ptr(),
157
5.21k
                     column.type, column.name});
158
5.21k
        }
159
5.21k
    }
160
161
2.21k
    temporary_block.insert(block.get_by_position(result));
162
163
2.21k
    ColumnNumbers temporary_argument_numbers(arguments_size);
164
7.43k
    for (int i = 0; i < arguments_size; ++i) {
165
5.21k
        temporary_argument_numbers[i] = i;
166
5.21k
    }
167
168
2.21k
    RETURN_IF_ERROR(_execute_skipped_constant_deal(context, temporary_block,
169
2.21k
                                                   temporary_argument_numbers, arguments_size,
170
2.21k
                                                   temporary_block.rows(), dry_run));
171
172
2.21k
    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.21k
    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.21k
    } else {
178
2.21k
        result_column = temporary_block.get_by_position(arguments_size).column;
179
2.21k
    }
180
    // We shuold handle the case where the result column is also a ColumnConst.
181
2.21k
    block.get_by_position(result).column = ColumnConst::create(result_column, input_rows_count);
182
2.21k
    *executed = true;
183
2.21k
    return Status::OK();
184
2.21k
}
185
186
Status PreparedFunctionImpl::default_implementation_for_nulls(
187
        FunctionContext* context, Block& block, const ColumnNumbers& args, uint32_t result,
188
275k
        size_t input_rows_count, bool dry_run, bool* executed) const {
189
275k
    *executed = false;
190
275k
    if (args.empty() || !use_default_implementation_for_nulls()) {
191
255k
        return Status::OK();
192
255k
    }
193
194
46.6k
    if (std::ranges::any_of(args, [&block](const auto& elem) {
195
46.6k
            return block.get_by_position(elem).column->only_null();
196
46.6k
        })) {
197
3.30k
        block.get_by_position(result).column =
198
3.30k
                block.get_by_position(result).type->create_column_const(input_rows_count, Field());
199
3.30k
        *executed = true;
200
3.30k
        return Status::OK();
201
3.30k
    }
202
203
17.0k
    if (have_null_column(block, args)) {
204
6.99k
        bool need_to_default = need_replace_null_data_to_default();
205
6.99k
        if (context) {
206
6.99k
            need_to_default &= context->check_overflow_for_decimal();
207
6.99k
        }
208
        // extract nested column from nulls
209
6.99k
        ColumnNumbers new_args;
210
18.8k
        for (auto arg : args) {
211
18.8k
            new_args.push_back(block.columns());
212
18.8k
            block.insert(block.get_by_position(arg).get_nested(need_to_default));
213
18.8k
            DCHECK(!block.get_by_position(new_args.back()).column->is_nullable());
214
18.8k
        }
215
6.99k
        RETURN_IF_ERROR(execute_without_low_cardinality_columns(context, block, new_args, result,
216
6.99k
                                                                block.rows(), dry_run));
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
6.99k
        block.get_by_position(result).column = wrap_in_nullable(
220
6.99k
                block.get_by_position(result).column, block, args, result, input_rows_count);
221
222
25.8k
        while (!new_args.empty()) {
223
18.8k
            block.erase(new_args.back());
224
18.8k
            new_args.pop_back();
225
18.8k
        }
226
6.99k
        *executed = true;
227
6.99k
        return Status::OK();
228
6.99k
    }
229
10.0k
    return Status::OK();
230
17.0k
}
231
232
Status PreparedFunctionImpl::execute_without_low_cardinality_columns(
233
        FunctionContext* context, Block& block, const ColumnNumbers& args, uint32_t result,
234
275k
        size_t input_rows_count, bool dry_run) const {
235
275k
    bool executed = false;
236
237
275k
    RETURN_IF_ERROR(default_implementation_for_constant_arguments(
238
275k
            context, block, args, result, input_rows_count, dry_run, &executed));
239
275k
    if (executed) {
240
2.21k
        return Status::OK();
241
2.21k
    }
242
243
273k
    return _execute_skipped_constant_deal(context, block, args, result, input_rows_count, dry_run);
244
275k
}
245
246
Status PreparedFunctionImpl::execute(FunctionContext* context, Block& block,
247
                                     const ColumnNumbers& args, uint32_t result,
248
268k
                                     size_t input_rows_count, bool dry_run) const {
249
268k
    return execute_without_low_cardinality_columns(context, block, args, result, input_rows_count,
250
268k
                                                   dry_run);
251
268k
}
252
253
14.5k
void FunctionBuilderImpl::check_number_of_arguments(size_t number_of_arguments) const {
254
14.5k
    if (is_variadic()) {
255
4.44k
        return;
256
4.44k
    }
257
258
10.1k
    size_t expected_number_of_arguments = get_number_of_arguments();
259
260
10.1k
    CHECK_EQ(number_of_arguments, expected_number_of_arguments) << fmt::format(
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
10.1k
}
264
265
14.5k
DataTypePtr FunctionBuilderImpl::get_return_type(const ColumnsWithTypeAndName& arguments) const {
266
14.5k
    check_number_of_arguments(arguments.size());
267
268
14.5k
    if (!arguments.empty() && use_default_implementation_for_nulls()) {
269
13.3k
        if (have_null_column(arguments)) {
270
10.3k
            ColumnNumbers numbers(arguments.size());
271
10.3k
            std::iota(numbers.begin(), numbers.end(), 0);
272
10.3k
            auto [nested_block, _] =
273
10.3k
                    create_block_with_nested_columns(Block(arguments), numbers, false);
274
10.3k
            auto return_type = get_return_type_impl(
275
10.3k
                    ColumnsWithTypeAndName(nested_block.begin(), nested_block.end()));
276
10.3k
            return make_nullable(return_type);
277
10.3k
        }
278
13.3k
    }
279
280
4.24k
    return get_return_type_impl(arguments);
281
14.5k
}
282
283
bool FunctionBuilderImpl::is_date_or_datetime_or_decimal(
284
3
        const DataTypePtr& return_type, const DataTypePtr& func_return_type) const {
285
3
    return (is_date_or_datetime(return_type->get_primitive_type()) &&
286
3
            is_date_or_datetime(func_return_type->get_primitive_type())) ||
287
3
           (is_date_v2_or_datetime_v2(return_type->get_primitive_type()) &&
288
3
            is_date_v2_or_datetime_v2(func_return_type->get_primitive_type())) ||
289
           // For some date functions such as str_to_date(string, string), return_type will
290
           // be datetimev2 if users enable datev2 but get_return_type(arguments) will still
291
           // return datetime. We need keep backward compatibility here.
292
3
           (is_date_v2_or_datetime_v2(return_type->get_primitive_type()) &&
293
3
            is_date_or_datetime(func_return_type->get_primitive_type())) ||
294
3
           (is_date_or_datetime(return_type->get_primitive_type()) &&
295
3
            is_date_v2_or_datetime_v2(func_return_type->get_primitive_type())) ||
296
3
           (is_decimal(return_type->get_primitive_type()) &&
297
3
            is_decimal(func_return_type->get_primitive_type()));
298
3
}
299
300
bool FunctionBuilderImpl::is_array_nested_type_date_or_datetime_or_decimal(
301
0
        const DataTypePtr& return_type, const DataTypePtr& func_return_type) const {
302
0
    auto return_type_ptr = return_type->is_nullable()
303
0
                                   ? ((DataTypeNullable*)return_type.get())->get_nested_type()
304
0
                                   : return_type;
305
0
    auto func_return_type_ptr =
306
0
            func_return_type->is_nullable()
307
0
                    ? ((DataTypeNullable*)func_return_type.get())->get_nested_type()
308
0
                    : func_return_type;
309
0
    if (!(return_type_ptr->get_primitive_type() == TYPE_ARRAY &&
310
0
          func_return_type_ptr->get_primitive_type() == TYPE_ARRAY)) {
311
0
        return false;
312
0
    }
313
0
    auto nested_nullable_return_type_ptr =
314
0
            (assert_cast<const DataTypeArray*>(return_type_ptr.get()))->get_nested_type();
315
0
    auto nested_nullable_func_return_type =
316
0
            (assert_cast<const DataTypeArray*>(func_return_type_ptr.get()))->get_nested_type();
317
    // There must be nullable inside array type.
318
0
    if (nested_nullable_return_type_ptr->is_nullable() &&
319
0
        nested_nullable_func_return_type->is_nullable()) {
320
0
        auto nested_return_type_ptr =
321
0
                ((DataTypeNullable*)(nested_nullable_return_type_ptr.get()))->get_nested_type();
322
0
        auto nested_func_return_type_ptr =
323
0
                ((DataTypeNullable*)(nested_nullable_func_return_type.get()))->get_nested_type();
324
0
        return is_date_or_datetime_or_decimal(nested_return_type_ptr, nested_func_return_type_ptr);
325
0
    }
326
0
    return false;
327
0
}
328
#include "common/compile_check_end.h"
329
} // namespace doris::vectorized