You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@arrow.apache.org by jo...@apache.org on 2023/01/19 14:38:30 UTC

[arrow] branch master updated: GH-15109: [Python] Allow creation of non empty struct array with zero field (#33764)

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

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


The following commit(s) were added to refs/heads/master by this push:
     new fc1f9ebbc4 GH-15109: [Python] Allow creation of non empty struct array with zero field (#33764)
fc1f9ebbc4 is described below

commit fc1f9ebbc4c3ae77d5cfc2f9322f4373d3d19b8a
Author: 0x26res <ar...@gmail.com>
AuthorDate: Thu Jan 19 14:38:21 2023 +0000

    GH-15109: [Python] Allow creation of non empty struct array with zero field (#33764)
    
    * Closes: #15109
    
    Authored-by: aandres <aa...@tradewelltech.co>
    Signed-off-by: Joris Van den Bossche <jo...@gmail.com>
---
 cpp/src/arrow/array/array_nested.cc | 5 ++---
 python/pyarrow/array.pxi            | 5 ++++-
 python/pyarrow/tests/test_array.py  | 5 +++++
 3 files changed, 11 insertions(+), 4 deletions(-)

diff --git a/cpp/src/arrow/array/array_nested.cc b/cpp/src/arrow/array/array_nested.cc
index 628259f0f6..68f6bfbc51 100644
--- a/cpp/src/arrow/array/array_nested.cc
+++ b/cpp/src/arrow/array/array_nested.cc
@@ -534,11 +534,10 @@ Result<std::shared_ptr<StructArray>> StructArray::Make(
   if (children.size() != fields.size()) {
     return Status::Invalid("Mismatching number of fields and child arrays");
   }
-  int64_t length = 0;
-  if (children.size() == 0) {
+  if (children.empty()) {
     return Status::Invalid("Can't infer struct array length with 0 child arrays");
   }
-  length = children.front()->length();
+  const int64_t length = children.front()->length();
   for (const auto& child : children) {
     if (length != child->length()) {
       return Status::Invalid("Mismatching child array lengths");
diff --git a/python/pyarrow/array.pxi b/python/pyarrow/array.pxi
index b2dff65677..da76b90666 100644
--- a/python/pyarrow/array.pxi
+++ b/python/pyarrow/array.pxi
@@ -2785,7 +2785,10 @@ cdef class StructArray(Array):
         if (c_arrays.size() == 0 and c_names.size() == 0 and
                 c_fields.size() == 0):
             # The C++ side doesn't allow this
-            return array([], struct([]))
+            if mask is None:
+                return array([], struct([]))
+            else:
+                return array([{}] * len(mask), struct([]), mask=mask)
 
         if names is not None:
             # XXX Cannot pass "nullptr" for a shared_ptr<T> argument:
diff --git a/python/pyarrow/tests/test_array.py b/python/pyarrow/tests/test_array.py
index b00e72e1bb..fa3de35904 100644
--- a/python/pyarrow/tests/test_array.py
+++ b/python/pyarrow/tests/test_array.py
@@ -714,6 +714,11 @@ def test_struct_from_arrays():
         pa.StructArray.from_arrays(
             arrays, fields, mask=pa.chunked_array([mask]))
 
+    # Non-empty array with no fields https://github.com/apache/arrow/issues/15109
+    arr = pa.StructArray.from_arrays([], [], mask=mask)
+    assert arr.is_null() == mask
+    assert arr.to_pylist() == [None, {}, {}]
+
 
 def test_struct_array_from_chunked():
     # ARROW-11780