ClickHouse/src/Processors/Formats/Impl/JSONRowOutputFormat.cpp

168 lines
4.4 KiB
C++
Raw Normal View History

2019-02-19 18:41:18 +00:00
#include <IO/WriteHelpers.h>
#include <IO/WriteBufferValidUTF8.h>
#include <Processors/Formats/Impl/JSONRowOutputFormat.h>
#include <Formats/FormatFactory.h>
2022-05-06 16:48:48 +00:00
#include <Formats/JSONUtils.h>
2019-02-19 18:41:18 +00:00
namespace DB
{
2020-09-02 04:05:02 +00:00
JSONRowOutputFormat::JSONRowOutputFormat(
WriteBuffer & out_,
const Block & header,
const RowOutputFormatParams & params_,
2020-09-02 04:05:02 +00:00
const FormatSettings & settings_,
bool yield_strings_)
: IRowOutputFormatWithUTF8Validation(true, header, out_, params_), settings(settings_), yield_strings(yield_strings_)
2019-02-19 18:41:18 +00:00
{
names = JSONUtils::makeNamesValidJSONStrings(header.getNames(), settings, true);
2019-02-19 18:41:18 +00:00
}
void JSONRowOutputFormat::writePrefix()
{
2022-05-18 09:25:26 +00:00
JSONUtils::writeObjectStart(*ostr);
JSONUtils::writeMetadata(names, types, settings, *ostr);
2022-05-18 09:25:26 +00:00
JSONUtils::writeFieldDelimiter(*ostr, 2);
JSONUtils::writeArrayStart(*ostr, 1, "data");
2019-02-19 18:41:18 +00:00
}
2021-03-09 14:46:52 +00:00
void JSONRowOutputFormat::writeField(const IColumn & column, const ISerialization & serialization, size_t row_num)
2019-02-19 18:41:18 +00:00
{
JSONUtils::writeFieldFromColumn(column, serialization, row_num, yield_strings, settings, *ostr, names[field_number], 3);
2019-02-19 18:41:18 +00:00
++field_number;
}
void JSONRowOutputFormat::writeFieldDelimiter()
{
2022-05-18 09:25:26 +00:00
JSONUtils::writeFieldDelimiter(*ostr);
2019-02-19 18:41:18 +00:00
}
void JSONRowOutputFormat::writeRowStartDelimiter()
{
2022-05-18 09:25:26 +00:00
JSONUtils::writeObjectStart(*ostr, 2);
2019-02-19 18:41:18 +00:00
}
void JSONRowOutputFormat::writeRowEndDelimiter()
{
2022-05-18 09:25:26 +00:00
JSONUtils::writeObjectEnd(*ostr, 2);
2019-02-19 18:41:18 +00:00
field_number = 0;
++row_count;
}
void JSONRowOutputFormat::writeRowBetweenDelimiter()
{
2022-05-18 09:25:26 +00:00
JSONUtils::writeFieldDelimiter(*ostr);
2019-02-19 18:41:18 +00:00
}
void JSONRowOutputFormat::writeSuffix()
{
2022-05-18 09:25:26 +00:00
JSONUtils::writeArrayEnd(*ostr, 1);
2019-02-19 18:41:18 +00:00
}
void JSONRowOutputFormat::writeBeforeTotals()
{
2022-05-18 09:25:26 +00:00
JSONUtils::writeFieldDelimiter(*ostr, 2);
JSONUtils::writeObjectStart(*ostr, 1, "totals");
2019-02-19 18:41:18 +00:00
}
void JSONRowOutputFormat::writeTotals(const Columns & columns, size_t row_num)
{
JSONUtils::writeColumns(columns, names, serializations, row_num, yield_strings, settings, *ostr, 2);
2019-02-19 18:41:18 +00:00
}
void JSONRowOutputFormat::writeAfterTotals()
{
2022-05-18 09:25:26 +00:00
JSONUtils::writeObjectEnd(*ostr, 1);
2019-02-19 18:41:18 +00:00
}
void JSONRowOutputFormat::writeBeforeExtremes()
{
2022-05-18 09:25:26 +00:00
JSONUtils::writeFieldDelimiter(*ostr, 2);
JSONUtils::writeObjectStart(*ostr, 1, "extremes");
2019-02-19 18:41:18 +00:00
}
void JSONRowOutputFormat::writeExtremesElement(const char * title, const Columns & columns, size_t row_num)
{
2022-05-18 09:25:26 +00:00
JSONUtils::writeObjectStart(*ostr, 2, title);
JSONUtils::writeColumns(columns, names, serializations, row_num, yield_strings, settings, *ostr, 3);
2022-05-18 09:25:26 +00:00
JSONUtils::writeObjectEnd(*ostr, 2);
2019-02-19 18:41:18 +00:00
}
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()
{
2022-05-18 09:25:26 +00:00
JSONUtils::writeObjectEnd(*ostr, 1);
2019-02-19 18:41:18 +00:00
}
void JSONRowOutputFormat::finalizeImpl()
2019-02-19 18:41:18 +00:00
{
auto outside_statistics = getOutsideStatistics();
if (outside_statistics)
2021-11-19 13:45:10 +00:00
statistics = std::move(*outside_statistics);
2022-05-18 09:25:26 +00:00
JSONUtils::writeAdditionalInfo(
2022-05-06 16:48:48 +00:00
row_count,
statistics.rows_before_limit,
statistics.applied_limit,
statistics.watch,
statistics.progress,
settings.write_statistics,
*ostr);
2019-02-19 18:41:18 +00:00
2022-05-18 09:25:26 +00:00
JSONUtils::writeObjectEnd(*ostr);
2019-02-19 18:41:18 +00:00
writeChar('\n', *ostr);
ostr->next();
}
void JSONRowOutputFormat::onProgress(const Progress & value)
{
statistics.progress.incrementPiecewiseAtomically(value);
2019-02-19 18:41:18 +00:00
}
2021-10-11 16:11:50 +00:00
void registerOutputFormatJSON(FormatFactory & factory)
2019-02-19 18:41:18 +00:00
{
2021-10-11 16:11:50 +00:00
factory.registerOutputFormat("JSON", [](
2019-02-19 18:41:18 +00:00
WriteBuffer & buf,
const Block & sample,
const RowOutputFormatParams & params,
2019-02-19 18:41:18 +00:00
const FormatSettings & format_settings)
{
return std::make_shared<JSONRowOutputFormat>(buf, sample, params, format_settings, false);
2020-09-02 04:05:02 +00:00
});
factory.markOutputFormatSupportsParallelFormatting("JSON");
2022-01-24 13:27:04 +00:00
factory.markFormatHasNoAppendSupport("JSON");
2021-10-11 16:11:50 +00:00
factory.registerOutputFormat("JSONStrings", [](
2020-09-02 04:05:02 +00:00
WriteBuffer & buf,
const Block & sample,
const RowOutputFormatParams & params,
2020-09-02 04:05:02 +00:00
const FormatSettings & format_settings)
{
return std::make_shared<JSONRowOutputFormat>(buf, sample, params, format_settings, true);
2019-02-19 18:41:18 +00:00
});
factory.markOutputFormatSupportsParallelFormatting("JSONStrings");
2022-01-24 13:27:04 +00:00
factory.markFormatHasNoAppendSupport("JSONStrings");
2019-02-19 18:41:18 +00:00
}
}