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

140 lines
4.3 KiB
C++
Raw Normal View History

2019-10-07 18:56:03 +00:00
#include <Common/PODArray.h>
2019-02-19 18:41:18 +00:00
#include <IO/WriteBuffer.h>
#include <IO/WriteHelpers.h>
#include <Formats/FormatFactory.h>
#include <Processors/Formats/Impl/PrettySpaceBlockOutputFormat.h>
namespace DB
{
void PrettySpaceBlockOutputFormat::write(const Chunk & chunk, PortKind port_kind)
{
UInt64 max_rows = format_settings.pretty.max_rows;
if (total_rows >= max_rows)
{
total_rows += chunk.getNumRows();
return;
}
size_t num_rows = chunk.getNumRows();
size_t num_columns = chunk.getNumColumns();
2020-04-22 06:34:20 +00:00
const auto & header = getPort(port_kind).getHeader();
const auto & columns = chunk.getColumns();
2019-02-19 18:41:18 +00:00
2021-03-09 14:46:52 +00:00
Serializations serializations(num_columns);
for (size_t i = 0; i < num_columns; ++i)
serializations[i] = header.getByPosition(i).type->getDefaultSerialization();
2019-02-19 18:41:18 +00:00
WidthsPerColumn widths;
Widths max_widths;
Widths name_widths;
calculateWidths(header, chunk, widths, max_widths, name_widths);
2020-10-02 15:13:28 +00:00
if (format_settings.pretty.output_format_pretty_row_numbers)
{
writeString(String(row_number_width, ' '), out);
}
2019-02-19 18:41:18 +00:00
/// Names
for (size_t i = 0; i < num_columns; ++i)
{
if (i != 0)
writeCString(" ", out);
else
writeChar(' ', out);
2019-02-19 18:41:18 +00:00
const ColumnWithTypeAndName & col = header.getByPosition(i);
if (col.type->shouldAlignRightInPrettyFormats())
{
for (ssize_t k = 0; k < std::max(static_cast<ssize_t>(0), static_cast<ssize_t>(max_widths[i] - name_widths[i])); ++k)
writeChar(' ', out);
if (format_settings.pretty.color)
writeCString("\033[1m", out);
writeString(col.name, out);
if (format_settings.pretty.color)
writeCString("\033[0m", out);
}
else
{
if (format_settings.pretty.color)
writeCString("\033[1m", out);
writeString(col.name, out);
if (format_settings.pretty.color)
writeCString("\033[0m", out);
for (ssize_t k = 0; k < std::max(static_cast<ssize_t>(0), static_cast<ssize_t>(max_widths[i] - name_widths[i])); ++k)
writeChar(' ', out);
}
}
writeCString("\n\n", out);
for (size_t row = 0; row < num_rows && total_rows + row < max_rows; ++row)
{
2020-10-02 15:13:28 +00:00
if (format_settings.pretty.output_format_pretty_row_numbers)
{
// Write row number;
auto row_num_string = std::to_string(row + 1) + ". ";
for (size_t i = 0; i < row_number_width - row_num_string.size(); ++i)
{
writeCString(" ", out);
}
writeString(row_num_string, out);
}
2019-02-19 18:41:18 +00:00
for (size_t column = 0; column < num_columns; ++column)
{
if (column != 0)
writeCString(" ", out);
2019-02-19 18:41:18 +00:00
2020-04-22 06:34:20 +00:00
const auto & type = *header.getByPosition(column).type;
2019-02-19 18:41:18 +00:00
auto & cur_width = widths[column].empty() ? max_widths[column] : widths[column][row];
2021-03-09 14:46:52 +00:00
writeValueWithPadding(*columns[column], *serializations[column],
row, cur_width, max_widths[column], type.shouldAlignRightInPrettyFormats());
2019-02-19 18:41:18 +00:00
}
writeChar('\n', out);
}
total_rows += num_rows;
}
void PrettySpaceBlockOutputFormat::writeSuffix()
{
if (total_rows >= format_settings.pretty.max_rows)
{
writeCString("\nShowed first ", out);
writeIntText(format_settings.pretty.max_rows, out);
writeCString(".\n", out);
}
}
2021-10-11 16:11:50 +00:00
void registerOutputFormatPrettySpace(FormatFactory & factory)
2019-02-19 18:41:18 +00:00
{
2021-10-11 16:11:50 +00:00
factory.registerOutputFormat("PrettySpace", [](
2019-02-19 18:41:18 +00:00
WriteBuffer & buf,
const Block & sample,
const RowOutputFormatParams &,
2019-02-19 18:41:18 +00:00
const FormatSettings & format_settings)
{
return std::make_shared<PrettySpaceBlockOutputFormat>(buf, sample, format_settings);
});
2021-10-11 16:11:50 +00:00
factory.registerOutputFormat("PrettySpaceNoEscapes", [](
2019-02-19 18:41:18 +00:00
WriteBuffer & buf,
const Block & sample,
const RowOutputFormatParams &,
2019-02-19 18:41:18 +00:00
const FormatSettings & format_settings)
{
FormatSettings changed_settings = format_settings;
changed_settings.pretty.color = false;
return std::make_shared<PrettySpaceBlockOutputFormat>(buf, sample, changed_settings);
});
}
}