Repository navigation
[Data] Fix Dataset.summary() NotImplementedError on native fixed_shape_tensor with permutation - #68483
Conversation
There was a problem hiding this comment.
Code Review
This pull request resolves an issue where calling summary() on datasets containing native arrow.fixed_shape_tensor columns with permutations on pyarrow 25+ raises a NotImplementedError. It addresses this by using pa.nulls for all-null columns and catching NotImplementedError during PyArrow array creation. A regression test was added to verify the fix. The review feedback correctly points out a missing numpy import in the new test function, which would otherwise cause a NameError during execution.
| def test_summary_native_fixed_shape_tensor_with_permutation( | ||
| self, restore_data_context | ||
| ): | ||
| """Regression test for #68280: summary() must not raise NotImplementedError on | ||
| native arrow.fixed_shape_tensor columns with permutation on pyarrow 25+.""" | ||
| from ray.data._internal.tensor_extensions.arrow import FixedShapeTensorFormat | ||
| from ray.data.context import DataContext | ||
|
|
||
| DataContext.get_current().arrow_fixed_shape_tensor_format = ( | ||
| FixedShapeTensorFormat.ARROW_NATIVE | ||
| ) | ||
| ds = ray.data.from_items( | ||
| [{"id": i, "emb": np.full((2, 2), i, dtype=np.int64)} for i in range(3)] | ||
| ) |
There was a problem hiding this comment.
The regression test uses np.full to construct the test dataset, but numpy is not imported in this test function or globally in the file. This will cause a NameError when the test is executed. Please import numpy as np within the test function.
| def test_summary_native_fixed_shape_tensor_with_permutation( | |
| self, restore_data_context | |
| ): | |
| """Regression test for #68280: summary() must not raise NotImplementedError on | |
| native arrow.fixed_shape_tensor columns with permutation on pyarrow 25+.""" | |
| from ray.data._internal.tensor_extensions.arrow import FixedShapeTensorFormat | |
| from ray.data.context import DataContext | |
| DataContext.get_current().arrow_fixed_shape_tensor_format = ( | |
| FixedShapeTensorFormat.ARROW_NATIVE | |
| ) | |
| ds = ray.data.from_items( | |
| [{"id": i, "emb": np.full((2, 2), i, dtype=np.int64)} for i in range(3)] | |
| ) | |
| def test_summary_native_fixed_shape_tensor_with_permutation( | |
| self, restore_data_context | |
| ): | |
| """Regression test for #68280: summary() must not raise NotImplementedError on | |
| native arrow.fixed_shape_tensor columns with permutation on pyarrow 25+.""" | |
| import numpy as np | |
| from ray.data._internal.tensor_extensions.arrow import FixedShapeTensorFormat | |
| from ray.data.context import DataContext | |
| DataContext.get_current().arrow_fixed_shape_tensor_format = ( | |
| FixedShapeTensorFormat.ARROW_NATIVE | |
| ) | |
| ds = ray.data.from_items( | |
| [{"id": i, "emb": np.full((2, 2), i, dtype=np.int64)} for i in range(3)] | |
| ) |
There was a problem hiding this comment.
Good catch — added import numpy as np in test_summary_native_fixed_shape_tensor_with_permutation in b776011.
…e_tensor with permutation on pyarrow 25+ Signed-off-by: Kunal Kumar <[email protected]>
cbf2042 to
81ca617
Compare
…on and wrap comment Signed-off-by: Kunal Kumar <[email protected]>
rayo5o
left a comment
There was a problem hiding this comment.
Checked this against the merge base (bebbc6e) on pyarrow 23.0.1, 24.0.0, 25.0.1 and 26.0.1, with 10 inputs (the issue repro, two tensor columns, a null tensor row, variable-shaped, mixed scalar types, columns=, multiple blocks) in V1, V2 and ARROW_NATIVE:
- 23 and 24: output identical to the merge base.
- 25 and 26: the merge base raises in 8 of the ARROW_NATIVE cases; this branch returns the same table as on 23. The only difference is the key order in the
approx_topkdicts, which also differs between pyarrow versions for V1/V2 on the merge base. pa.nullsvspa.array([None] * n, type=t)for 19 types, including Ray's V1/V2/variable-shaped/object extension types: equal whereverpa.arrayworks.
It isn't only ARROW_NATIVE users: with the default format (V2), reading a Parquet file whose tensor column was written by pyarrow (FixedShapeTensorArray.from_numpy_ndarray) gives the same NotImplementedError from summary() on the merge base with pyarrow 26, and works here.
On CI: premerge only runs the data tests in the arrow v23 lane (v25 is skip-on-premerge, v26 needs continuous-build), where the new test also passes on the merge base. Locally with the CI lock (pyarrow 25.0.1) it fails on the merge base and passes here; test_dataset_stats.py is 41 passed.
One suggestion on the test below.
| min_row = actual[actual[DatasetSummary.STATISTIC_COLUMN] == "min"] | ||
| assert len(min_row) == 1 | ||
| assert min_row["id"].iloc[0] == 0 | ||
| assert pd.isna(min_row["emb"].iloc[0]) |
There was a problem hiding this comment.
This test still passes if I drop the pa.nulls block and keep only the extra NotImplementedError catch. In that version the emb column of the type-preserving table comes back as null instead of the tensor type, and to_pandas() looks the same either way. If keeping the type is what the pa.nulls path is for, two more lines would pin it (pass on b776011, fail with only the except change, pyarrow 25.0.1):
emb_type = summary._stats_matching_column_dtype.schema.field("emb").type
assert isinstance(emb_type, pa.FixedShapeTensorType)
Description
On pyarrow 25+,
Dataset.summary()raisesNotImplementedErrorwhen building the type-preserving summary table for a dataset containing a nativearrow.fixed_shape_tensorcolumn with apermutation(created bypa.FixedShapeTensorArray.from_numpy_ndarray).Because the tensor column has no type-preserving statistics,
_build_summary_table(preserve_types=True)passes an all-Nonelist to_create_pyarrow_array, which calledpa.array([None, ...], type=tensor_type)and hit pyarrow 25's sequence-conversion guard.This PR mirrors the
pa.nullsapproach from #68072:col_datais allNoneandcol_typeis specified,_create_pyarrow_arrayconstructs the typed null array viapa.nulls(len(col_data), type=col_type).NotImplementedErroralongside(pa.ArrowTypeError, pa.ArrowInvalid)so unsupported sequence conversions cleanly fall back toconvert_to_pyarrow_array.test_summary_native_fixed_shape_tensor_with_permutationinpython/ray/data/tests/test_dataset_stats.py.Related issues
Fixes #68280
Additional information
pyarrow==26.0.1that_build_summary_table(..., preserve_types=True)produces a typed all-nullextension<arrow.fixed_shape_tensor[value_type=int64, shape=[2,2], permutation=[0,1]]>column instead of raisingNotImplementedError.