Skip to content

Commit 8ed2456

Browse files
Merge pull request ClickHouse#63493 from Volodyachan/fix-tabs-in-pretty-format
Tabs in pretty format
2 parents 7be88cc + c52af8c commit 8ed2456

8 files changed

+163
-35
lines changed

src/Processors/Formats/Impl/PrettyBlockOutputFormat.cpp

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ PrettyBlockOutputFormat::PrettyBlockOutputFormat(
2424
/// Note that number of code points is just a rough approximation of visible string width.
2525
void PrettyBlockOutputFormat::calculateWidths(
2626
const Block & header, const Chunk & chunk,
27-
WidthsPerColumn & widths, Widths & max_padded_widths, Widths & name_widths)
27+
WidthsPerColumn & widths, Widths & max_padded_widths, Widths & name_widths, size_t table_border_width)
2828
{
2929
size_t num_rows = std::min(chunk.getNumRows(), format_settings.pretty.max_rows);
3030

@@ -42,7 +42,7 @@ void PrettyBlockOutputFormat::calculateWidths(
4242

4343
/// Calculate widths of all values.
4444
String serialized_value;
45-
size_t prefix = 2; // Tab character adjustment
45+
size_t prefix = format_settings.pretty.output_format_pretty_row_numbers ? row_number_width + table_border_width : table_border_width; // Tab character adjustment
4646
for (size_t i = 0; i < num_columns; ++i)
4747
{
4848
const auto & elem = header.getByPosition(i);
@@ -187,7 +187,7 @@ void PrettyBlockOutputFormat::writeChunk(const Chunk & chunk, PortKind port_kind
187187
WidthsPerColumn widths;
188188
Widths max_widths;
189189
Widths name_widths;
190-
calculateWidths(header, chunk, widths, max_widths, name_widths);
190+
calculateWidths(header, chunk, widths, max_widths, name_widths, 2);
191191

192192
const GridSymbols & grid_symbols = format_settings.pretty.charset == FormatSettings::Pretty::Charset::UTF8 ?
193193
utf8_grid_symbols :
@@ -321,6 +321,7 @@ void PrettyBlockOutputFormat::writeChunk(const Chunk & chunk, PortKind port_kind
321321

322322
std::vector<String> transferred_row(num_columns);
323323
bool has_transferred_row = false;
324+
size_t prefix = format_settings.pretty.output_format_pretty_row_numbers ? row_number_width + 2 : 2;
324325

325326
for (size_t j = 0; j < num_columns; ++j)
326327
{
@@ -334,11 +335,13 @@ void PrettyBlockOutputFormat::writeChunk(const Chunk & chunk, PortKind port_kind
334335
serializations[j]->serializeText(*columns[j], i, out_serialize, format_settings);
335336
}
336337
if (cut_to_width && format_settings.pretty.preserve_border_for_multiline_string)
337-
splitValueAtBreakLine(serialized_value, transferred_row[j], cur_width);
338-
has_transferred_row |= !transferred_row[j].empty() && cur_width <= cut_to_width;
338+
splitValueAtBreakLine(serialized_value, transferred_row[j], cur_width, cut_to_width, prefix);
339+
has_transferred_row |= !transferred_row[j].empty();
339340

340341
writeValueWithPadding(serialized_value, cur_width, max_widths[j], cut_to_width,
341342
type.shouldAlignRightInPrettyFormats(), isNumber(type), !transferred_row[j].empty(), false);
343+
344+
prefix += max_widths[j] + 3;
342345
}
343346

344347
writeCString(grid_symbols.bar, out);
@@ -499,6 +502,7 @@ void PrettyBlockOutputFormat::writeTransferredRow(const Widths & max_widths, con
499502

500503
std::vector<String> new_transferred_row(num_columns);
501504
bool has_transferred_row = false;
505+
size_t prefix = format_settings.pretty.output_format_pretty_row_numbers ? row_number_width + 2 : 2;
502506

503507
for (size_t j = 0; j < num_columns; ++j)
504508
{
@@ -510,11 +514,13 @@ void PrettyBlockOutputFormat::writeTransferredRow(const Widths & max_widths, con
510514
const auto & type = *header.getByPosition(j).type;
511515
size_t cur_width = UTF8::computeWidth(reinterpret_cast<const UInt8 *>(transferred_row[j].data()), transferred_row[j].size());
512516
if (cut_to_width)
513-
splitValueAtBreakLine(transferred_row[j], new_transferred_row[j], cur_width);
514-
has_transferred_row |= !new_transferred_row[j].empty() && cur_width <= cut_to_width;
517+
splitValueAtBreakLine(transferred_row[j], new_transferred_row[j], cur_width, cut_to_width, prefix);
518+
has_transferred_row |= !new_transferred_row[j].empty();
515519

516520
writeValueWithPadding(transferred_row[j], cur_width, max_widths[j], cut_to_width,
517521
type.shouldAlignRightInPrettyFormats(), isNumber(type), !new_transferred_row[j].empty(), !transferred_row[j].empty());
522+
523+
prefix += max_widths[j] + 3;
518524
}
519525

520526
if (!space_block)
@@ -525,13 +531,14 @@ void PrettyBlockOutputFormat::writeTransferredRow(const Widths & max_widths, con
525531
writeTransferredRow(max_widths, header, new_transferred_row, cut_to_width, space_block);
526532
}
527533

528-
void PrettyBlockOutputFormat::splitValueAtBreakLine(String & value, String & transferred_value, size_t & value_width)
534+
void PrettyBlockOutputFormat::splitValueAtBreakLine(String & value, String & transferred_value, size_t & value_width, size_t cut_to_width, size_t prefix)
529535
{
530536
if (size_t break_line_pos = value.find_first_of('\n'); break_line_pos != String::npos)
531537
{
532-
transferred_value = value.substr(break_line_pos + 1);
538+
value_width = UTF8::computeWidth(reinterpret_cast<const UInt8 *>(value.data()), break_line_pos, prefix);
539+
if (value_width <= cut_to_width)
540+
transferred_value = value.substr(break_line_pos + 1);
533541
value = value.substr(0, break_line_pos);
534-
value_width = UTF8::computeWidth(reinterpret_cast<const UInt8 *>(value.data()), value.size());
535542
}
536543
}
537544

src/Processors/Formats/Impl/PrettyBlockOutputFormat.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,15 +44,15 @@ class PrettyBlockOutputFormat : public IOutputFormat
4444

4545
void calculateWidths(
4646
const Block & header, const Chunk & chunk,
47-
WidthsPerColumn & widths, Widths & max_padded_widths, Widths & name_widths);
47+
WidthsPerColumn & widths, Widths & max_padded_widths, Widths & name_widths, size_t table_border_width);
4848

4949
void writeValueWithPadding(
5050
String & value, size_t value_width, size_t pad_to_width, size_t cut_to_width,
5151
bool align_right, bool is_number, bool has_break_line, bool is_transferred_value);
5252

5353
void writeTransferredRow(const Widths & max_widths, const Block & header, std::vector<String> & transferred_row, size_t cut_to_width, bool space_block);
5454

55-
void splitValueAtBreakLine(String & value, String & transferred_value, size_t & value_width);
55+
void splitValueAtBreakLine(String & value, String & transferred_value, size_t & value_width, size_t cut_to_width, size_t prefix);
5656

5757
void resetFormatterImpl() override
5858
{

src/Processors/Formats/Impl/PrettyCompactBlockOutputFormat.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,8 @@ void PrettyCompactBlockOutputFormat::writeRow(
170170

171171
std::vector<String> transferred_row(num_columns);
172172
bool has_transferred_row = false;
173+
size_t prefix = format_settings.pretty.output_format_pretty_row_numbers ? row_number_width + 2 : 2;
174+
173175
for (size_t j = 0; j < num_columns; ++j)
174176
{
175177
if (j != 0)
@@ -183,11 +185,13 @@ void PrettyCompactBlockOutputFormat::writeRow(
183185
serializations[j]->serializeText(*columns[j], row_num, out_serialize, format_settings);
184186
}
185187
if (cut_to_width && format_settings.pretty.preserve_border_for_multiline_string)
186-
splitValueAtBreakLine(serialized_value, transferred_row[j], cur_width);
187-
has_transferred_row |= !transferred_row[j].empty() && cur_width <= cut_to_width;
188+
splitValueAtBreakLine(serialized_value, transferred_row[j], cur_width, cut_to_width, prefix);
189+
has_transferred_row |= !transferred_row[j].empty();
188190

189191
writeValueWithPadding(serialized_value, cur_width, max_widths[j], cut_to_width,
190192
type.shouldAlignRightInPrettyFormats(), isNumber(type), !transferred_row[j].empty(), false);
193+
194+
prefix += max_widths[j] + 3;
191195
}
192196

193197
writeCString(grid_symbols.bar, out);
@@ -208,7 +212,7 @@ void PrettyCompactBlockOutputFormat::writeChunk(const Chunk & chunk, PortKind po
208212
WidthsPerColumn widths;
209213
Widths max_widths;
210214
Widths name_widths;
211-
calculateWidths(header, chunk, widths, max_widths, name_widths);
215+
calculateWidths(header, chunk, widths, max_widths, name_widths, 2);
212216

213217
writeHeader(header, max_widths, name_widths);
214218

src/Processors/Formats/Impl/PrettySpaceBlockOutputFormat.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ void PrettySpaceBlockOutputFormat::writeChunk(const Chunk & chunk, PortKind port
3131
WidthsPerColumn widths;
3232
Widths max_widths;
3333
Widths name_widths;
34-
calculateWidths(header, chunk, widths, max_widths, name_widths);
34+
calculateWidths(header, chunk, widths, max_widths, name_widths, 1);
3535

3636
if (format_settings.pretty.output_format_pretty_row_numbers)
3737
writeString(String(row_number_width, ' '), out);
@@ -88,6 +88,7 @@ void PrettySpaceBlockOutputFormat::writeChunk(const Chunk & chunk, PortKind port
8888
writeCString("\033[0m", out);
8989

9090
}
91+
size_t prefix = format_settings.pretty.output_format_pretty_row_numbers ? row_number_width + 1 : 1;
9192
for (size_t column = 0; column < num_columns; ++column)
9293
{
9394
if (column != 0)
@@ -101,11 +102,13 @@ void PrettySpaceBlockOutputFormat::writeChunk(const Chunk & chunk, PortKind port
101102
serializations[column]->serializeText(*columns[column], row, out_serialize, format_settings);
102103
}
103104
if (cut_to_width && format_settings.pretty.preserve_border_for_multiline_string)
104-
splitValueAtBreakLine(serialized_value, transferred_row[column], cur_width);
105-
has_transferred_row |= !transferred_row[column].empty() && cur_width <= cut_to_width;
105+
splitValueAtBreakLine(serialized_value, transferred_row[column], cur_width, cur_width, prefix);
106+
has_transferred_row |= !transferred_row[column].empty();
106107

107108
writeValueWithPadding(serialized_value, cur_width, max_widths[column], cut_to_width,
108109
type.shouldAlignRightInPrettyFormats(), isNumber(type), !transferred_row[column].empty(), false);
110+
111+
prefix += max_widths[column] + 3;
109112
}
110113

111114
writeReadableNumberTip(chunk);

tests/queries/0_stateless/00298_enum_width_and_cast.reference

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@
55
1. │ Hello │ 0 │
66
2. │ \ │ 0 │
77
└───────┴───┘
8-
┌─x────────┬─y─┐
9-
1. │ Hello │ 0 │
10-
2. │ \ │ 0 │
8+
┌─x─────┬─y─┐
9+
1. │ Hello │ 0 │
10+
2. │ \ │ 0 │
1111
3. │ \t │ 0 │
12-
└──────────┴───┘
13-
┌─x────────┬─y─┬─toInt8(x)─┬─s─────┬─casted─┐
14-
1. │ Hello │ 0 │ -100 │ Hello │ Hello │
15-
2. │ \ │ 0 │ 0 │ \ │ \ │
12+
└───────┴───┘
13+
┌─x─────┬─y─┬─toInt8(x)─┬─s─────┬─casted─┐
14+
1. │ Hello │ 0 │ -100 │ Hello │ Hello │
15+
2. │ \ │ 0 │ 0 │ \ │ \ │
1616
3. │ \t │ 0 │ 111 │ \t │ \t │
17-
└──────────┴───┴───────────┴───────┴────────┘
17+
└───────┴───┴───────────┴───────┴────────┘

tests/queries/0_stateless/00730_unicode_terminal_format.reference

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,11 @@
6363
┡━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┩
6464
13. │ Ahoj │ Tento kód můžete upravit a spustit │
6565
└──────┴────────────────────────────────────┘
66-
┏━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━┓
67-
┃ c1 ┃ c2 ┃
68-
┡━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━┩
66+
┏━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━┓
67+
┃ c1 ┃ c2 ┃
68+
┡━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━┩
6969
14. │ Tabs Tabs │ Non-first Tabs │
70-
└─────────────┴───────────────────────┘
70+
└─────────────────┴───────────────────────┘
7171
┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
7272
┃ c1 ┃ c2 ┃
7373
┡━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┩
@@ -78,11 +78,11 @@
7878
┡━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━┩
7979
16. │ Russian ё and ё │ Zero bytes in middle │
8080
└──────────────────┴────────────────────────┘
81-
┏━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━┓
82-
┃ 'Tabs \t Tabs' ┃ 'Long\tTitle' ┃
83-
┡━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━┩
84-
1. │ Tabs Tabs │ Long Title
85-
└────────────────┴───────────────┘
81+
┏━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━┓
82+
┃ 'Tabs \t Tabs' ┃ 'Long\tTitle' ┃
83+
┡━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━┩
84+
1. │ Tabs Tabs │ Long Title │
85+
└──────────────────┴───────────────┘
8686
Row 1:
8787
──────
8888
'你好': 你好
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
┏━━━━┳━━━━━━━━━━━┳━━━━━━━━━━━━━━━━┓
2+
┃ id ┃ value ┃ value1 ┃
3+
┡━━━━╇━━━━━━━━━━━╇━━━━━━━━━━━━━━━━┩
4+
│ 0 │ test test │ something │
5+
└────┴───────────┴────────────────┘
6+
┏━━━━┳━━━━━━━━━━━┳━━━━━━━━━━━━━┓
7+
┃ id ┃ value ┃ value1 ┃
8+
┡━━━━╇━━━━━━━━━━━╇━━━━━━━━━━━━━┩
9+
1. │ 0 │ test test │ something │
10+
└────┴───────────┴─────────────┘
11+
┌─id─┬─value─┬─value1────┐
12+
│ 0 │ test …│ something │
13+
│ │… test │ │
14+
└────┴───────┴───────────┘
15+
┌─id─┬─value──────┬─value1────┐
16+
1. │ 0 │ test …│ something │
17+
│ │… test │ │
18+
└────┴────────────┴───────────┘
19+
id value value1
20+
21+
0 test … something
22+
… test
23+
id value value1
24+
25+
1. 0 test … something
26+
… test
27+
┌─id─┬─value─────┬─value1────┐
28+
│ 0 │ something │ test …│
29+
│ │ │… test │
30+
└────┴───────────┴───────────┘
31+
┌─id─┬─value─────┬─value1─┐
32+
1. │ 0 │ something │ test …│
33+
│ │ │… test │
34+
└────┴───────────┴────────┘
35+
id value value1
36+
37+
0 something test …
38+
… test
39+
id value value1
40+
41+
1. 0 something test …
42+
… test
43+
┏━━━━┳━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━┓
44+
┃ id ┃ value ┃ value1 ┃
45+
┡━━━━╇━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━┩
46+
│ 0 │ something │ test …│
47+
│ │ │… test │
48+
├────┼───────────────────┼───────────┤
49+
│ 1 │ some thing │ test …│
50+
│ │ │… test │
51+
└────┴───────────────────┴───────────┘
52+
┏━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━┓
53+
┃ id ┃ value ┃ value1 ┃
54+
┡━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━┩
55+
1. │ 0 │ something │ test …│
56+
│ │ │… test │
57+
├────┼────────────────────────┼───────────┤
58+
2. │ 1 │ some thing │ test …│
59+
│ │ │… test │
60+
└────┴────────────────────────┴───────────┘
61+
┏━━━━┳━━━━━━━┳━━━━━━━━┓
62+
┃ id ┃ value ┃ value1 ┃
63+
┡━━━━╇━━━━━━━╇━━━━━━━━┩
64+
│ 0 │ somet⋯│ test …│
65+
│ │ │…testt⋯ │
66+
└────┴───────┴────────┘
67+
┏━━━━┳━━━━━━━┳━━━━━━━━┓
68+
┃ id ┃ value ┃ value1 ┃
69+
┡━━━━╇━━━━━━━╇━━━━━━━━┩
70+
1. │ 0 │ somet⋯│ test …│
71+
│ │ │…testt⋯ │
72+
└────┴───────┴────────┘

0 commit comments

Comments
 (0)