2019-02-19 18:41:18 +00:00
|
|
|
#include <Processors/Formats/Impl/TabSeparatedRowOutputFormat.h>
|
|
|
|
#include <Formats/FormatFactory.h>
|
2021-10-20 11:48:54 +00:00
|
|
|
#include <Formats/registerWithNamesAndTypes.h>
|
2019-02-19 18:41:18 +00:00
|
|
|
#include <IO/WriteHelpers.h>
|
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
TabSeparatedRowOutputFormat::TabSeparatedRowOutputFormat(
|
2019-08-20 11:17:57 +00:00
|
|
|
WriteBuffer & out_,
|
|
|
|
const Block & header_,
|
|
|
|
bool with_names_,
|
|
|
|
bool with_types_,
|
2021-10-14 10:32:49 +00:00
|
|
|
bool is_raw_,
|
2020-10-07 18:51:10 +00:00
|
|
|
const RowOutputFormatParams & params_,
|
2019-08-20 11:17:57 +00:00
|
|
|
const FormatSettings & format_settings_)
|
2021-10-14 10:32:49 +00:00
|
|
|
: IRowOutputFormat(header_, out_, params_), with_names(with_names_), with_types(with_types_), is_raw(is_raw_), format_settings(format_settings_)
|
2019-02-19 18:41:18 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2021-10-14 10:32:49 +00:00
|
|
|
void TabSeparatedRowOutputFormat::writeLine(const std::vector<String> & values)
|
|
|
|
{
|
|
|
|
for (size_t i = 0; i < values.size(); ++i)
|
|
|
|
{
|
2021-12-20 14:39:15 +00:00
|
|
|
if (is_raw)
|
|
|
|
writeString(values[i], out);
|
|
|
|
else
|
|
|
|
writeEscapedString(values[i], out);
|
2021-10-14 10:32:49 +00:00
|
|
|
if (i + 1 == values.size())
|
|
|
|
writeRowEndDelimiter();
|
|
|
|
else
|
|
|
|
writeFieldDelimiter();
|
|
|
|
}
|
|
|
|
}
|
2019-02-19 18:41:18 +00:00
|
|
|
|
2021-11-02 13:40:41 +00:00
|
|
|
void TabSeparatedRowOutputFormat::writePrefix()
|
2019-02-19 18:41:18 +00:00
|
|
|
{
|
2020-04-22 06:34:20 +00:00
|
|
|
const auto & header = getPort(PortKind::Main).getHeader();
|
2019-02-19 18:41:18 +00:00
|
|
|
|
|
|
|
if (with_names)
|
2021-10-14 10:32:49 +00:00
|
|
|
writeLine(header.getNames());
|
2019-02-19 18:41:18 +00:00
|
|
|
|
|
|
|
if (with_types)
|
2021-10-14 10:32:49 +00:00
|
|
|
writeLine(header.getDataTypeNames());
|
2019-02-19 18:41:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-03-09 14:46:52 +00:00
|
|
|
void TabSeparatedRowOutputFormat::writeField(const IColumn & column, const ISerialization & serialization, size_t row_num)
|
2019-02-19 18:41:18 +00:00
|
|
|
{
|
2021-10-14 10:32:49 +00:00
|
|
|
if (is_raw)
|
|
|
|
serialization.serializeTextRaw(column, row_num, out, format_settings);
|
|
|
|
else
|
|
|
|
serialization.serializeTextEscaped(column, row_num, out, format_settings);
|
2019-02-19 18:41:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void TabSeparatedRowOutputFormat::writeFieldDelimiter()
|
|
|
|
{
|
|
|
|
writeChar('\t', out);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void TabSeparatedRowOutputFormat::writeRowEndDelimiter()
|
|
|
|
{
|
2020-02-03 07:40:12 +00:00
|
|
|
if (format_settings.tsv.crlf_end_of_line)
|
|
|
|
writeChar('\r', out);
|
2019-02-19 18:41:18 +00:00
|
|
|
writeChar('\n', out);
|
|
|
|
}
|
|
|
|
|
|
|
|
void TabSeparatedRowOutputFormat::writeBeforeTotals()
|
|
|
|
{
|
|
|
|
writeChar('\n', out);
|
|
|
|
}
|
|
|
|
|
|
|
|
void TabSeparatedRowOutputFormat::writeBeforeExtremes()
|
|
|
|
{
|
|
|
|
writeChar('\n', out);
|
|
|
|
}
|
|
|
|
|
2021-10-11 16:11:50 +00:00
|
|
|
void registerOutputFormatTabSeparated(FormatFactory & factory)
|
2019-02-19 18:41:18 +00:00
|
|
|
{
|
2021-10-14 10:32:49 +00:00
|
|
|
for (bool is_raw : {false, true})
|
2019-02-19 18:41:18 +00:00
|
|
|
{
|
2021-10-20 12:47:20 +00:00
|
|
|
auto register_func = [&](const String & format_name, bool with_names, bool with_types)
|
2019-02-19 18:41:18 +00:00
|
|
|
{
|
2021-10-20 12:47:20 +00:00
|
|
|
factory.registerOutputFormat(format_name, [is_raw, with_names, with_types](
|
2021-10-14 10:32:49 +00:00
|
|
|
WriteBuffer & buf,
|
|
|
|
const Block & sample,
|
|
|
|
const RowOutputFormatParams & params,
|
|
|
|
const FormatSettings & settings)
|
|
|
|
{
|
|
|
|
return std::make_shared<TabSeparatedRowOutputFormat>(buf, sample, with_names, with_types, is_raw, params, settings);
|
2021-10-20 12:47:20 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
factory.markOutputFormatSupportsParallelFormatting(format_name);
|
2021-10-14 10:32:49 +00:00
|
|
|
};
|
|
|
|
|
2021-10-20 12:47:20 +00:00
|
|
|
registerWithNamesAndTypes(is_raw ? "TSVRaw" : "TSV", register_func);
|
|
|
|
registerWithNamesAndTypes(is_raw ? "TabSeparatedRaw" : "TabSeparated", register_func);
|
2021-12-30 17:38:54 +00:00
|
|
|
if (is_raw)
|
|
|
|
registerWithNamesAndTypes("LineAsString", register_func);
|
2019-02-19 18:41:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|