Skip to content

Commit 588c744

Browse files
committed
fix: Change format when elements are truncated and add additional tests
1 parent f4885a6 commit 588c744

File tree

2 files changed

+40
-8
lines changed

2 files changed

+40
-8
lines changed

cpp/src/arrow/pretty_print.cc

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,9 @@ class PrettyPrinter {
5959
: options_(options), indent_(options.indent), sink_(sink) {}
6060

6161
inline void Write(std::string_view data);
62+
inline void Write(std::string_view data, const int max_chars);
6263
inline void WriteIndented(std::string_view data);
64+
inline void WriteIndented(std::string_view data, const int max_chars);
6365
inline void Newline();
6466
inline void Indent();
6567
inline void IndentAfterNewline();
@@ -105,20 +107,29 @@ void PrettyPrinter::CloseArray(const Array& array) {
105107
}
106108

107109
void PrettyPrinter::Write(std::string_view data) {
108-
int available_chars = options_.element_size_limit;
109-
for (size_t i = 0; i < data.size(); ++i) {
110-
if (available_chars-- > 0) {
111-
sink_->put(data[i]);
110+
Write(data, options_.element_size_limit);
111+
}
112+
113+
void PrettyPrinter::Write(std::string_view data, const int max_chars) {
114+
int curr_max = max_chars;
115+
for (auto c : data) {
116+
if (curr_max-- > 0) {
117+
sink_->put(c);
112118
} else {
113-
(*sink_) << "+" << data.size() - options_.element_size_limit;
119+
(*sink_) << " (... " << data.size() - max_chars << " chars omitted)";
114120
return;
115121
}
116122
}
117123
}
118124

119125
void PrettyPrinter::WriteIndented(std::string_view data) {
120126
Indent();
121-
Write(data);
127+
Write(data, options_.element_size_limit);
128+
}
129+
130+
void PrettyPrinter::WriteIndented(std::string_view data, const int max_chars) {
131+
Indent();
132+
Write(data, max_chars);
122133
}
123134

124135
void PrettyPrinter::Newline() {
@@ -242,7 +253,9 @@ class ArrayPrinter : public PrettyPrinter {
242253
enable_if_has_string_view<T, Status> WriteDataValues(const ArrayType& array) {
243254
return WriteValues(array, [&](int64_t i) {
244255
if constexpr (T::is_utf8) {
245-
this->Write("\"" + std::string{array.GetView(i)} + "\"");
256+
(*sink_) << "\"";
257+
this->Write(array.GetView(i), options_.element_size_limit - 2);
258+
(*sink_) << "\"";
246259
} else {
247260
this->Write(HexEncode(array.GetView(i)));
248261
}

cpp/src/arrow/pretty_print_test.cc

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,18 @@ TEST_F(TestPrettyPrint, PrimitiveType) {
177177
])expected";
178178
CheckPrimitive<StringType, std::string>({2, 10}, is_valid, values3, ex3_in2);
179179
CheckPrimitive<LargeStringType, std::string>({2, 10}, is_valid, values3, ex3_in2);
180+
181+
PrettyPrintOptions options{2, 10};
182+
options.element_size_limit = 2;
183+
static const char* ex3_in3 = R"expected( [
184+
" (... 3 chars omitted)",
185+
" (... 3 chars omitted)",
186+
null,
187+
" (... 3 chars omitted)",
188+
null
189+
])expected";
190+
CheckPrimitive<StringType, std::string>(options, is_valid, values3, ex3_in3);
191+
CheckPrimitive<LargeStringType, std::string>(options, is_valid, values3, ex3_in3);
180192
}
181193

182194
TEST_F(TestPrettyPrint, PrimitiveTypeNoNewlines) {
@@ -775,7 +787,8 @@ TEST_F(TestPrettyPrint, BinaryNoNewlines) {
775787

776788
// With truncated element size
777789
options.element_size_limit = 1;
778-
expected = "[6+5,6+5,...,,F+1]";
790+
expected =
791+
"[6 (... 5 chars omitted),6 (... 5 chars omitted),...,,F (... 1 chars omitted)]";
779792
CheckPrimitive<BinaryType, std::string>(options, is_valid, values, expected, false);
780793
}
781794

@@ -1108,6 +1121,12 @@ TEST_F(TestPrettyPrint, FixedSizeBinaryType) {
11081121
CheckArray(*array, {0, 10}, ex);
11091122
static const char* ex_2 = " [\n 666F6F,\n ...\n 62617A\n ]";
11101123
CheckArray(*array, {2, 1}, ex_2);
1124+
1125+
auto options = PrettyPrintOptions{2, 1};
1126+
options.element_size_limit = 3;
1127+
static const char* ex_3 =
1128+
" [\n 666 (... 3 chars omitted),\n ...\n 626 (... 3 chars omitted)\n ]";
1129+
CheckArray(*array, options, ex_3);
11111130
}
11121131

11131132
TEST_F(TestPrettyPrint, DecimalTypes) {

0 commit comments

Comments
 (0)