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
|
#include <IO/WriteHelpers.h>
#include <IO/WriteBufferValidUTF8.h>
#include <Processors/Formats/Impl/JSONRowOutputFormat.h>
#include <Formats/FormatFactory.h>
#include <Formats/JSONUtils.h>
namespace DB
{
JSONRowOutputFormat::JSONRowOutputFormat(
WriteBuffer & out_,
const Block & header,
const FormatSettings & settings_,
bool yield_strings_)
: RowOutputFormatWithExceptionHandlerAdaptor<RowOutputFormatWithUTF8ValidationAdaptor, bool>(header, out_, settings_.json.valid_output_on_exception, true), settings(settings_), yield_strings(yield_strings_)
{
names = JSONUtils::makeNamesValidJSONStrings(header.getNames(), settings, true);
ostr = RowOutputFormatWithExceptionHandlerAdaptor::getWriteBufferPtr();
}
void JSONRowOutputFormat::writePrefix()
{
JSONUtils::writeObjectStart(*ostr);
JSONUtils::writeMetadata(names, types, settings, *ostr);
JSONUtils::writeFieldDelimiter(*ostr, 2);
JSONUtils::writeArrayStart(*ostr, 1, "data");
}
void JSONRowOutputFormat::writeField(const IColumn & column, const ISerialization & serialization, size_t row_num)
{
JSONUtils::writeFieldFromColumn(column, serialization, row_num, yield_strings, settings, *ostr, names[field_number], 3);
++field_number;
}
void JSONRowOutputFormat::writeFieldDelimiter()
{
JSONUtils::writeFieldDelimiter(*ostr);
}
void JSONRowOutputFormat::writeRowStartDelimiter()
{
JSONUtils::writeObjectStart(*ostr, 2);
}
void JSONRowOutputFormat::writeRowEndDelimiter()
{
JSONUtils::writeObjectEnd(*ostr, 2);
field_number = 0;
++row_count;
}
void JSONRowOutputFormat::writeRowBetweenDelimiter()
{
JSONUtils::writeFieldDelimiter(*ostr);
}
void JSONRowOutputFormat::writeSuffix()
{
JSONUtils::writeArrayEnd(*ostr, 1);
}
void JSONRowOutputFormat::writeBeforeTotals()
{
JSONUtils::writeFieldDelimiter(*ostr, 2);
JSONUtils::writeObjectStart(*ostr, 1, "totals");
}
void JSONRowOutputFormat::writeTotals(const Columns & columns, size_t row_num)
{
JSONUtils::writeColumns(columns, names, serializations, row_num, yield_strings, settings, *ostr, 2);
}
void JSONRowOutputFormat::writeAfterTotals()
{
JSONUtils::writeObjectEnd(*ostr, 1);
}
void JSONRowOutputFormat::writeBeforeExtremes()
{
JSONUtils::writeFieldDelimiter(*ostr, 2);
JSONUtils::writeObjectStart(*ostr, 1, "extremes");
}
void JSONRowOutputFormat::writeExtremesElement(const char * title, const Columns & columns, size_t row_num)
{
JSONUtils::writeObjectStart(*ostr, 2, title);
JSONUtils::writeColumns(columns, names, serializations, row_num, yield_strings, settings, *ostr, 3);
JSONUtils::writeObjectEnd(*ostr, 2);
}
void JSONRowOutputFormat::writeMinExtreme(const Columns & columns, size_t row_num)
{
writeExtremesElement("min", columns, row_num);
}
void JSONRowOutputFormat::writeMaxExtreme(const Columns & columns, size_t row_num)
{
writeExtremesElement("max", columns, row_num);
}
void JSONRowOutputFormat::writeAfterExtremes()
{
JSONUtils::writeObjectEnd(*ostr, 1);
}
void JSONRowOutputFormat::finalizeImpl()
{
JSONUtils::writeAdditionalInfo(
row_count,
statistics.rows_before_limit,
statistics.applied_limit,
statistics.watch,
statistics.progress,
settings.write_statistics && exception_message.empty(),
*ostr);
if (!exception_message.empty())
{
writeCString(",\n\n", *ostr);
JSONUtils::writeException(exception_message, *ostr, settings, 1);
}
JSONUtils::writeObjectEnd(*ostr);
writeChar('\n', *ostr);
ostr->next();
}
void JSONRowOutputFormat::resetFormatterImpl()
{
RowOutputFormatWithExceptionHandlerAdaptor::resetFormatterImpl();
ostr = RowOutputFormatWithExceptionHandlerAdaptor::getWriteBufferPtr();
row_count = 0;
statistics = Statistics();
}
void JSONRowOutputFormat::onProgress(const Progress & value)
{
statistics.progress.incrementPiecewiseAtomically(value);
}
void registerOutputFormatJSON(FormatFactory & factory)
{
factory.registerOutputFormat("JSON", [](
WriteBuffer & buf,
const Block & sample,
const FormatSettings & format_settings)
{
return std::make_shared<JSONRowOutputFormat>(buf, sample, format_settings, false);
});
factory.markOutputFormatSupportsParallelFormatting("JSON");
factory.markFormatHasNoAppendSupport("JSON");
factory.registerOutputFormat("JSONStrings", [](
WriteBuffer & buf,
const Block & sample,
const FormatSettings & format_settings)
{
return std::make_shared<JSONRowOutputFormat>(buf, sample, format_settings, true);
});
factory.markOutputFormatSupportsParallelFormatting("JSONStrings");
factory.markFormatHasNoAppendSupport("JSONStrings");
}
}
|