forked from apache/doris
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunction.cpp
More file actions
424 lines (366 loc) · 18.4 KB
/
Copy pathfunction.cpp
File metadata and controls
424 lines (366 loc) · 18.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
// This file is copied from
// https://github.com/ClickHouse/ClickHouse/blob/master/src/Functions/IFunction.cpp
// and modified by Doris
#include "exprs/function/function.h"
#include <algorithm>
#include <memory>
#include <numeric>
#include "common/status.h"
#include "core/assert_cast.h"
#include "core/column/column.h"
#include "core/column/column_const.h"
#include "core/column/column_nullable.h"
#include "core/column/column_vector.h"
#include "core/data_type/data_type_array.h"
#include "core/data_type/data_type_nothing.h"
#include "core/data_type/data_type_nullable.h"
#include "core/data_type/define_primitive_type.h"
#include "core/data_type/primitive_type.h"
#include "core/field.h"
#include "exec/common/util.hpp"
#include "exprs/aggregate/aggregate_function.h"
#include "exprs/function/function_helpers.h"
#include "storage/index/zone_map/zonemap_eval_context.h"
namespace doris {
ColumnPtr wrap_in_nullable(const ColumnPtr& src, const Block& block, const ColumnNumbers& args,
size_t input_rows_count) {
ColumnPtr result_null_map_column;
/// If result is already nullable.
ColumnPtr src_not_nullable = src;
MutableColumnPtr mutable_result_null_map_column;
if (auto nullable = check_and_get_column_ptr<ColumnNullable>(src)) {
src_not_nullable = nullable->get_nested_column_ptr();
result_null_map_column = nullable->get_null_map_column_ptr();
}
for (const auto& arg : args) {
const ColumnWithTypeAndName& elem = block.get_by_position(arg);
if (!elem.type->is_nullable() || is_column_const(*elem.column)) {
continue;
}
if (auto nullable = cast_to_column<ColumnNullable>(elem.column); nullable->has_null()) {
const ColumnPtr& null_map_column = nullable->get_null_map_column_ptr();
if (!result_null_map_column) { // NOLINT(bugprone-use-after-move)
result_null_map_column = null_map_column;
continue;
}
if (!mutable_result_null_map_column) {
mutable_result_null_map_column = (*std::move(result_null_map_column)).mutate();
}
NullMap& result_null_map =
assert_cast<ColumnUInt8&>(*mutable_result_null_map_column).get_data();
const NullMap& src_null_map =
assert_cast<const ColumnUInt8&>(*null_map_column).get_data();
VectorizedUtils::update_null_map(result_null_map, src_null_map);
}
}
// Commit merged null map back: result_null_map_column was moved into
// mutable_result_null_map_column when merging 2+ nullable args with nulls.
if (mutable_result_null_map_column) {
result_null_map_column = std::move(mutable_result_null_map_column);
}
if (!result_null_map_column) {
if (is_column_const(*src)) {
return ColumnConst::create(
make_nullable(assert_cast<const ColumnConst&>(*src).get_data_column_ptr(),
false),
input_rows_count);
}
return ColumnNullable::create(src, ColumnUInt8::create(input_rows_count, 0));
}
return ColumnNullable::create(src_not_nullable, result_null_map_column);
}
bool have_null_column(const Block& block, const ColumnNumbers& args) {
return std::ranges::any_of(args, [&block](const auto& elem) {
return block.get_by_position(elem).type->is_nullable();
});
}
bool have_null_column(const ColumnsWithTypeAndName& args) {
return std::ranges::any_of(args, [](const auto& elem) { return elem.type->is_nullable(); });
}
inline Status PreparedFunctionImpl::_execute_skipped_constant_deal(FunctionContext* context,
Block& block,
const ColumnNumbers& args,
uint32_t result,
size_t input_rows_count) const {
bool executed = false;
RETURN_IF_ERROR(default_implementation_for_nulls(context, block, args, result, input_rows_count,
&executed));
if (executed) {
return Status::OK();
}
return execute_impl(context, block, args, result, input_rows_count);
}
Status PreparedFunctionImpl::default_implementation_for_constant_arguments(
FunctionContext* context, Block& block, const ColumnNumbers& args, uint32_t result,
size_t input_rows_count, bool* executed) const {
*executed = false;
ColumnNumbers args_expect_const = get_arguments_that_are_always_constant();
// Check that these arguments are really constant.
for (auto arg_num : args_expect_const) {
if (arg_num < args.size() &&
!is_column_const(*block.get_by_position(args[arg_num]).column)) {
return Status::InvalidArgument("Argument at index {} for function {} must be constant",
arg_num, get_name());
}
}
if (args.empty() || !use_default_implementation_for_constants() ||
!VectorizedUtils::all_arguments_are_constant(block, args)) {
return Status::OK();
}
// now all columns are const.
Block temporary_block;
int arguments_size = (int)args.size();
for (size_t arg_num = 0; arg_num < arguments_size; ++arg_num) {
const ColumnWithTypeAndName& column = block.get_by_position(args[arg_num]);
// Columns in const_list --> column_const, others --> nested_column
// that's because some functions supposes some specific columns always constant.
// If we unpack it, there will be unnecessary cost of virtual judge.
if (args_expect_const.end() !=
std::find(args_expect_const.begin(), args_expect_const.end(), arg_num)) {
temporary_block.insert({column.column, column.type, column.name});
} else {
temporary_block.insert(
{assert_cast<const ColumnConst*>(column.column.get())->get_data_column_ptr(),
column.type, column.name});
}
}
temporary_block.insert(block.get_by_position(result));
ColumnNumbers temporary_argument_numbers(arguments_size);
for (int i = 0; i < arguments_size; ++i) {
temporary_argument_numbers[i] = i;
}
RETURN_IF_ERROR(_execute_skipped_constant_deal(context, temporary_block,
temporary_argument_numbers, arguments_size,
temporary_block.rows()));
ColumnPtr result_column;
/// extremely rare case, when we have function with completely const arguments
/// but some of them produced by non is_deterministic function
if (temporary_block.get_by_position(arguments_size).column->size() > 1) {
result_column = temporary_block.get_by_position(arguments_size).column->clone_resized(1);
} else {
result_column = temporary_block.get_by_position(arguments_size).column;
}
// We shuold handle the case where the result column is also a ColumnConst.
block.get_by_position(result).column = ColumnConst::create(result_column, input_rows_count);
*executed = true;
return Status::OK();
}
Status PreparedFunctionImpl::default_implementation_for_nulls(
FunctionContext* context, Block& block, const ColumnNumbers& args, uint32_t result,
size_t input_rows_count, bool* executed) const {
*executed = false;
if (args.empty() || !use_default_implementation_for_nulls()) {
return Status::OK();
}
if (std::ranges::any_of(args, [&block](const auto& elem) {
return block.get_by_position(elem).column->only_null();
})) {
block.get_by_position(result).column =
block.get_by_position(result).type->create_column_const(input_rows_count, Field());
*executed = true;
return Status::OK();
}
if (have_null_column(block, args)) {
bool need_to_default = need_replace_null_data_to_default();
// extract nested column from nulls
ColumnNumbers new_args;
Block new_block;
for (int i = 0; i < args.size(); ++i) {
uint32_t arg = args[i];
new_args.push_back(i);
new_block.insert(block.get_by_position(arg).unnest_nullable(need_to_default));
}
new_block.insert(block.get_by_position(result));
int new_result = new_block.columns() - 1;
RETURN_IF_ERROR(default_execute(context, new_block, new_args, new_result, block.rows()));
// After run with nested, wrap them in null. Before this, block.get_by_position(result).type
// is not compatible with get_by_position(result).column
block.get_by_position(result).column = wrap_in_nullable(
new_block.get_by_position(new_result).column, block, args, input_rows_count);
*executed = true;
return Status::OK();
}
return Status::OK();
}
Status PreparedFunctionImpl::default_execute(FunctionContext* context, Block& block,
const ColumnNumbers& args, uint32_t result,
size_t input_rows_count) const {
bool executed = false;
RETURN_IF_ERROR(default_implementation_for_constant_arguments(context, block, args, result,
input_rows_count, &executed));
if (executed) {
return Status::OK();
}
return _execute_skipped_constant_deal(context, block, args, result, input_rows_count);
}
Status PreparedFunctionImpl::execute(FunctionContext* context, Block& block,
const ColumnNumbers& args, uint32_t result,
size_t input_rows_count) const {
return default_execute(context, block, args, result, input_rows_count);
}
void FunctionBuilderImpl::check_number_of_arguments(size_t number_of_arguments) const {
if (is_variadic()) {
return;
}
size_t expected_number_of_arguments = get_number_of_arguments();
DCHECK_EQ(number_of_arguments, expected_number_of_arguments) << fmt::format(
"Number of arguments for function {} doesn't match: passed {} , should be {}",
get_name(), number_of_arguments, expected_number_of_arguments);
if (number_of_arguments != expected_number_of_arguments) {
throw Exception(
ErrorCode::INVALID_ARGUMENT,
"Number of arguments for function {} doesn't match: passed {} , should be {}",
get_name(), number_of_arguments, expected_number_of_arguments);
}
}
DataTypePtr FunctionBuilderImpl::get_return_type(const ColumnsWithTypeAndName& arguments) const {
check_number_of_arguments(arguments.size());
if (!arguments.empty() && use_default_implementation_for_nulls()) {
if (have_null_column(arguments)) {
ColumnNumbers numbers(arguments.size());
std::iota(numbers.begin(), numbers.end(), 0);
auto [nested_block, _] =
create_block_with_nested_columns(Block(arguments), numbers, false);
auto return_type = get_return_type_impl(
ColumnsWithTypeAndName(nested_block.begin(), nested_block.end()));
if (!return_type) {
return nullptr;
}
return make_nullable(return_type);
}
}
return get_return_type_impl(arguments);
}
bool FunctionBuilderImpl::is_date_or_datetime_or_decimal(
const DataTypePtr& return_type, const DataTypePtr& func_return_type) const {
return (is_date_or_datetime(return_type->get_primitive_type()) &&
is_date_or_datetime(func_return_type->get_primitive_type())) ||
(is_date_v2_or_datetime_v2(return_type->get_primitive_type()) &&
is_date_v2_or_datetime_v2(func_return_type->get_primitive_type())) ||
// For some date functions such as str_to_date(string, string), return_type will
// be datetimev2 if users enable datev2 but get_return_type(arguments) will still
// return datetime. We need keep backward compatibility here.
(is_date_v2_or_datetime_v2(return_type->get_primitive_type()) &&
is_date_or_datetime(func_return_type->get_primitive_type())) ||
(is_date_or_datetime(return_type->get_primitive_type()) &&
is_date_v2_or_datetime_v2(func_return_type->get_primitive_type())) ||
(is_decimal(return_type->get_primitive_type()) &&
is_decimal(func_return_type->get_primitive_type())) ||
(is_time_type(return_type->get_primitive_type()) &&
is_time_type(func_return_type->get_primitive_type()));
}
bool contains_date_or_datetime_or_decimal(const DataTypePtr& type) {
auto type_ptr = type->is_nullable() ? ((DataTypeNullable*)type.get())->get_nested_type() : type;
switch (type_ptr->get_primitive_type()) {
case TYPE_ARRAY: {
const auto* array_type = assert_cast<const DataTypeArray*>(type_ptr.get());
return contains_date_or_datetime_or_decimal(array_type->get_nested_type());
}
case TYPE_MAP: {
const auto* map_type = assert_cast<const DataTypeMap*>(type_ptr.get());
return contains_date_or_datetime_or_decimal(map_type->get_key_type()) ||
contains_date_or_datetime_or_decimal(map_type->get_value_type());
}
case TYPE_STRUCT: {
const auto* struct_type = assert_cast<const DataTypeStruct*>(type_ptr.get());
const auto& elements = struct_type->get_elements();
return std::ranges::any_of(elements, [](const DataTypePtr& element) {
return contains_date_or_datetime_or_decimal(element);
});
}
default:
// For scalar types, check if it's date/datetime/decimal
return is_date_or_datetime(type_ptr->get_primitive_type()) ||
is_date_v2_or_datetime_v2(type_ptr->get_primitive_type()) ||
is_decimal(type_ptr->get_primitive_type()) ||
is_time_type(type_ptr->get_primitive_type());
}
}
// make sure array/map/struct and nested array/map/struct can be check
bool FunctionBuilderImpl::is_nested_type_date_or_datetime_or_decimal(
const DataTypePtr& return_type, const DataTypePtr& func_return_type) const {
auto return_type_ptr = return_type->is_nullable()
? ((DataTypeNullable*)return_type.get())->get_nested_type()
: return_type;
auto func_return_type_ptr =
func_return_type->is_nullable()
? ((DataTypeNullable*)func_return_type.get())->get_nested_type()
: func_return_type;
// make sure that map/struct/array also need to check
if (return_type_ptr->get_primitive_type() != func_return_type_ptr->get_primitive_type()) {
return false;
}
// Check if this type contains date/datetime/decimal types
if (!contains_date_or_datetime_or_decimal(return_type_ptr)) {
// If no date/datetime/decimal types, just pass through
return true;
}
// If contains date/datetime/decimal types, recursively check each element
switch (return_type_ptr->get_primitive_type()) {
case TYPE_ARRAY: {
auto nested_return_type = remove_nullable(
(assert_cast<const DataTypeArray*>(return_type_ptr.get()))->get_nested_type());
auto nested_func_type = remove_nullable(
(assert_cast<const DataTypeArray*>(func_return_type_ptr.get()))->get_nested_type());
return is_nested_type_date_or_datetime_or_decimal(nested_return_type, nested_func_type);
}
case TYPE_MAP: {
const auto* return_map = assert_cast<const DataTypeMap*>(return_type_ptr.get());
const auto* func_map = assert_cast<const DataTypeMap*>(func_return_type_ptr.get());
auto key_return = remove_nullable(return_map->get_key_type());
auto key_func = remove_nullable(func_map->get_key_type());
auto value_return = remove_nullable(return_map->get_value_type());
auto value_func = remove_nullable(func_map->get_value_type());
return is_nested_type_date_or_datetime_or_decimal(key_return, key_func) &&
is_nested_type_date_or_datetime_or_decimal(value_return, value_func);
}
case TYPE_STRUCT: {
const auto* return_struct = assert_cast<const DataTypeStruct*>(return_type_ptr.get());
const auto* func_struct = assert_cast<const DataTypeStruct*>(func_return_type_ptr.get());
auto return_elements = return_struct->get_elements();
auto func_elements = func_struct->get_elements();
if (return_elements.size() != func_elements.size()) {
return false;
}
for (size_t i = 0; i < return_elements.size(); i++) {
auto elem_return = remove_nullable(return_elements[i]);
auto elem_func = remove_nullable(func_elements[i]);
if (!is_nested_type_date_or_datetime_or_decimal(elem_return, elem_func)) {
return false;
}
}
return true;
}
default:
return is_date_or_datetime_or_decimal(return_type_ptr, func_return_type_ptr);
}
}
ZoneMapFilterResult IFunctionBase::evaluate_zonemap_filter(
const ZoneMapEvalContext& ctx, const VExprSPtrs& function_arguments) const {
return unsupported_zonemap_filter(ctx);
}
ZoneMapFilterResult IFunctionBase::evaluate_dictionary_filter(
const DictionaryEvalContext& ctx, const VExprSPtrs& function_arguments) const {
return ZoneMapFilterResult::kUnsupported;
}
ZoneMapFilterResult IFunctionBase::evaluate_bloom_filter(
const BloomFilterEvalContext& ctx, const VExprSPtrs& function_arguments) const {
return ZoneMapFilterResult::kUnsupported;
}
} // namespace doris