forked from apache/doris
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcolumn_type_convert.cpp
More file actions
437 lines (399 loc) · 20 KB
/
Copy pathcolumn_type_convert.cpp
File metadata and controls
437 lines (399 loc) · 20 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
425
426
427
428
429
430
431
432
433
434
435
436
437
// 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 "format/column_type_convert.h"
#include "common/cast_set.h"
#include "core/column/column_nullable.h"
#include "core/data_type/define_primitive_type.h"
namespace doris::converter {
#define FOR_LOGICAL_INTEGER_TYPES(M) \
M(TYPE_TINYINT) \
M(TYPE_SMALLINT) \
M(TYPE_INT) \
M(TYPE_BIGINT) \
M(TYPE_LARGEINT)
#define FOR_LOGICAL_NUMERIC_TYPES(M) \
M(TYPE_TINYINT) \
M(TYPE_SMALLINT) \
M(TYPE_INT) \
M(TYPE_BIGINT) \
M(TYPE_LARGEINT) \
M(TYPE_FLOAT) \
M(TYPE_DOUBLE)
#define FOR_LOGICAL_DECIMAL_TYPES(M) \
M(TYPE_DECIMAL32) \
M(TYPE_DECIMAL64) \
M(TYPE_DECIMAL128I) \
M(TYPE_DECIMAL256)
#define FOR_LOGICAL_TIME_TYPES(M) \
M(TYPE_DATETIME) \
M(TYPE_DATE) \
M(TYPE_DATETIMEV2) \
M(TYPE_DATEV2)
#define FOR_ALL_LOGICAL_TYPES(M) \
M(TYPE_BOOLEAN) \
M(TYPE_TINYINT) \
M(TYPE_SMALLINT) \
M(TYPE_INT) \
M(TYPE_BIGINT) \
M(TYPE_LARGEINT) \
M(TYPE_FLOAT) \
M(TYPE_DOUBLE) \
M(TYPE_DECIMALV2) \
M(TYPE_DECIMAL32) \
M(TYPE_DECIMAL64) \
M(TYPE_DECIMAL128I) \
M(TYPE_DECIMAL256) \
M(TYPE_DATETIME) \
M(TYPE_DATE) \
M(TYPE_DATETIMEV2) \
M(TYPE_DATEV2)
static bool _is_numeric_type(PrimitiveType type) {
switch (type) {
case TYPE_BOOLEAN:
case TYPE_TINYINT:
case TYPE_SMALLINT:
case TYPE_INT:
case TYPE_BIGINT:
case TYPE_LARGEINT:
case TYPE_FLOAT:
case TYPE_DOUBLE:
return true;
default:
return false;
}
}
static bool _is_decimal_type(doris::PrimitiveType type) {
switch (type) {
case TYPE_DECIMALV2:
case TYPE_DECIMAL32:
case TYPE_DECIMAL64:
case TYPE_DECIMAL128I:
case TYPE_DECIMAL256:
return true;
default:
return false;
}
}
ColumnPtr ColumnTypeConverter::get_column(const DataTypePtr& src_type, ColumnPtr& dst_column,
const DataTypePtr& dst_type) {
if (is_consistent()) {
if (_cached_src_type == nullptr) {
_cached_src_type = dst_type;
}
return dst_column;
}
if (!_cached_src_column) {
_cached_src_type = dst_type->is_nullable()
? get_data_type_with_default_argument(make_nullable(src_type))
: get_data_type_with_default_argument(remove_nullable(src_type));
_cached_src_column = _cached_src_type->create_column();
}
// remove the old cached data
auto cached_src_column = IColumn::mutate(std::move(_cached_src_column));
cached_src_column->clear();
_cached_src_column = std::move(cached_src_column);
return _cached_src_column;
}
static std::unique_ptr<ColumnTypeConverter> _numeric_converter(const DataTypePtr& src_type,
const DataTypePtr& dst_type) {
PrimitiveType src_primitive_type = src_type->get_primitive_type();
PrimitiveType dst_primitive_type = dst_type->get_primitive_type();
switch (dst_primitive_type) {
#define DISPATCH(DST_PTYPE) \
case DST_PTYPE: { \
switch (src_primitive_type) { \
case TYPE_TINYINT: \
return std::make_unique<IntegerToIntegerConverter<TYPE_TINYINT, DST_PTYPE>>(); \
case TYPE_SMALLINT: \
return std::make_unique<IntegerToIntegerConverter<TYPE_SMALLINT, DST_PTYPE>>(); \
case TYPE_INT: \
return std::make_unique<IntegerToIntegerConverter<TYPE_INT, DST_PTYPE>>(); \
case TYPE_BIGINT: \
return std::make_unique<IntegerToIntegerConverter<TYPE_BIGINT, DST_PTYPE>>(); \
case TYPE_LARGEINT: \
return std::make_unique<IntegerToIntegerConverter<TYPE_LARGEINT, DST_PTYPE>>(); \
default: \
return std::make_unique<UnsupportedConverter>(src_type, dst_type); \
} \
}
FOR_LOGICAL_INTEGER_TYPES(DISPATCH)
#undef DISPATCH
case TYPE_FLOAT: {
switch (src_primitive_type) {
#define DISPATCH(SRC_PTYPE) \
case SRC_PTYPE: { \
return std::make_unique<NumericToFloatPointConverter<SRC_PTYPE, TYPE_FLOAT>>(); \
}
FOR_LOGICAL_INTEGER_TYPES(DISPATCH)
#undef DISPATCH
default:
return std::make_unique<UnsupportedConverter>(src_type, dst_type);
}
}
case TYPE_DOUBLE: {
switch (src_primitive_type) {
#define DISPATCH(SRC_PTYPE) \
case SRC_PTYPE: { \
return std::make_unique<NumericToFloatPointConverter<SRC_PTYPE, TYPE_DOUBLE>>(); \
}
FOR_LOGICAL_NUMERIC_TYPES(DISPATCH)
#undef DISPATCH
default:
return std::make_unique<UnsupportedConverter>(src_type, dst_type);
}
}
default:
return std::make_unique<UnsupportedConverter>(src_type, dst_type);
}
}
template <FileFormat fileFormat = COMMON>
static std::unique_ptr<ColumnTypeConverter> _to_string_converter(const DataTypePtr& src_type,
const DataTypePtr& dst_type) {
PrimitiveType src_primitive_type = src_type->get_primitive_type();
// numeric type to string, using native std::to_string
if (src_primitive_type == TYPE_BOOLEAN) {
return std::make_unique<BooleanToStringConverter>();
} else if (_is_numeric_type(src_primitive_type)) {
switch (src_primitive_type) {
#define DISPATCH(SRC_PTYPE) \
case SRC_PTYPE: \
return std::make_unique<NumericToStringConverter<SRC_PTYPE, fileFormat>>();
FOR_LOGICAL_NUMERIC_TYPES(DISPATCH)
#undef DISPATCH
default:
return std::make_unique<UnsupportedConverter>(src_type, dst_type);
}
} else if (_is_decimal_type(src_primitive_type)) { // decimal type to string
switch (src_primitive_type) {
#define DISPATCH(SRC_PTYPE) \
case SRC_PTYPE: \
return std::make_unique<DecimalToStringConverter<SRC_PTYPE>>(src_type->get_scale());
FOR_LOGICAL_DECIMAL_TYPES(DISPATCH)
#undef DISPATCH
default:
return std::make_unique<UnsupportedConverter>(src_type, dst_type);
}
} else if (is_date_type(src_primitive_type)) { // date and datetime type to string
switch (src_primitive_type) {
#define DISPATCH(SRC_PTYPE) \
case SRC_PTYPE: \
return std::make_unique<TimeToStringConverter<SRC_PTYPE>>();
FOR_LOGICAL_TIME_TYPES(DISPATCH)
#undef DISPATCH
default:
return std::make_unique<UnsupportedConverter>(src_type, dst_type);
}
} else if (is_varbinary(src_primitive_type)) {
return std::make_unique<VarBinaryConverter<TYPE_STRING, TYPE_STRING>>();
}
return std::make_unique<UnsupportedConverter>(src_type, dst_type);
}
template <FileFormat fileFormat = COMMON>
static std::unique_ptr<ColumnTypeConverter> _from_string_converter(const DataTypePtr& src_type,
const DataTypePtr& dst_type) {
PrimitiveType dst_primitive_type = dst_type->get_primitive_type();
switch (dst_primitive_type) {
#define DISPATCH(DST_PTYPE) \
case DST_PTYPE: \
return std::make_unique<CastStringConverter<DST_PTYPE, fileFormat>>( \
remove_nullable(dst_type));
FOR_ALL_LOGICAL_TYPES(DISPATCH)
#undef DISPATCH
case TYPE_VARBINARY: {
return std::make_unique<VarBinaryConverter<TYPE_STRING, TYPE_VARBINARY>>();
}
default:
return std::make_unique<UnsupportedConverter>(src_type, dst_type);
}
}
static std::unique_ptr<ColumnTypeConverter> _numeric_to_decimal_converter(
const DataTypePtr& src_type, const DataTypePtr& dst_type) {
PrimitiveType src_primitive_type = src_type->get_primitive_type();
PrimitiveType dst_primitive_type = dst_type->get_primitive_type();
int scale = dst_type->get_scale();
int precision = dst_type->get_precision();
switch (src_primitive_type) {
#define DISPATCH(SRC_PTYPE) \
case SRC_PTYPE: { \
switch (dst_primitive_type) { \
case TYPE_DECIMAL32: \
return std::make_unique<NumericToDecimalConverter<SRC_PTYPE, TYPE_DECIMAL32>>( \
precision, scale); \
case TYPE_DECIMAL64: \
return std::make_unique<NumericToDecimalConverter<SRC_PTYPE, TYPE_DECIMAL64>>( \
precision, scale); \
case TYPE_DECIMAL128I: \
return std::make_unique<NumericToDecimalConverter<SRC_PTYPE, TYPE_DECIMAL128I>>( \
precision, scale); \
case TYPE_DECIMAL256: \
return std::make_unique<NumericToDecimalConverter<SRC_PTYPE, TYPE_DECIMAL256>>( \
precision, scale); \
default: \
return std::make_unique<UnsupportedConverter>(src_type, dst_type); \
} \
}
FOR_LOGICAL_NUMERIC_TYPES(DISPATCH)
#undef DISPATCH
default:
return std::make_unique<UnsupportedConverter>(src_type, dst_type);
}
}
static std::unique_ptr<ColumnTypeConverter> _datetime_to_numeric_converter(
const DataTypePtr& src_type, const DataTypePtr& dst_type) {
PrimitiveType dst_primitive_type = dst_type->get_primitive_type();
switch (dst_primitive_type) {
#define DISPATCH(DST_TYPE) \
case DST_TYPE: { \
return std::make_unique<DateTimeToNumericConverter<DST_TYPE>>(); \
}
FOR_LOGICAL_INTEGER_TYPES(DISPATCH)
#undef DISPATCH
default: {
return std::make_unique<UnsupportedConverter>(src_type, dst_type);
}
};
}
static std::unique_ptr<ColumnTypeConverter> _decimal_to_numeric_converter(
const DataTypePtr& src_type, const DataTypePtr& dst_type) {
PrimitiveType src_primitive_type = src_type->get_primitive_type();
PrimitiveType dst_primitive_type = dst_type->get_primitive_type();
int scale = src_type->get_scale();
switch (dst_primitive_type) {
#define DISPATCH(DST_PTYPE) \
case DST_PTYPE: { \
switch (src_primitive_type) { \
case TYPE_DECIMAL32: \
return std::make_unique<DecimalToNumericConverter<TYPE_DECIMAL32, DST_PTYPE>>(scale); \
case TYPE_DECIMAL64: \
return std::make_unique<DecimalToNumericConverter<TYPE_DECIMAL64, DST_PTYPE>>(scale); \
case TYPE_DECIMAL128I: \
return std::make_unique<DecimalToNumericConverter<TYPE_DECIMAL128I, DST_PTYPE>>( \
scale); \
case TYPE_DECIMAL256: \
return std::make_unique<DecimalToNumericConverter<TYPE_DECIMAL256, DST_PTYPE>>(scale); \
default: \
return std::make_unique<UnsupportedConverter>(src_type, dst_type); \
} \
}
FOR_LOGICAL_NUMERIC_TYPES(DISPATCH)
#undef DISPATCH
default:
return std::make_unique<UnsupportedConverter>(src_type, dst_type);
}
}
static std::unique_ptr<ColumnTypeConverter> _decimal_converter(const DataTypePtr& src_type,
const DataTypePtr& dst_type) {
int from_precision = src_type->get_precision();
int from_scale = src_type->get_scale();
int to_precision = dst_type->get_precision();
int to_scale = dst_type->get_scale();
if (from_scale == to_scale && from_precision == to_precision) {
return std::make_unique<ConsistentConverter>();
}
PrimitiveType src_primitive_type = src_type->get_primitive_type();
PrimitiveType dst_primitive_type = dst_type->get_primitive_type();
switch (dst_primitive_type) {
#define DISPATCH(DST_PTYPE) \
case DST_PTYPE: { \
switch (src_primitive_type) { \
case TYPE_DECIMAL32: \
return std::make_unique<DecimalToDecimalConverter<TYPE_DECIMAL32, DST_PTYPE>>( \
from_precision, from_scale, to_precision, to_scale); \
case TYPE_DECIMAL64: \
return std::make_unique<DecimalToDecimalConverter<TYPE_DECIMAL64, DST_PTYPE>>( \
from_precision, from_scale, to_precision, to_scale); \
case TYPE_DECIMAL128I: \
return std::make_unique<DecimalToDecimalConverter<TYPE_DECIMAL128I, DST_PTYPE>>( \
from_precision, from_scale, to_precision, to_scale); \
case TYPE_DECIMAL256: \
return std::make_unique<DecimalToDecimalConverter<TYPE_DECIMAL256, DST_PTYPE>>( \
from_precision, from_scale, to_precision, to_scale); \
default: \
return std::make_unique<UnsupportedConverter>(src_type, dst_type); \
} \
}
FOR_LOGICAL_DECIMAL_TYPES(DISPATCH)
#undef DISPATCH
default:
return std::make_unique<UnsupportedConverter>(src_type, dst_type);
}
}
std::unique_ptr<ColumnTypeConverter> ColumnTypeConverter::get_converter(const DataTypePtr& src_type,
const DataTypePtr& dst_type,
FileFormat file_format) {
PrimitiveType src_primitive_type = src_type->get_primitive_type();
PrimitiveType dst_primitive_type = dst_type->get_primitive_type();
//todo: type to varchar/char.
if (is_string_type(src_primitive_type) && is_string_type(dst_primitive_type)) {
return std::make_unique<ConsistentConverter>();
}
if (_is_decimal_type(src_primitive_type) && _is_decimal_type(dst_primitive_type)) {
return _decimal_converter(src_type, dst_type);
}
if (src_primitive_type == dst_primitive_type) {
return std::make_unique<ConsistentConverter>();
}
// from numeric type to numeric type, use native static cast
// example: float -> int
if (_is_numeric_type(src_primitive_type) && _is_numeric_type(dst_primitive_type)) {
return _numeric_converter(src_type, dst_type);
}
auto str_type = is_string_type(src_primitive_type)
? DataTypeFactory::instance().create_data_type(
PrimitiveType::TYPE_STRING, src_type->is_nullable())
: src_type;
// change to string type
// example: decimal -> string
if (is_string_type(dst_primitive_type)) {
if (file_format == ORC) {
return _to_string_converter<ORC>(str_type, dst_type);
} else {
return _to_string_converter<COMMON>(str_type, dst_type);
}
}
// string type to other type
// example: string -> date
if (is_string_type(src_primitive_type)) {
if (file_format == ORC) {
return _from_string_converter<ORC>(str_type, dst_type);
} else {
return _from_string_converter<COMMON>(str_type, dst_type);
}
}
// date to datetime, datetime to date
// only support date & datetime v2
if (src_primitive_type == TYPE_DATEV2 && dst_primitive_type == TYPE_DATETIMEV2) {
return std::make_unique<TimeV2Converter<TYPE_DATEV2, TYPE_DATETIMEV2>>();
}
if (src_primitive_type == TYPE_DATETIMEV2 && dst_primitive_type == TYPE_DATEV2) {
return std::make_unique<TimeV2Converter<TYPE_DATETIMEV2, TYPE_DATEV2>>();
}
// datetime to bigint (ms)
if (src_primitive_type == TYPE_DATETIMEV2 && _is_numeric_type(dst_primitive_type)) {
return _datetime_to_numeric_converter(src_type, dst_type);
}
// numeric to decimal
if (_is_numeric_type(src_primitive_type) && _is_decimal_type(dst_primitive_type)) {
return _numeric_to_decimal_converter(src_type, dst_type);
}
// decimal to numeric
if (_is_decimal_type(src_primitive_type) && _is_numeric_type(dst_primitive_type)) {
return _decimal_to_numeric_converter(src_type, dst_type);
}
return std::make_unique<UnsupportedConverter>(src_type, dst_type);
}
} // namespace doris::converter