This is an automated email from the ASF dual-hosted git repository.

lidavidm pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/arrow.git


The following commit(s) were added to refs/heads/main by this push:
     new a7e2147ae0 GH-36933: [Python] Pointless ellipsis in array repr (#37168)
a7e2147ae0 is described below

commit a7e2147ae0af65b361583347707154635db13b22
Author: Ray Zhang <[email protected]>
AuthorDate: Mon Aug 21 10:40:17 2023 -0400

    GH-36933: [Python] Pointless ellipsis in array repr (#37168)
    
    ### Rationale for this change
    
    Explained in the issue
    
    ### What changes are included in this PR?
    
    Added a check when adding ellipsis to see if the ellipsis is meaningful.
    Rewrite some of tests affected by this change.
    
    ### Are these changes tested?
    
    Yes
    
    ### Are there any user-facing changes?
    
    Yes, if the window = 2 and the array is [1, 2, 3, 4, 5], its print will be 
[1, 2, 3, 4, 5] instead of [1, 2, ..., 4, 5]
    
    * Closes: #36933
    
    Authored-by: Ray Zhang <[email protected]>
    Signed-off-by: David Li <[email protected]>
---
 cpp/src/arrow/pretty_print.cc      |  4 +++-
 cpp/src/arrow/pretty_print_test.cc | 33 +++++++++++++++++++++++----------
 2 files changed, 26 insertions(+), 11 deletions(-)

diff --git a/cpp/src/arrow/pretty_print.cc b/cpp/src/arrow/pretty_print.cc
index 0577f0be4d..03e2051c2f 100644
--- a/cpp/src/arrow/pretty_print.cc
+++ b/cpp/src/arrow/pretty_print.cc
@@ -145,7 +145,9 @@ class ArrayPrinter : public PrettyPrinter {
     int window = is_container ? options_.container_window : options_.window;
     for (int64_t i = 0; i < array.length(); ++i) {
       const bool is_last = (i == array.length() - 1);
-      if ((i >= window) && (i < (array.length() - window))) {
+      // check if `length == 2 * window + 1` to eliminate ellipsis for only 
one element
+      if ((array.length() != 2 * window + 1) && (i >= window) &&
+          (i < (array.length() - window))) {
         IndentAfterNewline();
         (*sink_) << "...";
         if (!is_last && options_.skip_new_lines) {
diff --git a/cpp/src/arrow/pretty_print_test.cc 
b/cpp/src/arrow/pretty_print_test.cc
index bebbc6e82e..9a6e347c0b 100644
--- a/cpp/src/arrow/pretty_print_test.cc
+++ b/cpp/src/arrow/pretty_print_test.cc
@@ -123,15 +123,23 @@ TEST_F(TestPrettyPrint, PrimitiveType) {
     null
   ])expected";
   CheckPrimitive<Int32Type, int32_t>({2, 10}, is_valid, values, ex_in2);
+
   static const char* ex_in2_w2 = R"expected(  [
     0,
     1,
-    ...
+    null,
     3,
     null
   ])expected";
   CheckPrimitive<Int32Type, int32_t>({2, 2}, is_valid, values, ex_in2_w2);
 
+  static const char* ex_in2_w1 = R"expected(  [
+    0,
+    ...
+    null
+  ])expected";
+  CheckPrimitive<Int32Type, int32_t>({2, 1}, is_valid, values, ex_in2_w1);
+
   std::vector<double> values2 = {0., 1., 2., 3., 4.};
   static const char* ex2 = R"expected([
   0,
@@ -738,7 +746,7 @@ TEST_F(TestPrettyPrint, ListType) {
     null
   ],
   [],
-  ...
+  null,
   [
     4,
     6,
@@ -790,7 +798,12 @@ TEST_F(TestPrettyPrint, ListTypeNoNewlines) {
   options.window = 2;
   options.container_window = 2;
   CheckArray(*empty_array, options, "[]", false);
-  CheckArray(*array, options, "[[NA],[],...,[4,5,...,7,8],[2,3]]", false);
+  CheckArray(*array, options, "[[NA],[],NA,[4,5,6,7,8],[2,3]]", false);
+
+  options.window = 1;
+  options.container_window = 2;
+  CheckArray(*empty_array, options, "[]", false);
+  CheckArray(*array, options, "[[NA],[],NA,[4,...,8],[2,3]]", false);
 }
 
 TEST_F(TestPrettyPrint, MapType) {
@@ -854,7 +867,7 @@ TEST_F(TestPrettyPrint, FixedSizeListType) {
     3,
     null
   ],
-  ...
+  null,
   [
     4,
     6,
@@ -876,23 +889,23 @@ TEST_F(TestPrettyPrint, FixedSizeListType) {
               R"expected([
   [
     null,
-    ...
+    0,
     1
   ],
   [
     2,
-    ...
+    3,
     null
   ],
   null,
   [
     4,
-    ...
+    6,
     7
   ],
   [
     8,
-    ...
+    9,
     5
   ]
 ])expected");
@@ -901,13 +914,13 @@ TEST_F(TestPrettyPrint, FixedSizeListType) {
               R"expected([
   [
     null,
-    ...
+    0,
     1
   ],
   ...
   [
     8,
-    ...
+    9,
     5
   ]
 ])expected");

Reply via email to