Skip to content

GH-50012: [Python] Fix list_ storage crashes when values exceed int32 offsets#50016

Open
AnkitAhlawat7742 wants to merge 2 commits into
apache:mainfrom
AnkitAhlawat7742:fix_list_storage_crashes
Open

GH-50012: [Python] Fix list_ storage crashes when values exceed int32 offsets#50016
AnkitAhlawat7742 wants to merge 2 commits into
apache:mainfrom
AnkitAhlawat7742:fix_list_storage_crashes

Conversation

@AnkitAhlawat7742
Copy link
Copy Markdown
Contributor

@AnkitAhlawat7742 AnkitAhlawat7742 commented May 22, 2026

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.

@github-actions
Copy link
Copy Markdown

⚠️ GitHub issue #50012 has been automatically assigned in GitHub to PR creator.

@AnkitAhlawat7742
Copy link
Copy Markdown
Contributor Author

Hi @AlenkaF ,
Please review this PR

@kou kou changed the title GH-50012[Python]: Fix list_ storage crashes when values exceed int32 offsets GH-50012: [Python] Fix list_ storage crashes when values exceed int32 offsets May 25, 2026
@kou kou requested a review from Copilot May 25, 2026 20:53
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 a ChunkedArray.
  • 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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants