forked from ClickHouse/ClickHouse
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIDataType.cpp
More file actions
522 lines (453 loc) · 21 KB
/
Copy pathIDataType.cpp
File metadata and controls
522 lines (453 loc) · 21 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
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
#include <cstddef>
#include <Columns/IColumn.h>
#include <Columns/ColumnConst.h>
#include <Columns/ColumnSparse.h>
#include <Columns/ColumnReplicated.h>
#include <Common/checkStackSize.h>
#include <Common/Exception.h>
#include <Common/quoteString.h>
#include <Common/SipHash.h>
#include <IO/WriteHelpers.h>
#include <DataTypes/IDataType.h>
#include <DataTypes/DataTypeCustom.h>
#include <DataTypes/NestedUtils.h>
#include <DataTypes/Serializations/SerializationSparse.h>
#include <DataTypes/Serializations/SerializationReplicated.h>
#include <DataTypes/Serializations/SerializationInfo.h>
#include <DataTypes/Serializations/SerializationDetached.h>
namespace DB
{
namespace ErrorCodes
{
extern const int LOGICAL_ERROR;
extern const int DATA_TYPE_CANNOT_BE_PROMOTED;
extern const int ILLEGAL_COLUMN;
extern const int NOT_IMPLEMENTED;
}
IDataType::IDataType() = default;
IDataType::~IDataType() = default;
String IDataType::getName() const
{
if (custom_name)
return custom_name->getName();
return doGetName();
}
String IDataType::getPrettyName(size_t indent) const
{
if (custom_name)
return custom_name->getName();
return doGetPrettyName(indent);
}
void IDataType::updateHash(SipHash & hash) const
{
if (custom_name)
{
hash.update(custom_name->getName().size());
hash.update(custom_name->getName());
}
else
hash.update(size_t(0));
hash.update(getTypeId());
updateHashImpl(hash);
}
void IDataType::updateAvgValueSizeHint(const IColumn & column, double & avg_value_size_hint)
{
/// Update the average value size hint if amount of read rows isn't too small
size_t column_size = column.size();
if (column_size > 10)
{
double current_avg_value_size = static_cast<double>(column.byteSize()) / static_cast<double>(column_size);
/// Heuristic is chosen so that avg_value_size_hint increases rapidly but decreases slowly.
if (current_avg_value_size > avg_value_size_hint)
avg_value_size_hint = std::min(1024., current_avg_value_size); /// avoid overestimation
else if (current_avg_value_size * 2 < avg_value_size_hint)
avg_value_size_hint = (current_avg_value_size + avg_value_size_hint * 3) / 4;
}
}
MutableColumnPtr IDataType::createUninitializedColumnWithSize(size_t size) const
{
auto column = createColumn();
return column->cloneResized(size);
}
MutableColumnPtr IDataType::createColumn(const ISerialization & serialization) const
{
auto kind_stack = serialization.getKindStack();
auto column = createColumn();
for (auto kind : kind_stack)
{
if (kind == ISerialization::Kind::SPARSE)
column = ColumnSparse::create(std::move(column));
else if (kind == ISerialization::Kind::REPLICATED)
column = ColumnReplicated::create(std::move(column), ColumnUInt8::create());
}
return column;
}
MutableColumnConstPtr IDataType::createColumnConst(size_t size, const Field & field) const
{
auto column = createColumn();
column->insert(field);
return ColumnConst::create(std::move(column), size);
}
MutableColumnConstPtr IDataType::createColumnConstWithDefaultValue(size_t size) const
{
return createColumnConst(size, getDefault());
}
DataTypePtr IDataType::promoteNumericType() const
{
throw Exception(ErrorCodes::DATA_TYPE_CANNOT_BE_PROMOTED, "Data type {} can't be promoted.", getName());
}
size_t IDataType::getSizeOfValueInMemory() const
{
throw Exception(ErrorCodes::LOGICAL_ERROR, "Value of type {} in memory is not of fixed size.", getName());
}
void IDataType::forEachSubcolumn(
const SubcolumnCallback & callback,
const SubstreamData & data)
{
ISerialization::StreamCallback callback_with_data = [&](const auto & subpath)
{
for (size_t i = 0; i < subpath.size(); ++i)
{
size_t prefix_len = i + 1;
if (!subpath[i].visited && ISerialization::hasSubcolumnForPath(subpath, prefix_len))
{
auto name = ISerialization::getSubcolumnNameForStream(subpath, prefix_len);
auto subdata = ISerialization::createFromPath(subpath, prefix_len);
auto path_copy = subpath;
path_copy.resize(prefix_len);
callback(path_copy, name, subdata);
}
subpath[i].visited = true;
}
};
ISerialization::EnumerateStreamsSettings settings;
settings.position_independent_encoding = false;
settings.enumerate_virtual_streams = true;
data.serialization->enumerateStreams(settings, callback_with_data, data);
}
std::unique_ptr<IDataType::SubstreamData> IDataType::getSubcolumnData(
std::string_view subcolumn_name,
const SubstreamData & data,
size_t initial_array_level,
bool throw_if_null)
{
std::unique_ptr<IDataType::SubstreamData> res;
/// Track whether res was set by an exact name match, so that exact matches
/// always take priority over prefix (dynamic subcolumn) matches.
/// This matters when e.g. JSON has typed paths "a" (Array(JSON)) and "a.b" (Int64):
/// without this, the prefix match on "a" would fire first (sorted order) and
/// the exact match on "a.b" would be skipped because res is already set.
bool res_from_exact_match = false;
ISerialization::StreamCallback callback_with_data = [&](const auto & subpath)
{
for (size_t i = 0; i < subpath.size(); ++i)
{
size_t prefix_len = i + 1;
if (!subpath[i].visited && ISerialization::hasSubcolumnForPath(subpath, prefix_len))
{
auto name = ISerialization::getSubcolumnNameForStream(subpath, prefix_len, false, initial_array_level);
/// Create data from path only if it's requested subcolumn.
/// Use the first exact match to be consistent with ColumnsDescription::addSubcolumns
/// which also keeps the first subcolumn when there are name collisions
/// (e.g. "null" can match both Nullable's null-map and a Tuple element named "null").
/// Exact matches always take priority over prefix matches regardless of iteration order.
if (name == subcolumn_name && !res_from_exact_match)
{
res = std::make_unique<SubstreamData>(ISerialization::createFromPath(subpath, prefix_len));
res_from_exact_match = true;
}
/// Check if this subcolumn is a prefix of requested subcolumn and it can create dynamic subcolumns.
/// Only use prefix matches when no exact match has been found.
else if (!res_from_exact_match && subcolumn_name.starts_with(name + ".") && subpath[i].data.type && subpath[i].data.type->hasDynamicSubcolumnsData())
{
auto dynamic_subcolumn_name = subcolumn_name.substr(name.size() + 1);
auto dynamic_subcolumn_data = subpath[i].data.type->getDynamicSubcolumnData(
dynamic_subcolumn_name,
subpath[i].data,
initial_array_level + ISerialization::getArrayLevel(subpath, prefix_len),
false);
if (dynamic_subcolumn_data)
{
/// Create requested subcolumn using dynamic subcolumn data.
auto tmp_subpath = subpath;
if (tmp_subpath[i].creator)
{
dynamic_subcolumn_data->type = tmp_subpath[i].creator->create(dynamic_subcolumn_data->type);
dynamic_subcolumn_data->column = tmp_subpath[i].creator->create(dynamic_subcolumn_data->column);
dynamic_subcolumn_data->serialization = tmp_subpath[i].creator->create(dynamic_subcolumn_data->serialization, dynamic_subcolumn_data->type);
}
tmp_subpath[i].data = *dynamic_subcolumn_data;
res = std::make_unique<SubstreamData>(ISerialization::createFromPath(tmp_subpath, prefix_len));
}
}
}
subpath[i].visited = true;
}
};
ISerialization::EnumerateStreamsSettings settings;
settings.position_independent_encoding = false;
/// Don't enumerate dynamic subcolumns, they are handled separately.
settings.enumerate_dynamic_streams = false;
settings.enumerate_virtual_streams = true;
settings.array_level = initial_array_level;
data.serialization->enumerateStreams(settings, callback_with_data, data);
if (!res && data.type->hasDynamicSubcolumnsData())
return data.type->getDynamicSubcolumnData(subcolumn_name, data, settings.array_level, throw_if_null);
if (!res && throw_if_null)
throw Exception(ErrorCodes::ILLEGAL_COLUMN, "There is no subcolumn {} in type {}", subcolumn_name, data.type->getName());
return res;
}
std::unique_ptr<IDataType::SubstreamData> IDataType::getDynamicSubcolumnData(
std::string_view /*subcolumn_name*/,
const SubstreamData & /*data*/,
size_t /*initial_array_level*/,
bool throw_if_null) const
{
if (throw_if_null)
throw Exception(ErrorCodes::NOT_IMPLEMENTED, "Method getDynamicSubcolumnData is not implemented for type {}", getName());
return nullptr;
}
bool IDataType::hasSubcolumn(std::string_view subcolumn_name) const
{
return tryGetSubcolumnType(subcolumn_name) != nullptr;
}
bool IDataType::hasDynamicSubcolumns() const
{
if (hasDynamicSubcolumnsData())
return true;
bool has_dynamic_subcolumns = false;
auto data = SubstreamData(getDefaultSerialization()).withType(getPtr());
auto callback = [&](const SubstreamPath &, const String &, const SubstreamData & subcolumn_data)
{
has_dynamic_subcolumns |= subcolumn_data.type && subcolumn_data.type->hasDynamicSubcolumnsData();
};
forEachSubcolumn(callback, data);
return has_dynamic_subcolumns;
}
DataTypePtr IDataType::tryGetSubcolumnType(std::string_view subcolumn_name) const
{
auto data = SubstreamData(getDefaultSerialization()).withType(getPtr());
auto subcolumn_data = getSubcolumnData(subcolumn_name, data, {}, false);
return subcolumn_data ? subcolumn_data->type : nullptr;
}
DataTypePtr IDataType::getSubcolumnType(std::string_view subcolumn_name) const
{
auto data = SubstreamData(getDefaultSerialization()).withType(getPtr());
return getSubcolumnData(subcolumn_name, data, {}, true)->type;
}
ColumnPtr IDataType::tryGetSubcolumn(std::string_view subcolumn_name, const ColumnPtr & column) const
{
auto data = SubstreamData(getSerialization(*getSerializationInfo(*column))).withType(getPtr()).withColumn(column);
auto subcolumn_data = getSubcolumnData(subcolumn_name, data, {}, false);
return subcolumn_data ? subcolumn_data->column : nullptr;
}
ColumnPtr IDataType::getSubcolumn(std::string_view subcolumn_name, const ColumnPtr & column) const
{
auto data = SubstreamData(getSerialization(*getSerializationInfo(*column))).withType(getPtr()).withColumn(column);
return getSubcolumnData(subcolumn_name, data, {}, true)->column;
}
SerializationPtr IDataType::getSubcolumnSerialization(std::string_view subcolumn_name, const SerializationPtr & serialization) const
{
auto data = SubstreamData(serialization).withType(getPtr());
return getSubcolumnData(subcolumn_name, data, {}, true)->serialization;
}
Names IDataType::getSubcolumnNames() const
{
Names res;
forEachSubcolumn([&](const auto &, const auto & name, const auto &)
{
res.push_back(name);
}, SubstreamData(getDefaultSerialization()));
return res;
}
void IDataType::insertDefaultInto(IColumn & column) const
{
column.insertDefault();
}
void IDataType::insertManyDefaultsInto(IColumn & column, size_t n) const
{
column.reserve(column.size() + n);
for (size_t i = 0; i < n; ++i)
insertDefaultInto(column);
}
void IDataType::setCustomization(DataTypeCustomDescPtr custom_desc_) const
{
/// replace only if not null
if (custom_desc_->name)
custom_name = std::move(custom_desc_->name);
if (custom_desc_->serialization)
custom_serialization = std::move(custom_desc_->serialization);
}
MutableSerializationInfoPtr IDataType::createSerializationInfo(const SerializationInfoSettings & settings) const
{
return std::make_shared<SerializationInfo>(ISerialization::KindStack{ISerialization::Kind::DEFAULT}, settings);
}
SerializationInfoPtr IDataType::getSerializationInfo(const IColumn & column) const
{
if (const auto * column_const = checkAndGetColumn<ColumnConst>(&column))
return getSerializationInfo(column_const->getDataColumn());
/// Enable all supported serialization features when deriving info from an existing column. Since the column
/// reflects the actual in-memory state, the serialization info must accept any variant that the column may contain.
return std::make_shared<SerializationInfo>(
ISerialization::getKindStack(column), SerializationInfoSettings::enableAllSupportedSerializations());
}
SerializationPtr IDataType::getDefaultSerialization() const
{
checkStackSize();
if (custom_serialization)
return custom_serialization;
return doGetSerialization(SerializationInfoSettings{});
}
SerializationPtr IDataType::wrapSerializationBasedOnKindStack(SerializationPtr serialization, const ISerialization::KindStack & kind_stack, const SerializationInfoSettings & settings) const
{
for (auto kind : kind_stack)
{
if (settings.canUseSparseSerialization(*this) && kind == ISerialization::Kind::SPARSE)
serialization = SerializationSparse::create(serialization);
else if (kind == ISerialization::Kind::DETACHED)
serialization = SerializationDetached::create(serialization);
else if (kind == ISerialization::Kind::REPLICATED)
serialization = SerializationReplicated::create(serialization);
}
return serialization;
}
SerializationPtr IDataType::getSerialization(const SerializationInfo & info) const
{
return wrapSerializationBasedOnKindStack(getSerialization(info.getSettings()), info.getKindStack(), info.getSettings());
}
SerializationPtr IDataType::getSerialization(const SerializationInfoSettings & settings) const
{
checkStackSize();
if (custom_serialization)
return custom_serialization;
return doGetSerialization(settings);
}
// static
SerializationPtr IDataType::getSerialization(const NameAndTypePair & column, const SerializationInfo & info)
{
if (column.isSubcolumn())
{
const auto & type_in_storage = column.getTypeInStorage();
auto serialization = type_in_storage->getSerialization(info);
return type_in_storage->getSubcolumnSerialization(column.getSubcolumnName(), serialization);
}
return column.type->getSerialization(info);
}
// static
SerializationPtr IDataType::getSerialization(const NameAndTypePair & column, const SerializationInfoSettings & settings)
{
if (column.isSubcolumn())
{
const auto & type_in_storage = column.getTypeInStorage();
auto serialization = type_in_storage->getSerialization(settings);
return type_in_storage->getSubcolumnSerialization(column.getSubcolumnName(), serialization);
}
return column.type->getSerialization(settings);
}
// static
SerializationPtr IDataType::getSerialization(const NameAndTypePair & column)
{
if (column.isSubcolumn())
{
const auto & type_in_storage = column.getTypeInStorage();
auto serialization = type_in_storage->getDefaultSerialization();
return type_in_storage->getSubcolumnSerialization(column.getSubcolumnName(), serialization);
}
return column.type->getDefaultSerialization();
}
#define FOR_TYPES_OF_TYPE(M) \
M(TypeIndex) \
M(const IDataType &) \
M(const DataTypePtr &) \
M(WhichDataType)
#define DISPATCH(TYPE) \
bool isUInt8(TYPE data_type) { return WhichDataType(data_type).isUInt8(); } \
bool isUInt16(TYPE data_type) { return WhichDataType(data_type).isUInt16(); } \
bool isUInt32(TYPE data_type) { return WhichDataType(data_type).isUInt32(); } \
bool isUInt64(TYPE data_type) { return WhichDataType(data_type).isUInt64(); } \
bool isUInt128(TYPE data_type) { return WhichDataType(data_type).isUInt128(); } \
bool isUInt256(TYPE data_type) { return WhichDataType(data_type).isUInt256(); } \
bool isNativeUInt(TYPE data_type) { return WhichDataType(data_type).isNativeUInt(); } \
bool isUInt(TYPE data_type) { return WhichDataType(data_type).isUInt(); } \
\
bool isInt8(TYPE data_type) { return WhichDataType(data_type).isInt8(); } \
bool isInt16(TYPE data_type) { return WhichDataType(data_type).isInt16(); } \
bool isInt32(TYPE data_type) { return WhichDataType(data_type).isInt32(); } \
bool isInt64(TYPE data_type) { return WhichDataType(data_type).isInt64(); } \
bool isInt128(TYPE data_type) { return WhichDataType(data_type).isInt128(); } \
bool isInt256(TYPE data_type) { return WhichDataType(data_type).isInt256(); } \
bool isNativeInt(TYPE data_type) { return WhichDataType(data_type).isNativeInt(); } \
bool isInt(TYPE data_type) { return WhichDataType(data_type).isInt(); } \
\
bool isInteger(TYPE data_type) { return WhichDataType(data_type).isInteger(); } \
bool isNativeInteger(TYPE data_type) { return WhichDataType(data_type).isNativeInteger(); } \
\
bool isDecimal(TYPE data_type) { return WhichDataType(data_type).isDecimal(); } \
bool isDecimal64(TYPE data_type) { return WhichDataType(data_type).isDecimal64(); } \
\
bool isFloat(TYPE data_type) { return WhichDataType(data_type).isFloat(); } \
bool isNativeFloat(TYPE data_type) { return WhichDataType(data_type).isNativeFloat(); } \
\
bool isIntegerOrDecimal(TYPE data_type) { return WhichDataType(data_type).isIntegerOrDecimal(); } \
bool isNativeNumber(TYPE data_type) { return WhichDataType(data_type).isNativeNumber(); } \
bool isNumber(TYPE data_type) { return WhichDataType(data_type).isNumber(); } \
\
bool isEnum8(TYPE data_type) { return WhichDataType(data_type).isEnum8(); } \
bool isEnum16(TYPE data_type) { return WhichDataType(data_type).isEnum16(); } \
bool isEnum(TYPE data_type) { return WhichDataType(data_type).isEnum(); } \
\
bool isDate(TYPE data_type) { return WhichDataType(data_type).isDate(); } \
bool isDate32(TYPE data_type) { return WhichDataType(data_type).isDate32(); } \
bool isDateOrDate32(TYPE data_type) { return WhichDataType(data_type).isDateOrDate32(); } \
bool isDateTime(TYPE data_type) { return WhichDataType(data_type).isDateTime(); } \
bool isTime(TYPE data_type) { return WhichDataType(data_type).isTime(); } \
bool isDateTime64(TYPE data_type) { return WhichDataType(data_type).isDateTime64(); } \
bool isTime64(TYPE data_type) { return WhichDataType(data_type).isTime64(); } \
bool isDateTimeOrDateTime64(TYPE data_type) { return WhichDataType(data_type).isDateTimeOrDateTime64(); } \
bool isDateOrDate32OrDateTimeOrDateTime64(TYPE data_type) { return WhichDataType(data_type).isDateOrDate32OrDateTimeOrDateTime64(); } \
bool isDateOrDate32OrTimeOrTime64OrDateTimeOrDateTime64(TYPE data_type) { return WhichDataType(data_type).isDateOrDate32OrTimeOrTime64OrDateTimeOrDateTime64(); } \
\
bool isString(TYPE data_type) { return WhichDataType(data_type).isString(); } \
bool isFixedString(TYPE data_type) { return WhichDataType(data_type).isFixedString(); } \
bool isStringOrFixedString(TYPE data_type) { return WhichDataType(data_type).isStringOrFixedString(); } \
\
bool isUUID(TYPE data_type) { return WhichDataType(data_type).isUUID(); } \
bool isIPv4(TYPE data_type) { return WhichDataType(data_type).isIPv4(); } \
bool isIPv6(TYPE data_type) { return WhichDataType(data_type).isIPv6(); } \
bool isArray(TYPE data_type) { return WhichDataType(data_type).isArray(); } \
bool isTuple(TYPE data_type) { return WhichDataType(data_type).isTuple(); } \
bool isMap(TYPE data_type) {return WhichDataType(data_type).isMap(); } \
bool isInterval(TYPE data_type) {return WhichDataType(data_type).isInterval(); } \
bool isVariant(TYPE data_type) { return WhichDataType(data_type).isVariant(); } \
bool isDynamic(TYPE data_type) { return WhichDataType(data_type).isDynamic(); } \
bool isObject(TYPE data_type) { return WhichDataType(data_type).isObject(); } \
bool isNothing(TYPE data_type) { return WhichDataType(data_type).isNothing(); } \
bool isQBit(TYPE data_type) { return WhichDataType(data_type).isQBit(); } \
\
bool isColumnedAsNumber(TYPE data_type) \
{ \
WhichDataType which(data_type); \
return which.isInteger() || which.isFloat() || which.isDateOrDate32OrTimeOrTime64OrDateTimeOrDateTime64() || which.isUUID() || which.isIPv4() || which.isIPv6(); \
} \
\
bool isColumnedAsDecimal(TYPE data_type) \
{ \
WhichDataType which(data_type); \
return which.isDecimal() || which.isDateTime64() || which.isTime64(); \
} \
\
bool isNotCreatable(TYPE data_type) \
{ \
WhichDataType which(data_type); \
return which.isNothing() || which.isFunction() || which.isSet(); \
} \
\
bool isNotDecimalButComparableToDecimal(TYPE data_type) \
{ \
WhichDataType which(data_type); \
return which.isInt() || which.isUInt() || which.isFloat(); \
} \
FOR_TYPES_OF_TYPE(DISPATCH)
#undef DISPATCH
#undef FOR_TYPES_OF_TYPE
}