2017-07-16 22:41:15 +00:00
|
|
|
#include <cmath>
|
|
|
|
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <IO/WriteHelpers.h>
|
2017-07-12 16:51:39 +00:00
|
|
|
#include <IO/WriteBufferFromString.h>
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <DataStreams/VerticalRowOutputStream.h>
|
2017-07-11 20:53:09 +00:00
|
|
|
#include <Common/UTF8Helpers.h>
|
2011-11-28 04:05:53 +00:00
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2017-05-22 19:00:45 +00:00
|
|
|
VerticalRowOutputStream::VerticalRowOutputStream(
|
2017-12-01 20:21:35 +00:00
|
|
|
WriteBuffer & ostr_, const Block & sample_, size_t max_rows_)
|
2017-05-22 19:00:45 +00:00
|
|
|
: ostr(ostr_), sample(sample_), max_rows(max_rows_)
|
2011-11-28 04:05:53 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
size_t columns = sample.columns();
|
|
|
|
|
2017-07-11 21:41:37 +00:00
|
|
|
using Widths = std::vector<size_t>;
|
|
|
|
Widths name_widths(columns);
|
2017-04-01 07:20:54 +00:00
|
|
|
size_t max_name_width = 0;
|
|
|
|
|
2017-07-12 16:51:39 +00:00
|
|
|
String serialized_value;
|
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
for (size_t i = 0; i < columns; ++i)
|
|
|
|
{
|
2017-07-11 20:53:09 +00:00
|
|
|
/// Note that number of code points is just a rough approximation of visible string width.
|
|
|
|
const String & name = sample.getByPosition(i).name;
|
2017-07-12 16:51:39 +00:00
|
|
|
|
|
|
|
{
|
|
|
|
/// We need to obtain length in escaped form.
|
|
|
|
WriteBufferFromString out(serialized_value);
|
|
|
|
writeEscapedString(name, out);
|
|
|
|
}
|
|
|
|
|
|
|
|
name_widths[i] = UTF8::countCodePoints(reinterpret_cast<const UInt8 *>(serialized_value.data()), serialized_value.size());
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
if (name_widths[i] > max_name_width)
|
|
|
|
max_name_width = name_widths[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
names_and_paddings.resize(columns);
|
|
|
|
for (size_t i = 0; i < columns; ++i)
|
|
|
|
{
|
|
|
|
WriteBufferFromString out(names_and_paddings[i]);
|
|
|
|
writeEscapedString(sample.getByPosition(i).name, out);
|
|
|
|
writeCString(": ", out);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (size_t i = 0; i < columns; ++i)
|
|
|
|
names_and_paddings[i].resize(max_name_width + strlen(": "), ' ');
|
2011-11-28 04:05:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-08-13 01:57:35 +00:00
|
|
|
void VerticalRowOutputStream::flush()
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
ostr.next();
|
2016-08-13 01:57:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-02-16 16:39:39 +00:00
|
|
|
void VerticalRowOutputStream::writeField(const IColumn & column, const IDataType & type, size_t row_num)
|
2011-11-28 04:05:53 +00:00
|
|
|
{
|
2017-05-22 19:00:45 +00:00
|
|
|
if (row_number > max_rows)
|
|
|
|
return;
|
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
writeString(names_and_paddings[field_number], ostr);
|
|
|
|
writeValue(column, type, row_num);
|
|
|
|
writeChar('\n', ostr);
|
2016-12-30 00:15:50 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
++field_number;
|
2011-11-28 04:05:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-02-16 16:39:39 +00:00
|
|
|
void VerticalRowOutputStream::writeValue(const IColumn & column, const IDataType & type, size_t row_num) const
|
2015-09-20 04:04:52 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
type.serializeTextEscaped(column, row_num, ostr);
|
2015-09-20 04:04:52 +00:00
|
|
|
}
|
|
|
|
|
2016-02-16 16:39:39 +00:00
|
|
|
void VerticalRawRowOutputStream::writeValue(const IColumn & column, const IDataType & type, size_t row_num) const
|
2015-09-20 04:04:52 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
type.serializeText(column, row_num, ostr);
|
2015-09-20 04:04:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-11-28 04:05:53 +00:00
|
|
|
void VerticalRowOutputStream::writeRowStartDelimiter()
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
++row_number;
|
2017-05-22 19:00:45 +00:00
|
|
|
|
|
|
|
if (row_number > max_rows)
|
|
|
|
return;
|
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
writeCString("Row ", ostr);
|
|
|
|
writeIntText(row_number, ostr);
|
|
|
|
writeCString(":\n", ostr);
|
|
|
|
|
|
|
|
size_t width = log10(row_number + 1) + 1 + strlen("Row :");
|
|
|
|
for (size_t i = 0; i < width; ++i)
|
|
|
|
writeCString("─", ostr);
|
|
|
|
writeChar('\n', ostr);
|
2011-11-28 04:05:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void VerticalRowOutputStream::writeRowBetweenDelimiter()
|
2017-05-22 19:00:45 +00:00
|
|
|
{
|
|
|
|
if (row_number > max_rows)
|
|
|
|
return;
|
|
|
|
|
|
|
|
writeCString("\n", ostr);
|
|
|
|
field_number = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void VerticalRowOutputStream::writeSuffix()
|
|
|
|
{
|
|
|
|
if (row_number > max_rows)
|
|
|
|
{
|
|
|
|
writeCString("Showed first ", ostr);
|
|
|
|
writeIntText(max_rows, ostr);
|
|
|
|
writeCString(".\n", ostr);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (totals || extremes)
|
|
|
|
{
|
|
|
|
writeCString("\n", ostr);
|
|
|
|
writeTotals();
|
|
|
|
writeExtremes();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void VerticalRowOutputStream::writeSpecialRow(const Block & block, size_t row_num, const char * title)
|
2011-11-28 04:05:53 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
writeCString("\n", ostr);
|
2017-05-22 19:00:45 +00:00
|
|
|
|
|
|
|
row_number = 0;
|
2017-04-01 07:20:54 +00:00
|
|
|
field_number = 0;
|
2017-05-22 19:00:45 +00:00
|
|
|
|
|
|
|
size_t columns = block.columns();
|
|
|
|
|
|
|
|
writeCString(title, ostr);
|
|
|
|
writeCString(":\n", ostr);
|
|
|
|
|
|
|
|
size_t width = strlen(title) + 1;
|
|
|
|
for (size_t i = 0; i < width; ++i)
|
|
|
|
writeCString("─", ostr);
|
|
|
|
writeChar('\n', ostr);
|
|
|
|
|
|
|
|
for (size_t i = 0; i < columns; ++i)
|
|
|
|
{
|
|
|
|
if (i != 0)
|
|
|
|
writeFieldDelimiter();
|
|
|
|
|
|
|
|
auto & col = block.getByPosition(i);
|
2017-09-01 18:21:01 +00:00
|
|
|
writeField(*col.column, *col.type, row_num);
|
2017-05-22 19:00:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void VerticalRowOutputStream::writeTotals()
|
|
|
|
{
|
|
|
|
if (totals)
|
|
|
|
{
|
|
|
|
writeSpecialRow(totals, 0, "Totals");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void VerticalRowOutputStream::writeExtremes()
|
|
|
|
{
|
|
|
|
if (extremes)
|
|
|
|
{
|
|
|
|
writeSpecialRow(extremes, 0, "Min");
|
|
|
|
writeSpecialRow(extremes, 1, "Max");
|
|
|
|
}
|
2011-11-28 04:05:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|