forked from ClickHouse/ClickHouse
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSerializationInfo.cpp
More file actions
348 lines (281 loc) · 9.97 KB
/
SerializationInfo.cpp
File metadata and controls
348 lines (281 loc) · 9.97 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
#include <DataTypes/Serializations/SerializationInfo.h>
#include <Columns/ColumnSparse.h>
#include <DataTypes/IDataType.h>
#include <IO/ReadHelpers.h>
#include <IO/WriteHelpers.h>
#include <Core/Block.h>
#include <base/EnumReflection.h>
#include <Poco/JSON/JSON.h>
#include <Poco/JSON/Object.h>
#include <Poco/JSON/Stringifier.h>
#include <Poco/JSON/Parser.h>
namespace DB
{
namespace ErrorCodes
{
extern const int CORRUPTED_DATA;
}
namespace
{
constexpr auto KEY_VERSION = "version";
constexpr auto KEY_NUM_ROWS = "num_rows";
constexpr auto KEY_COLUMNS = "columns";
constexpr auto KEY_NUM_DEFAULTS = "num_defaults";
constexpr auto KEY_KIND = "kind";
constexpr auto KEY_NAME = "name";
}
void SerializationInfo::Data::add(const IColumn & column)
{
size_t rows = column.size();
double ratio = column.getRatioOfDefaultRows(ColumnSparse::DEFAULT_ROWS_SEARCH_SAMPLE_RATIO);
num_rows += rows;
num_defaults += static_cast<size_t>(ratio * rows);
}
void SerializationInfo::Data::add(const Data & other)
{
num_rows += other.num_rows;
num_defaults += other.num_defaults;
}
void SerializationInfo::Data::remove(const Data & other)
{
num_rows -= other.num_rows;
num_defaults -= other.num_defaults;
}
void SerializationInfo::Data::addDefaults(size_t length)
{
num_rows += length;
num_defaults += length;
}
SerializationInfo::SerializationInfo(ISerialization::Kind kind_, const Settings & settings_)
: settings(settings_)
, kind(kind_)
{
}
SerializationInfo::SerializationInfo(ISerialization::Kind kind_, const Settings & settings_, const Data & data_)
: settings(settings_)
, kind(kind_)
, data(data_)
{
}
void SerializationInfo::add(const IColumn & column)
{
data.add(column);
if (settings.choose_kind)
kind = chooseKind(data, settings);
}
void SerializationInfo::add(const SerializationInfo & other)
{
data.add(other.data);
if (settings.choose_kind)
kind = chooseKind(data, settings);
}
void SerializationInfo::remove(const SerializationInfo & other)
{
data.remove(other.data);
if (settings.choose_kind)
kind = chooseKind(data, settings);
}
void SerializationInfo::addDefaults(size_t length)
{
data.addDefaults(length);
if (settings.choose_kind)
kind = chooseKind(data, settings);
}
void SerializationInfo::replaceData(const SerializationInfo & other)
{
data = other.data;
}
MutableSerializationInfoPtr SerializationInfo::clone() const
{
return std::make_shared<SerializationInfo>(kind, settings, data);
}
/// Returns true if all rows with default values of type 'lhs'
/// are mapped to default values of type 'rhs' after conversion.
static bool preserveDefaultsAfterConversion(const IDataType & lhs, const IDataType & rhs)
{
if (lhs.equals(rhs))
return true;
bool lhs_is_columned_as_numeric = isColumnedAsNumber(lhs) || isColumnedAsDecimal(lhs);
bool rhs_is_columned_as_numeric = isColumnedAsNumber(rhs) || isColumnedAsDecimal(rhs);
if (lhs_is_columned_as_numeric && rhs_is_columned_as_numeric)
return true;
if (isStringOrFixedString(lhs) && isStringOrFixedString(rhs))
return true;
return false;
}
std::shared_ptr<SerializationInfo> SerializationInfo::createWithType(
const IDataType & old_type,
const IDataType & new_type,
const Settings & new_settings) const
{
auto new_kind = kind;
if (new_kind == ISerialization::Kind::SPARSE)
{
if (!new_type.supportsSparseSerialization()
|| !preserveDefaultsAfterConversion(old_type, new_type))
new_kind = ISerialization::Kind::DEFAULT;
}
return std::make_shared<SerializationInfo>(new_kind, new_settings);
}
void SerializationInfo::serialializeKindBinary(WriteBuffer & out) const
{
writeBinary(static_cast<UInt8>(kind), out);
}
void SerializationInfo::deserializeFromKindsBinary(ReadBuffer & in)
{
UInt8 kind_num;
readBinary(kind_num, in);
auto maybe_kind = magic_enum::enum_cast<ISerialization::Kind>(kind_num);
if (!maybe_kind)
throw Exception(ErrorCodes::CORRUPTED_DATA, "Unknown serialization kind {}", std::to_string(kind_num));
kind = *maybe_kind;
}
void SerializationInfo::toJSON(Poco::JSON::Object & object) const
{
object.set(KEY_KIND, ISerialization::kindToString(kind));
object.set(KEY_NUM_DEFAULTS, data.num_defaults);
object.set(KEY_NUM_ROWS, data.num_rows);
}
void SerializationInfo::fromJSON(const Poco::JSON::Object & object)
{
if (!object.has(KEY_KIND) || !object.has(KEY_NUM_DEFAULTS) || !object.has(KEY_NUM_ROWS))
throw Exception(ErrorCodes::CORRUPTED_DATA,
"Missed field '{}' or '{}' or '{}' in SerializationInfo of columns",
KEY_KIND, KEY_NUM_DEFAULTS, KEY_NUM_ROWS);
data.num_rows = object.getValue<size_t>(KEY_NUM_ROWS);
data.num_defaults = object.getValue<size_t>(KEY_NUM_DEFAULTS);
kind = ISerialization::stringToKind(object.getValue<String>(KEY_KIND));
}
ISerialization::Kind SerializationInfo::chooseKind(const Data & data, const Settings & settings)
{
double ratio = data.num_rows ? std::min(static_cast<double>(data.num_defaults) / data.num_rows, 1.0) : 0.0;
return ratio > settings.ratio_of_defaults_for_sparse ? ISerialization::Kind::SPARSE : ISerialization::Kind::DEFAULT;
}
SerializationInfoByName::SerializationInfoByName(
const NamesAndTypesList & columns,
const SerializationInfo::Settings & settings)
{
if (settings.isAlwaysDefault())
return;
for (const auto & column : columns)
if (column.type->supportsSparseSerialization())
emplace(column.name, column.type->createSerializationInfo(settings));
}
void SerializationInfoByName::add(const Block & block)
{
for (const auto & column : block)
{
auto it = find(column.name);
if (it == end())
continue;
it->second->add(*column.column);
}
}
void SerializationInfoByName::add(const SerializationInfoByName & other)
{
for (const auto & [name, info] : other)
add(name, *info);
}
void SerializationInfoByName::add(const String & name, const SerializationInfo & info)
{
if (auto it = find(name); it != end())
it->second->add(info);
}
void SerializationInfoByName::remove(const SerializationInfoByName & other)
{
for (const auto & [name, info] : other)
remove(name, *info);
}
void SerializationInfoByName::remove(const String & name, const SerializationInfo & info)
{
if (auto it = find(name); it != end())
it->second->remove(info);
}
SerializationInfoPtr SerializationInfoByName::tryGet(const String & name) const
{
auto it = find(name);
return it == end() ? nullptr : it->second;
}
MutableSerializationInfoPtr SerializationInfoByName::tryGet(const String & name)
{
auto it = find(name);
return it == end() ? nullptr : it->second;
}
void SerializationInfoByName::replaceData(const SerializationInfoByName & other)
{
for (const auto & [name, new_info] : other)
{
auto & old_info = (*this)[name];
if (old_info)
old_info->replaceData(*new_info);
else
old_info = new_info->clone();
}
}
ISerialization::Kind SerializationInfoByName::getKind(const String & column_name) const
{
auto it = find(column_name);
return it != end() ? it->second->getKind() : ISerialization::Kind::DEFAULT;
}
void SerializationInfoByName::writeJSON(WriteBuffer & out) const
{
Poco::JSON::Object object;
object.set(KEY_VERSION, SERIALIZATION_INFO_VERSION);
Poco::JSON::Array column_infos;
for (const auto & [name, info] : *this)
{
Poco::JSON::Object info_json;
info->toJSON(info_json);
info_json.set(KEY_NAME, name);
column_infos.add(std::move(info_json)); /// NOLINT
}
object.set(KEY_COLUMNS, std::move(column_infos)); /// NOLINT
std::ostringstream oss; // STYLE_CHECK_ALLOW_STD_STRING_STREAM
oss.exceptions(std::ios::failbit);
Poco::JSON::Stringifier::stringify(object, oss);
writeString(oss.str(), out);
}
SerializationInfoByName SerializationInfoByName::readJSONFromString(
const NamesAndTypesList & columns, const Settings & settings, const std::string & json_str)
{
Poco::JSON::Parser parser;
auto object = parser.parse(json_str).extract<Poco::JSON::Object::Ptr>();
if (!object->has(KEY_VERSION))
throw Exception(ErrorCodes::CORRUPTED_DATA, "Missed version of serialization infos");
if (object->getValue<size_t>(KEY_VERSION) > SERIALIZATION_INFO_VERSION)
throw Exception(ErrorCodes::CORRUPTED_DATA,
"Unknown version of serialization infos ({}). Should be less or equal than {}",
object->getValue<size_t>(KEY_VERSION), SERIALIZATION_INFO_VERSION);
SerializationInfoByName infos;
if (object->has(KEY_COLUMNS))
{
std::unordered_map<std::string_view, const IDataType *> column_type_by_name;
for (const auto & [name, type] : columns)
column_type_by_name.emplace(name, type.get());
auto array = object->getArray(KEY_COLUMNS);
for (const auto & elem : *array)
{
const auto & elem_object = elem.extract<Poco::JSON::Object::Ptr>();
if (!elem_object->has(KEY_NAME))
throw Exception(ErrorCodes::CORRUPTED_DATA,
"Missed field '{}' in serialization infos", KEY_NAME);
auto name = elem_object->getValue<String>(KEY_NAME);
auto it = column_type_by_name.find(name);
if (it == column_type_by_name.end())
throw Exception(ErrorCodes::CORRUPTED_DATA,
"Found unexpected column '{}' in serialization infos", name);
auto info = it->second->createSerializationInfo(settings);
info->fromJSON(*elem_object);
infos.emplace(name, std::move(info));
}
}
return infos;
}
SerializationInfoByName SerializationInfoByName::readJSON(
const NamesAndTypesList & columns, const Settings & settings, ReadBuffer & in)
{
String json_str;
readString(json_str, in);
return readJSONFromString(columns, settings, json_str);
}
}