Improve format JSONColumns when result is empty

This commit is contained in:
flynn 2023-02-05 13:13:21 +00:00
parent 496cacf25e
commit f89a6cf68d
6 changed files with 27 additions and 1 deletions

View File

@ -8,7 +8,7 @@ namespace DB
{
JSONColumnsBlockOutputFormat::JSONColumnsBlockOutputFormat(WriteBuffer & out_, const Block & header_, const FormatSettings & format_settings_, bool validate_utf8, size_t indent_)
: JSONColumnsBlockOutputFormatBase(out_, header_, format_settings_, validate_utf8), indent(indent_)
: JSONColumnsBlockOutputFormatBase(out_, header_, format_settings_, validate_utf8), indent(indent_), header(header_)
{
names = JSONUtils::makeNamesValidJSONStrings(header_.getNames(), format_settings, validate_utf8);
}
@ -25,6 +25,18 @@ void JSONColumnsBlockOutputFormat::writeColumnStart(size_t column_index)
void JSONColumnsBlockOutputFormat::writeChunkEnd()
{
/// Write empty chunk
if (!written_rows)
{
const auto & columns = header.getColumns();
for (size_t i = 0; i != columns.size(); ++i)
{
writeColumnStart(i);
writeColumn(*columns[i], *serializations[i]);
writeColumnEnd(i == columns.size() - 1);
}
}
JSONUtils::writeObjectEnd(*ostr, indent);
writeChar('\n', *ostr);
}

View File

@ -27,6 +27,8 @@ protected:
Names names;
size_t indent;
Block header;
};
}

View File

@ -42,6 +42,7 @@ void JSONColumnsBlockOutputFormatBase::writeChunk(Chunk & chunk)
writeColumn(*columns[i], *serializations[i]);
writeColumnEnd(i == columns.size() - 1);
}
written_rows += chunk.getNumRows();
writeChunkEnd();
}

View File

@ -36,6 +36,8 @@ protected:
const Serializations serializations;
Chunk mono_chunk;
size_t written_rows = 0;
};
}

View File

@ -0,0 +1,4 @@
{
"n": [],
"s": []
}

View File

@ -0,0 +1,5 @@
DROP TABLE IF EXISTS json_columns;
CREATE TABLE json_columns (n UInt32, s String) ENGINE = MergeTree order by n;
SELECT * FROM json_columns FORMAT JSONColumns;