2019-02-19 18:41:18 +00:00
|
|
|
#include <Processors/Formats/Impl/CSVRowOutputFormat.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
|
|
|
|
{
|
|
|
|
|
|
|
|
|
2021-10-14 10:32:49 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2021-10-14 10:32:49 +00:00
|
|
|
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
|
|
|
|
2021-11-02 13:40:41 +00:00
|
|
|
void CSVRowOutputFormat::writePrefix()
|
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)
|
2021-10-14 10:32:49 +00:00
|
|
|
writeLine(sample.getNames());
|
2019-02-19 18:41:18 +00:00
|
|
|
|
2021-10-14 10:32:49 +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
|
|
|
{
|
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, [with_names, with_types](
|
2021-10-14 10:32:49 +00:00
|
|
|
WriteBuffer & buf,
|
|
|
|
const Block & sample,
|
|
|
|
const RowOutputFormatParams & params,
|
|
|
|
const FormatSettings & format_settings)
|
2019-02-19 18:41:18 +00:00
|
|
|
{
|
2021-10-14 10:32:49 +00:00
|
|
|
return std::make_shared<CSVRowOutputFormat>(buf, sample, with_names, with_types, params, format_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("CSV", register_func);
|
2019-02-19 18:41:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|