Merge pull request #46053 from ucasfl/json-columns

Closes https://github.com/ClickHouse/ClickHouse/issues/46024
This commit is contained in:
Vladimir C 2023-02-06 12:22:13 +01:00 committed by GitHub
commit 5686703e79
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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_) 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); names = JSONUtils::makeNamesValidJSONStrings(header_.getNames(), format_settings, validate_utf8);
} }
@ -25,6 +25,18 @@ void JSONColumnsBlockOutputFormat::writeColumnStart(size_t column_index)
void JSONColumnsBlockOutputFormat::writeChunkEnd() 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); JSONUtils::writeObjectEnd(*ostr, indent);
writeChar('\n', *ostr); writeChar('\n', *ostr);
} }

View File

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

View File

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

View File

@ -36,6 +36,8 @@ protected:
const Serializations serializations; const Serializations serializations;
Chunk mono_chunk; 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;