Skip to content

Commit 641ae95

Browse files
committed
Improve tests + nits
1 parent e070ce9 commit 641ae95

File tree

6 files changed

+58
-34
lines changed

6 files changed

+58
-34
lines changed

cpp/src/arrow/pretty_print.cc

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -110,10 +110,11 @@ void PrettyPrinter::Write(std::string_view data) {
110110
Write(data, options_.element_size_limit);
111111
}
112112

113-
void PrettyPrinter::Write(std::string_view data, const int max_chars) {
113+
void PrettyPrinter::Write(std::string_view data, int max_chars) {
114114
(*sink_) << data.substr(0, max_chars);
115-
if (data.size() > static_cast<uint64_t>(max_chars)) {
116-
(*sink_) << " (... " << data.size() - max_chars << " chars omitted)";
115+
if (data.size() > static_cast<size_t>(max_chars)) {
116+
(*sink_) << " (... " << data.size() - static_cast<size_t>(max_chars)
117+
<< " chars omitted)";
117118
}
118119
}
119120

@@ -122,7 +123,7 @@ void PrettyPrinter::WriteIndented(std::string_view data) {
122123
Write(data, options_.element_size_limit);
123124
}
124125

125-
void PrettyPrinter::WriteIndented(std::string_view data, const int max_chars) {
126+
void PrettyPrinter::WriteIndented(std::string_view data, int max_chars) {
126127
Indent();
127128
Write(data, max_chars);
128129
}
@@ -192,7 +193,7 @@ class ArrayPrinter : public PrettyPrinter {
192193

193194
template <typename ArrayType, typename Formatter>
194195
Status WritePrimitiveValues(const ArrayType& array, Formatter* formatter) {
195-
auto appender = [&](std::string_view v) { this->Write(v); };
196+
auto appender = [&](std::string_view v) { Write(v); };
196197
auto format_func = [&](int64_t i) {
197198
(*formatter)(array.GetView(i), appender);
198199
return Status::OK();

cpp/src/arrow/pretty_print_test.cc

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include <memory>
2626
#include <sstream>
2727
#include <string>
28+
#include <string_view>
2829
#include <vector>
2930

3031
#include "arrow/array.h"
@@ -47,37 +48,37 @@ class TestPrettyPrint : public ::testing::Test {
4748
};
4849

4950
template <typename T>
50-
void CheckStream(const T& obj, const PrettyPrintOptions& options, const char* expected) {
51+
void CheckStream(const T& obj, const PrettyPrintOptions& options,
52+
std::string_view expected) {
5153
std::ostringstream sink;
5254
ASSERT_OK(PrettyPrint(obj, options, &sink));
5355
std::string result = sink.str();
54-
ASSERT_EQ(std::string(expected, strlen(expected)), result);
56+
ASSERT_EQ(expected, result);
5557
}
5658

57-
void CheckArray(const Array& arr, const PrettyPrintOptions& options, const char* expected,
58-
bool check_operator = true) {
59+
void CheckArray(const Array& arr, const PrettyPrintOptions& options,
60+
std::string_view expected, bool check_operator = true) {
5961
ARROW_SCOPED_TRACE("For datatype: ", arr.type()->ToString());
6062
CheckStream(arr, options, expected);
6163

62-
if (options.indent == 0 && check_operator) {
64+
if (options.indent == 0 && options.element_size_limit == 100 && check_operator) {
6365
std::stringstream ss;
6466
ss << arr;
65-
std::string result = std::string(expected, strlen(expected));
66-
ASSERT_EQ(result, ss.str());
67+
ASSERT_EQ(expected, ss.str());
6768
}
6869
}
6970

7071
template <typename T>
71-
void Check(const T& obj, const PrettyPrintOptions& options, const char* expected) {
72+
void Check(const T& obj, const PrettyPrintOptions& options, std::string_view expected) {
7273
std::string result;
7374
ASSERT_OK(PrettyPrint(obj, options, &result));
74-
ASSERT_EQ(std::string(expected, strlen(expected)), result);
75+
ASSERT_EQ(expected, result);
7576
}
7677

7778
template <typename TYPE, typename C_TYPE>
7879
void CheckPrimitive(const std::shared_ptr<DataType>& type,
7980
const PrettyPrintOptions& options, const std::vector<bool>& is_valid,
80-
const std::vector<C_TYPE>& values, const char* expected,
81+
const std::vector<C_TYPE>& values, std::string_view expected,
8182
bool check_operator = true) {
8283
std::shared_ptr<Array> array;
8384
ArrayFromVector<TYPE, C_TYPE>(type, is_valid, values, &array);
@@ -86,7 +87,7 @@ void CheckPrimitive(const std::shared_ptr<DataType>& type,
8687

8788
template <typename TYPE, typename C_TYPE>
8889
void CheckPrimitive(const PrettyPrintOptions& options, const std::vector<bool>& is_valid,
89-
const std::vector<C_TYPE>& values, const char* expected,
90+
const std::vector<C_TYPE>& values, std::string_view expected,
9091
bool check_operator = true) {
9192
CheckPrimitive<TYPE, C_TYPE>(TypeTraits<TYPE>::type_singleton(), options, is_valid,
9293
values, expected, check_operator);
@@ -158,12 +159,12 @@ TEST_F(TestPrettyPrint, PrimitiveType) {
158159
])expected";
159160
CheckPrimitive<DoubleType, double>({2, 10}, is_valid, values2, ex2_in2);
160161

161-
std::vector<std::string> values3 = {"foo", "bar", "", "baz", ""};
162+
std::vector<std::string> values3 = {"foo", "bar", "", "a longer string", ""};
162163
static const char* ex3 = R"expected([
163164
"foo",
164165
"bar",
165166
null,
166-
"baz",
167+
"a longer string",
167168
null
168169
])expected";
169170
CheckPrimitive<StringType, std::string>({0, 10}, is_valid, values3, ex3);
@@ -172,19 +173,19 @@ TEST_F(TestPrettyPrint, PrimitiveType) {
172173
"foo",
173174
"bar",
174175
null,
175-
"baz",
176+
"a longer string",
176177
null
177178
])expected";
178179
CheckPrimitive<StringType, std::string>({2, 10}, is_valid, values3, ex3_in2);
179180
CheckPrimitive<LargeStringType, std::string>({2, 10}, is_valid, values3, ex3_in2);
180181

181182
PrettyPrintOptions options{2, 10};
182-
options.element_size_limit = 2;
183+
options.element_size_limit = 8;
183184
static const char* ex3_in3 = R"expected( [
184-
" (... 3 chars omitted)",
185-
" (... 3 chars omitted)",
185+
"foo",
186+
"bar",
186187
null,
187-
" (... 3 chars omitted)",
188+
"a long (... 9 chars omitted)",
188189
null
189190
])expected";
190191
CheckPrimitive<StringType, std::string>(options, is_valid, values3, ex3_in3);
@@ -1139,6 +1140,12 @@ TEST_F(TestPrettyPrint, DecimalTypes) {
11391140

11401141
static const char* ex = "[\n 123.4567,\n 456.7891,\n null\n]";
11411142
CheckArray(*array, {0}, ex);
1143+
1144+
auto options = PrettyPrintOptions();
1145+
options.element_size_limit = 3;
1146+
static const char* ex_2 =
1147+
"[\n 123 (... 5 chars omitted),\n 456 (... 5 chars omitted),\n null\n]";
1148+
CheckArray(*array, options, ex_2);
11421149
}
11431150
}
11441151

python/pyarrow/array.pxi

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1384,9 +1384,8 @@ cdef class Array(_PandasConvertible):
13841384
skip_new_lines : bool
13851385
If the array should be rendered as a single line of text
13861386
or if each element should be on its own line.
1387-
element_size_limit : int
1388-
Maximum number of characters of a single element before it is truncated,
1389-
by default ``100``.
1387+
element_size_limit : int, default 100
1388+
Maximum number of characters of a single element before it is truncated.
13901389
"""
13911390
cdef:
13921391
c_string result

python/pyarrow/table.pxi

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
12
# Licensed to the Apache Software Foundation (ASF) under one
23
# or more contributor license agreements. See the NOTICE file
34
# distributed with this work for additional information
@@ -137,9 +138,9 @@ cdef class ChunkedArray(_PandasConvertible):
137138
skip_new_lines : bool
138139
If the array should be rendered as a single line of text
139140
or if each element should be on its own line.
140-
element_size_limit : int
141-
Maximum number of characters of a single element before it is truncated,
142-
by default ``100``.
141+
element_size_limit : int, default 100
142+
Maximum number of characters of a single element before it is truncated.
143+
143144
Examples
144145
--------
145146
>>> import pyarrow as pa

python/pyarrow/tests/test_array.py

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -75,17 +75,32 @@ def test_constructor_raises():
7575

7676

7777
def test_list_format():
78-
arr = pa.array([[1], None, [200, 3, None]])
79-
result = arr.to_string(element_size_limit=2)
78+
arr = pa.array([["foo"], None, ["bar", "a longer string", None]])
79+
result = arr.to_string()
80+
expected = """\
81+
[
82+
[
83+
"foo"
84+
],
85+
null,
86+
[
87+
"bar",
88+
"a longer string",
89+
null
90+
]
91+
]"""
92+
assert result == expected
93+
94+
result = arr.to_string(element_size_limit=10)
8095
expected = """\
8196
[
8297
[
83-
1
98+
"foo"
8499
],
85100
null,
86101
[
87-
20 (... 1 chars omitted),
88-
3,
102+
"bar",
103+
"a longer (... 7 chars omitted)",
89104
null
90105
]
91106
]"""

python/pyarrow/types.pxi

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3581,6 +3581,7 @@ cdef class Schema(_Weakrefable):
35813581
Display Schema-level KeyValueMetadata
35823582
element_size_limit : int, default 100
35833583
Maximum number of characters of a single element before it is truncated.
3584+
35843585
Returns
35853586
-------
35863587
str : the formatted output

0 commit comments

Comments
 (0)