Repository navigation
Expand file tree
/
Copy pathSerializationJSON.cpp
More file actions
405 lines (362 loc) · 15.4 KB
/
Copy pathSerializationJSON.cpp
File metadata and controls
405 lines (362 loc) · 15.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
#include <DataTypes/Serializations/SerializationJSON.h>
#include <IO/WriteHelpers.h>
#include <IO/ReadHelpers.h>
#if USE_SIMDJSON
#include <Common/JSONParsers/SimdJSONParser.h>
#endif
#if USE_RAPIDJSON
#include <Common/JSONParsers/RapidJSONParser.h>
#endif
#include <Common/JSONParsers/DummyJSONParser.h>
namespace DB
{
namespace ErrorCodes
{
extern const int INCORRECT_DATA;
}
template <typename Parser>
SerializationJSON<Parser>::SerializationJSON(
std::unordered_map<String, SerializationPtr> typed_paths_serializations_,
const std::unordered_set<String> & paths_to_skip_,
const std::vector<String> & path_regexps_to_skip_,
std::unique_ptr<JSONExtractTreeNode<Parser>> json_extract_tree_)
: SerializationObject(std::move(typed_paths_serializations_), paths_to_skip_, path_regexps_to_skip_)
, json_extract_tree(std::move(json_extract_tree_))
{
}
namespace
{
/// Struct that represents elements of the JSON path.
/// "a.b.c" -> ["a", "b", "c"]
struct PathElements
{
explicit PathElements(const String & path)
{
const char * start = path.data();
const char * end = start + path.size();
const char * pos = start;
const char * last_dot_pos = pos - 1;
for (pos = start; pos != end; ++pos)
{
if (*pos == '.')
{
elements.emplace_back(last_dot_pos + 1, size_t(pos - last_dot_pos - 1));
last_dot_pos = pos;
}
}
elements.emplace_back(last_dot_pos + 1, size_t(pos - last_dot_pos - 1));
}
size_t size() const { return elements.size(); }
std::vector<std::string_view> elements;
};
/// Struct that represents a prefix of a JSON path. Used during output of the JSON object.
struct Prefix
{
/// Shrink current prefix to the common prefix of current prefix and specified path.
/// For example, if current prefix is a.b.c.d and path is a.b.e, then shrink the prefix to a.b.
void shrinkToCommonPrefix(const PathElements & path_elements)
{
/// Don't include last element in path_elements in the prefix.
size_t i = 0;
while (i != elements.size() && i != (path_elements.elements.size() - 1) && elements[i].first == path_elements.elements[i])
++i;
elements.resize(i);
}
/// Check is_first flag in current object.
bool isFirstInCurrentObject() const
{
if (elements.empty())
return root_is_first_flag;
return elements.back().second;
}
/// Set flag is_first = false in current object.
void setNotFirstInCurrentObject()
{
if (elements.empty())
root_is_first_flag = false;
else
elements.back().second = false;
}
size_t size() const { return elements.size(); }
/// Elements of the prefix: (path element, is_first flag in this prefix).
/// is_first flag indicates if we already serialized some key in the object with such prefix.
std::vector<std::pair<std::string_view, bool>> elements;
bool root_is_first_flag = true;
};
}
template <typename Parser>
void SerializationJSON<Parser>::serializeTextImpl(const IColumn & column, size_t row_num, WriteBuffer & ostr, const FormatSettings & settings, bool pretty, size_t indent) const
{
const auto & column_object = assert_cast<const ColumnObject &>(column);
const auto & typed_paths = column_object.getTypedPaths();
const auto & dynamic_paths = column_object.getDynamicPaths();
const auto & shared_data_offsets = column_object.getSharedDataOffsets();
const auto [shared_data_paths, shared_data_values] = column_object.getSharedDataPathsAndValues();
size_t shared_data_offset = shared_data_offsets[static_cast<ssize_t>(row_num) - 1];
size_t shared_data_end = shared_data_offsets[static_cast<ssize_t>(row_num)];
/// We need to convert the set of paths in this row to a JSON object.
/// To do it, we first collect all the paths from current row, then we sort them
/// and construct the resulting JSON object by iterating over sorted list of paths.
/// For example:
/// b.c, a.b, a.a, b.e, g, h.u.t -> a.a, a.b, b.c, b.e, g, h.u.t -> {"a" : {"a" : ..., "b" : ...}, "b" : {"c" : ..., "e" : ...}, "g" : ..., "h" : {"u" : {"t" : ...}}}.
std::vector<String> sorted_paths;
sorted_paths.reserve(typed_paths.size() + dynamic_paths.size() + (shared_data_end - shared_data_offset));
for (const auto & [path, _] : typed_paths)
sorted_paths.emplace_back(path);
for (const auto & [path, dynamic_column] : dynamic_paths)
{
/// We consider null value and absence of the path in a row as equivalent cases, because we cannot actually distinguish them.
/// So, we don't output null values at all.
if (!dynamic_column->isNullAt(row_num))
sorted_paths.emplace_back(path);
}
for (size_t i = shared_data_offset; i != shared_data_end; ++i)
{
auto path = shared_data_paths->getDataAt(i).toString();
sorted_paths.emplace_back(path);
}
std::sort(sorted_paths.begin(), sorted_paths.end());
if (pretty)
writeCString("{\n", ostr);
else
writeChar('{', ostr);
size_t index_in_shared_data_values = shared_data_offset;
/// current_prefix represents the path of the object we are currently serializing keys in.
Prefix current_prefix;
for (const auto & path : sorted_paths)
{
PathElements path_elements(path);
/// Change prefix to common prefix between current prefix and current path.
/// If prefix changed (it can only decrease), close all finished objects.
/// For example:
/// Current prefix: a.b.c.d
/// Current path: a.b.e.f
/// It means now we have : {..., "a" : {"b" : {"c" : {"d" : ...
/// Common prefix will be a.b, so it means we should close objects a.b.c.d and a.b.c: {..., "a" : {"b" : {"c" : {"d" : ...}}
/// and continue serializing keys in object a.b
size_t prev_prefix_size = current_prefix.size();
current_prefix.shrinkToCommonPrefix(path_elements);
size_t prefix_size = current_prefix.size();
if (prefix_size != prev_prefix_size)
{
size_t objects_to_close = prev_prefix_size - prefix_size;
if (pretty)
{
writeChar('\n', ostr);
for (size_t i = 0; i != objects_to_close; ++i)
{
writeChar(settings.json.pretty_print_indent, (indent + prefix_size + objects_to_close - i) * settings.json.pretty_print_indent_multiplier, ostr);
if (i != objects_to_close - 1)
writeCString("}\n", ostr);
else
writeChar('}', ostr);
}
}
else
{
for (size_t i = 0; i != objects_to_close; ++i)
writeChar('}', ostr);
}
}
/// Now we are inside object that has common prefix with current path.
/// We should go inside all objects in current path.
/// From the example above we should open object a.b.e:
/// {..., "a" : {"b" : {"c" : {"d" : ...}}, "e" : {
if (prefix_size + 1 < path_elements.size())
{
for (size_t i = prefix_size; i != path_elements.size() - 1; ++i)
{
/// Write comma before the key if it's not the first key in this prefix.
if (!current_prefix.isFirstInCurrentObject())
{
if (pretty)
writeCString(",\n", ostr);
else
writeChar(',', ostr);
}
else
{
current_prefix.setNotFirstInCurrentObject();
}
if (pretty)
{
writeChar(settings.json.pretty_print_indent, (indent + i + 1) * settings.json.pretty_print_indent_multiplier, ostr);
writeJSONString(path_elements.elements[i], ostr, settings);
writeCString(" : {\n", ostr);
}
else
{
writeJSONString(path_elements.elements[i], ostr, settings);
writeCString(":{", ostr);
}
/// Update current prefix.
current_prefix.elements.emplace_back(path_elements.elements[i], true);
}
}
/// Write comma before the key if it's not the first key in this prefix.
if (!current_prefix.isFirstInCurrentObject())
{
if (pretty)
writeCString(",\n", ostr);
else
writeChar(',', ostr);
}
else
{
current_prefix.setNotFirstInCurrentObject();
}
if (pretty)
{
writeChar(settings.json.pretty_print_indent, (indent + current_prefix.size() + 1) * settings.json.pretty_print_indent_multiplier, ostr);
writeJSONString(path_elements.elements.back(), ostr, settings);
writeCString(" : ", ostr);
}
else
{
writeJSONString(path_elements.elements.back(), ostr, settings);
writeCString(":", ostr);
}
/// Serialize value of current path.
if (auto typed_it = typed_paths.find(path); typed_it != typed_paths.end())
{
if (pretty)
typed_path_serializations.at(path)->serializeTextJSONPretty(*typed_it->second, row_num, ostr, settings, indent + current_prefix.size() + 1);
else
typed_path_serializations.at(path)->serializeTextJSON(*typed_it->second, row_num, ostr, settings);
}
else if (auto dynamic_it = dynamic_paths.find(path); dynamic_it != dynamic_paths.end())
{
if (pretty)
dynamic_serialization->serializeTextJSONPretty(*dynamic_it->second, row_num, ostr, settings, indent + current_prefix.size() + 1);
else
dynamic_serialization->serializeTextJSON(*dynamic_it->second, row_num, ostr, settings);
}
else
{
/// To serialize value stored in shared data we should first deserialize it from binary format.
auto tmp_dynamic_column = ColumnDynamic::create();
tmp_dynamic_column->reserve(1);
ColumnObject::deserializeValueFromSharedData(shared_data_values, index_in_shared_data_values++, *tmp_dynamic_column);
if (pretty)
dynamic_serialization->serializeTextJSONPretty(*tmp_dynamic_column, 0, ostr, settings, indent + current_prefix.size() + 1);
else
dynamic_serialization->serializeTextJSON(*tmp_dynamic_column, 0, ostr, settings);
}
}
/// Close all remaining open objects.
if (pretty)
{
writeChar('\n', ostr);
for (size_t i = 0; i != current_prefix.elements.size(); ++i)
{
writeChar(settings.json.pretty_print_indent, (indent + current_prefix.size() - i) * settings.json.pretty_print_indent_multiplier, ostr);
writeCString("}\n", ostr);
}
writeChar(settings.json.pretty_print_indent, indent * settings.json.pretty_print_indent_multiplier, ostr);
writeChar('}', ostr);
}
else
{
for (size_t i = 0; i != current_prefix.elements.size(); ++i)
writeChar('}', ostr);
writeChar('}', ostr);
}
}
template <typename Parser>
void SerializationJSON<Parser>::deserializeObject(IColumn & column, std::string_view object, const FormatSettings & settings) const
{
typename Parser::Element document;
auto parser = parsers_pool.get([] { return new Parser; });
if (!parser->parse(object, document))
throw Exception(ErrorCodes::INCORRECT_DATA, "Cannot parse JSON object here: {}", object);
String error;
if (!json_extract_tree->insertResultToColumn(column, document, insert_settings, settings, error))
throw Exception(ErrorCodes::INCORRECT_DATA, "Cannot insert data into JSON column: {}", error);
}
template <typename Parser>
void SerializationJSON<Parser>::serializeText(const IColumn & column, size_t row_num, WriteBuffer & ostr, const FormatSettings & settings) const
{
serializeTextImpl(column, row_num, ostr, settings);
}
template <typename Parser>
void SerializationJSON<Parser>::deserializeWholeText(IColumn & column, ReadBuffer & istr, const FormatSettings & settings) const
{
String object;
readStringUntilEOF(object, istr);
deserializeObject(column, object, settings);
}
template <typename Parser>
void SerializationJSON<Parser>::serializeTextEscaped(const IColumn & column, size_t row_num, WriteBuffer & ostr, const FormatSettings & settings) const
{
WriteBufferFromOwnString buf;
serializeTextImpl(column, row_num, buf, settings);
writeEscapedString(buf.str(), ostr);
}
template <typename Parser>
void SerializationJSON<Parser>::deserializeTextEscaped(IColumn & column, ReadBuffer & istr, const FormatSettings & settings) const
{
String object;
readEscapedString(object, istr);
deserializeObject(column, object, settings);
}
template <typename Parser>
void SerializationJSON<Parser>::serializeTextQuoted(const IColumn & column, size_t row_num, WriteBuffer & ostr, const FormatSettings & settings) const
{
WriteBufferFromOwnString buf;
serializeTextImpl(column, row_num, buf, settings);
writeQuotedString(buf.str(), ostr);
}
template <typename Parser>
void SerializationJSON<Parser>::deserializeTextQuoted(IColumn & column, ReadBuffer & istr, const FormatSettings & settings) const
{
String object;
readQuotedString(object, istr);
deserializeObject(column, object, settings);
}
template <typename Parser>
void SerializationJSON<Parser>::serializeTextCSV(const IColumn & column, size_t row_num, WriteBuffer & ostr, const FormatSettings & settings) const
{
WriteBufferFromOwnString buf;
serializeTextImpl(column, row_num, buf, settings);
writeCSVString(buf.str(), ostr);
}
template <typename Parser>
void SerializationJSON<Parser>::deserializeTextCSV(IColumn & column, ReadBuffer & istr, const FormatSettings & settings) const
{
String object;
readCSVString(object, istr, settings.csv);
deserializeObject(column, object, settings);
}
template <typename Parser>
void SerializationJSON<Parser>::serializeTextXML(const IColumn & column, size_t row_num, WriteBuffer & ostr, const FormatSettings & settings) const
{
WriteBufferFromOwnString buf;
serializeTextImpl(column, row_num, buf, settings);
writeXMLStringForTextElement(buf.str(), ostr);
}
template <typename Parser>
void SerializationJSON<Parser>::serializeTextJSON(const IColumn & column, size_t row_num, WriteBuffer & ostr, const FormatSettings & settings) const
{
serializeTextImpl(column, row_num, ostr, settings);
}
template <typename Parser>
void SerializationJSON<Parser>::serializeTextJSONPretty(const IColumn & column, size_t row_num, WriteBuffer & ostr, const FormatSettings & settings, size_t indent) const
{
serializeTextImpl(column, row_num, ostr, settings, true, indent);
}
template <typename Parser>
void SerializationJSON<Parser>::deserializeTextJSON(IColumn & column, ReadBuffer & istr, const FormatSettings & settings) const
{
String object_buffer;
auto object_view = readJSONObjectAsViewPossiblyInvalid(istr, object_buffer);
deserializeObject(column, object_view, settings);
}
#if USE_SIMDJSON
template class SerializationJSON<SimdJSONParser>;
#endif
#if USE_RAPIDJSON
template class SerializationJSON<RapidJSONParser>;
#else
template class SerializationJSON<DummyJSONParser>;
#endif
}