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

94 lines
2.4 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 <Formats/registerWithNamesAndTypes.h>
2019-02-19 18:41:18 +00:00
#include <IO/WriteHelpers.h>
namespace DB
{
CSVRowOutputFormat::CSVRowOutputFormat(WriteBuffer & out_, const Block & header_, bool with_names_, bool with_types_, const RowOutputFormatParams & params_, const FormatSettings & format_settings_)
: 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-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;
}
void CSVRowOutputFormat::writeLine(const std::vector<String> & values)
{
for (size_t i = 0; i < values.size(); ++i)
{
writeCSVString(values[i], out);
if (i + 1 == values.size())
writeRowEndDelimiter();
else
writeFieldDelimiter();
}
}
2019-02-19 18:41:18 +00:00
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
if (with_names)
writeLine(sample.getNames());
2019-02-19 18:41:18 +00:00
if (with_types)
writeLine(sample.getDataTypeNames());
2019-02-19 18:41:18 +00:00
}
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
{
auto get_output_creator = [](bool with_names, bool with_types)
2019-02-19 18:41:18 +00:00
{
return [with_names, with_types](
WriteBuffer & buf,
const Block & sample,
const RowOutputFormatParams & params,
const FormatSettings & format_settings)
2019-02-19 18:41:18 +00:00
{
return std::make_shared<CSVRowOutputFormat>(buf, sample, with_names, with_types, params, format_settings);
};
};
registerOutputFormatWithNamesAndTypes(factory, "CSV", get_output_creator, true);
2019-02-19 18:41:18 +00:00
}
}