2017-04-01 09:19:00 +00:00
|
|
|
#include <DataStreams/JSONCompactRowOutputStream.h>
|
2013-05-15 11:48:00 +00:00
|
|
|
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <IO/WriteHelpers.h>
|
2013-05-15 11:48:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2016-09-30 16:21:09 +00:00
|
|
|
JSONCompactRowOutputStream::JSONCompactRowOutputStream(WriteBuffer & ostr_, const Block & sample_, bool write_statistics_, bool force_quoting_64bit_integers_)
|
2017-04-01 07:20:54 +00:00
|
|
|
: JSONRowOutputStream(ostr_, sample_, write_statistics_, force_quoting_64bit_integers_)
|
2013-05-15 11:48:00 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-02-16 16:39:39 +00:00
|
|
|
void JSONCompactRowOutputStream::writeField(const IColumn & column, const IDataType & type, size_t row_num)
|
2013-05-15 11:48:00 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
type.serializeTextJSON(column, row_num, *ostr, force_quoting_64bit_integers);
|
|
|
|
++field_number;
|
2013-05-15 11:48:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void JSONCompactRowOutputStream::writeFieldDelimiter()
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
writeCString(", ", *ostr);
|
2013-05-15 11:48:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void JSONCompactRowOutputStream::writeRowStartDelimiter()
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
if (row_count > 0)
|
|
|
|
writeCString(",\n", *ostr);
|
|
|
|
writeCString("\t\t[", *ostr);
|
2013-05-15 11:48:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void JSONCompactRowOutputStream::writeRowEndDelimiter()
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
writeChar(']', *ostr);
|
|
|
|
field_number = 0;
|
|
|
|
++row_count;
|
2013-05-15 11:48:00 +00:00
|
|
|
}
|
|
|
|
|
2013-09-01 04:55:41 +00:00
|
|
|
|
|
|
|
void JSONCompactRowOutputStream::writeTotals()
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
if (totals)
|
|
|
|
{
|
|
|
|
writeCString(",\n", *ostr);
|
|
|
|
writeChar('\n', *ostr);
|
|
|
|
writeCString("\t\"totals\": [", *ostr);
|
|
|
|
|
|
|
|
size_t totals_columns = totals.columns();
|
|
|
|
for (size_t i = 0; i < totals_columns; ++i)
|
|
|
|
{
|
|
|
|
if (i != 0)
|
|
|
|
writeChar(',', *ostr);
|
|
|
|
|
|
|
|
const ColumnWithTypeAndName & column = totals.safeGetByPosition(i);
|
|
|
|
column.type->serializeTextJSON(*column.column.get(), 0, *ostr, force_quoting_64bit_integers);
|
|
|
|
}
|
|
|
|
|
|
|
|
writeChar(']', *ostr);
|
|
|
|
}
|
2013-09-01 04:55:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-09-30 16:21:09 +00:00
|
|
|
static void writeExtremesElement(const char * title, const Block & extremes, size_t row_num, WriteBuffer & ostr, bool force_quoting_64bit_integers)
|
2013-09-07 02:03:13 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
writeCString("\t\t\"", ostr);
|
|
|
|
writeCString(title, ostr);
|
|
|
|
writeCString("\": [", ostr);
|
2013-09-07 02:03:13 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
size_t extremes_columns = extremes.columns();
|
|
|
|
for (size_t i = 0; i < extremes_columns; ++i)
|
|
|
|
{
|
|
|
|
if (i != 0)
|
|
|
|
writeChar(',', ostr);
|
2013-09-07 02:03:13 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
const ColumnWithTypeAndName & column = extremes.safeGetByPosition(i);
|
|
|
|
column.type->serializeTextJSON(*column.column.get(), row_num, ostr, force_quoting_64bit_integers);
|
|
|
|
}
|
2013-09-07 02:03:13 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
writeChar(']', ostr);
|
2013-09-07 02:03:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void JSONCompactRowOutputStream::writeExtremes()
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
if (extremes)
|
|
|
|
{
|
|
|
|
writeCString(",\n", *ostr);
|
|
|
|
writeChar('\n', *ostr);
|
|
|
|
writeCString("\t\"extremes\":\n", *ostr);
|
|
|
|
writeCString("\t{\n", *ostr);
|
|
|
|
|
|
|
|
writeExtremesElement("min", extremes, 0, *ostr, force_quoting_64bit_integers);
|
|
|
|
writeCString(",\n", *ostr);
|
|
|
|
writeExtremesElement("max", extremes, 1, *ostr, force_quoting_64bit_integers);
|
|
|
|
|
|
|
|
writeChar('\n', *ostr);
|
|
|
|
writeCString("\t}", *ostr);
|
|
|
|
}
|
2013-09-07 02:03:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-05-15 11:48:00 +00:00
|
|
|
}
|