forked from apache/doris
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunction_cast.cpp
More file actions
399 lines (335 loc) · 17.4 KB
/
Copy pathfunction_cast.cpp
File metadata and controls
399 lines (335 loc) · 17.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
// 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.
#include <utility>
#include "core/data_type/data_type_agg_state.h"
#include "core/data_type/data_type_decimal.h"
#include "core/data_type/data_type_number.h" // IWYU pragma: keep
#include "core/data_type/data_type_quantilestate.h"
#include "core/data_type/primitive_type.h"
#include "exprs/function/cast/cast_to_array.h"
#include "exprs/function/cast/cast_to_jsonb.h"
#include "exprs/function/cast/cast_to_map.h"
#include "exprs/function/cast/cast_to_struct.h"
#include "exprs/function/cast/cast_to_variant.h"
#include "exprs/function/cast/cast_wrapper_decls.h"
#include "exprs/function/simple_function_factory.h"
namespace doris {
namespace CastWrapper {
WrapperType create_hll_wrapper(FunctionContext* context, const DataTypePtr& from_type_untyped,
const DataTypeHLL& to_type) {
/// Conversion from String through parsing.
if (check_and_get_data_type<DataTypeString>(from_type_untyped.get())) {
return cast_from_string_to_generic;
}
return CastWrapper::create_unsupport_wrapper("Cast to HLL only support from String type");
}
WrapperType create_bitmap_wrapper(FunctionContext* context, const DataTypePtr& from_type_untyped,
const DataTypeBitMap& to_type) {
/// Conversion from String through parsing.
if (check_and_get_data_type<DataTypeString>(from_type_untyped.get())) {
return cast_from_string_to_generic;
}
return CastWrapper::create_unsupport_wrapper("Cast to BitMap only support from String type");
}
WrapperType create_quantile_state_wrapper(FunctionContext* context,
const DataTypePtr& from_type_untyped,
const DataTypeQuantileState& to_type) {
/// Conversion from String through parsing.
if (check_and_get_data_type<DataTypeString>(from_type_untyped.get())) {
return cast_from_string_to_generic;
}
return CastWrapper::create_unsupport_wrapper(
"Cast to QuantileState only support from String type");
}
WrapperType create_varbinary_wrapper(const DataTypePtr& from_type_untyped) {
/// Conversion from String through parsing.
if (check_and_get_data_type<DataTypeString>(from_type_untyped.get())) {
return cast_from_string_to_generic;
}
return CastWrapper::create_unsupport_wrapper("Cast to Varbinary only support from String type");
}
WrapperType prepare_unpack_dictionaries(FunctionContext* context, const DataTypePtr& from_type,
const DataTypePtr& to_type) {
const auto& from_nested = from_type;
const auto& to_nested = to_type;
if (from_type->is_null_literal()) {
if (!to_nested->is_nullable()) {
return CastWrapper::create_unsupport_wrapper(
"Cannot convert NULL to a non-nullable type");
}
return [](FunctionContext* context, Block& block, const ColumnNumbers&, uint32_t result,
size_t input_rows_count, const NullMap::value_type* null_map = nullptr) {
/// TODO: remove this in the future.
auto& res = block.get_by_position(result);
res.column = res.type->create_column_const_with_default_value(input_rows_count)
->convert_to_full_column_if_const();
return Status::OK();
};
}
auto wrapper = prepare_remove_nullable(context, from_nested, to_nested);
return wrapper;
}
bool need_replace_null_data_to_default(FunctionContext* context, const DataTypePtr& from_type,
const DataTypePtr& to_type) {
if (from_type->equals(*to_type)) {
return false;
}
auto make_default_wrapper = [&](const auto& types) -> bool {
using Types = std::decay_t<decltype(types)>;
using ToDataType = typename Types::LeftType;
if constexpr (!(IsDataTypeDecimalOrNumber<ToDataType> || IsDatelikeV1Types<ToDataType> ||
IsDatelikeV2Types<ToDataType> ||
std::is_same_v<ToDataType, DataTypeTimeV2>)) {
return false;
}
return call_on_index_and_data_type<
ToDataType>(from_type->get_primitive_type(), [&](const auto& types2) -> bool {
using Types2 = std::decay_t<decltype(types2)>;
using FromDataType = typename Types2::LeftType;
if constexpr (!(IsDataTypeDecimalOrNumber<FromDataType> ||
IsDatelikeV1Types<FromDataType> || IsDatelikeV2Types<FromDataType> ||
std::is_same_v<FromDataType, DataTypeTimeV2>)) {
return false;
}
if constexpr (IsDataTypeDecimal<FromDataType> || IsDataTypeDecimal<ToDataType>) {
using FromFieldType = typename FromDataType::FieldType;
using ToFieldType = typename ToDataType::FieldType;
UInt32 from_precision = NumberTraits::max_ascii_len<FromFieldType>();
UInt32 from_scale = 0;
if constexpr (IsDataTypeDecimal<FromDataType>) {
const auto* from_decimal_type =
check_and_get_data_type<FromDataType>(from_type.get());
from_precision =
NumberTraits::max_ascii_len<typename FromFieldType::NativeType>();
from_scale = from_decimal_type->get_scale();
}
UInt32 to_max_digits = 0;
UInt32 to_precision = 0;
UInt32 to_scale = 0;
if constexpr (IsDataTypeDecimal<ToDataType>) {
to_max_digits = NumberTraits::max_ascii_len<typename ToFieldType::NativeType>();
const auto* to_decimal_type =
check_and_get_data_type<ToDataType>(to_type.get());
to_precision = to_decimal_type->get_precision();
ToDataType::check_type_precision(to_precision);
to_scale = to_decimal_type->get_scale();
ToDataType::check_type_scale(to_scale);
}
if constexpr (IsIntegralV<ToFieldType> || std::is_floating_point_v<ToFieldType>) {
to_max_digits = NumberTraits::max_ascii_len<ToFieldType>();
to_precision = to_max_digits;
}
bool narrow_integral = context->check_overflow_for_decimal() &&
(to_precision - to_scale) <= (from_precision - from_scale);
bool multiply_may_overflow = context->check_overflow_for_decimal();
if (to_scale > from_scale) {
multiply_may_overflow &=
(from_precision + to_scale - from_scale) >= to_max_digits;
}
return narrow_integral || multiply_may_overflow;
}
return false;
});
};
return call_on_index_and_data_type<void>(to_type->get_primitive_type(), make_default_wrapper);
}
WrapperType prepare_remove_nullable(FunctionContext* context, const DataTypePtr& from_type,
const DataTypePtr& to_type) {
/// Determine whether pre-processing and/or post-processing must take place during conversion.
bool result_is_nullable = to_type->is_nullable();
if (result_is_nullable) {
return [from_type, to_type](FunctionContext* context, Block& block,
const ColumnNumbers& arguments, uint32_t result,
size_t input_rows_count,
const NullMap::value_type* null_map = nullptr) {
auto from_type_not_nullable = remove_nullable(from_type);
auto to_type_not_nullable = remove_nullable(to_type);
bool replace_null_data_to_default = need_replace_null_data_to_default(
context, from_type_not_nullable, to_type_not_nullable);
auto nested_result_index = block.columns();
block.insert(block.get_by_position(result).unnest_nullable());
auto nested_source_index = block.columns();
block.insert(block.get_by_position(arguments[0])
.unnest_nullable(replace_null_data_to_default));
const auto& arg_col = block.get_by_position(arguments[0]);
const NullMap::value_type* arg_null_map = nullptr;
if (const auto* nullable = check_and_get_column<ColumnNullable>(*arg_col.column)) {
arg_null_map = nullable->get_null_map_data().data();
}
RETURN_IF_ERROR(prepare_impl(context, from_type_not_nullable, to_type_not_nullable)(
context, block, {nested_source_index}, nested_result_index, input_rows_count,
arg_null_map));
block.get_by_position(result).column =
wrap_in_nullable(block.get_by_position(nested_result_index).column, block,
arguments, input_rows_count);
block.erase(nested_source_index);
block.erase(nested_result_index);
return Status::OK();
};
} else {
return prepare_impl(context, from_type, to_type);
}
}
/// 'from_type' and 'to_type' are nested types in case of Nullable.
/// 'requested_result_is_nullable' is true if CAST to Nullable type is requested.
WrapperType prepare_impl(FunctionContext* context, const DataTypePtr& origin_from_type,
const DataTypePtr& origin_to_type) {
auto to_type = get_serialized_type(origin_to_type);
auto from_type = get_serialized_type(origin_from_type);
if (from_type->equals(*to_type)) {
return create_identity_wrapper(from_type);
}
// variant needs to be judged first
if (to_type->get_primitive_type() == PrimitiveType::TYPE_VARIANT) {
return create_cast_to_variant_wrapper(from_type,
static_cast<const DataTypeVariant&>(*to_type));
}
if (from_type->get_primitive_type() == PrimitiveType::TYPE_VARIANT) {
return create_cast_from_variant_wrapper(static_cast<const DataTypeVariant&>(*from_type),
to_type);
}
if (from_type->get_primitive_type() == PrimitiveType::TYPE_JSONB) {
return create_cast_from_jsonb_wrapper(static_cast<const DataTypeJsonb&>(*from_type),
to_type,
context ? context->jsonb_string_as_string() : false);
}
switch (to_type->get_primitive_type()) {
case PrimitiveType::TYPE_BOOLEAN:
return create_boolean_wrapper(context, from_type);
case PrimitiveType::TYPE_TINYINT:
case PrimitiveType::TYPE_SMALLINT:
case PrimitiveType::TYPE_INT:
case PrimitiveType::TYPE_BIGINT:
case PrimitiveType::TYPE_LARGEINT:
return create_int_wrapper(context, from_type, to_type->get_primitive_type());
case PrimitiveType::TYPE_FLOAT:
case PrimitiveType::TYPE_DOUBLE:
return create_float_wrapper(context, from_type, to_type->get_primitive_type());
case PrimitiveType::TYPE_DATE:
case PrimitiveType::TYPE_DATETIME:
case PrimitiveType::TYPE_DATEV2:
case PrimitiveType::TYPE_DATETIMEV2:
case PrimitiveType::TYPE_TIMEV2:
return create_datelike_wrapper(context, from_type, to_type->get_primitive_type());
case PrimitiveType::TYPE_TIMESTAMPTZ:
return create_timestamptz_wrapper(context, from_type);
case PrimitiveType::TYPE_IPV4:
case PrimitiveType::TYPE_IPV6:
return create_ip_wrapper(context, from_type, to_type->get_primitive_type());
case PrimitiveType::TYPE_DECIMALV2:
case PrimitiveType::TYPE_DECIMAL32:
case PrimitiveType::TYPE_DECIMAL64:
case PrimitiveType::TYPE_DECIMAL128I:
case PrimitiveType::TYPE_DECIMAL256:
return create_decimal_wrapper(context, from_type, to_type->get_primitive_type());
case PrimitiveType::TYPE_CHAR:
case PrimitiveType::TYPE_VARCHAR:
case PrimitiveType::TYPE_STRING:
return create_string_wrapper(from_type);
case PrimitiveType::TYPE_ARRAY:
return create_array_wrapper(context, from_type,
static_cast<const DataTypeArray&>(*to_type));
case PrimitiveType::TYPE_STRUCT:
return create_struct_wrapper(context, from_type,
static_cast<const DataTypeStruct&>(*to_type));
case PrimitiveType::TYPE_MAP:
return create_map_wrapper(context, from_type, static_cast<const DataTypeMap&>(*to_type));
case PrimitiveType::TYPE_HLL:
return create_hll_wrapper(context, from_type, static_cast<const DataTypeHLL&>(*to_type));
case PrimitiveType::TYPE_BITMAP:
return create_bitmap_wrapper(context, from_type,
static_cast<const DataTypeBitMap&>(*to_type));
case PrimitiveType::TYPE_QUANTILE_STATE:
return create_quantile_state_wrapper(context, from_type,
static_cast<const DataTypeQuantileState&>(*to_type));
case PrimitiveType::TYPE_JSONB:
return create_cast_to_jsonb_wrapper(from_type, static_cast<const DataTypeJsonb&>(*to_type),
context ? context->string_as_jsonb_string() : false);
case PrimitiveType::TYPE_VARBINARY:
return create_varbinary_wrapper(from_type);
default:
break;
}
return create_unsupport_wrapper(from_type->get_name(), to_type->get_name());
}
} // namespace CastWrapper
class PreparedFunctionCast : public PreparedFunctionImpl {
public:
explicit PreparedFunctionCast(CastWrapper::WrapperType&& wrapper_function_, const char* name_)
: wrapper_function(std::move(wrapper_function_)), name(name_) {}
String get_name() const override { return name; }
protected:
Status execute_impl(FunctionContext* context, Block& block, const ColumnNumbers& arguments,
uint32_t result, size_t input_rows_count) const override {
return wrapper_function(context, block, arguments, result, input_rows_count, nullptr);
}
bool use_default_implementation_for_nulls() const override { return false; }
ColumnNumbers get_arguments_that_are_always_constant() const override { return {1}; }
private:
CastWrapper::WrapperType wrapper_function;
const char* name;
};
class FunctionCast final : public IFunctionBase {
public:
FunctionCast(const char* name_, DataTypes argument_types_, DataTypePtr return_type_)
: name(name_),
argument_types(std::move(argument_types_)),
return_type(std::move(return_type_)) {}
const DataTypes& get_argument_types() const override { return argument_types; }
const DataTypePtr& get_return_type() const override { return return_type; }
PreparedFunctionPtr prepare(FunctionContext* context, const Block& /*sample_block*/,
const ColumnNumbers& /*arguments*/,
uint32_t /*result*/) const override {
return std::make_shared<PreparedFunctionCast>(
CastWrapper::prepare_unpack_dictionaries(context, get_argument_types()[0],
get_return_type()),
name);
}
String get_name() const override { return name; }
bool is_use_default_implementation_for_constants() const override { return true; }
private:
const char* name = nullptr;
DataTypes argument_types;
DataTypePtr return_type;
};
class FunctionBuilderCast : public FunctionBuilderImpl {
public:
static constexpr auto name = "CAST";
static FunctionBuilderPtr create() { return std::make_shared<FunctionBuilderCast>(); }
FunctionBuilderCast() = default;
String get_name() const override { return name; }
size_t get_number_of_arguments() const override { return 2; }
ColumnNumbers get_arguments_that_are_always_constant() const override { return {1}; }
protected:
FunctionBasePtr build_impl(const ColumnsWithTypeAndName& arguments,
const DataTypePtr& return_type) const override {
DataTypes data_types(arguments.size());
for (size_t i = 0; i < arguments.size(); ++i) {
data_types[i] = arguments[i].type;
}
return std::make_shared<FunctionCast>(name, data_types, return_type);
}
bool skip_return_type_check() const override { return true; }
DataTypePtr get_return_type_impl(const ColumnsWithTypeAndName& arguments) const override {
return nullptr;
}
bool use_default_implementation_for_nulls() const override { return false; }
};
void register_function_cast(SimpleFunctionFactory& factory) {
factory.register_function<FunctionBuilderCast>();
}
} // namespace doris