From 6d529036efc40e8f92528afa3f61f99f6434810f Mon Sep 17 00:00:00 2001 From: rogeryk Date: Sun, 25 Feb 2024 16:00:15 +0800 Subject: [PATCH 1/4] Improve pretty format if a block consists of a single numeric value and exceeds one million. --- .../Formats/Impl/PrettyBlockOutputFormat.cpp | 14 ++ .../Formats/Impl/PrettyBlockOutputFormat.h | 1 + .../Impl/PrettyCompactBlockOutputFormat.cpp | 14 +- .../Impl/PrettyCompactBlockOutputFormat.h | 3 +- .../Impl/PrettySpaceBlockOutputFormat.cpp | 19 +- ..._readable_number_on_single_value.reference | 236 ++++++++++++++++++ ..._print_readable_number_on_single_value.sql | 65 +++++ 7 files changed, 341 insertions(+), 11 deletions(-) create mode 100644 tests/queries/0_stateless/02998_pretty_format_print_readable_number_on_single_value.reference create mode 100644 tests/queries/0_stateless/02998_pretty_format_print_readable_number_on_single_value.sql diff --git a/src/Processors/Formats/Impl/PrettyBlockOutputFormat.cpp b/src/Processors/Formats/Impl/PrettyBlockOutputFormat.cpp index eee0b24b5ba..93ddd7ed8fa 100644 --- a/src/Processors/Formats/Impl/PrettyBlockOutputFormat.cpp +++ b/src/Processors/Formats/Impl/PrettyBlockOutputFormat.cpp @@ -6,6 +6,7 @@ #include #include #include +#include namespace DB @@ -161,6 +162,7 @@ void PrettyBlockOutputFormat::writeChunk(const Chunk & chunk, PortKind port_kind auto num_columns = chunk.getNumColumns(); const auto & columns = chunk.getColumns(); const auto & header = getPort(port_kind).getHeader(); + auto single_numeric_value = num_rows == 1 && num_columns == 1 && WhichDataType(columns[0]->getDataType()).isNumber(); WidthsPerColumn widths; Widths max_widths; @@ -305,6 +307,11 @@ void PrettyBlockOutputFormat::writeChunk(const Chunk & chunk, PortKind port_kind } writeCString(grid_symbols.bar, out); + if (single_numeric_value) { + auto value = columns[0]->getFloat64(0); + if (value > 1'000'000) + writeReadableNumberTip(value); + } writeCString("\n", out); } @@ -410,6 +417,13 @@ void PrettyBlockOutputFormat::writeSuffix() } } +void PrettyBlockOutputFormat::writeReadableNumberTip(double value) +{ + writeCString("\033[90m -- ", out); + formatReadableQuantity(value, out, 2); + writeCString(" \033[0m", out); +} + void registerOutputFormatPretty(FormatFactory & factory) { registerPrettyFormatWithNoEscapesAndMonoBlock(factory, "Pretty"); diff --git a/src/Processors/Formats/Impl/PrettyBlockOutputFormat.h b/src/Processors/Formats/Impl/PrettyBlockOutputFormat.h index 253a6a958cc..69dfb050122 100644 --- a/src/Processors/Formats/Impl/PrettyBlockOutputFormat.h +++ b/src/Processors/Formats/Impl/PrettyBlockOutputFormat.h @@ -38,6 +38,7 @@ protected: virtual void writeChunk(const Chunk & chunk, PortKind port_kind); void writeMonoChunkIfNeeded(); void writeSuffix() override; + void writeReadableNumberTip(double number); void onRowsReadBeforeUpdate() override { total_rows = getRowsReadBefore(); } diff --git a/src/Processors/Formats/Impl/PrettyCompactBlockOutputFormat.cpp b/src/Processors/Formats/Impl/PrettyCompactBlockOutputFormat.cpp index b547ce9358a..61ed3cbcdb7 100644 --- a/src/Processors/Formats/Impl/PrettyCompactBlockOutputFormat.cpp +++ b/src/Processors/Formats/Impl/PrettyCompactBlockOutputFormat.cpp @@ -1,4 +1,5 @@ #include +#include #include #include #include @@ -139,7 +140,8 @@ void PrettyCompactBlockOutputFormat::writeRow( const Block & header, const Columns & columns, const WidthsPerColumn & widths, - const Widths & max_widths) + const Widths & max_widths, + bool single_numeric_value) { if (format_settings.pretty.output_format_pretty_row_numbers) { @@ -171,6 +173,11 @@ void PrettyCompactBlockOutputFormat::writeRow( } writeCString(grid_symbols.bar, out); + if (single_numeric_value) { + auto value = columns[0]->getFloat64(0); + if (value > 1'000'000) + writeReadableNumberTip(value); + } writeCString("\n", out); } @@ -179,8 +186,10 @@ void PrettyCompactBlockOutputFormat::writeChunk(const Chunk & chunk, PortKind po UInt64 max_rows = format_settings.pretty.max_rows; size_t num_rows = chunk.getNumRows(); + size_t num_columns = chunk.getNumColumns(); const auto & header = getPort(port_kind).getHeader(); const auto & columns = chunk.getColumns(); + auto single_numeric_value = num_rows == 1 && num_columns == 1 && WhichDataType(columns[0]->getDataType()).isNumber(); WidthsPerColumn widths; Widths max_widths; @@ -190,7 +199,8 @@ void PrettyCompactBlockOutputFormat::writeChunk(const Chunk & chunk, PortKind po writeHeader(header, max_widths, name_widths); for (size_t i = 0; i < num_rows && total_rows + i < max_rows; ++i) - writeRow(i, header, columns, widths, max_widths); + writeRow(i, header, columns, widths, max_widths, single_numeric_value); + writeBottom(max_widths); diff --git a/src/Processors/Formats/Impl/PrettyCompactBlockOutputFormat.h b/src/Processors/Formats/Impl/PrettyCompactBlockOutputFormat.h index 20cb931f282..2529c1713c4 100644 --- a/src/Processors/Formats/Impl/PrettyCompactBlockOutputFormat.h +++ b/src/Processors/Formats/Impl/PrettyCompactBlockOutputFormat.h @@ -24,7 +24,8 @@ private: const Block & header, const Columns & columns, const WidthsPerColumn & widths, - const Widths & max_widths); + const Widths & max_widths, + bool single_numeric_value); void writeChunk(const Chunk & chunk, PortKind port_kind) override; }; diff --git a/src/Processors/Formats/Impl/PrettySpaceBlockOutputFormat.cpp b/src/Processors/Formats/Impl/PrettySpaceBlockOutputFormat.cpp index f8e2ede869f..6f20e3eca1f 100644 --- a/src/Processors/Formats/Impl/PrettySpaceBlockOutputFormat.cpp +++ b/src/Processors/Formats/Impl/PrettySpaceBlockOutputFormat.cpp @@ -1,8 +1,8 @@ -#include +#include #include #include -#include #include +#include namespace DB @@ -23,6 +23,7 @@ void PrettySpaceBlockOutputFormat::writeChunk(const Chunk & chunk, PortKind port size_t num_columns = chunk.getNumColumns(); const auto & header = getPort(port_kind).getHeader(); const auto & columns = chunk.getColumns(); + auto single_number_value = num_rows == 1 && num_columns == 1 && WhichDataType(columns[0]->getDataType()).isNumber(); WidthsPerColumn widths; Widths max_widths; @@ -30,9 +31,7 @@ void PrettySpaceBlockOutputFormat::writeChunk(const Chunk & chunk, PortKind port calculateWidths(header, chunk, widths, max_widths, name_widths); if (format_settings.pretty.output_format_pretty_row_numbers) - { writeString(String(row_number_width, ' '), out); - } /// Names for (size_t i = 0; i < num_columns; ++i) { @@ -75,9 +74,7 @@ void PrettySpaceBlockOutputFormat::writeChunk(const Chunk & chunk, PortKind port // Write row number; auto row_num_string = std::to_string(row + 1 + total_rows) + ". "; for (size_t i = 0; i < row_number_width - row_num_string.size(); ++i) - { writeCString(" ", out); - } writeString(row_num_string, out); } for (size_t column = 0; column < num_columns; ++column) @@ -87,10 +84,16 @@ void PrettySpaceBlockOutputFormat::writeChunk(const Chunk & chunk, PortKind port const auto & type = *header.getByPosition(column).type; auto & cur_width = widths[column].empty() ? max_widths[column] : widths[column][row]; - writeValueWithPadding(*columns[column], *serializations[column], - row, cur_width, max_widths[column], type.shouldAlignRightInPrettyFormats()); + writeValueWithPadding( + *columns[column], *serializations[column], row, cur_width, max_widths[column], type.shouldAlignRightInPrettyFormats()); } + if (single_number_value) + { + auto value = columns[0]->getFloat64(0); + if (value > 1'000'000) + writeReadableNumberTip(value); + } writeChar('\n', out); } diff --git a/tests/queries/0_stateless/02998_pretty_format_print_readable_number_on_single_value.reference b/tests/queries/0_stateless/02998_pretty_format_print_readable_number_on_single_value.reference new file mode 100644 index 00000000000..d5fa113c173 --- /dev/null +++ b/tests/queries/0_stateless/02998_pretty_format_print_readable_number_on_single_value.reference @@ -0,0 +1,236 @@ +┏━━━━━━━━━┓ +┃ a ┃ +┡━━━━━━━━━┩ +│ 1000000 │ +└─────────┘ +┏━━━━━━━━━┓ +┃ a ┃ +┡━━━━━━━━━┩ +│ 1000000 │ +└─────────┘ +┏━━━━━━━━━┓ +┃ a ┃ +┡━━━━━━━━━┩ +│ 1000000 │ +└─────────┘ +┏━━━━━━━━━┓ +┃ a ┃ +┡━━━━━━━━━┩ +│ 1000000 │ +└─────────┘ +┌───────a─┐ +│ 1000000 │ +└─────────┘ +┌───────a─┐ +│ 1000000 │ +└─────────┘ +┌───────a─┐ +│ 1000000 │ +└─────────┘ +┌───────a─┐ +│ 1000000 │ +└─────────┘ + a + + 1000000 + a + + 1000000 + a + + 1000000 + a + + 1000000 +┏━━━━━━━━━┓ +┃ a ┃ +┡━━━━━━━━━┩ +│ 1000001 │ -- 1.00 million  +└─────────┘ +┏━━━━━━━━━┓ +┃ a ┃ +┡━━━━━━━━━┩ +│ 1000001 │ -- 1.00 million  +└─────────┘ +┏━━━━━━━━━┓ +┃ a ┃ +┡━━━━━━━━━┩ +│ 1000001 │ -- 1.00 million  +└─────────┘ +┏━━━━━━━━━┓ +┃ a ┃ +┡━━━━━━━━━┩ +│ 1000001 │ -- 1.00 million  +└─────────┘ +┌───────a─┐ +│ 1000001 │ -- 1.00 million  +└─────────┘ +┌───────a─┐ +│ 1000001 │ -- 1.00 million  +└─────────┘ +┌───────a─┐ +│ 1000001 │ -- 1.00 million  +└─────────┘ +┌───────a─┐ +│ 1000001 │ -- 1.00 million  +└─────────┘ + a + + 1000001  -- 1.00 million  + a + + 1000001  -- 1.00 million  + a + + 1000001  -- 1.00 million  + a + + 1000001  -- 1.00 million  +┏━━━━━━━━━━━━┓ +┃ a ┃ +┡━━━━━━━━━━━━┩ +│ 1000000000 │ -- 1.00 billion  +└────────────┘ +┏━━━━━━━━━━━━┓ +┃ a ┃ +┡━━━━━━━━━━━━┩ +│ 1000000000 │ -- 1.00 billion  +└────────────┘ +┏━━━━━━━━━━━━┓ +┃ a ┃ +┡━━━━━━━━━━━━┩ +│ 1000000000 │ -- 1.00 billion  +└────────────┘ +┏━━━━━━━━━━━━┓ +┃ a ┃ +┡━━━━━━━━━━━━┩ +│ 1000000000 │ -- 1.00 billion  +└────────────┘ +┌──────────a─┐ +│ 1000000000 │ -- 1.00 billion  +└────────────┘ +┌──────────a─┐ +│ 1000000000 │ -- 1.00 billion  +└────────────┘ +┌──────────a─┐ +│ 1000000000 │ -- 1.00 billion  +└────────────┘ +┌──────────a─┐ +│ 1000000000 │ -- 1.00 billion  +└────────────┘ + a + + 1000000000  -- 1.00 billion  + a + + 1000000000  -- 1.00 billion  + a + + 1000000000  -- 1.00 billion  + a + + 1000000000  -- 1.00 billion  +┏━━━━━━━━━━━━┳━━━━━━━━━━━━┓ +┃ a ┃ b ┃ +┡━━━━━━━━━━━━╇━━━━━━━━━━━━┩ +│ 1000000000 │ 1000000000 │ +└────────────┴────────────┘ +┏━━━━━━━━━━━━┳━━━━━━━━━━━━┓ +┃ a ┃ b ┃ +┡━━━━━━━━━━━━╇━━━━━━━━━━━━┩ +│ 1000000000 │ 1000000000 │ +└────────────┴────────────┘ +┏━━━━━━━━━━━━┳━━━━━━━━━━━━┓ +┃ a ┃ b ┃ +┡━━━━━━━━━━━━╇━━━━━━━━━━━━┩ +│ 1000000000 │ 1000000000 │ +└────────────┴────────────┘ +┏━━━━━━━━━━━━┳━━━━━━━━━━━━┓ +┃ a ┃ b ┃ +┡━━━━━━━━━━━━╇━━━━━━━━━━━━┩ +│ 1000000000 │ 1000000000 │ +└────────────┴────────────┘ +┌──────────a─┬──────────b─┐ +│ 1000000000 │ 1000000000 │ +└────────────┴────────────┘ +┌──────────a─┬──────────b─┐ +│ 1000000000 │ 1000000000 │ +└────────────┴────────────┘ +┌──────────a─┬──────────b─┐ +│ 1000000000 │ 1000000000 │ +└────────────┴────────────┘ +┌──────────a─┬──────────b─┐ +│ 1000000000 │ 1000000000 │ +└────────────┴────────────┘ + a b + + 1000000000 1000000000 + a b + + 1000000000 1000000000 + a b + + 1000000000 1000000000 + a b + + 1000000000 1000000000 +┏━━━━━━━━━━━━┓ +┃ a ┃ +┡━━━━━━━━━━━━┩ +│ 1000000000 │ +├────────────┤ +│ 1000000000 │ +└────────────┘ +┏━━━━━━━━━━━━┓ +┃ a ┃ +┡━━━━━━━━━━━━┩ +│ 1000000000 │ +├────────────┤ +│ 1000000000 │ +└────────────┘ +┏━━━━━━━━━━━━┓ +┃ a ┃ +┡━━━━━━━━━━━━┩ +│ 1000000000 │ +├────────────┤ +│ 1000000000 │ +└────────────┘ +┏━━━━━━━━━━━━┓ +┃ a ┃ +┡━━━━━━━━━━━━┩ +│ 1000000000 │ +├────────────┤ +│ 1000000000 │ +└────────────┘ +┌──────────a─┐ +│ 1000000000 │ +│ 1000000000 │ +└────────────┘ +┌──────────a─┐ +│ 1000000000 │ +│ 1000000000 │ +└────────────┘ +┌──────────a─┐ +│ 1000000000 │ +│ 1000000000 │ +└────────────┘ +┌──────────a─┐ +│ 1000000000 │ +│ 1000000000 │ +└────────────┘ + a + + 1000000000 + 1000000000 + a + + 1000000000 + 1000000000 + a + + 1000000000 + 1000000000 + a + + 1000000000 + 1000000000 diff --git a/tests/queries/0_stateless/02998_pretty_format_print_readable_number_on_single_value.sql b/tests/queries/0_stateless/02998_pretty_format_print_readable_number_on_single_value.sql new file mode 100644 index 00000000000..2fe1b4f21c5 --- /dev/null +++ b/tests/queries/0_stateless/02998_pretty_format_print_readable_number_on_single_value.sql @@ -0,0 +1,65 @@ +SELECT 1_000_000 as a FORMAT Pretty; +SELECT 1_000_000 as a FORMAT PrettyNoEscapes; +SELECT 1_000_000 as a FORMAT PrettyMonoBlock; +SELECT 1_000_000 as a FORMAT PrettyNoEscapesMonoBlock; +SELECT 1_000_000 as a FORMAT PrettyCompact; +SELECT 1_000_000 as a FORMAT PrettyCompactNoEscapes; +SELECT 1_000_000 as a FORMAT PrettyCompactMonoBlock; +SELECT 1_000_000 as a FORMAT PrettyCompactNoEscapesMonoBlock; +SELECT 1_000_000 as a FORMAT PrettySpace; +SELECT 1_000_000 as a FORMAT PrettySpaceNoEscapes; +SELECT 1_000_000 as a FORMAT PrettySpaceMonoBlock; +SELECT 1_000_000 as a FORMAT PrettySpaceNoEscapesMonoBlock; + +SELECT 1_000_001 as a FORMAT Pretty; +SELECT 1_000_001 as a FORMAT PrettyNoEscapes; +SELECT 1_000_001 as a FORMAT PrettyMonoBlock; +SELECT 1_000_001 as a FORMAT PrettyNoEscapesMonoBlock; +SELECT 1_000_001 as a FORMAT PrettyCompact; +SELECT 1_000_001 as a FORMAT PrettyCompactNoEscapes; +SELECT 1_000_001 as a FORMAT PrettyCompactMonoBlock; +SELECT 1_000_001 as a FORMAT PrettyCompactNoEscapesMonoBlock; +SELECT 1_000_001 as a FORMAT PrettySpace; +SELECT 1_000_001 as a FORMAT PrettySpaceNoEscapes; +SELECT 1_000_001 as a FORMAT PrettySpaceMonoBlock; +SELECT 1_000_001 as a FORMAT PrettySpaceNoEscapesMonoBlock; + +SELECT 1_000_000_000 as a FORMAT Pretty; +SELECT 1_000_000_000 as a FORMAT PrettyNoEscapes; +SELECT 1_000_000_000 as a FORMAT PrettyMonoBlock; +SELECT 1_000_000_000 as a FORMAT PrettyNoEscapesMonoBlock; +SELECT 1_000_000_000 as a FORMAT PrettyCompact; +SELECT 1_000_000_000 as a FORMAT PrettyCompactNoEscapes; +SELECT 1_000_000_000 as a FORMAT PrettyCompactMonoBlock; +SELECT 1_000_000_000 as a FORMAT PrettyCompactNoEscapesMonoBlock; +SELECT 1_000_000_000 as a FORMAT PrettySpace; +SELECT 1_000_000_000 as a FORMAT PrettySpaceNoEscapes; +SELECT 1_000_000_000 as a FORMAT PrettySpaceMonoBlock; +SELECT 1_000_000_000 as a FORMAT PrettySpaceNoEscapesMonoBlock; + +SELECT 1_000_000_000 as a, 1_000_000_000 as b FORMAT Pretty; +SELECT 1_000_000_000 as a, 1_000_000_000 as b FORMAT PrettyNoEscapes; +SELECT 1_000_000_000 as a, 1_000_000_000 as b FORMAT PrettyMonoBlock; +SELECT 1_000_000_000 as a, 1_000_000_000 as b FORMAT PrettyNoEscapesMonoBlock; +SELECT 1_000_000_000 as a, 1_000_000_000 as b FORMAT PrettyCompact; +SELECT 1_000_000_000 as a, 1_000_000_000 as b FORMAT PrettyCompactNoEscapes; +SELECT 1_000_000_000 as a, 1_000_000_000 as b FORMAT PrettyCompactMonoBlock; +SELECT 1_000_000_000 as a, 1_000_000_000 as b FORMAT PrettyCompactNoEscapesMonoBlock; +SELECT 1_000_000_000 as a, 1_000_000_000 as b FORMAT PrettySpace; +SELECT 1_000_000_000 as a, 1_000_000_000 as b FORMAT PrettySpaceNoEscapes; +SELECT 1_000_000_000 as a, 1_000_000_000 as b FORMAT PrettySpaceMonoBlock; +SELECT 1_000_000_000 as a, 1_000_000_000 as b FORMAT PrettySpaceNoEscapesMonoBlock; + +SELECT 1_000_000_000 as a FROM system.numbers LIMIT 2 FORMAT Pretty; +SELECT 1_000_000_000 as a FROM system.numbers LIMIT 2 FORMAT PrettyNoEscapes; +SELECT 1_000_000_000 as a FROM system.numbers LIMIT 2 FORMAT PrettyMonoBlock; +SELECT 1_000_000_000 as a FROM system.numbers LIMIT 2 FORMAT PrettyNoEscapesMonoBlock; +SELECT 1_000_000_000 as a FROM system.numbers LIMIT 2 FORMAT PrettyCompact; +SELECT 1_000_000_000 as a FROM system.numbers LIMIT 2 FORMAT PrettyCompactNoEscapes; +SELECT 1_000_000_000 as a FROM system.numbers LIMIT 2 FORMAT PrettyCompactMonoBlock; +SELECT 1_000_000_000 as a FROM system.numbers LIMIT 2 FORMAT PrettyCompactNoEscapesMonoBlock; +SELECT 1_000_000_000 as a FROM system.numbers LIMIT 2 FORMAT PrettySpace; +SELECT 1_000_000_000 as a FROM system.numbers LIMIT 2 FORMAT PrettySpaceNoEscapes; +SELECT 1_000_000_000 as a FROM system.numbers LIMIT 2 FORMAT PrettySpaceMonoBlock; +SELECT 1_000_000_000 as a FROM system.numbers LIMIT 2 FORMAT PrettySpaceNoEscapesMonoBlock; + From 85a15cd855408f10c862228662b2cb00a3d4b68e Mon Sep 17 00:00:00 2001 From: rogeryk Date: Sun, 25 Feb 2024 17:22:25 +0800 Subject: [PATCH 2/4] Fix style --- src/Processors/Formats/Impl/PrettyBlockOutputFormat.cpp | 3 ++- src/Processors/Formats/Impl/PrettyCompactBlockOutputFormat.cpp | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/Processors/Formats/Impl/PrettyBlockOutputFormat.cpp b/src/Processors/Formats/Impl/PrettyBlockOutputFormat.cpp index 93ddd7ed8fa..41d47694c84 100644 --- a/src/Processors/Formats/Impl/PrettyBlockOutputFormat.cpp +++ b/src/Processors/Formats/Impl/PrettyBlockOutputFormat.cpp @@ -307,7 +307,8 @@ void PrettyBlockOutputFormat::writeChunk(const Chunk & chunk, PortKind port_kind } writeCString(grid_symbols.bar, out); - if (single_numeric_value) { + if (single_numeric_value) + { auto value = columns[0]->getFloat64(0); if (value > 1'000'000) writeReadableNumberTip(value); diff --git a/src/Processors/Formats/Impl/PrettyCompactBlockOutputFormat.cpp b/src/Processors/Formats/Impl/PrettyCompactBlockOutputFormat.cpp index 61ed3cbcdb7..8f629e66565 100644 --- a/src/Processors/Formats/Impl/PrettyCompactBlockOutputFormat.cpp +++ b/src/Processors/Formats/Impl/PrettyCompactBlockOutputFormat.cpp @@ -173,7 +173,8 @@ void PrettyCompactBlockOutputFormat::writeRow( } writeCString(grid_symbols.bar, out); - if (single_numeric_value) { + if (single_numeric_value) + { auto value = columns[0]->getFloat64(0); if (value > 1'000'000) writeReadableNumberTip(value); From 7a92f542b4ac80f2bda5efe3587baa515f4369a5 Mon Sep 17 00:00:00 2001 From: rogeryk Date: Mon, 26 Feb 2024 20:19:53 +0800 Subject: [PATCH 3/4] Add setting output_format_pretty_single_large_number_tip_threshold --- .../operations/settings/settings-formats.md | 27 ++++++ src/Core/Settings.h | 1 + src/Core/SettingsChangesHistory.h | 1 + src/Formats/FormatFactory.cpp | 1 + src/Formats/FormatSettings.h | 1 + .../Formats/Impl/PrettyBlockOutputFormat.cpp | 25 +++-- .../Formats/Impl/PrettyBlockOutputFormat.h | 2 +- .../Impl/PrettyCompactBlockOutputFormat.cpp | 18 +--- .../Impl/PrettyCompactBlockOutputFormat.h | 5 +- .../Impl/PrettySpaceBlockOutputFormat.cpp | 8 +- ..._readable_number_on_single_value.reference | 92 ++++++++++++++----- ..._print_readable_number_on_single_value.sql | 14 +++ 12 files changed, 137 insertions(+), 58 deletions(-) diff --git a/docs/en/operations/settings/settings-formats.md b/docs/en/operations/settings/settings-formats.md index 9265fffa323..f7d9586dd5b 100644 --- a/docs/en/operations/settings/settings-formats.md +++ b/docs/en/operations/settings/settings-formats.md @@ -1656,6 +1656,33 @@ Result: └─────────────────────────┴─────────┘ ``` +### output_format_pretty_single_large_number_tip_threshold {#output_format_pretty_single_large_number_tip_threshold} + +Print a readable number tip on the right side of the table if the block consists of a single number which exceeds +this value (except 0). + +Possible values: + +- 0 — The readable number tip will not be printed. +- Positive integer — The readable number tip will be printed if the single number exceeds this value. + +Default value: `1000000`. + +**Example** + +Query: + +```sql +SELECT 1000000000 as a; +``` + +Result: +```text +┌──────────a─┐ +│ 1000000000 │ -- 1.00 billion +└────────────┘ +``` + ## Template format settings {#template-format-settings} ### format_template_resultset {#format_template_resultset} diff --git a/src/Core/Settings.h b/src/Core/Settings.h index 433195af9c3..62c531f9c5c 100644 --- a/src/Core/Settings.h +++ b/src/Core/Settings.h @@ -1116,6 +1116,7 @@ class IColumn; M(Bool, output_format_enable_streaming, false, "Enable streaming in output formats that support it.", 0) \ M(Bool, output_format_write_statistics, true, "Write statistics about read rows, bytes, time elapsed in suitable output formats.", 0) \ M(Bool, output_format_pretty_row_numbers, false, "Add row numbers before each row for pretty output format", 0) \ + M(UInt64, output_format_pretty_single_large_number_tip_threshold, 1'000'000, "Print a readable number tip on the right side of the table if the block consists of a single number which exceeds this value (except 0)", 0) \ M(Bool, insert_distributed_one_random_shard, false, "If setting is enabled, inserting into distributed table will choose a random shard to write when there is no sharding key", 0) \ \ M(Bool, exact_rows_before_limit, false, "When enabled, ClickHouse will provide exact value for rows_before_limit_at_least statistic, but with the cost that the data before limit will have to be read completely", 0) \ diff --git a/src/Core/SettingsChangesHistory.h b/src/Core/SettingsChangesHistory.h index e97a411e2c1..47eff6765ff 100644 --- a/src/Core/SettingsChangesHistory.h +++ b/src/Core/SettingsChangesHistory.h @@ -86,6 +86,7 @@ static std::map sett { {"24.2", { {"output_format_values_escape_quote_with_quote", false, false, "If true escape ' with '', otherwise quoted with \\'"}, + {"output_format_pretty_single_large_number_tip_threshold", 0, 1'000'000, "Print a readable number tip on the right side of the table if the block consists of a single number which exceeds this value (except 0)"}, {"input_format_try_infer_exponent_floats", true, false, "Don't infer floats in exponential notation by default"}, {"async_insert_max_data_size", 1000000, 10485760, "The previous value appeared to be too small."}, {"async_insert_poll_timeout_ms", 10, 10, "Timeout in milliseconds for polling data from asynchronous insert queue"}, diff --git a/src/Formats/FormatFactory.cpp b/src/Formats/FormatFactory.cpp index 0654dd01e49..a4a08d762b9 100644 --- a/src/Formats/FormatFactory.cpp +++ b/src/Formats/FormatFactory.cpp @@ -150,6 +150,7 @@ FormatSettings getFormatSettings(const ContextPtr & context, const Settings & se format_settings.pretty.max_rows = settings.output_format_pretty_max_rows; format_settings.pretty.max_value_width = settings.output_format_pretty_max_value_width; format_settings.pretty.output_format_pretty_row_numbers = settings.output_format_pretty_row_numbers; + format_settings.pretty.output_format_pretty_single_large_number_tip_threshold = settings.output_format_pretty_single_large_number_tip_threshold; format_settings.protobuf.input_flatten_google_wrappers = settings.input_format_protobuf_flatten_google_wrappers; format_settings.protobuf.output_nullables_with_google_wrappers = settings.output_format_protobuf_nullables_with_google_wrappers; format_settings.protobuf.skip_fields_with_unsupported_types_in_schema_inference = settings.input_format_protobuf_skip_fields_with_unsupported_types_in_schema_inference; diff --git a/src/Formats/FormatSettings.h b/src/Formats/FormatSettings.h index aa37216d381..07708fe53d0 100644 --- a/src/Formats/FormatSettings.h +++ b/src/Formats/FormatSettings.h @@ -277,6 +277,7 @@ struct FormatSettings SettingFieldUInt64Auto color{"auto"}; bool output_format_pretty_row_numbers = false; + UInt64 output_format_pretty_single_large_number_tip_threshold = 1'000'000; enum class Charset { diff --git a/src/Processors/Formats/Impl/PrettyBlockOutputFormat.cpp b/src/Processors/Formats/Impl/PrettyBlockOutputFormat.cpp index 41d47694c84..4c20a852117 100644 --- a/src/Processors/Formats/Impl/PrettyBlockOutputFormat.cpp +++ b/src/Processors/Formats/Impl/PrettyBlockOutputFormat.cpp @@ -162,7 +162,6 @@ void PrettyBlockOutputFormat::writeChunk(const Chunk & chunk, PortKind port_kind auto num_columns = chunk.getNumColumns(); const auto & columns = chunk.getColumns(); const auto & header = getPort(port_kind).getHeader(); - auto single_numeric_value = num_rows == 1 && num_columns == 1 && WhichDataType(columns[0]->getDataType()).isNumber(); WidthsPerColumn widths; Widths max_widths; @@ -307,12 +306,7 @@ void PrettyBlockOutputFormat::writeChunk(const Chunk & chunk, PortKind port_kind } writeCString(grid_symbols.bar, out); - if (single_numeric_value) - { - auto value = columns[0]->getFloat64(0); - if (value > 1'000'000) - writeReadableNumberTip(value); - } + writeReadableNumberTip(chunk); writeCString("\n", out); } @@ -418,11 +412,22 @@ void PrettyBlockOutputFormat::writeSuffix() } } -void PrettyBlockOutputFormat::writeReadableNumberTip(double value) +void PrettyBlockOutputFormat::writeReadableNumberTip(const Chunk & chunk) { - writeCString("\033[90m -- ", out); + auto columns = chunk.getColumns(); + auto is_single_number = chunk.getNumRows() == 1 && chunk.getNumColumns() == 1 && WhichDataType(columns[0]->getDataType()).isNumber(); + if (!is_single_number) + return; + auto value = columns[0]->getFloat64(0); + auto threshold = format_settings.pretty.output_format_pretty_single_large_number_tip_threshold; + if (threshold == 0 || value <= threshold) + return; + if (color) + writeCString("\033[90m", out); + writeCString(" -- ", out); formatReadableQuantity(value, out, 2); - writeCString(" \033[0m", out); + if (color) + writeCString("\033[0m", out); } void registerOutputFormatPretty(FormatFactory & factory) diff --git a/src/Processors/Formats/Impl/PrettyBlockOutputFormat.h b/src/Processors/Formats/Impl/PrettyBlockOutputFormat.h index 69dfb050122..680d39d8700 100644 --- a/src/Processors/Formats/Impl/PrettyBlockOutputFormat.h +++ b/src/Processors/Formats/Impl/PrettyBlockOutputFormat.h @@ -38,7 +38,7 @@ protected: virtual void writeChunk(const Chunk & chunk, PortKind port_kind); void writeMonoChunkIfNeeded(); void writeSuffix() override; - void writeReadableNumberTip(double number); + void writeReadableNumberTip(const Chunk & chunk); void onRowsReadBeforeUpdate() override { total_rows = getRowsReadBefore(); } diff --git a/src/Processors/Formats/Impl/PrettyCompactBlockOutputFormat.cpp b/src/Processors/Formats/Impl/PrettyCompactBlockOutputFormat.cpp index 8f629e66565..bda51770838 100644 --- a/src/Processors/Formats/Impl/PrettyCompactBlockOutputFormat.cpp +++ b/src/Processors/Formats/Impl/PrettyCompactBlockOutputFormat.cpp @@ -138,10 +138,9 @@ void PrettyCompactBlockOutputFormat::writeBottom(const Widths & max_widths) void PrettyCompactBlockOutputFormat::writeRow( size_t row_num, const Block & header, - const Columns & columns, + const Chunk & chunk, const WidthsPerColumn & widths, - const Widths & max_widths, - bool single_numeric_value) + const Widths & max_widths) { if (format_settings.pretty.output_format_pretty_row_numbers) { @@ -159,6 +158,7 @@ void PrettyCompactBlockOutputFormat::writeRow( ascii_grid_symbols; size_t num_columns = max_widths.size(); + const auto & columns = chunk.getColumns(); writeCString(grid_symbols.bar, out); @@ -173,12 +173,7 @@ void PrettyCompactBlockOutputFormat::writeRow( } writeCString(grid_symbols.bar, out); - if (single_numeric_value) - { - auto value = columns[0]->getFloat64(0); - if (value > 1'000'000) - writeReadableNumberTip(value); - } + writeReadableNumberTip(chunk); writeCString("\n", out); } @@ -187,10 +182,7 @@ void PrettyCompactBlockOutputFormat::writeChunk(const Chunk & chunk, PortKind po UInt64 max_rows = format_settings.pretty.max_rows; size_t num_rows = chunk.getNumRows(); - size_t num_columns = chunk.getNumColumns(); const auto & header = getPort(port_kind).getHeader(); - const auto & columns = chunk.getColumns(); - auto single_numeric_value = num_rows == 1 && num_columns == 1 && WhichDataType(columns[0]->getDataType()).isNumber(); WidthsPerColumn widths; Widths max_widths; @@ -200,7 +192,7 @@ void PrettyCompactBlockOutputFormat::writeChunk(const Chunk & chunk, PortKind po writeHeader(header, max_widths, name_widths); for (size_t i = 0; i < num_rows && total_rows + i < max_rows; ++i) - writeRow(i, header, columns, widths, max_widths, single_numeric_value); + writeRow(i, header, chunk, widths, max_widths); writeBottom(max_widths); diff --git a/src/Processors/Formats/Impl/PrettyCompactBlockOutputFormat.h b/src/Processors/Formats/Impl/PrettyCompactBlockOutputFormat.h index 2529c1713c4..7dbf7dbed0e 100644 --- a/src/Processors/Formats/Impl/PrettyCompactBlockOutputFormat.h +++ b/src/Processors/Formats/Impl/PrettyCompactBlockOutputFormat.h @@ -22,10 +22,9 @@ private: void writeRow( size_t row_num, const Block & header, - const Columns & columns, + const Chunk & columns, const WidthsPerColumn & widths, - const Widths & max_widths, - bool single_numeric_value); + const Widths & max_widths); void writeChunk(const Chunk & chunk, PortKind port_kind) override; }; diff --git a/src/Processors/Formats/Impl/PrettySpaceBlockOutputFormat.cpp b/src/Processors/Formats/Impl/PrettySpaceBlockOutputFormat.cpp index 6f20e3eca1f..e92863c93fb 100644 --- a/src/Processors/Formats/Impl/PrettySpaceBlockOutputFormat.cpp +++ b/src/Processors/Formats/Impl/PrettySpaceBlockOutputFormat.cpp @@ -23,7 +23,6 @@ void PrettySpaceBlockOutputFormat::writeChunk(const Chunk & chunk, PortKind port size_t num_columns = chunk.getNumColumns(); const auto & header = getPort(port_kind).getHeader(); const auto & columns = chunk.getColumns(); - auto single_number_value = num_rows == 1 && num_columns == 1 && WhichDataType(columns[0]->getDataType()).isNumber(); WidthsPerColumn widths; Widths max_widths; @@ -88,12 +87,7 @@ void PrettySpaceBlockOutputFormat::writeChunk(const Chunk & chunk, PortKind port *columns[column], *serializations[column], row, cur_width, max_widths[column], type.shouldAlignRightInPrettyFormats()); } - if (single_number_value) - { - auto value = columns[0]->getFloat64(0); - if (value > 1'000'000) - writeReadableNumberTip(value); - } + writeReadableNumberTip(chunk); writeChar('\n', out); } diff --git a/tests/queries/0_stateless/02998_pretty_format_print_readable_number_on_single_value.reference b/tests/queries/0_stateless/02998_pretty_format_print_readable_number_on_single_value.reference index d5fa113c173..482ad58862c 100644 --- a/tests/queries/0_stateless/02998_pretty_format_print_readable_number_on_single_value.reference +++ b/tests/queries/0_stateless/02998_pretty_format_print_readable_number_on_single_value.reference @@ -45,91 +45,135 @@ ┏━━━━━━━━━┓ ┃ a ┃ ┡━━━━━━━━━┩ -│ 1000001 │ -- 1.00 million  +│ 1000000 │ -- 1.00 million └─────────┘ ┏━━━━━━━━━┓ ┃ a ┃ ┡━━━━━━━━━┩ -│ 1000001 │ -- 1.00 million  +│ 1000000 │ -- 1.00 million └─────────┘ ┏━━━━━━━━━┓ ┃ a ┃ ┡━━━━━━━━━┩ -│ 1000001 │ -- 1.00 million  +│ 1000000 │ -- 1.00 million └─────────┘ ┏━━━━━━━━━┓ ┃ a ┃ ┡━━━━━━━━━┩ -│ 1000001 │ -- 1.00 million  +│ 1000000 │ -- 1.00 million └─────────┘ ┌───────a─┐ -│ 1000001 │ -- 1.00 million  +│ 1000000 │ -- 1.00 million └─────────┘ ┌───────a─┐ -│ 1000001 │ -- 1.00 million  +│ 1000000 │ -- 1.00 million └─────────┘ ┌───────a─┐ -│ 1000001 │ -- 1.00 million  +│ 1000000 │ -- 1.00 million └─────────┘ ┌───────a─┐ -│ 1000001 │ -- 1.00 million  +│ 1000000 │ -- 1.00 million └─────────┘ a - 1000001  -- 1.00 million  + 1000000 -- 1.00 million a - 1000001  -- 1.00 million  + 1000000 -- 1.00 million a - 1000001  -- 1.00 million  + 1000000 -- 1.00 million a - 1000001  -- 1.00 million  + 1000000 -- 1.00 million +┏━━━━━━━━━┓ +┃ a ┃ +┡━━━━━━━━━┩ +│ 1000001 │ -- 1.00 million +└─────────┘ +┏━━━━━━━━━┓ +┃ a ┃ +┡━━━━━━━━━┩ +│ 1000001 │ -- 1.00 million +└─────────┘ +┏━━━━━━━━━┓ +┃ a ┃ +┡━━━━━━━━━┩ +│ 1000001 │ -- 1.00 million +└─────────┘ +┏━━━━━━━━━┓ +┃ a ┃ +┡━━━━━━━━━┩ +│ 1000001 │ -- 1.00 million +└─────────┘ +┌───────a─┐ +│ 1000001 │ -- 1.00 million +└─────────┘ +┌───────a─┐ +│ 1000001 │ -- 1.00 million +└─────────┘ +┌───────a─┐ +│ 1000001 │ -- 1.00 million +└─────────┘ +┌───────a─┐ +│ 1000001 │ -- 1.00 million +└─────────┘ + a + + 1000001 -- 1.00 million + a + + 1000001 -- 1.00 million + a + + 1000001 -- 1.00 million + a + + 1000001 -- 1.00 million ┏━━━━━━━━━━━━┓ ┃ a ┃ ┡━━━━━━━━━━━━┩ -│ 1000000000 │ -- 1.00 billion  +│ 1000000000 │ -- 1.00 billion └────────────┘ ┏━━━━━━━━━━━━┓ ┃ a ┃ ┡━━━━━━━━━━━━┩ -│ 1000000000 │ -- 1.00 billion  +│ 1000000000 │ -- 1.00 billion └────────────┘ ┏━━━━━━━━━━━━┓ ┃ a ┃ ┡━━━━━━━━━━━━┩ -│ 1000000000 │ -- 1.00 billion  +│ 1000000000 │ -- 1.00 billion └────────────┘ ┏━━━━━━━━━━━━┓ ┃ a ┃ ┡━━━━━━━━━━━━┩ -│ 1000000000 │ -- 1.00 billion  +│ 1000000000 │ -- 1.00 billion └────────────┘ ┌──────────a─┐ -│ 1000000000 │ -- 1.00 billion  +│ 1000000000 │ -- 1.00 billion └────────────┘ ┌──────────a─┐ -│ 1000000000 │ -- 1.00 billion  +│ 1000000000 │ -- 1.00 billion └────────────┘ ┌──────────a─┐ -│ 1000000000 │ -- 1.00 billion  +│ 1000000000 │ -- 1.00 billion └────────────┘ ┌──────────a─┐ -│ 1000000000 │ -- 1.00 billion  +│ 1000000000 │ -- 1.00 billion └────────────┘ a - 1000000000  -- 1.00 billion  + 1000000000 -- 1.00 billion a - 1000000000  -- 1.00 billion  + 1000000000 -- 1.00 billion a - 1000000000  -- 1.00 billion  + 1000000000 -- 1.00 billion a - 1000000000  -- 1.00 billion  + 1000000000 -- 1.00 billion ┏━━━━━━━━━━━━┳━━━━━━━━━━━━┓ ┃ a ┃ b ┃ ┡━━━━━━━━━━━━╇━━━━━━━━━━━━┩ diff --git a/tests/queries/0_stateless/02998_pretty_format_print_readable_number_on_single_value.sql b/tests/queries/0_stateless/02998_pretty_format_print_readable_number_on_single_value.sql index 2fe1b4f21c5..6bbe9064c42 100644 --- a/tests/queries/0_stateless/02998_pretty_format_print_readable_number_on_single_value.sql +++ b/tests/queries/0_stateless/02998_pretty_format_print_readable_number_on_single_value.sql @@ -11,6 +11,20 @@ SELECT 1_000_000 as a FORMAT PrettySpaceNoEscapes; SELECT 1_000_000 as a FORMAT PrettySpaceMonoBlock; SELECT 1_000_000 as a FORMAT PrettySpaceNoEscapesMonoBlock; + +SELECT 1_000_000 as a SETTINGS output_format_pretty_single_large_number_tip_threshold = 1000 FORMAT Pretty; +SELECT 1_000_000 as a SETTINGS output_format_pretty_single_large_number_tip_threshold = 1000 FORMAT PrettyNoEscapes; +SELECT 1_000_000 as a SETTINGS output_format_pretty_single_large_number_tip_threshold = 1000 FORMAT PrettyMonoBlock; +SELECT 1_000_000 as a SETTINGS output_format_pretty_single_large_number_tip_threshold = 1000 FORMAT PrettyNoEscapesMonoBlock; +SELECT 1_000_000 as a SETTINGS output_format_pretty_single_large_number_tip_threshold = 1000 FORMAT PrettyCompact; +SELECT 1_000_000 as a SETTINGS output_format_pretty_single_large_number_tip_threshold = 1000 FORMAT PrettyCompactNoEscapes; +SELECT 1_000_000 as a SETTINGS output_format_pretty_single_large_number_tip_threshold = 1000 FORMAT PrettyCompactMonoBlock; +SELECT 1_000_000 as a SETTINGS output_format_pretty_single_large_number_tip_threshold = 1000 FORMAT PrettyCompactNoEscapesMonoBlock; +SELECT 1_000_000 as a SETTINGS output_format_pretty_single_large_number_tip_threshold = 1000 FORMAT PrettySpace; +SELECT 1_000_000 as a SETTINGS output_format_pretty_single_large_number_tip_threshold = 1000 FORMAT PrettySpaceNoEscapes; +SELECT 1_000_000 as a SETTINGS output_format_pretty_single_large_number_tip_threshold = 1000 FORMAT PrettySpaceMonoBlock; +SELECT 1_000_000 as a SETTINGS output_format_pretty_single_large_number_tip_threshold = 1000 FORMAT PrettySpaceNoEscapesMonoBlock; + SELECT 1_000_001 as a FORMAT Pretty; SELECT 1_000_001 as a FORMAT PrettyNoEscapes; SELECT 1_000_001 as a FORMAT PrettyMonoBlock; From aa31f0c613df960ee7925a78d7c5dde168559f05 Mon Sep 17 00:00:00 2001 From: rogeryk Date: Tue, 27 Feb 2024 09:17:01 +0800 Subject: [PATCH 4/4] Fix style --- src/Processors/Formats/Impl/PrettyCompactBlockOutputFormat.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Processors/Formats/Impl/PrettyCompactBlockOutputFormat.h b/src/Processors/Formats/Impl/PrettyCompactBlockOutputFormat.h index 7dbf7dbed0e..911fc2e950c 100644 --- a/src/Processors/Formats/Impl/PrettyCompactBlockOutputFormat.h +++ b/src/Processors/Formats/Impl/PrettyCompactBlockOutputFormat.h @@ -22,7 +22,7 @@ private: void writeRow( size_t row_num, const Block & header, - const Chunk & columns, + const Chunk & chunk, const WidthsPerColumn & widths, const Widths & max_widths);