Coverage Report

Created: 2024-11-21 18:14

/root/doris/be/src/vec/functions/function.cpp
Line
Count
Source (jump to first uncovered line)
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/columns/columns_number.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
43
ColumnPtr wrap_in_nullable(const ColumnPtr& src, const Block& block, const ColumnNumbers& args,
44
6.67k
                           uint32_t result, size_t input_rows_count) {
45
6.67k
    ColumnPtr result_null_map_column;
46
    /// If result is already nullable.
47
6.67k
    ColumnPtr src_not_nullable = src;
48
6.67k
    MutableColumnPtr mutable_result_null_map_column;
49
50
6.67k
    if (const auto* nullable = check_and_get_column<ColumnNullable>(*src)) {
51
2.04k
        src_not_nullable = nullable->get_nested_column_ptr();
52
2.04k
        result_null_map_column = nullable->get_null_map_column_ptr();
53
2.04k
    }
54
55
18.1k
    for (const auto& arg : args) {
56
18.1k
        const ColumnWithTypeAndName& elem = block.get_by_position(arg);
57
18.1k
        if (!elem.type->is_nullable() || is_column_const(*elem.column)) {
58
7.38k
            continue;
59
7.38k
        }
60
61
10.8k
        if (const auto* nullable = assert_cast<const ColumnNullable*>(elem.column.get());
62
10.8k
            nullable->has_null()) {
63
197
            const ColumnPtr& null_map_column = nullable->get_null_map_column_ptr();
64
197
            if (!result_null_map_column) { // NOLINT(bugprone-use-after-move)
65
87
                result_null_map_column = null_map_column->clone_resized(input_rows_count);
66
87
                continue;
67
87
            }
68
69
110
            if (!mutable_result_null_map_column) {
70
88
                mutable_result_null_map_column =
71
88
                        std::move(result_null_map_column)->assume_mutable();
72
88
            }
73
74
110
            NullMap& result_null_map =
75
110
                    assert_cast<ColumnUInt8&>(*mutable_result_null_map_column).get_data();
76
110
            const NullMap& src_null_map =
77
110
                    assert_cast<const ColumnUInt8&>(*null_map_column).get_data();
78
79
110
            VectorizedUtils::update_null_map(result_null_map, src_null_map);
80
110
        }
81
10.8k
    }
82
83
6.67k
    if (!result_null_map_column) {
84
4.54k
        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
4.54k
        return ColumnNullable::create(src, ColumnUInt8::create(input_rows_count, 0));
91
4.54k
    }
92
93
2.13k
    return ColumnNullable::create(src_not_nullable, result_null_map_column);
94
6.67k
}
95
96
16.1k
bool have_null_column(const Block& block, const ColumnNumbers& args) {
97
27.5k
    return std::ranges::any_of(args, [&block](const auto& elem) {
98
27.5k
        return block.get_by_position(elem).type->is_nullable();
99
27.5k
    });
100
16.1k
}
101
102
12.8k
bool have_null_column(const ColumnsWithTypeAndName& args) {
103
12.8k
    return std::ranges::any_of(args, [](const auto& elem) { return elem.type->is_nullable(); });
104
12.8k
}
105
106
inline Status PreparedFunctionImpl::_execute_skipped_constant_deal(
107
        FunctionContext* context, Block& block, const ColumnNumbers& args, uint32_t result,
108
20.8k
        size_t input_rows_count, bool dry_run) const {
109
20.8k
    bool executed = false;
110
20.8k
    RETURN_IF_ERROR(default_implementation_for_nulls(context, block, args, result, input_rows_count,
111
20.8k
                                                     dry_run, &executed));
112
20.8k
    if (executed) {
113
9.81k
        return Status::OK();
114
9.81k
    }
115
116
10.9k
    if (dry_run) {
117
0
        return execute_impl_dry_run(context, block, args, result, input_rows_count);
118
10.9k
    } else {
119
10.9k
        return execute_impl(context, block, args, result, input_rows_count);
120
10.9k
    }
121
10.9k
}
122
123
Status PreparedFunctionImpl::default_implementation_for_constant_arguments(
124
        FunctionContext* context, Block& block, const ColumnNumbers& args, uint32_t result,
125
20.8k
        size_t input_rows_count, bool dry_run, bool* executed) const {
126
20.8k
    *executed = false;
127
20.8k
    ColumnNumbers args_expect_const = get_arguments_that_are_always_constant();
128
129
    // Check that these arguments are really constant.
130
20.8k
    for (auto arg_num : args_expect_const) {
131
312
        if (arg_num < args.size() &&
132
312
            !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
312
    }
137
138
20.8k
    if (args.empty() || !use_default_implementation_for_constants() ||
139
20.8k
        !VectorizedUtils::all_arguments_are_constant(block, args)) {
140
18.7k
        return Status::OK();
141
18.7k
    }
142
143
    // now all columns are const.
144
2.03k
    Block temporary_block;
145
146
2.03k
    size_t arguments_size = args.size();
147
6.92k
    for (size_t arg_num = 0; arg_num < arguments_size; ++arg_num) {
148
4.89k
        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
4.89k
        if (args_expect_const.end() !=
153
4.89k
            std::find(args_expect_const.begin(), args_expect_const.end(), arg_num)) {
154
0
            temporary_block.insert({column.column, column.type, column.name});
155
4.89k
        } else {
156
4.89k
            temporary_block.insert(
157
4.89k
                    {assert_cast<const ColumnConst*>(column.column.get())->get_data_column_ptr(),
158
4.89k
                     column.type, column.name});
159
4.89k
        }
160
4.89k
    }
161
162
2.03k
    temporary_block.insert(block.get_by_position(result));
163
164
2.03k
    ColumnNumbers temporary_argument_numbers(arguments_size);
165
6.92k
    for (size_t i = 0; i < arguments_size; ++i) {
166
4.89k
        temporary_argument_numbers[i] = i;
167
4.89k
    }
168
169
2.03k
    RETURN_IF_ERROR(_execute_skipped_constant_deal(context, temporary_block,
170
2.03k
                                                   temporary_argument_numbers, arguments_size,
171
2.03k
                                                   temporary_block.rows(), dry_run));
172
173
2.03k
    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.03k
    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.03k
    } else {
179
2.03k
        result_column = temporary_block.get_by_position(arguments_size).column;
180
2.03k
    }
181
    // We shuold handle the case where the result column is also a ColumnConst.
182
2.03k
    block.get_by_position(result).column = ColumnConst::create(result_column, input_rows_count);
183
2.03k
    *executed = true;
184
2.03k
    return Status::OK();
185
2.03k
}
186
187
Status PreparedFunctionImpl::default_implementation_for_nulls(
188
        FunctionContext* context, Block& block, const ColumnNumbers& args, uint32_t result,
189
20.8k
        size_t input_rows_count, bool dry_run, bool* executed) const {
190
20.8k
    *executed = false;
191
20.8k
    if (args.empty() || !use_default_implementation_for_nulls()) {
192
1.42k
        return Status::OK();
193
1.42k
    }
194
195
44.7k
    if (std::ranges::any_of(args, [&block](const auto& elem) {
196
44.7k
            return block.get_by_position(elem).column->only_null();
197
44.7k
        })) {
198
3.26k
        block.get_by_position(result).column =
199
3.26k
                block.get_by_position(result).type->create_column_const(input_rows_count, Null());
200
3.26k
        *executed = true;
201
3.26k
        return Status::OK();
202
3.26k
    }
203
204
16.1k
    if (have_null_column(block, args)) {
205
6.55k
        bool need_to_default = need_replace_null_data_to_default();
206
6.55k
        if (context) {
207
6.54k
            need_to_default &= context->check_overflow_for_decimal();
208
6.54k
        }
209
        // extract nested column from nulls
210
6.55k
        ColumnNumbers new_args;
211
17.9k
        for (auto arg : args) {
212
17.9k
            new_args.push_back(block.columns());
213
17.9k
            block.insert(block.get_by_position(arg).get_nested(need_to_default));
214
17.9k
            DCHECK(!block.get_by_position(new_args.back()).column->is_nullable());
215
17.9k
        }
216
6.55k
        RETURN_IF_ERROR(execute_without_low_cardinality_columns(context, block, new_args, result,
217
6.55k
                                                                block.rows(), dry_run));
218
        // After run with nested, wrap them in null. Before this, block.get_by_position(result).type
219
        // is not compatible with get_by_position(result).column
220
6.55k
        block.get_by_position(result).column = wrap_in_nullable(
221
6.55k
                block.get_by_position(result).column, block, args, result, input_rows_count);
222
223
24.4k
        while (!new_args.empty()) {
224
17.9k
            block.erase(new_args.back());
225
17.9k
            new_args.pop_back();
226
17.9k
        }
227
6.55k
        *executed = true;
228
6.55k
        return Status::OK();
229
6.55k
    }
230
9.55k
    return Status::OK();
231
16.1k
}
232
233
Status PreparedFunctionImpl::execute_without_low_cardinality_columns(
234
        FunctionContext* context, Block& block, const ColumnNumbers& args, uint32_t result,
235
20.8k
        size_t input_rows_count, bool dry_run) const {
236
20.8k
    bool executed = false;
237
238
20.8k
    RETURN_IF_ERROR(default_implementation_for_constant_arguments(
239
20.8k
            context, block, args, result, input_rows_count, dry_run, &executed));
240
20.8k
    if (executed) {
241
2.03k
        return Status::OK();
242
2.03k
    }
243
244
18.7k
    return _execute_skipped_constant_deal(context, block, args, result, input_rows_count, dry_run);
245
20.8k
}
246
247
Status PreparedFunctionImpl::execute(FunctionContext* context, Block& block,
248
                                     const ColumnNumbers& args, uint32_t result,
249
14.2k
                                     size_t input_rows_count, bool dry_run) const {
250
14.2k
    return execute_without_low_cardinality_columns(context, block, args, result, input_rows_count,
251
14.2k
                                                   dry_run);
252
14.2k
}
253
254
14.1k
void FunctionBuilderImpl::check_number_of_arguments(size_t number_of_arguments) const {
255
14.1k
    if (is_variadic()) {
256
4.28k
        return;
257
4.28k
    }
258
259
9.87k
    size_t expected_number_of_arguments = get_number_of_arguments();
260
261
9.87k
    CHECK_EQ(number_of_arguments, expected_number_of_arguments) << fmt::format(
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
9.87k
}
265
266
DataTypePtr FunctionBuilderImpl::get_return_type_without_low_cardinality(
267
14.1k
        const ColumnsWithTypeAndName& arguments) const {
268
14.1k
    check_number_of_arguments(arguments.size());
269
270
14.1k
    if (!arguments.empty() && use_default_implementation_for_nulls()) {
271
12.8k
        if (have_null_column(arguments)) {
272
9.81k
            ColumnNumbers numbers(arguments.size());
273
9.81k
            std::iota(numbers.begin(), numbers.end(), 0);
274
9.81k
            auto [nested_block, _] =
275
9.81k
                    create_block_with_nested_columns(Block(arguments), numbers, false);
276
9.81k
            auto return_type = get_return_type_impl(
277
9.81k
                    ColumnsWithTypeAndName(nested_block.begin(), nested_block.end()));
278
9.81k
            return make_nullable(return_type);
279
9.81k
        }
280
12.8k
    }
281
282
4.34k
    return get_return_type_impl(arguments);
283
14.1k
}
284
285
14.1k
DataTypePtr FunctionBuilderImpl::get_return_type(const ColumnsWithTypeAndName& arguments) const {
286
14.1k
    if (use_default_implementation_for_low_cardinality_columns()) {
287
14.0k
        ColumnsWithTypeAndName args_without_low_cardinality(arguments);
288
289
33.3k
        for (ColumnWithTypeAndName& arg : args_without_low_cardinality) {
290
33.3k
            bool is_const = arg.column && is_column_const(*arg.column);
291
33.3k
            if (is_const) {
292
17.1k
                arg.column = assert_cast<const ColumnConst&>(*arg.column).remove_low_cardinality();
293
17.1k
            }
294
33.3k
        }
295
296
14.0k
        auto type_without_low_cardinality =
297
14.0k
                get_return_type_without_low_cardinality(args_without_low_cardinality);
298
299
14.0k
        return type_without_low_cardinality;
300
14.0k
    }
301
302
130
    return get_return_type_without_low_cardinality(arguments);
303
14.1k
}
304
305
bool FunctionBuilderImpl::is_date_or_datetime_or_decimal(
306
2
        const DataTypePtr& return_type, const DataTypePtr& func_return_type) const {
307
2
    return (is_date_or_datetime(return_type->is_nullable()
308
2
                                        ? ((DataTypeNullable*)return_type.get())->get_nested_type()
309
2
                                        : return_type) &&
310
2
            is_date_or_datetime(
311
2
                    func_return_type->is_nullable()
312
2
                            ? ((DataTypeNullable*)func_return_type.get())->get_nested_type()
313
2
                            : func_return_type)) ||
314
2
           (is_date_v2_or_datetime_v2(
315
0
                    return_type->is_nullable()
316
0
                            ? ((DataTypeNullable*)return_type.get())->get_nested_type()
317
0
                            : return_type) &&
318
0
            is_date_v2_or_datetime_v2(
319
0
                    func_return_type->is_nullable()
320
0
                            ? ((DataTypeNullable*)func_return_type.get())->get_nested_type()
321
0
                            : func_return_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
2
           (is_date_v2_or_datetime_v2(
326
0
                    return_type->is_nullable()
327
0
                            ? ((DataTypeNullable*)return_type.get())->get_nested_type()
328
0
                            : return_type) &&
329
0
            is_date_or_datetime(
330
0
                    func_return_type->is_nullable()
331
0
                            ? ((DataTypeNullable*)func_return_type.get())->get_nested_type()
332
0
                            : func_return_type)) ||
333
2
           (is_date_or_datetime(return_type->is_nullable()
334
0
                                        ? ((DataTypeNullable*)return_type.get())->get_nested_type()
335
0
                                        : return_type) &&
336
0
            is_date_v2_or_datetime_v2(
337
0
                    func_return_type->is_nullable()
338
0
                            ? ((DataTypeNullable*)func_return_type.get())->get_nested_type()
339
0
                            : func_return_type)) ||
340
2
           (is_decimal(return_type->is_nullable()
341
0
                               ? ((DataTypeNullable*)return_type.get())->get_nested_type()
342
0
                               : return_type) &&
343
0
            is_decimal(func_return_type->is_nullable()
344
0
                               ? ((DataTypeNullable*)func_return_type.get())->get_nested_type()
345
0
                               : func_return_type));
346
2
}
347
348
bool FunctionBuilderImpl::is_array_nested_type_date_or_datetime_or_decimal(
349
0
        const DataTypePtr& return_type, const DataTypePtr& func_return_type) const {
350
0
    auto return_type_ptr = return_type->is_nullable()
351
0
                                   ? ((DataTypeNullable*)return_type.get())->get_nested_type()
352
0
                                   : return_type;
353
0
    auto func_return_type_ptr =
354
0
            func_return_type->is_nullable()
355
0
                    ? ((DataTypeNullable*)func_return_type.get())->get_nested_type()
356
0
                    : func_return_type;
357
0
    if (!(is_array(return_type_ptr) && is_array(func_return_type_ptr))) {
358
0
        return false;
359
0
    }
360
0
    auto nested_nullable_return_type_ptr =
361
0
            (assert_cast<const DataTypeArray*>(return_type_ptr.get()))->get_nested_type();
362
0
    auto nested_nullable_func_return_type =
363
0
            (assert_cast<const DataTypeArray*>(func_return_type_ptr.get()))->get_nested_type();
364
    // There must be nullable inside array type.
365
0
    if (nested_nullable_return_type_ptr->is_nullable() &&
366
0
        nested_nullable_func_return_type->is_nullable()) {
367
0
        auto nested_return_type_ptr =
368
0
                ((DataTypeNullable*)(nested_nullable_return_type_ptr.get()))->get_nested_type();
369
0
        auto nested_func_return_type_ptr =
370
0
                ((DataTypeNullable*)(nested_nullable_func_return_type.get()))->get_nested_type();
371
0
        return is_date_or_datetime_or_decimal(nested_return_type_ptr, nested_func_return_type_ptr);
372
0
    }
373
0
    return false;
374
0
}
375
} // namespace doris::vectorized