Skip to content

BUG: Arrow setitem segfaults when len > 145 000 #52075

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Mar 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions pandas/core/arrays/arrow/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -1634,6 +1634,10 @@ def _replace_with_mask(
indices = pa.array(indices, type=pa.int64())
replacements = replacements.take(indices)
return cls._if_else(mask, replacements, values)
if isinstance(values, pa.ChunkedArray) and pa.types.is_boolean(values.type):
# GH#52059 replace_with_mask segfaults for chunked array
# https://github.com/apache/arrow/issues/34634
values = values.combine_chunks()
try:
return pc.replace_with_mask(values, mask, replacements)
except pa.ArrowNotImplementedError:
Expand Down
9 changes: 9 additions & 0 deletions pandas/tests/extension/test_arrow.py
Original file line number Diff line number Diff line change
Expand Up @@ -2368,3 +2368,12 @@ def test_pickle_old_arrowextensionarray():
tm.assert_extension_array_equal(result, expected)
assert result._pa_array == pa.chunked_array(data)
assert not hasattr(result, "_data")


def test_setitem_boolean_replace_with_mask_segfault():
# GH#52059
N = 145_000
arr = ArrowExtensionArray(pa.chunked_array([np.ones((N,), dtype=np.bool_)]))
Comment on lines +2375 to +2376
Copy link
Member

Choose a reason for hiding this comment

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

I don't think you need this > 145_000. Based on @lukemanley's report, this just happens for any (also tiny) chunked array.
(I suppose the larger number came from reading where depending on the size it was creating a chunked array or not?)

Copy link
Member Author

Choose a reason for hiding this comment

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

The test did not fail with a smaller size, this was confusing to me as well but could this only get to fail with arrow functionality only

Copy link
Member

@jorisvandenbossche jorisvandenbossche Mar 21, 2023

Choose a reason for hiding this comment

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

OK, that might be a different issue. But so this test is currently not strictly testing the replace_with_mask issue giving invalid return arrays, because it seems the == is not actually doing its work ... (another bug!)

So also with a N of 2, I see the invalid output, the == just passes:

In [14]: N = 2

In [15]: arr = pd.arrays.ArrowExtensionArray(pa.chunked_array([np.ones((N,), dtype=np.bool_)]))

In [16]: expected = arr.copy()

In [17]: arr[np.zeros((N,), dtype=np.bool_)] = False

In [18]: expected._data
Out[18]: 
<pyarrow.lib.ChunkedArray object at 0x7fa0f51e2de0>
[
  [
    true,
    true
  ]
]

In [19]: arr._data
Out[19]: 
<pyarrow.lib.ChunkedArray object at 0x7fa0f52e6390>
[
<Invalid array: Buffer #1 too small in array of type bool and length 2: expected at least 1 byte(s), got 0
/home/joris/scipy/repos/arrow/cpp/src/arrow/array/validate.cc:116  ValidateLayout(*data.type)>
]

In [20]: expected._data == arr._data
Out[20]: True

Copy link
Member Author

Choose a reason for hiding this comment

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

Using 145_000 causes a segfault on my machine without the change though, so this should be good enough as a test for now?

expected = arr.copy()
arr[np.zeros((N,), dtype=np.bool_)] = False
assert arr._pa_array == expected._pa_array