2018-06-10 19:22:49 +00:00
|
|
|
#include <Formats/TabSeparatedRowOutputStream.h>
|
|
|
|
#include <Formats/TabSeparatedRawRowOutputStream.h>
|
|
|
|
#include <Formats/FormatFactory.h>
|
|
|
|
#include <Formats/BlockOutputStreamFromRowOutputStream.h>
|
2010-03-04 19:20:28 +00:00
|
|
|
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <IO/WriteHelpers.h>
|
2010-06-04 18:25:25 +00:00
|
|
|
|
2010-03-04 19:20:28 +00:00
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2018-06-08 01:51:55 +00:00
|
|
|
TabSeparatedRowOutputStream::TabSeparatedRowOutputStream(
|
|
|
|
WriteBuffer & ostr_, const Block & sample_, bool with_names_, bool with_types_, const FormatSettings & format_settings)
|
|
|
|
: ostr(ostr_), sample(sample_), with_names(with_names_), with_types(with_types_), format_settings(format_settings)
|
2010-03-04 19:20:28 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-08-13 01:57:35 +00:00
|
|
|
void TabSeparatedRowOutputStream::flush()
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
ostr.next();
|
2016-08-13 01:57:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-11-06 06:22:52 +00:00
|
|
|
void TabSeparatedRowOutputStream::writePrefix()
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
size_t columns = sample.columns();
|
|
|
|
|
|
|
|
if (with_names)
|
|
|
|
{
|
|
|
|
for (size_t i = 0; i < columns; ++i)
|
|
|
|
{
|
|
|
|
writeEscapedString(sample.safeGetByPosition(i).name, ostr);
|
|
|
|
writeChar(i == columns - 1 ? '\n' : '\t', ostr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (with_types)
|
|
|
|
{
|
|
|
|
for (size_t i = 0; i < columns; ++i)
|
|
|
|
{
|
|
|
|
writeEscapedString(sample.safeGetByPosition(i).type->getName(), ostr);
|
|
|
|
writeChar(i == columns - 1 ? '\n' : '\t', ostr);
|
|
|
|
}
|
|
|
|
}
|
2011-11-06 06:22:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-02-16 16:39:39 +00:00
|
|
|
void TabSeparatedRowOutputStream::writeField(const IColumn & column, const IDataType & type, size_t row_num)
|
2010-03-04 19:20:28 +00:00
|
|
|
{
|
2018-12-13 13:41:47 +00:00
|
|
|
type.serializeAsTextEscaped(column, row_num, ostr, format_settings);
|
2010-03-04 19:20:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void TabSeparatedRowOutputStream::writeFieldDelimiter()
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
writeChar('\t', ostr);
|
2010-03-04 19:20:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void TabSeparatedRowOutputStream::writeRowEndDelimiter()
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
writeChar('\n', ostr);
|
2010-03-04 19:20:28 +00:00
|
|
|
}
|
|
|
|
|
2014-03-02 19:16:45 +00:00
|
|
|
|
|
|
|
void TabSeparatedRowOutputStream::writeSuffix()
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
writeTotals();
|
|
|
|
writeExtremes();
|
2014-03-02 19:16:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void TabSeparatedRowOutputStream::writeTotals()
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
if (totals)
|
|
|
|
{
|
|
|
|
size_t columns = totals.columns();
|
|
|
|
|
|
|
|
writeChar('\n', ostr);
|
|
|
|
writeRowStartDelimiter();
|
|
|
|
|
|
|
|
for (size_t j = 0; j < columns; ++j)
|
|
|
|
{
|
|
|
|
if (j != 0)
|
|
|
|
writeFieldDelimiter();
|
|
|
|
writeField(*totals.getByPosition(j).column.get(), *totals.getByPosition(j).type.get(), 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
writeRowEndDelimiter();
|
|
|
|
}
|
2014-03-02 19:16:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void TabSeparatedRowOutputStream::writeExtremes()
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
if (extremes)
|
|
|
|
{
|
|
|
|
size_t rows = extremes.rows();
|
|
|
|
size_t columns = extremes.columns();
|
|
|
|
|
|
|
|
writeChar('\n', ostr);
|
|
|
|
|
|
|
|
for (size_t i = 0; i < rows; ++i)
|
|
|
|
{
|
|
|
|
if (i != 0)
|
|
|
|
writeRowBetweenDelimiter();
|
|
|
|
|
|
|
|
writeRowStartDelimiter();
|
|
|
|
|
|
|
|
for (size_t j = 0; j < columns; ++j)
|
|
|
|
{
|
|
|
|
if (j != 0)
|
|
|
|
writeFieldDelimiter();
|
|
|
|
writeField(*extremes.getByPosition(j).column.get(), *extremes.getByPosition(j).type.get(), i);
|
|
|
|
}
|
|
|
|
|
|
|
|
writeRowEndDelimiter();
|
|
|
|
}
|
|
|
|
}
|
2014-03-02 19:16:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-06-10 19:22:49 +00:00
|
|
|
void registerOutputFormatTabSeparated(FormatFactory & factory)
|
|
|
|
{
|
|
|
|
for (auto name : {"TabSeparated", "TSV"})
|
|
|
|
{
|
|
|
|
factory.registerOutputFormat(name, [](
|
|
|
|
WriteBuffer & buf,
|
|
|
|
const Block & sample,
|
|
|
|
const Context &,
|
|
|
|
const FormatSettings & settings)
|
|
|
|
{
|
|
|
|
return std::make_shared<BlockOutputStreamFromRowOutputStream>(
|
|
|
|
std::make_shared<TabSeparatedRowOutputStream>(buf, sample, false, false, settings), sample);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
for (auto name : {"TabSeparatedRaw", "TSVRaw"})
|
|
|
|
{
|
|
|
|
factory.registerOutputFormat(name, [](
|
|
|
|
WriteBuffer & buf,
|
|
|
|
const Block & sample,
|
|
|
|
const Context &,
|
|
|
|
const FormatSettings & settings)
|
|
|
|
{
|
|
|
|
return std::make_shared<BlockOutputStreamFromRowOutputStream>(
|
|
|
|
std::make_shared<TabSeparatedRawRowOutputStream>(buf, sample, false, false, settings), sample);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
for (auto name : {"TabSeparatedWithNames", "TSVWithNames"})
|
|
|
|
{
|
|
|
|
factory.registerOutputFormat(name, [](
|
|
|
|
WriteBuffer & buf,
|
|
|
|
const Block & sample,
|
|
|
|
const Context &,
|
|
|
|
const FormatSettings & settings)
|
|
|
|
{
|
|
|
|
return std::make_shared<BlockOutputStreamFromRowOutputStream>(
|
|
|
|
std::make_shared<TabSeparatedRowOutputStream>(buf, sample, true, false, settings), sample);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
for (auto name : {"TabSeparatedWithNamesAndTypes", "TSVWithNamesAndTypes"})
|
|
|
|
{
|
|
|
|
factory.registerOutputFormat(name, [](
|
|
|
|
WriteBuffer & buf,
|
|
|
|
const Block & sample,
|
|
|
|
const Context &,
|
|
|
|
const FormatSettings & settings)
|
|
|
|
{
|
|
|
|
return std::make_shared<BlockOutputStreamFromRowOutputStream>(
|
|
|
|
std::make_shared<TabSeparatedRowOutputStream>(buf, sample, true, true, settings), sample);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-03-04 19:20:28 +00:00
|
|
|
}
|