make it a setting and not a format

This commit is contained in:
Alexander Kuzmenkov 2020-11-17 22:45:17 +03:00
parent 2bd6584636
commit a8f29f7da6
6 changed files with 23 additions and 27 deletions

View File

@ -441,6 +441,7 @@ class IColumn;
M(Bool, output_format_json_escape_forward_slashes, true, "Controls escaping forward slashes for string outputs in JSON output format. This is intended for compatibility with JavaScript. Don't confuse with backslashes that are always escaped.", 0) \
M(Bool, output_format_json_write_metadata, true, "Write metadata in JSON output format.", 0) \
M(Bool, output_format_json_named_tuple_as_object, false, "Serialize named tuple columns as JSON objects.", 0) \
M(Bool, output_format_json_array_of_rows, false, "Output a JSON array of all rows in JSONEachRow(Compact) format.", 0) \
\
M(UInt64, output_format_pretty_max_rows, 10000, "Rows limit for Pretty formats.", 0) \
M(UInt64, output_format_pretty_max_column_pad_width, 250, "Maximum width to pad all values in a column in Pretty formats.", 0) \

View File

@ -78,6 +78,7 @@ FormatSettings getFormatSettings(const Context & context,
format_settings.import_nested_json = settings.input_format_import_nested_json;
format_settings.input_allow_errors_num = settings.input_format_allow_errors_num;
format_settings.input_allow_errors_ratio = settings.input_format_allow_errors_ratio;
format_settings.json.array_of_rows = settings.output_format_json_array_of_rows;
format_settings.json.escape_forward_slashes = settings.output_format_json_escape_forward_slashes;
format_settings.json.named_tuple_as_object = settings.output_format_json_named_tuple_as_object;
format_settings.json.quote_64bit_integers = settings.output_format_json_quote_64bit_integers;

View File

@ -61,25 +61,30 @@ void JSONEachRowRowOutputFormat::writeRowStartDelimiter()
void JSONEachRowRowOutputFormat::writeRowEndDelimiter()
{
// Why this weird if?
// Why do we need this weird `if`?
//
// The reason is the formatRow function that is broken with respect to
// row-between delimiters. It should not write them, but it does, and then
// hacks around it by having a special formatRowNoNewline function
// which, as you guessed, removes the newline from the end of row. But the
// row-between delimiter goes into a second row, so it turns out to be in
// the second line, and the removal doesn't work. But the row-between
// delimiter in this format is also written incorrectly (not written at all,
// in fact), so the test (01420_format_row) works! All good.
// hacks around it by having a special formatRowNoNewline version, which, as
// you guessed, removes the newline from the end of row. But the row-between
// delimiter goes into a second row, so it turns out to be in the beginning
// of the line, and the removal doesn't work. There is also a second bug --
// the row-between delimiter in this format is written incorrectly. In fact,
// it is not written at all, and the newline is written in a row-end
// delimiter ("}\n" instead of the correct "}"). With these two bugs
// combined, the test 01420_format_row works perfectly.
//
// A proper implementation of formatRow would use IRowOutputFormat directly,
// and not write row-between delimiters, instead of using IOutputFormat
// processor and its crutch row callback. We would also need to expose
// IRowOutputFormat which we don't do now.
// processor and its crutch row callback. This would require exposing
// IRowOutputFormat, which we don't do now, but which can be generally useful
// for other cases such as parallel formatting, that also require a control
// flow different from the usual IOutputFormat.
//
// I just don't have time or energy to redo all of this properly, but I need
// to support JSON array output here. I don't want to copy the entire
// JSONEachRow output code, so I preserve the bug for compatibility.
// I just don't have time or energy to redo all of this, but I need to
// support JSON array output here, which requires proper ",\n" row-between
// delimiters. For compatibility, I preserve the bug in case of non-array
// output.
if (settings.json.array_of_rows)
{
writeCString("}", out);
@ -145,18 +150,6 @@ void registerOutputFormatProcessorJSONEachRow(FormatFactory & factory)
return std::make_shared<JSONEachRowRowOutputFormat>(buf, sample, params,
settings);
});
factory.registerOutputFormatProcessor("JSONArray", [](
WriteBuffer & buf,
const Block & sample,
const RowOutputFormatParams & params,
const FormatSettings & _format_settings)
{
FormatSettings settings = _format_settings;
settings.json.array_of_rows = true;
return std::make_shared<JSONEachRowRowOutputFormat>(buf, sample, params,
settings);
});
}
}

View File

@ -0,0 +1,4 @@
set output_format_json_array_of_rows = 1;
select number a, number * 2 b from numbers(3) format JSONEachRow;
select * from numbers(1) format JSONEachRow;
select * from numbers(1) where null format JSONEachRow;

View File

@ -1,3 +0,0 @@
select number a, number * 2 b from numbers(3) format JSONArray;
select * from numbers(1) format JSONArray;
select * from numbers(1) where null format JSONArray;