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

90 lines
2.3 KiB
C++
Raw Normal View History

2019-02-19 18:41:18 +00:00
#include <Processors/Formats/Impl/CSVRowOutputFormat.h>
#include <Formats/FormatFactory.h>
#include <IO/WriteHelpers.h>
namespace DB
{
CSVRowOutputFormat::CSVRowOutputFormat(WriteBuffer & out_, const Block & header_, bool with_names_, const RowOutputFormatParams & params_, const FormatSettings & format_settings_)
: IRowOutputFormat(header_, out_, params_), with_names(with_names_), format_settings(format_settings_)
2019-02-19 18:41:18 +00:00
{
2020-04-22 06:34:20 +00:00
const auto & sample = getPort(PortKind::Main).getHeader();
2019-02-19 18:41:18 +00:00
size_t columns = sample.columns();
data_types.resize(columns);
for (size_t i = 0; i < columns; ++i)
data_types[i] = sample.safeGetByPosition(i).type;
}
2020-07-07 11:45:20 +00:00
void CSVRowOutputFormat::doWritePrefix()
2019-02-19 18:41:18 +00:00
{
2020-04-22 06:34:20 +00:00
const auto & sample = getPort(PortKind::Main).getHeader();
2019-02-19 18:41:18 +00:00
size_t columns = sample.columns();
if (with_names)
{
for (size_t i = 0; i < columns; ++i)
{
writeCSVString(sample.safeGetByPosition(i).name, out);
char delimiter = format_settings.csv.delimiter;
if (i + 1 == columns)
delimiter = '\n';
writeChar(delimiter, out);
}
}
}
2021-03-09 14:46:52 +00:00
void CSVRowOutputFormat::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.serializeTextCSV(column, row_num, out, format_settings);
2019-02-19 18:41:18 +00:00
}
void CSVRowOutputFormat::writeFieldDelimiter()
{
writeChar(format_settings.csv.delimiter, out);
}
void CSVRowOutputFormat::writeRowEndDelimiter()
{
2020-01-31 13:02:53 +00:00
if (format_settings.csv.crlf_end_of_line)
writeChar('\r', out);
2019-02-19 18:41:18 +00:00
writeChar('\n', out);
}
void CSVRowOutputFormat::writeBeforeTotals()
{
writeChar('\n', out);
}
void CSVRowOutputFormat::writeBeforeExtremes()
{
writeChar('\n', out);
}
2021-10-11 16:11:50 +00:00
void registerOutputFormatCSV(FormatFactory & factory)
2019-02-19 18:41:18 +00:00
{
for (bool with_names : {false, true})
{
2021-10-11 16:11:50 +00:00
factory.registerOutputFormat(with_names ? "CSVWithNames" : "CSV", [=](
2019-02-19 18:41:18 +00:00
WriteBuffer & buf,
const Block & sample,
const RowOutputFormatParams & params,
2019-02-19 18:41:18 +00:00
const FormatSettings & format_settings)
{
return std::make_shared<CSVRowOutputFormat>(buf, sample, with_names, params, format_settings);
2019-02-19 18:41:18 +00:00
});
2020-10-06 14:02:01 +00:00
factory.markOutputFormatSupportsParallelFormatting(with_names ? "CSVWithNames" : "CSV");
2019-02-19 18:41:18 +00:00
}
}
}