2019-02-19 18:41:18 +00:00
|
|
|
#include <Processors/Formats/Impl/TabSeparatedRowOutputFormat.h>
|
|
|
|
#include <Processors/Formats/Impl/TabSeparatedRawRowOutputFormat.h>
|
|
|
|
#include <Formats/FormatFactory.h>
|
|
|
|
#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_,
|
2020-10-07 18:51:10 +00:00
|
|
|
const RowOutputFormatParams & params_,
|
2019-08-20 11:17:57 +00:00
|
|
|
const FormatSettings & format_settings_)
|
2020-10-07 18:51:10 +00:00
|
|
|
: IRowOutputFormat(header_, out_, params_), with_names(with_names_), with_types(with_types_), format_settings(format_settings_)
|
2019-02-19 18:41:18 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-08-04 20:49:30 +00:00
|
|
|
void TabSeparatedRowOutputFormat::doWritePrefix()
|
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
|
|
|
size_t columns = header.columns();
|
|
|
|
|
|
|
|
if (with_names)
|
|
|
|
{
|
|
|
|
for (size_t i = 0; i < columns; ++i)
|
|
|
|
{
|
|
|
|
writeEscapedString(header.safeGetByPosition(i).name, out);
|
|
|
|
writeChar(i == columns - 1 ? '\n' : '\t', out);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (with_types)
|
|
|
|
{
|
|
|
|
for (size_t i = 0; i < columns; ++i)
|
|
|
|
{
|
|
|
|
writeEscapedString(header.safeGetByPosition(i).type->getName(), out);
|
|
|
|
writeChar(i == columns - 1 ? '\n' : '\t', out);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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-03-09 14:46:52 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void registerOutputFormatProcessorTabSeparated(FormatFactory & factory)
|
|
|
|
{
|
2020-04-22 07:03:43 +00:00
|
|
|
for (const auto * name : {"TabSeparated", "TSV"})
|
2019-02-19 18:41:18 +00:00
|
|
|
{
|
|
|
|
factory.registerOutputFormatProcessor(name, [](
|
|
|
|
WriteBuffer & buf,
|
|
|
|
const Block & sample,
|
2020-10-06 12:47:52 +00:00
|
|
|
const RowOutputFormatParams & params,
|
2019-02-19 18:41:18 +00:00
|
|
|
const FormatSettings & settings)
|
|
|
|
{
|
2020-10-06 12:47:52 +00:00
|
|
|
return std::make_shared<TabSeparatedRowOutputFormat>(buf, sample, false, false, params, settings);
|
2019-02-19 18:41:18 +00:00
|
|
|
});
|
2020-10-06 14:02:01 +00:00
|
|
|
factory.markOutputFormatSupportsParallelFormatting(name);
|
2019-02-19 18:41:18 +00:00
|
|
|
}
|
|
|
|
|
2020-04-22 07:03:43 +00:00
|
|
|
for (const auto * name : {"TabSeparatedRaw", "TSVRaw"})
|
2019-02-19 18:41:18 +00:00
|
|
|
{
|
|
|
|
factory.registerOutputFormatProcessor(name, [](
|
|
|
|
WriteBuffer & buf,
|
|
|
|
const Block & sample,
|
2020-10-06 12:47:52 +00:00
|
|
|
const RowOutputFormatParams & params,
|
2019-02-19 18:41:18 +00:00
|
|
|
const FormatSettings & settings)
|
|
|
|
{
|
2020-10-06 12:47:52 +00:00
|
|
|
return std::make_shared<TabSeparatedRawRowOutputFormat>(buf, sample, false, false, params, settings);
|
2019-02-19 18:41:18 +00:00
|
|
|
});
|
2020-10-06 14:02:01 +00:00
|
|
|
factory.markOutputFormatSupportsParallelFormatting(name);
|
2019-02-19 18:41:18 +00:00
|
|
|
}
|
|
|
|
|
2020-04-22 07:03:43 +00:00
|
|
|
for (const auto * name : {"TabSeparatedWithNames", "TSVWithNames"})
|
2019-02-19 18:41:18 +00:00
|
|
|
{
|
|
|
|
factory.registerOutputFormatProcessor(name, [](
|
|
|
|
WriteBuffer & buf,
|
|
|
|
const Block & sample,
|
2020-10-06 12:47:52 +00:00
|
|
|
const RowOutputFormatParams & params,
|
2019-02-19 18:41:18 +00:00
|
|
|
const FormatSettings & settings)
|
|
|
|
{
|
2020-10-06 12:47:52 +00:00
|
|
|
return std::make_shared<TabSeparatedRowOutputFormat>(buf, sample, true, false, params, settings);
|
2019-02-19 18:41:18 +00:00
|
|
|
});
|
2020-10-06 14:02:01 +00:00
|
|
|
factory.markOutputFormatSupportsParallelFormatting(name);
|
2019-02-19 18:41:18 +00:00
|
|
|
}
|
|
|
|
|
2020-04-22 07:03:43 +00:00
|
|
|
for (const auto * name : {"TabSeparatedWithNamesAndTypes", "TSVWithNamesAndTypes"})
|
2019-02-19 18:41:18 +00:00
|
|
|
{
|
|
|
|
factory.registerOutputFormatProcessor(name, [](
|
|
|
|
WriteBuffer & buf,
|
|
|
|
const Block & sample,
|
2020-10-06 12:47:52 +00:00
|
|
|
const RowOutputFormatParams & params,
|
2019-02-19 18:41:18 +00:00
|
|
|
const FormatSettings & settings)
|
|
|
|
{
|
2020-10-06 12:47:52 +00:00
|
|
|
return std::make_shared<TabSeparatedRowOutputFormat>(buf, sample, true, true, params, settings);
|
2019-02-19 18:41:18 +00:00
|
|
|
});
|
2020-10-06 14:02:01 +00:00
|
|
|
factory.markOutputFormatSupportsParallelFormatting(name);
|
2019-02-19 18:41:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|