Coverage Report

Created: 2025-09-22 13:36

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 "vec/aggregate_functions/aggregate_function.h"
29
#include "vec/columns/column.h"
30
#include "vec/columns/column_const.h"
31
#include "vec/columns/column_nullable.h"
32
#include "vec/columns/column_vector.h"
33
#include "vec/common/assert_cast.h"
34
#include "vec/core/field.h"
35
#include "vec/data_types/data_type_array.h"
36
#include "vec/data_types/data_type_nothing.h"
37
#include "vec/data_types/data_type_nullable.h"
38
#include "vec/functions/function_helpers.h"
39
#include "vec/utils/util.hpp"
40
41
namespace doris::vectorized {
42
#include "common/compile_check_begin.h"
43
ColumnPtr wrap_in_nullable(const ColumnPtr& src, const Block& block, const ColumnNumbers& args,
44
68.7k
                           uint32_t result, size_t input_rows_count) {
45
68.7k
    ColumnPtr result_null_map_column;
46
    /// If result is already nullable.
47
68.7k
    ColumnPtr src_not_nullable = src;
48
68.7k
    MutableColumnPtr mutable_result_null_map_column;
49
50
68.7k
    if (const auto* nullable = check_and_get_column<ColumnNullable>(*src)) {
51
8.43k
        src_not_nullable = nullable->get_nested_column_ptr();
52
8.43k
        result_null_map_column = nullable->get_null_map_column_ptr();
53
8.43k
    }
54
55
93.0k
    for (const auto& arg : args) {
56
93.0k
        const ColumnWithTypeAndName& elem = block.get_by_position(arg);
57
93.0k
        if (!elem.type->is_nullable() || is_column_const(*elem.column)) {
58
19.7k
            continue;
59
19.7k
        }
60
61
73.3k
        if (const auto* nullable = assert_cast<const ColumnNullable*>(elem.column.get());
62
73.3k
            nullable->has_null()) {
63
978
            const ColumnPtr& null_map_column = nullable->get_null_map_column_ptr();
64
978
            if (!result_null_map_column) { // NOLINT(bugprone-use-after-move)
65
886
                result_null_map_column = null_map_column->clone_resized(input_rows_count);
66
886
                continue;
67
886
            }
68
69
92
            if (!mutable_result_null_map_column) {
70
68
                mutable_result_null_map_column =
71
68
                        std::move(result_null_map_column)->assume_mutable();
72
68
            }
73
74
92
            NullMap& result_null_map =
75
92
                    assert_cast<ColumnUInt8&>(*mutable_result_null_map_column).get_data();
76
92
            const NullMap& src_null_map =
77
92
                    assert_cast<const ColumnUInt8&>(*null_map_column).get_data();
78
79
92
            VectorizedUtils::update_null_map(result_null_map, src_null_map);
80
92
        }
81
73.3k
    }
82
83
68.7k
    if (!result_null_map_column) {
84
59.4k
        if (is_column_const(*src)) {
85
0
            return ColumnConst::create(
86
0
                    make_nullable(assert_cast<const ColumnConst&>(*src).get_data_column_ptr(),
87
0
                                  false),
88
0
                    input_rows_count);
89
0
        }
90
59.4k
        return ColumnNullable::create(src, ColumnUInt8::create(input_rows_count, 0));
91
59.4k
    }
92
93
9.32k
    return ColumnNullable::create(src_not_nullable, result_null_map_column);
94
68.7k
}
95
96
15.8k
bool have_null_column(const Block& block, const ColumnNumbers& args) {
97
28.7k
    return std::ranges::any_of(args, [&block](const auto& elem) {
98
28.7k
        return block.get_by_position(elem).type->is_nullable();
99
28.7k
    });
100
15.8k
}
101
102
11.3k
bool have_null_column(const ColumnsWithTypeAndName& args) {
103
11.4k
    return std::ranges::any_of(args, [](const auto& elem) { return elem.type->is_nullable(); });
104
11.3k
}
105
106
inline Status PreparedFunctionImpl::_execute_skipped_constant_deal(
107
        FunctionContext* context, Block& block, const ColumnNumbers& args, uint32_t result,
108
105k
        size_t input_rows_count, bool dry_run) const {
109
105k
    bool executed = false;
110
105k
    RETURN_IF_ERROR(default_implementation_for_nulls(context, block, args, result, input_rows_count,
111
105k
                                                     dry_run, &executed));
112
105k
    if (executed) {
113
11.2k
        return Status::OK();
114
11.2k
    }
115
116
94.4k
    if (dry_run) {
117
0
        return execute_impl_dry_run(context, block, args, result, input_rows_count);
118
94.4k
    } else {
119
94.4k
        return execute_impl(context, block, args, result, input_rows_count);
120
94.4k
    }
121
94.4k
}
122
123
Status PreparedFunctionImpl::default_implementation_for_constant_arguments(
124
        FunctionContext* context, Block& block, const ColumnNumbers& args, uint32_t result,
125
105k
        size_t input_rows_count, bool dry_run, bool* executed) const {
126
105k
    *executed = false;
127
105k
    ColumnNumbers args_expect_const = get_arguments_that_are_always_constant();
128
129
    // Check that these arguments are really constant.
130
105k
    for (auto arg_num : args_expect_const) {
131
85.4k
        if (arg_num < args.size() &&
132
85.4k
            !is_column_const(*block.get_by_position(args[arg_num]).column)) {
133
0
            return Status::InvalidArgument("Argument at index {} for function {} must be constant",
134
0
                                           arg_num, get_name());
135
0
        }
136
85.4k
    }
137
138
105k
    if (args.empty() || !use_default_implementation_for_constants() ||
139
105k
        !VectorizedUtils::all_arguments_are_constant(block, args)) {
140
103k
        return Status::OK();
141
103k
    }
142
143
    // now all columns are const.
144
2.40k
    Block temporary_block;
145
146
2.40k
    int arguments_size = (int)args.size();
147
8.05k
    for (size_t arg_num = 0; arg_num < arguments_size; ++arg_num) {
148
5.64k
        const ColumnWithTypeAndName& column = block.get_by_position(args[arg_num]);
149
        // Columns in const_list --> column_const,    others --> nested_column
150
        // that's because some functions supposes some specific columns always constant.
151
        // If we unpack it, there will be unnecessary cost of virtual judge.
152
5.64k
        if (args_expect_const.end() !=
153
5.64k
            std::find(args_expect_const.begin(), args_expect_const.end(), arg_num)) {
154
0
            temporary_block.insert({column.column, column.type, column.name});
155
5.64k
        } else {
156
5.64k
            temporary_block.insert(
157
5.64k
                    {assert_cast<const ColumnConst*>(column.column.get())->get_data_column_ptr(),
158
5.64k
                     column.type, column.name});
159
5.64k
        }
160
5.64k
    }
161
162
2.40k
    temporary_block.insert(block.get_by_position(result));
163
164
2.40k
    ColumnNumbers temporary_argument_numbers(arguments_size);
165
8.05k
    for (int i = 0; i < arguments_size; ++i) {
166
5.64k
        temporary_argument_numbers[i] = i;
167
5.64k
    }
168
169
2.40k
    RETURN_IF_ERROR(_execute_skipped_constant_deal(context, temporary_block,
170
2.40k
                                                   temporary_argument_numbers, arguments_size,
171
2.40k
                                                   temporary_block.rows(), dry_run));
172
173
2.40k
    ColumnPtr result_column;
174
    /// extremely rare case, when we have function with completely const arguments
175
    /// but some of them produced by non is_deterministic function
176
2.40k
    if (temporary_block.get_by_position(arguments_size).column->size() > 1) {
177
0
        result_column = temporary_block.get_by_position(arguments_size).column->clone_resized(1);
178
2.40k
    } else {
179
2.40k
        result_column = temporary_block.get_by_position(arguments_size).column;
180
2.40k
    }
181
    // We shuold handle the case where the result column is also a ColumnConst.
182
2.40k
    block.get_by_position(result).column = ColumnConst::create(result_column, input_rows_count);
183
2.40k
    *executed = true;
184
2.40k
    return Status::OK();
185
2.40k
}
186
187
Status PreparedFunctionImpl::default_implementation_for_nulls(
188
        FunctionContext* context, Block& block, const ColumnNumbers& args, uint32_t result,
189
105k
        size_t input_rows_count, bool dry_run, bool* executed) const {
190
105k
    *executed = false;
191
105k
    if (args.empty() || !use_default_implementation_for_nulls()) {
192
86.5k
        return Status::OK();
193
86.5k
    }
194
195
47.7k
    if (std::ranges::any_of(args, [&block](const auto& elem) {
196
47.7k
            return block.get_by_position(elem).column->only_null();
197
47.7k
        })) {
198
3.37k
        block.get_by_position(result).column =
199
3.37k
                block.get_by_position(result).type->create_column_const(input_rows_count, Field());
200
3.37k
        *executed = true;
201
3.37k
        return Status::OK();
202
3.37k
    }
203
204
15.8k
    if (have_null_column(block, args)) {
205
7.83k
        bool need_to_default = need_replace_null_data_to_default();
206
        // extract nested column from nulls
207
7.83k
        ColumnNumbers new_args;
208
20.7k
        for (auto arg : args) {
209
20.7k
            new_args.push_back(block.columns());
210
20.7k
            block.insert(block.get_by_position(arg).unnest_nullable(need_to_default));
211
20.7k
            DCHECK(!block.get_by_position(new_args.back()).column->is_nullable());
212
20.7k
        }
213
7.83k
        RETURN_IF_ERROR(execute_without_low_cardinality_columns(context, block, new_args, result,
214
7.83k
                                                                block.rows(), dry_run));
215
        // After run with nested, wrap them in null. Before this, block.get_by_position(result).type
216
        // is not compatible with get_by_position(result).column
217
7.83k
        block.get_by_position(result).column = wrap_in_nullable(
218
7.83k
                block.get_by_position(result).column, block, args, result, input_rows_count);
219
220
28.6k
        while (!new_args.empty()) {
221
20.7k
            block.erase(new_args.back());
222
20.7k
            new_args.pop_back();
223
20.7k
        }
224
7.83k
        *executed = true;
225
7.83k
        return Status::OK();
226
7.83k
    }
227
7.97k
    return Status::OK();
228
15.8k
}
229
230
Status PreparedFunctionImpl::execute_without_low_cardinality_columns(
231
        FunctionContext* context, Block& block, const ColumnNumbers& args, uint32_t result,
232
105k
        size_t input_rows_count, bool dry_run) const {
233
105k
    bool executed = false;
234
235
105k
    RETURN_IF_ERROR(default_implementation_for_constant_arguments(
236
105k
            context, block, args, result, input_rows_count, dry_run, &executed));
237
105k
    if (executed) {
238
2.40k
        return Status::OK();
239
2.40k
    }
240
241
103k
    return _execute_skipped_constant_deal(context, block, args, result, input_rows_count, dry_run);
242
105k
}
243
244
Status PreparedFunctionImpl::execute(FunctionContext* context, Block& block,
245
                                     const ColumnNumbers& args, uint32_t result,
246
97.8k
                                     size_t input_rows_count, bool dry_run) const {
247
97.8k
    return execute_without_low_cardinality_columns(context, block, args, result, input_rows_count,
248
97.8k
                                                   dry_run);
249
97.8k
}
250
251
12.5k
void FunctionBuilderImpl::check_number_of_arguments(size_t number_of_arguments) const {
252
12.5k
    if (is_variadic()) {
253
3.83k
        return;
254
3.83k
    }
255
256
8.70k
    size_t expected_number_of_arguments = get_number_of_arguments();
257
258
8.70k
    DCHECK_EQ(number_of_arguments, expected_number_of_arguments) << fmt::format(
259
0
            "Number of arguments for function {} doesn't match: passed {} , should be {}",
260
0
            get_name(), number_of_arguments, expected_number_of_arguments);
261
8.70k
    if (number_of_arguments != expected_number_of_arguments) {
262
0
        throw Exception(
263
0
                ErrorCode::INVALID_ARGUMENT,
264
0
                "Number of arguments for function {} doesn't match: passed {} , should be {}",
265
0
                get_name(), number_of_arguments, expected_number_of_arguments);
266
0
    }
267
8.70k
}
268
269
12.5k
DataTypePtr FunctionBuilderImpl::get_return_type(const ColumnsWithTypeAndName& arguments) const {
270
12.5k
    check_number_of_arguments(arguments.size());
271
272
12.5k
    if (!arguments.empty() && use_default_implementation_for_nulls()) {
273
11.3k
        if (have_null_column(arguments)) {
274
11.2k
            ColumnNumbers numbers(arguments.size());
275
11.2k
            std::iota(numbers.begin(), numbers.end(), 0);
276
11.2k
            auto [nested_block, _] =
277
11.2k
                    create_block_with_nested_columns(Block(arguments), numbers, false);
278
11.2k
            auto return_type = get_return_type_impl(
279
11.2k
                    ColumnsWithTypeAndName(nested_block.begin(), nested_block.end()));
280
11.2k
            return make_nullable(return_type);
281
11.2k
        }
282
11.3k
    }
283
284
1.29k
    return get_return_type_impl(arguments);
285
12.5k
}
286
287
bool FunctionBuilderImpl::is_date_or_datetime_or_decimal(
288
4
        const DataTypePtr& return_type, const DataTypePtr& func_return_type) const {
289
4
    return (is_date_or_datetime(return_type->get_primitive_type()) &&
290
4
            is_date_or_datetime(func_return_type->get_primitive_type())) ||
291
4
           (is_date_v2_or_datetime_v2(return_type->get_primitive_type()) &&
292
3
            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
4
           (is_date_v2_or_datetime_v2(return_type->get_primitive_type()) &&
297
3
            is_date_or_datetime(func_return_type->get_primitive_type())) ||
298
4
           (is_date_or_datetime(return_type->get_primitive_type()) &&
299
3
            is_date_v2_or_datetime_v2(func_return_type->get_primitive_type())) ||
300
4
           (is_decimal(return_type->get_primitive_type()) &&
301
3
            is_decimal(func_return_type->get_primitive_type()));
302
4
}
303
304
bool FunctionBuilderImpl::is_array_nested_type_date_or_datetime_or_decimal(
305
1
        const DataTypePtr& return_type, const DataTypePtr& func_return_type) const {
306
1
    auto return_type_ptr = return_type->is_nullable()
307
1
                                   ? ((DataTypeNullable*)return_type.get())->get_nested_type()
308
1
                                   : return_type;
309
1
    auto func_return_type_ptr =
310
1
            func_return_type->is_nullable()
311
1
                    ? ((DataTypeNullable*)func_return_type.get())->get_nested_type()
312
1
                    : func_return_type;
313
1
    if (!(return_type_ptr->get_primitive_type() == TYPE_ARRAY &&
314
1
          func_return_type_ptr->get_primitive_type() == TYPE_ARRAY)) {
315
1
        return false;
316
1
    }
317
0
    auto nested_nullable_return_type_ptr =
318
0
            (assert_cast<const DataTypeArray*>(return_type_ptr.get()))->get_nested_type();
319
0
    auto nested_nullable_func_return_type =
320
0
            (assert_cast<const DataTypeArray*>(func_return_type_ptr.get()))->get_nested_type();
321
    // There must be nullable inside array type.
322
0
    if (nested_nullable_return_type_ptr->is_nullable() &&
323
0
        nested_nullable_func_return_type->is_nullable()) {
324
0
        auto nested_return_type_ptr =
325
0
                ((DataTypeNullable*)(nested_nullable_return_type_ptr.get()))->get_nested_type();
326
0
        auto nested_func_return_type_ptr =
327
0
                ((DataTypeNullable*)(nested_nullable_func_return_type.get()))->get_nested_type();
328
0
        return is_date_or_datetime_or_decimal(nested_return_type_ptr, nested_func_return_type_ptr);
329
0
    }
330
0
    return false;
331
0
}
332
#include "common/compile_check_end.h"
333
} // namespace doris::vectorized