2013-05-15 11:18:58 +00:00
|
|
|
#include <DB/IO/WriteHelpers.h>
|
2015-02-15 09:45:29 +00:00
|
|
|
#include <DB/IO/WriteBufferValidUTF8.h>
|
|
|
|
#include <DB/DataStreams/JSONRowOutputStream.h>
|
2013-05-15 11:18:58 +00:00
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
using Poco::SharedPtr;
|
|
|
|
|
|
|
|
|
2013-05-22 14:57:43 +00:00
|
|
|
JSONRowOutputStream::JSONRowOutputStream(WriteBuffer & ostr_, const Block & sample_)
|
2015-02-15 09:45:29 +00:00
|
|
|
: dst_ostr(ostr_)
|
2013-05-15 11:18:58 +00:00
|
|
|
{
|
|
|
|
NamesAndTypesList columns(sample_.getColumnsList());
|
|
|
|
fields.assign(columns.begin(), columns.end());
|
2015-02-15 09:45:29 +00:00
|
|
|
|
|
|
|
bool have_non_numeric_columns = false;
|
|
|
|
for (size_t i = 0; i < sample_.columns(); ++i)
|
|
|
|
{
|
|
|
|
if (!sample_.unsafeGetByPosition(i).type->isNumeric())
|
|
|
|
{
|
|
|
|
have_non_numeric_columns = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (have_non_numeric_columns)
|
|
|
|
{
|
|
|
|
validating_ostr.reset(new WriteBufferValidUTF8(dst_ostr));
|
|
|
|
ostr = validating_ostr.get();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
ostr = &dst_ostr;
|
2013-05-15 11:18:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void JSONRowOutputStream::writePrefix()
|
|
|
|
{
|
2015-02-15 09:45:29 +00:00
|
|
|
writeCString("{\n", *ostr);
|
|
|
|
writeCString("\t\"meta\":\n", *ostr);
|
|
|
|
writeCString("\t[\n", *ostr);
|
|
|
|
|
2013-05-15 11:18:58 +00:00
|
|
|
for (size_t i = 0; i < fields.size(); ++i)
|
|
|
|
{
|
2015-02-15 09:45:29 +00:00
|
|
|
writeCString("\t\t{\n", *ostr);
|
|
|
|
|
|
|
|
writeCString("\t\t\t\"name\": ", *ostr);
|
|
|
|
writeDoubleQuotedString(fields[i].name, *ostr);
|
|
|
|
writeCString(",\n", *ostr);
|
|
|
|
writeCString("\t\t\t\"type\": ", *ostr);
|
|
|
|
writeDoubleQuotedString(fields[i].type->getName(), *ostr);
|
|
|
|
writeChar('\n', *ostr);
|
|
|
|
|
|
|
|
writeCString("\t\t}", *ostr);
|
2013-05-15 11:18:58 +00:00
|
|
|
if (i + 1 < fields.size())
|
2015-02-15 09:45:29 +00:00
|
|
|
writeChar(',', *ostr);
|
|
|
|
writeChar('\n', *ostr);
|
2013-05-15 11:18:58 +00:00
|
|
|
}
|
2015-02-15 09:45:29 +00:00
|
|
|
|
|
|
|
writeCString("\t],\n", *ostr);
|
|
|
|
writeChar('\n', *ostr);
|
|
|
|
writeCString("\t\"data\":\n", *ostr);
|
|
|
|
writeCString("\t[\n", *ostr);
|
2013-05-15 11:18:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void JSONRowOutputStream::writeField(const Field & field)
|
|
|
|
{
|
2015-02-15 09:45:29 +00:00
|
|
|
writeCString("\t\t\t", *ostr);
|
|
|
|
writeDoubleQuotedString(fields[field_number].name, *ostr);
|
|
|
|
writeCString(": ", *ostr);
|
|
|
|
fields[field_number].type->serializeTextJSON(field, *ostr);
|
2013-05-15 11:18:58 +00:00
|
|
|
++field_number;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void JSONRowOutputStream::writeFieldDelimiter()
|
|
|
|
{
|
2015-02-15 09:45:29 +00:00
|
|
|
writeCString(",\n", *ostr);
|
2013-05-15 11:18:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void JSONRowOutputStream::writeRowStartDelimiter()
|
|
|
|
{
|
|
|
|
if (row_count > 0)
|
2015-02-15 09:45:29 +00:00
|
|
|
writeCString(",\n", *ostr);
|
|
|
|
writeCString("\t\t{\n", *ostr);
|
2013-05-15 11:18:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void JSONRowOutputStream::writeRowEndDelimiter()
|
|
|
|
{
|
2015-02-15 09:45:29 +00:00
|
|
|
writeChar('\n', *ostr);
|
|
|
|
writeCString("\t\t}", *ostr);
|
2013-05-15 11:18:58 +00:00
|
|
|
field_number = 0;
|
|
|
|
++row_count;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void JSONRowOutputStream::writeSuffix()
|
|
|
|
{
|
2015-02-15 09:45:29 +00:00
|
|
|
writeChar('\n', *ostr);
|
|
|
|
writeCString("\t]", *ostr);
|
2013-09-01 04:55:41 +00:00
|
|
|
|
|
|
|
writeTotals();
|
2013-09-07 02:03:13 +00:00
|
|
|
writeExtremes();
|
2013-09-01 04:55:41 +00:00
|
|
|
|
2015-02-15 09:45:29 +00:00
|
|
|
writeCString(",\n\n", *ostr);
|
|
|
|
writeCString("\t\"rows\": ", *ostr);
|
|
|
|
writeIntText(row_count, *ostr);
|
|
|
|
|
2013-05-20 12:21:51 +00:00
|
|
|
writeRowsBeforeLimitAtLeast();
|
2015-02-15 09:45:29 +00:00
|
|
|
|
|
|
|
writeChar('\n', *ostr);
|
|
|
|
writeCString("}\n", *ostr);
|
|
|
|
ostr->next();
|
2013-05-15 11:18:58 +00:00
|
|
|
}
|
|
|
|
|
2013-05-20 12:21:51 +00:00
|
|
|
void JSONRowOutputStream::writeRowsBeforeLimitAtLeast()
|
|
|
|
{
|
2013-05-22 14:57:43 +00:00
|
|
|
if (applied_limit)
|
2013-05-20 12:21:51 +00:00
|
|
|
{
|
2015-02-15 09:45:29 +00:00
|
|
|
writeCString(",\n\n", *ostr);
|
|
|
|
writeCString("\t\"rows_before_limit_at_least\": ", *ostr);
|
|
|
|
writeIntText(rows_before_limit, *ostr);
|
2013-05-20 12:21:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-01 04:55:41 +00:00
|
|
|
void JSONRowOutputStream::writeTotals()
|
|
|
|
{
|
|
|
|
if (totals)
|
|
|
|
{
|
2015-02-15 09:45:29 +00:00
|
|
|
writeCString(",\n", *ostr);
|
|
|
|
writeChar('\n', *ostr);
|
|
|
|
writeCString("\t\"totals\":\n", *ostr);
|
|
|
|
writeCString("\t{\n", *ostr);
|
2013-09-01 04:55:41 +00:00
|
|
|
|
|
|
|
size_t totals_columns = totals.columns();
|
|
|
|
for (size_t i = 0; i < totals_columns; ++i)
|
|
|
|
{
|
2015-07-17 01:27:35 +00:00
|
|
|
const ColumnWithTypeAndName & column = totals.getByPosition(i);
|
2013-09-01 04:55:41 +00:00
|
|
|
|
|
|
|
if (i != 0)
|
2015-02-15 09:45:29 +00:00
|
|
|
writeCString(",\n", *ostr);
|
2013-09-01 04:55:41 +00:00
|
|
|
|
2015-02-15 09:45:29 +00:00
|
|
|
writeCString("\t\t", *ostr);
|
|
|
|
writeDoubleQuotedString(column.name, *ostr);
|
|
|
|
writeCString(": ", *ostr);
|
|
|
|
column.type->serializeTextJSON((*column.column)[0], *ostr);
|
2013-09-01 04:55:41 +00:00
|
|
|
}
|
|
|
|
|
2015-02-15 09:45:29 +00:00
|
|
|
writeChar('\n', *ostr);
|
|
|
|
writeCString("\t}", *ostr);
|
2013-09-01 04:55:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-07 02:03:13 +00:00
|
|
|
|
|
|
|
static void writeExtremesElement(const char * title, const Block & extremes, size_t row_num, WriteBuffer & ostr)
|
|
|
|
{
|
|
|
|
writeCString("\t\t\"", ostr);
|
|
|
|
writeCString(title, ostr);
|
|
|
|
writeCString("\":\n", ostr);
|
|
|
|
writeCString("\t\t{\n", ostr);
|
|
|
|
|
|
|
|
size_t extremes_columns = extremes.columns();
|
|
|
|
for (size_t i = 0; i < extremes_columns; ++i)
|
|
|
|
{
|
2015-07-17 01:27:35 +00:00
|
|
|
const ColumnWithTypeAndName & column = extremes.getByPosition(i);
|
2013-09-07 02:03:13 +00:00
|
|
|
|
|
|
|
if (i != 0)
|
|
|
|
writeCString(",\n", ostr);
|
|
|
|
|
|
|
|
writeCString("\t\t\t", ostr);
|
|
|
|
writeDoubleQuotedString(column.name, ostr);
|
|
|
|
writeCString(": ", ostr);
|
|
|
|
column.type->serializeTextJSON((*column.column)[row_num], ostr);
|
|
|
|
}
|
|
|
|
|
|
|
|
writeChar('\n', ostr);
|
|
|
|
writeCString("\t\t}", ostr);
|
|
|
|
}
|
|
|
|
|
|
|
|
void JSONRowOutputStream::writeExtremes()
|
|
|
|
{
|
|
|
|
if (extremes)
|
|
|
|
{
|
2015-02-15 09:45:29 +00:00
|
|
|
writeCString(",\n", *ostr);
|
|
|
|
writeChar('\n', *ostr);
|
|
|
|
writeCString("\t\"extremes\":\n", *ostr);
|
|
|
|
writeCString("\t{\n", *ostr);
|
2013-09-07 02:03:13 +00:00
|
|
|
|
2015-02-15 09:45:29 +00:00
|
|
|
writeExtremesElement("min", extremes, 0, *ostr);
|
|
|
|
writeCString(",\n", *ostr);
|
|
|
|
writeExtremesElement("max", extremes, 1, *ostr);
|
2013-09-07 02:03:13 +00:00
|
|
|
|
2015-02-15 09:45:29 +00:00
|
|
|
writeChar('\n', *ostr);
|
|
|
|
writeCString("\t}", *ostr);
|
2013-09-07 02:03:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-15 11:18:58 +00:00
|
|
|
}
|