ClickHouse/src/Processors/Formats/Impl/TabSeparatedRowOutputFormat.cpp

106 lines
2.9 KiB
C++
Raw Normal View History

2019-02-19 18:41:18 +00:00
#include <Processors/Formats/Impl/TabSeparatedRowOutputFormat.h>
#include <Formats/FormatFactory.h>
#include <Formats/registerWithNamesAndTypes.h>
2019-02-19 18:41:18 +00:00
#include <IO/WriteHelpers.h>
namespace DB
{
TabSeparatedRowOutputFormat::TabSeparatedRowOutputFormat(
WriteBuffer & out_,
const Block & header_,
bool with_names_,
bool with_types_,
bool is_raw_,
const RowOutputFormatParams & params_,
const FormatSettings & format_settings_)
: 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
{
}
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);
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)
writeLine(header.getNames());
2019-02-19 18:41:18 +00:00
if (with_types)
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
{
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
{
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](
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-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
}
}
}