Repository navigation
[Data] Respect child value-array slice offsets when converting tensor columns to NumPy and pandas - #68633
[Data] Respect child value-array slice offsets when converting tensor columns to NumPy and pandas#68633m4ru1 wants to merge 1 commit into
Conversation
… columns to NumPy and pandas Fix incorrect tensor values returned by Table.to_pandas(), .to_numpy(), and fused Dataset NumPy/pandas map_batches() stages when a fixed-shape or variable-shaped tensor column is backed by a sliced Arrow value array, and use integer element counts when unpacking 0-dimensional boolean tensor rows. Closes ray-project#68632 Signed-off-by: mora <[email protected]>
There was a problem hiding this comment.
Code Review
This pull request refactors Arrow tensor-to-NumPy conversions to correctly account for child array offsets when dealing with sliced arrays, ensuring zero-copy views remain accurate. It also adds comprehensive unit tests for sliced child tensors under various formats. The review feedback correctly identifies that pa.repeat is used in the new test helpers instead of pa.compute.repeat, which would result in an AttributeError.
| storage = tensors.storage | ||
| variable_shaped = isinstance(tensors, ArrowVariableShapedTensorArray) | ||
| data = storage.field("data") if variable_shaped else storage | ||
| values = pa.concat_arrays([pa.repeat(data.values[0], 9), data.values]).slice(9) |
There was a problem hiding this comment.
Using pa.repeat will raise an AttributeError because repeat is not in the top-level pyarrow namespace. It should be pa.compute.repeat instead.
| values = pa.concat_arrays([pa.repeat(data.values[0], 9), data.values]).slice(9) | |
| values = pa.concat_arrays([pa.compute.repeat(data.values[0], 9), data.values]).slice(9) |
| storage = tensors.storage | ||
| variable_shaped = isinstance(tensors, ArrowVariableShapedTensorArray) | ||
| data = storage.field("data") if variable_shaped else storage | ||
| values = pa.concat_arrays([pa.repeat(data.values[0], 9), data.values]).slice(9) |
There was a problem hiding this comment.
Using pa.repeat will raise an AttributeError because repeat is not in the top-level pyarrow namespace. It should be pa.compute.repeat instead.
| values = pa.concat_arrays([pa.repeat(data.values[0], 9), data.values]).slice(9) | |
| values = pa.concat_arrays([pa.compute.repeat(data.values[0], 9), data.values]).slice(9) |
Summary
Fix incorrect tensor values returned by
Table.to_pandas(),.to_numpy(), and fused NumPy/pandasDataset.map_batches()stages when a tensor column's underlying Arrow value array is sliced, closing #68632. Whereas #66122 and #67283 built and reused dense buffer views over unsliced value arrays (and #66997 updates pandasTensorArray.take), this accounts for non-zero slice offsets on the Arrow child value array itself.Before
When an Arrow
TableorDatasetcontains a fixed-shape or variable-shaped tensor column backed by a sliced Arrow value array,Table.to_pandas(),.to_numpy(), and fusedDataset.map_batches(..., batch_format="numpy" | "pandas")stages index into the underlying data buffer using only list row offsets without the child value array's slice offset, returning values from preceding un-sliced bytes.After
Anchor tensor buffer views at the combined list row offset and child value-array slice offset across both fixed-shape and variable-shaped tensor conversions, sharing a single buffer-view path that also uses integer element counts when unpacking 0-dimensional boolean rows.
Single-chunk contiguous non-boolean columns retain zero-copy views over the shared Arrow buffer, null-bearing and ragged columns keep per-row views with
Nonefor null rows, and boolean columns continue unpacking bits into separate storage. PyArrow's nativepa.fixed_shape_tensorpath is unchanged.flowchart LR subgraph Before["Before (Block / data format)"] B1["Tensor buffer view (row offset only)"] -. ignores child slice start .-> B2["to_pandas / NumPy batches read prefix values"] end subgraph After["After (Block / data format)"] A1["Tensor buffer view (row + child slice offsets)"] -. starts at sliced buffer position .-> A2["to_pandas & NumPy/pandas batches match to_pylist()"] end classDef bad fill:#ffcdd2,stroke:#c62828,color:#000 classDef changed fill:#fff9c4,stroke:#f9a825,color:#000 classDef good fill:#c8e6c9,stroke:#2e7d32,color:#000 class B1,B2 bad class A1 changed class A2 goodSubsystems
Block / data format.
Testing
Local validation passed on Python 3.11.15 using the Ray 3.0.0.dev0 wheel (
a872863f2b) with updatedray.datasources (e4462656e8+ working diff) loaded on both driver and workers across PyArrow 23.0.0, 26.0.0, and 26.0.1. Tests check V1/V2 fixed-shape and variable-shaped conversions across numeric, byte-crossing boolean, Unicode, 0-dimensional boolean, null-bearing, multi-chunk, zero-copy memory-sharing, and fused workerDatasetpipelines.Pre-commit hooks (including
pyrefly-data) and pytest lint checks also passed.AI assistance was used.