GH-50012: [Python] Fix list_ storage crashes when values exceed int32 offsets#50016
Open
AnkitAhlawat7742 wants to merge 2 commits into
Open
GH-50012: [Python] Fix list_ storage crashes when values exceed int32 offsets#50016AnkitAhlawat7742 wants to merge 2 commits into
AnkitAhlawat7742 wants to merge 2 commits into
Conversation
|
|
Contributor
Author
|
Hi @AlenkaF , |
Contributor
There was a problem hiding this comment.
Pull request overview
Fixes a PyArrow crash when constructing extension arrays whose storage conversion auto-chunks (e.g., list_ offset overflow producing a ChunkedArray), by wrapping each storage chunk as an ExtensionArray instead of calling ExtensionArray.from_storage() on the full ChunkedArray.
Changes:
- Update
pa.array()to support extension wrapping when the intermediate result is aChunkedArray. - Add regression tests covering list offset overflow behavior for an extension type with
list_storage, plus a non-overflow sanity test.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 5 comments.
| File | Description |
|---|---|
| python/pyarrow/array.pxi | Wrap each chunk with ExtensionArray.from_storage() when conversion yields a ChunkedArray, then reassemble as a chunked extension array. |
| python/pyarrow/tests/test_extension_type.py | Add new extension type + tests for overflow-to-chunked behavior and a non-overflow case. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+2148
to
+2199
| try: | ||
| pa.register_extension_type(ListExtensionType()) | ||
| except pa.ArrowKeyError: | ||
| pass | ||
|
|
||
| schema = pa.schema({"col": ListExtensionType()}) | ||
|
|
||
| # Create data that exceeds int32 max cumulative values | ||
| # 5 rows × 500M values = 2.5B > int32 max (2,147,483,647) | ||
| arr = np.zeros(500_000_000, dtype=np.uint8) | ||
| rows = [{"col": {"data": arr}} for _ in range(5)] | ||
|
|
||
| result = pa.Table.from_pylist(rows, schema=schema) | ||
|
|
||
| assert result.num_rows == 5 | ||
| assert result.num_columns == 1 | ||
| assert result.schema[0].type == ListExtensionType() | ||
|
|
||
| col = result.column(0) | ||
| assert isinstance(col, pa.ChunkedArray) | ||
| assert col.type == ListExtensionType() | ||
|
|
||
| for chunk_idx in range(col.num_chunks): | ||
| chunk_data = col.chunk(chunk_idx) | ||
| assert chunk_data.type == ListExtensionType() | ||
|
|
||
|
|
||
| @pytest.mark.numpy | ||
| def test_extension_type_no_overflow(): | ||
| """Test that extension types work normally when there's no overflow.""" | ||
| try: | ||
| pa.register_extension_type(ListExtensionType()) | ||
| except pa.ArrowKeyError: | ||
| # Already registered | ||
| pass | ||
|
|
||
| schema = pa.schema({"col": ListExtensionType()}) | ||
|
|
||
| # Small data that won't overflow | ||
| arr = np.array([1, 2, 3], dtype=np.uint8) | ||
| rows = [{"col": {"data": arr}} for _ in range(3)] | ||
|
|
||
| result = pa.Table.from_pylist(rows, schema=schema) | ||
|
|
||
| assert result.num_rows == 3 | ||
| assert result.num_columns == 1 | ||
| assert result.schema[0].type == ListExtensionType() | ||
|
|
||
| # The column should be a ChunkedArray with a single chunk | ||
| col = result.column(0) | ||
| assert isinstance(col, pa.ChunkedArray) | ||
| assert col.type == ListExtensionType() |
Comment on lines
+2148
to
+2199
| try: | ||
| pa.register_extension_type(ListExtensionType()) | ||
| except pa.ArrowKeyError: | ||
| pass | ||
|
|
||
| schema = pa.schema({"col": ListExtensionType()}) | ||
|
|
||
| # Create data that exceeds int32 max cumulative values | ||
| # 5 rows × 500M values = 2.5B > int32 max (2,147,483,647) | ||
| arr = np.zeros(500_000_000, dtype=np.uint8) | ||
| rows = [{"col": {"data": arr}} for _ in range(5)] | ||
|
|
||
| result = pa.Table.from_pylist(rows, schema=schema) | ||
|
|
||
| assert result.num_rows == 5 | ||
| assert result.num_columns == 1 | ||
| assert result.schema[0].type == ListExtensionType() | ||
|
|
||
| col = result.column(0) | ||
| assert isinstance(col, pa.ChunkedArray) | ||
| assert col.type == ListExtensionType() | ||
|
|
||
| for chunk_idx in range(col.num_chunks): | ||
| chunk_data = col.chunk(chunk_idx) | ||
| assert chunk_data.type == ListExtensionType() | ||
|
|
||
|
|
||
| @pytest.mark.numpy | ||
| def test_extension_type_no_overflow(): | ||
| """Test that extension types work normally when there's no overflow.""" | ||
| try: | ||
| pa.register_extension_type(ListExtensionType()) | ||
| except pa.ArrowKeyError: | ||
| # Already registered | ||
| pass | ||
|
|
||
| schema = pa.schema({"col": ListExtensionType()}) | ||
|
|
||
| # Small data that won't overflow | ||
| arr = np.array([1, 2, 3], dtype=np.uint8) | ||
| rows = [{"col": {"data": arr}} for _ in range(3)] | ||
|
|
||
| result = pa.Table.from_pylist(rows, schema=schema) | ||
|
|
||
| assert result.num_rows == 3 | ||
| assert result.num_columns == 1 | ||
| assert result.schema[0].type == ListExtensionType() | ||
|
|
||
| # The column should be a ChunkedArray with a single chunk | ||
| col = result.column(0) | ||
| assert isinstance(col, pa.ChunkedArray) | ||
| assert col.type == ListExtensionType() |
Comment on lines
+2142
to
+2158
| @pytest.mark.large_memory | ||
| @pytest.mark.numpy | ||
| def test_extension_type_list_overflow(): | ||
| """ | ||
| Test that extension types with list fields handle int32 offset overflow. | ||
| """ | ||
| try: | ||
| pa.register_extension_type(ListExtensionType()) | ||
| except pa.ArrowKeyError: | ||
| pass | ||
|
|
||
| schema = pa.schema({"col": ListExtensionType()}) | ||
|
|
||
| # Create data that exceeds int32 max cumulative values | ||
| # 5 rows × 500M values = 2.5B > int32 max (2,147,483,647) | ||
| arr = np.zeros(500_000_000, dtype=np.uint8) | ||
| rows = [{"col": {"data": arr}} for _ in range(5)] |
Comment on lines
+2166
to
+2172
| col = result.column(0) | ||
| assert isinstance(col, pa.ChunkedArray) | ||
| assert col.type == ListExtensionType() | ||
|
|
||
| for chunk_idx in range(col.num_chunks): | ||
| chunk_data = col.chunk(chunk_idx) | ||
| assert chunk_data.type == ListExtensionType() |
|
|
||
| # The column should be a ChunkedArray with a single chunk | ||
| col = result.column(0) | ||
| assert isinstance(col, pa.ChunkedArray) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Rationale for this change
When data exceeds int32 limits, properly wraps each chunk as ExtensionArray
What changes are included in this PR?
Modified extension type handling to support both Array and ChunkedArray storage types.
Are these changes tested?
Yes , Manually tested the changes
Are there any user-facing changes?
No
This PR contains a "Critical Fix".
This change fixes a crash in list_ storage . when list data exceeds int32 limits, PyArrow automatically creates a ChunkedArray. However, ExtensionArray.from_storage() only accepts Array objects, not ChunkedArray.