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-37979: [C++] Add support for specifying custom Array opening and closing delimiters to arrow::PrettyPrintDelimiters #38187

Merged
merged 3 commits into from
Oct 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions cpp/src/arrow/pretty_print.cc
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ void PrettyPrinter::OpenArray(const Array& array) {
if (!options_.skip_new_lines) {
Indent();
}
(*sink_) << "[";
(*sink_) << options_.array_delimiters.open;
if (array.length() > 0) {
Newline();
indent_ += options_.indent_size;
Expand All @@ -101,7 +101,7 @@ void PrettyPrinter::CloseArray(const Array& array) {
Indent();
}
}
(*sink_) << "]";
(*sink_) << options_.array_delimiters.close;
}

void PrettyPrinter::Write(std::string_view data) { (*sink_) << data; }
Expand Down Expand Up @@ -449,7 +449,7 @@ Status PrettyPrint(const ChunkedArray& chunked_arr, const PrettyPrintOptions& op
for (int i = 0; i < indent; ++i) {
(*sink) << " ";
}
(*sink) << "[";
(*sink) << options.chunked_array_delimiters.open;
if (!skip_new_lines) {
*sink << "\n";
}
Expand Down Expand Up @@ -488,7 +488,7 @@ Status PrettyPrint(const ChunkedArray& chunked_arr, const PrettyPrintOptions& op
for (int i = 0; i < indent; ++i) {
(*sink) << " ";
}
(*sink) << "]";
(*sink) << options.chunked_array_delimiters.close;

return Status::OK();
}
Expand Down
6 changes: 6 additions & 0 deletions cpp/src/arrow/pretty_print.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ class Table;
/// \brief Options for controlling which delimiters to use when printing
/// an Array or ChunkedArray.
struct ARROW_EXPORT PrettyPrintDelimiters {
/// Delimiter to use when opening an Array or ChunkedArray (e.g. "[")
std::string open = "[";

/// Delimiter to use when closing an Array or ChunkedArray (e.g. "]")
std::string close = "]";

/// Delimiter for separating individual elements of an Array (e.g. ","),
/// or individual chunks of a ChunkedArray
std::string element = ",";
Expand Down
73 changes: 73 additions & 0 deletions cpp/src/arrow/pretty_print_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,25 @@ TEST_F(TestPrettyPrint, ArrayCustomElementDelimiter) {
}
}

TEST_F(TestPrettyPrint, ArrayCustomOpenCloseDelimiter) {
PrettyPrintOptions options{};
// Use a custom opening Array delimiter of "{", rather than the default "]".
options.array_delimiters.open = "{";
// Use a custom closing Array delimiter of "}", rather than the default "]".
options.array_delimiters.close = "}";

std::vector<bool> is_valid = {true, true, false, true, false};
std::vector<int32_t> values = {1, 2, 3, 4, 5};
static const char* expected = R"expected({
1,
2,
null,
4,
null
})expected";
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 @@ -1131,6 +1150,60 @@ TEST_F(TestPrettyPrint, ChunkedArrayCustomElementDelimiter) {
}
}

TEST_F(TestPrettyPrint, ChunkedArrayCustomOpenCloseDelimiter) {
PrettyPrintOptions options{};
// Use a custom opening Array delimiter of "{", rather than the default "]".
options.array_delimiters.open = "{";
// Use a custom closing Array delimiter of "}", rather than the default "]".
options.array_delimiters.close = "}";
// Use a custom opening ChunkedArray delimiter of "<", rather than the default "]".
options.chunked_array_delimiters.open = "<";
// Use a custom closing ChunkedArray delimiter of ">", rather than the default "]".
options.chunked_array_delimiters.close = ">";

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";

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