2011-11-07 04:51:37 +00:00
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <DataStreams/PrettyBlockOutputStream.h>
|
|
|
|
#include <IO/WriteBuffer.h>
|
|
|
|
#include <IO/WriteHelpers.h>
|
2017-07-31 21:39:24 +00:00
|
|
|
#include <IO/WriteBufferFromString.h>
|
2017-07-11 20:53:09 +00:00
|
|
|
#include <Common/UTF8Helpers.h>
|
2011-11-07 01:15:37 +00:00
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2017-06-13 02:06:53 +00:00
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int ILLEGAL_COLUMN;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-12-30 00:15:50 +00:00
|
|
|
PrettyBlockOutputStream::PrettyBlockOutputStream(WriteBuffer & ostr_, bool no_escapes_, size_t max_rows_, const Context & context_)
|
2017-04-01 07:20:54 +00:00
|
|
|
: ostr(ostr_), max_rows(max_rows_), no_escapes(no_escapes_), context(context_)
|
2011-11-07 04:51:37 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
struct winsize w;
|
|
|
|
if (0 == ioctl(STDOUT_FILENO, TIOCGWINSZ, &w))
|
|
|
|
terminal_width = w.ws_col;
|
2011-11-07 04:51:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-08-13 01:57:35 +00:00
|
|
|
void PrettyBlockOutputStream::flush()
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
ostr.next();
|
2016-08-13 01:57:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-07-11 21:41:37 +00:00
|
|
|
/// Evaluate the visible width of the values and column names.
|
|
|
|
/// Note that number of code points is just a rough approximation of visible string width.
|
|
|
|
void PrettyBlockOutputStream::calculateWidths(const Block & block, WidthsPerColumn & widths, Widths & max_widths, Widths & name_widths)
|
2011-11-07 01:15:37 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
size_t rows = block.rows();
|
|
|
|
size_t columns = block.columns();
|
|
|
|
|
2017-07-11 21:41:37 +00:00
|
|
|
widths.resize(columns);
|
|
|
|
max_widths.resize_fill(columns);
|
2017-04-01 07:20:54 +00:00
|
|
|
name_widths.resize(columns);
|
|
|
|
|
|
|
|
/// Calculate widths of all values.
|
2017-07-11 20:53:09 +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
|
|
|
const ColumnWithTypeAndName & elem = block.getByPosition(i);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-12-09 10:14:45 +00:00
|
|
|
widths[i].resize(rows);
|
2017-07-11 20:53:09 +00:00
|
|
|
|
2017-12-09 10:14:45 +00:00
|
|
|
for (size_t j = 0; j < rows; ++j)
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
|
|
|
{
|
2017-07-11 20:53:09 +00:00
|
|
|
WriteBufferFromString out(serialized_value);
|
2017-12-09 10:14:45 +00:00
|
|
|
elem.type->serializeTextEscaped(*elem.column, j, out);
|
2017-07-11 20:53:09 +00:00
|
|
|
}
|
2017-12-09 10:14:45 +00:00
|
|
|
|
|
|
|
widths[i][j] = UTF8::countCodePoints(reinterpret_cast<const UInt8 *>(serialized_value.data()), serialized_value.size());
|
|
|
|
max_widths[i] = std::max(max_widths[i], widths[i][j]);
|
2017-07-11 20:53:09 +00:00
|
|
|
}
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-07-11 20:53:09 +00:00
|
|
|
/// And also calculate widths for names of columns.
|
|
|
|
{
|
2017-07-12 16:51:39 +00:00
|
|
|
/// We need to obtain length in escaped form.
|
|
|
|
{
|
|
|
|
WriteBufferFromString out(serialized_value);
|
|
|
|
writeEscapedString(elem.name, out);
|
|
|
|
}
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2017-07-12 16:51:39 +00:00
|
|
|
name_widths[i] = UTF8::countCodePoints(reinterpret_cast<const UInt8 *>(serialized_value.data()), serialized_value.size());
|
2017-07-11 20:53:09 +00:00
|
|
|
max_widths[i] = std::max(max_widths[i], name_widths[i]);
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
}
|
2011-11-28 04:05:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-07-11 21:41:37 +00:00
|
|
|
void PrettyBlockOutputStream::write(const Block & block)
|
2011-11-28 04:05:53 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
if (total_rows >= max_rows)
|
|
|
|
{
|
2017-07-11 21:41:37 +00:00
|
|
|
total_rows += block.rows();
|
2017-04-01 07:20:54 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t rows = block.rows();
|
|
|
|
size_t columns = block.columns();
|
|
|
|
|
2017-07-11 21:41:37 +00:00
|
|
|
WidthsPerColumn widths;
|
|
|
|
Widths max_widths;
|
|
|
|
Widths name_widths;
|
|
|
|
calculateWidths(block, widths, max_widths, name_widths);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
/// Create separators
|
|
|
|
std::stringstream top_separator;
|
|
|
|
std::stringstream middle_names_separator;
|
|
|
|
std::stringstream middle_values_separator;
|
|
|
|
std::stringstream bottom_separator;
|
|
|
|
|
2017-07-11 21:41:37 +00:00
|
|
|
top_separator << "┏";
|
|
|
|
middle_names_separator << "┡";
|
|
|
|
middle_values_separator << "├";
|
|
|
|
bottom_separator << "└";
|
2017-04-01 07:20:54 +00:00
|
|
|
for (size_t i = 0; i < columns; ++i)
|
|
|
|
{
|
|
|
|
if (i != 0)
|
|
|
|
{
|
2017-07-11 21:41:37 +00:00
|
|
|
top_separator << "┳";
|
|
|
|
middle_names_separator << "╇";
|
|
|
|
middle_values_separator << "┼";
|
|
|
|
bottom_separator << "┴";
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for (size_t j = 0; j < max_widths[i] + 2; ++j)
|
|
|
|
{
|
2017-07-11 21:41:37 +00:00
|
|
|
top_separator << "━";
|
|
|
|
middle_names_separator << "━";
|
|
|
|
middle_values_separator << "─";
|
|
|
|
bottom_separator << "─";
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
}
|
2017-07-11 21:41:37 +00:00
|
|
|
top_separator << "┓\n";
|
|
|
|
middle_names_separator << "┩\n";
|
|
|
|
middle_values_separator << "┤\n";
|
|
|
|
bottom_separator << "┘\n";
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
std::string top_separator_s = top_separator.str();
|
|
|
|
std::string middle_names_separator_s = middle_names_separator.str();
|
|
|
|
std::string middle_values_separator_s = middle_values_separator.str();
|
|
|
|
std::string bottom_separator_s = bottom_separator.str();
|
|
|
|
|
|
|
|
/// Output the block
|
|
|
|
writeString(top_separator_s, ostr);
|
|
|
|
|
|
|
|
/// Names
|
|
|
|
writeCString("┃ ", ostr);
|
|
|
|
for (size_t i = 0; i < columns; ++i)
|
|
|
|
{
|
|
|
|
if (i != 0)
|
|
|
|
writeCString(" ┃ ", ostr);
|
|
|
|
|
2017-07-11 21:41:37 +00:00
|
|
|
const ColumnWithTypeAndName & col = block.getByPosition(i);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
if (!no_escapes)
|
|
|
|
writeCString("\033[1m", ostr);
|
|
|
|
|
2017-12-09 06:32:22 +00:00
|
|
|
if (col.type->shouldAlignRightInPrettyFormats())
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
|
|
|
for (size_t k = 0; k < max_widths[i] - name_widths[i]; ++k)
|
|
|
|
writeChar(' ', ostr);
|
|
|
|
|
|
|
|
writeEscapedString(col.name, ostr);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
writeEscapedString(col.name, ostr);
|
|
|
|
|
|
|
|
for (size_t k = 0; k < max_widths[i] - name_widths[i]; ++k)
|
|
|
|
writeChar(' ', ostr);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!no_escapes)
|
|
|
|
writeCString("\033[0m", ostr);
|
|
|
|
}
|
|
|
|
writeCString(" ┃\n", ostr);
|
|
|
|
|
|
|
|
writeString(middle_names_separator_s, ostr);
|
|
|
|
|
|
|
|
for (size_t i = 0; i < rows && total_rows + i < max_rows; ++i)
|
|
|
|
{
|
|
|
|
if (i != 0)
|
|
|
|
writeString(middle_values_separator_s, ostr);
|
|
|
|
|
|
|
|
writeCString("│ ", ostr);
|
|
|
|
|
|
|
|
for (size_t j = 0; j < columns; ++j)
|
|
|
|
{
|
|
|
|
if (j != 0)
|
|
|
|
writeCString(" │ ", ostr);
|
|
|
|
|
2017-07-11 21:41:37 +00:00
|
|
|
writeValueWithPadding(block.getByPosition(j), i, widths[j].empty() ? max_widths[j] : widths[j][i], max_widths[j]);
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
writeCString(" │\n", ostr);
|
|
|
|
}
|
|
|
|
|
|
|
|
writeString(bottom_separator_s, ostr);
|
|
|
|
|
|
|
|
total_rows += rows;
|
2011-11-07 04:51:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-07-11 21:41:37 +00:00
|
|
|
void PrettyBlockOutputStream::writeValueWithPadding(const ColumnWithTypeAndName & elem, size_t row_num, size_t value_width, size_t pad_to_width)
|
|
|
|
{
|
|
|
|
auto writePadding = [&]()
|
|
|
|
{
|
|
|
|
for (size_t k = 0; k < pad_to_width - value_width; ++k)
|
|
|
|
writeChar(' ', ostr);
|
|
|
|
};
|
|
|
|
|
2017-12-09 06:32:22 +00:00
|
|
|
if (elem.type->shouldAlignRightInPrettyFormats())
|
2017-07-11 21:41:37 +00:00
|
|
|
{
|
|
|
|
writePadding();
|
|
|
|
elem.type->serializeTextEscaped(*elem.column.get(), row_num, ostr);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
elem.type->serializeTextEscaped(*elem.column.get(), row_num, ostr);
|
|
|
|
writePadding();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-11-07 04:51:37 +00:00
|
|
|
void PrettyBlockOutputStream::writeSuffix()
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
if (total_rows >= max_rows)
|
|
|
|
{
|
|
|
|
writeCString(" Showed first ", ostr);
|
|
|
|
writeIntText(max_rows, ostr);
|
|
|
|
writeCString(".\n", ostr);
|
|
|
|
}
|
|
|
|
|
|
|
|
total_rows = 0;
|
|
|
|
writeTotals();
|
|
|
|
writeExtremes();
|
2014-03-02 19:16:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void PrettyBlockOutputStream::writeTotals()
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
if (totals)
|
|
|
|
{
|
|
|
|
writeCString("\nTotals:\n", ostr);
|
|
|
|
write(totals);
|
|
|
|
}
|
2014-03-02 19:16:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void PrettyBlockOutputStream::writeExtremes()
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
if (extremes)
|
|
|
|
{
|
|
|
|
writeCString("\nExtremes:\n", ostr);
|
|
|
|
write(extremes);
|
|
|
|
}
|
2011-11-07 01:15:37 +00:00
|
|
|
}
|
|
|
|
|
2011-11-07 04:51:37 +00:00
|
|
|
|
2011-11-07 01:15:37 +00:00
|
|
|
}
|