m4ru1 opened a new issue, #55834:
URL: https://github.com/apache/arrow/issues/55834

   ### Describe the bug, including details regarding any error messages, 
version, and platform.
   
   Converting a `FixedShapeTensorArray` to a `Tensor` or NumPy `ndarray` (via 
`to_tensor()` / `to_numpy_ndarray()` in Python or `ToTensor()` in C++) ignores 
the child `values` slice offset and silently reads from the beginning of the 
backing buffer whenever the child array is sliced. In addition, valid 
zero-element tensor arrays and scalars either raise `ArrowInvalid: strides must 
not involve buffer over run` when backed by a shorter sliced buffer or 
terminate with `SIGSEGV` when the zero-length values buffer is omitted (tested 
on PyArrow 26.0.1 and `main` on macOS arm64):
   
   ```python
   values = pa.array(np.arange(12, dtype=np.int64)).slice(4, 8)  # [4, 5, ..., 
11]
   arr = pa.ExtensionArray.from_storage(pa.fixed_shape_tensor(pa.int64(), (2, 
2)), pa.FixedSizeListArray.from_arrays(values, 4))
   arr[0].to_numpy()            # OK:    [[4, 5], [6, 7]]
   arr.to_numpy_ndarray()[0]    # Wrong: [[0, 1], [2, 3]]
   ```
   
   Self-contained reproduction (`pyarrow 26.0.1`, macOS arm64):
   
   ```python
   import subprocess
   import sys
   import numpy as np
   import pyarrow as pa
   
   # 1. Sliced child values array -> wrong tensor elements
   values = pa.array(np.arange(12, dtype=np.int64)).slice(4, 8)
   storage = pa.FixedSizeListArray.from_arrays(values, 4)
   arr = pa.ExtensionArray.from_storage(pa.fixed_shape_tensor(pa.int64(), (2, 
2)), storage)
   print("per-row:", [row.to_numpy().tolist() for row in arr])
   print("batch:  ", arr.to_numpy_ndarray().tolist())
   
   # 2. Zero-sized tensor shape over shorter sliced buffer -> spurious overrun 
error
   empty_slice = pa.array([0, 1], type=pa.int64()).slice(1, 0)
   zero_arr = pa.ExtensionArray.from_storage(
       pa.fixed_shape_tensor(pa.int64(), (0, 2)),
       pa.Array.from_buffers(pa.list_(pa.int64(), 0), 20, [None], 
children=[empty_slice]),
   )
   zero_arr.validate(full=True)
   try:
       zero_arr.to_tensor()
   except pa.ArrowInvalid as exc:
       print("zero-shape error:", exc)
   
   # 3. Valid empty tensor array & scalar with omitted values buffer -> SIGSEGV
   for expr in ["arr.to_tensor()", "arr[0].to_tensor()"]:
       code = f"""
   import pyarrow as pa, resource
   resource.setrlimit(resource.RLIMIT_CORE, (0, 0))
   empty_values = pa.Array.from_buffers(pa.int64(), 0, [None, None])
   arr = pa.ExtensionArray.from_storage(
       pa.fixed_shape_tensor(pa.int64(), (0, 2)),
       pa.Array.from_buffers(pa.list_(pa.int64(), 0), 1, [None], 
children=[empty_values]),
   )
   arr.validate(full=True)
   {expr}
   """
       proc = subprocess.run([sys.executable, "-c", code], capture_output=True, 
text=True)
       print(f"{expr} returncode:", proc.returncode)
   ```
   
   Observed output:
   
   ```text
   per-row: [[[4, 5], [6, 7]], [[8, 9], [10, 11]]]
   batch:   [[[0, 1], [2, 3]], [[4, 5], [6, 7]]]
   zero-shape error: strides must not involve buffer over run
   arr.to_tensor() returncode: -11
   arr[0].to_tensor() returncode: -11
   ```
   
   Expected behavior:
   Converting a `FixedShapeTensorArray` to a `Tensor` or NumPy `ndarray` should 
return values starting at the sliced child offset (matching per-row scalar 
conversion), and valid zero-element tensor arrays and scalars should convert 
cleanly to empty tensors without stride-overrun errors or crashes.
   
   **Cause:** `FixedShapeTensorArray::ToTensorWithNulls` in 
`cpp/src/arrow/extension/fixed_shape_tensor.cc` omits the child array's offset 
and exact byte length when slicing the values buffer, and both it and 
`internal::SliceTensorBuffer` in `cpp/src/arrow/extension/tensor_internal.cc` 
dereference null values buffers on zero-element inputs.
   
   ### Component(s)
   
   C++, Python
   


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