Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GH-37978: [C++] Add support for specifying custom Array element delimiter to arrow::PrettyPrintOptions #37981

Merged
merged 12 commits into from
Oct 5, 2023
Merged
6 changes: 3 additions & 3 deletions cpp/src/arrow/pretty_print.cc
Original file line number Diff line number Diff line change
Expand Up @@ -151,22 +151,22 @@ class ArrayPrinter : public PrettyPrinter {
IndentAfterNewline();
(*sink_) << "...";
if (!is_last && options_.skip_new_lines) {
(*sink_) << ",";
(*sink_) << options_.array_element_delimiter;
}
i = array.length() - window - 1;
} else if (array.IsNull(i)) {
IndentAfterNewline();
(*sink_) << options_.null_rep;
if (!is_last) {
(*sink_) << ",";
(*sink_) << options_.array_element_delimiter;
}
} else {
if (indent_non_null_values) {
IndentAfterNewline();
}
RETURN_NOT_OK(func(i));
if (!is_last) {
(*sink_) << ",";
(*sink_) << options_.array_element_delimiter;
}
}
Newline();
Expand Down
3 changes: 3 additions & 0 deletions cpp/src/arrow/pretty_print.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ struct PrettyPrintOptions {

/// If true, display schema metadata when pretty-printing a Schema
bool show_schema_metadata = true;

/// Delimiter for separating individual elements of an Array (e.g. ",")
std::string array_element_delimiter = ",";
kevingurney marked this conversation as resolved.
Show resolved Hide resolved
kevingurney marked this conversation as resolved.
Show resolved Hide resolved
};

/// \brief Print human-readable representation of RecordBatch
Expand Down
78 changes: 78 additions & 0 deletions cpp/src/arrow/pretty_print_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,53 @@ TEST_F(TestPrettyPrint, PrimitiveTypeNoNewlines) {
CheckPrimitive<Int32Type, int32_t>(options, is_valid, values, expected, false);
}

TEST_F(TestPrettyPrint, PrimitiveTypeCustomArrayElementDelimiter) {
PrettyPrintOptions options{};
// Display array contents on one line.
options.skip_new_lines = true;
// Display maximum of 3 elements at the beginning and at the end of the array
options.window = 3;
kevingurney marked this conversation as resolved.
Show resolved Hide resolved
// Use a custom array element delimiter of " | ",
// rather than the default delimiter (i.e. ",").
options.array_element_delimiter = " | ";

// Short array without ellipsis
{
std::vector<bool> is_valid = {true, true, false, true, false};
std::vector<int32_t> values = {1, 2, 3, 4, 5};
const char* expected = "[1 | 2 | null | 4 | null]";
CheckPrimitive<Int32Type, int32_t>(options, is_valid, values, expected, false);
}

// Longer array with ellipsis
{
std::vector<bool> is_valid = {
true,
false,
true,
true,
false,
true,
false,
true,
true
};
std::vector<int32_t> values = {
1,
2,
3,
4,
5,
6,
7,
8,
9
};
const char* expected = "[1 | null | 3 | ... | null | 8 | 9]";
CheckPrimitive<Int32Type, int32_t>(options, is_valid, values, expected, false);
}
}

TEST_F(TestPrettyPrint, Int8) {
static const char* expected = R"expected([
0,
Expand Down Expand Up @@ -1020,6 +1067,37 @@ TEST_F(TestPrettyPrint, ChunkedArrayPrimitiveType) {
CheckStream(chunked_array_2, {0}, expected_2);
}

TEST_F(TestPrettyPrint, ChunkedArrayPrimitiveTypeCustomArrayElementDelimiter) {
PrettyPrintOptions options{};
// Display array contents on one line.
options.skip_new_lines = true;
// Display maximum of 3 elements at the beginning and at the end of the array
options.window = 3;
// Use a custom array element delimiter of " | ",
// rather than the default delimiter (i.e. ",").
options.array_element_delimiter = " | ";

const auto chunk = ArrayFromJSON(int32(), "[1, 2, null, 4, null]");

// ChunkedArray with 1 chunk
{
const ChunkedArray chunked_array(chunk);

static const char* expected = R"expected([[1 | 2 | null | 4 | null]])expected";
CheckStream(chunked_array, options, expected);
}

// ChunkedArray with 2 chunks
{
const ChunkedArray chunked_array({chunk, chunk});

static const char* expected =
R"expected([[1 | 2 | null | 4 | null],[1 | 2 | null | 4 | null]])expected";
kevingurney marked this conversation as resolved.
Show resolved Hide resolved

CheckStream(chunked_array, options, expected);
}
}

TEST_F(TestPrettyPrint, TablePrimitive) {
std::shared_ptr<Field> int_field = field("column", int32());
auto array = ArrayFromJSON(int_field->type(), "[0, 1, null, 3, null]");
Expand Down
Loading