From 07c12b9d0a50961366eec453763650e1bf369132 Mon Sep 17 00:00:00 2001 From: Kevin Gurney Date: Tue, 10 Oct 2023 12:50:49 -0400 Subject: [PATCH 1/3] Add `open` and `close` properties to `PrettyPrintDelimiters` struct. --- cpp/src/arrow/pretty_print.h | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/cpp/src/arrow/pretty_print.h b/cpp/src/arrow/pretty_print.h index 96a214c68b8..c2000647a0e 100644 --- a/cpp/src/arrow/pretty_print.h +++ b/cpp/src/arrow/pretty_print.h @@ -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 = ","; From 874d05dc7331d4f4df5336060bc56d728d23883b Mon Sep 17 00:00:00 2001 From: Kevin Gurney Date: Tue, 10 Oct 2023 13:03:15 -0400 Subject: [PATCH 2/3] 1. Use new `open` and `close` delimiters in `pretty_print.cc`. 2. Fix typo (`open` should use `"[]"` and `close` should use `"]"`. --- cpp/src/arrow/pretty_print.cc | 8 ++++---- cpp/src/arrow/pretty_print.h | 4 ++-- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/cpp/src/arrow/pretty_print.cc b/cpp/src/arrow/pretty_print.cc index a4a1fa90c28..a5410df7e9a 100644 --- a/cpp/src/arrow/pretty_print.cc +++ b/cpp/src/arrow/pretty_print.cc @@ -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; @@ -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; } @@ -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"; } @@ -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(); } diff --git a/cpp/src/arrow/pretty_print.h b/cpp/src/arrow/pretty_print.h index c2000647a0e..ad68726716c 100644 --- a/cpp/src/arrow/pretty_print.h +++ b/cpp/src/arrow/pretty_print.h @@ -37,10 +37,10 @@ class Table; /// an Array or ChunkedArray. struct ARROW_EXPORT PrettyPrintDelimiters { /// Delimiter to use when opening an Array or ChunkedArray (e.g. "[") - std::string open = "]"; + std::string open = "["; /// Delimiter to use when closing an Array or ChunkedArray (e.g. "]") - std::string close = "["; + std::string close = "]"; /// Delimiter for separating individual elements of an Array (e.g. ","), /// or individual chunks of a ChunkedArray From 2b7aece4e42830229a7d2d923d37feefa7613e99 Mon Sep 17 00:00:00 2001 From: Kevin Gurney Date: Tue, 10 Oct 2023 13:23:33 -0400 Subject: [PATCH 3/3] Add `ArrayCustomOpenCloseDelimiter` and `ChunkedArrayCustomOpenCloseDelimiter` tests. --- cpp/src/arrow/pretty_print_test.cc | 73 ++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) diff --git a/cpp/src/arrow/pretty_print_test.cc b/cpp/src/arrow/pretty_print_test.cc index 45bb4ecffe0..9217e190d5b 100644 --- a/cpp/src/arrow/pretty_print_test.cc +++ b/cpp/src/arrow/pretty_print_test.cc @@ -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 is_valid = {true, true, false, true, false}; + std::vector values = {1, 2, 3, 4, 5}; + static const char* expected = R"expected({ + 1, + 2, + null, + 4, + null +})expected"; + CheckPrimitive(options, is_valid, values, expected, false); +} + TEST_F(TestPrettyPrint, Int8) { static const char* expected = R"expected([ 0, @@ -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 int_field = field("column", int32()); auto array = ArrayFromJSON(int_field->type(), "[0, 1, null, 3, null]");