raulcd commented on code in PR #38334:
URL: https://github.com/apache/arrow/pull/38334#discussion_r1981271781


##########
python/pyarrow/array.pxi:
##########
@@ -3204,23 +3204,34 @@ cdef class StructArray(Array):
 
         c_mask = c_mask_inverted_from_obj(mask, memory_pool)
 
-        arrays = [asarray(x) for x in arrays]
-        for arr in arrays:
-            c_array = pyarrow_unwrap_array(arr)
-            if c_array == nullptr:
-                raise TypeError(f"Expected Array, got {arr.__class__}")
-            c_arrays.push_back(c_array)
         if names is not None:
             for name in names:
                 c_names.push_back(tobytes(name))
+            arrays = [asarray(x) for x in arrays]
         else:
+            py_fields = []
             for item in fields:
                 if isinstance(item, tuple):
                     py_field = field(*item)
                 else:
                     py_field = item
+                py_fields.append(py_field)
                 c_fields.push_back(py_field.sp_field)
 
+            # This needs to be enumerate, not zip, in order to make
+            # sure that there is a field provided for every array.
+            try:
+                arrays = [asarray(ary, py_fields[i].type)
+                          for i, ary in enumerate(arrays)]
+            except IndexError:
+                raise ValueError("Must pass same number of arrays as fields")

Review Comment:
   It would be nice to get some tests for those exceptions



##########
python/pyarrow/tests/test_array.py:
##########
@@ -710,9 +710,9 @@ def test_struct_from_arrays():
     assert arr.to_pylist() == []
 
     # Inconsistent fields
-    fa2 = pa.field("a", pa.int32())
-    with pytest.raises(ValueError, match="int64 vs int32"):
-        pa.StructArray.from_arrays([a, b, c], fields=[fa2, fb, fc])

Review Comment:
   I have to investigate further but think there's something wrong here. This 
use case should still raise an error as the underlying `a` child array is 
int64. I am not entirely sure if we are making a copy or what is happening, as 
I am not familiar with this area of the codebase, but this should raise an 
error AFAIU.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to