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

Conversation

phofl
Copy link
Member

@phofl phofl commented Mar 19, 2023

@phofl phofl added the Arrow pyarrow functionality label Mar 19, 2023
@phofl phofl added this to the 2.0 milestone Mar 19, 2023
def test_setitem_boolean_replace_with_mask_segfault():
# GH#52059
N = 145_000
arr = ArrowExtensionArray(pa.chunked_array([np.array([True] * N)]))
Copy link
Member

Choose a reason for hiding this comment

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

nitpick np.ones(N, dtype=bool) is about 1200x faster

Copy link
Member Author

Choose a reason for hiding this comment

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

changed

@@ -1634,6 +1634,9 @@ 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):
Copy link
Member

Choose a reason for hiding this comment

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

Could you add the pyarrow issue link here too?

(mentioned in #52059 (comment))

Copy link
Member Author

Choose a reason for hiding this comment

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

added

@@ -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):
Copy link
Member

Choose a reason for hiding this comment

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

You could limit it to combine the chunks only when the values have boolean type

Copy link
Member Author

Choose a reason for hiding this comment

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

done

Comment on lines +2375 to +2376
N = 145_000
arr = ArrowExtensionArray(pa.chunked_array([np.ones((N,), dtype=np.bool_)]))
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?

Copy link
Member

@mroeschke mroeschke left a comment

Choose a reason for hiding this comment

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

LGTM (I think the larger test size is okay, but would be nice to shrink in the future)

@mroeschke mroeschke merged commit 10000db into pandas-dev:main Mar 27, 2023
@lumberbot-app
Copy link

lumberbot-app bot commented Mar 27, 2023

Owee, I'm MrMeeseeks, Look at me.

There seem to be a conflict, please backport manually. Here are approximate instructions:

  1. Checkout backport branch and update it.
git checkout 2.0.x
git pull
  1. Cherry pick the first parent branch of the this PR on top of the older branch:
git cherry-pick -x -m1 10000db023208c1db0bba6a7d819bfe87dc49908
  1. You will likely have some merge/cherry-pick conflict here, fix them and commit:
git commit -am 'Backport PR #52075: BUG: Arrow setitem segfaults when len > 145 000'
  1. Push to a named branch:
git push YOURFORK 2.0.x:auto-backport-of-pr-52075-on-2.0.x
  1. Create a PR against branch 2.0.x, I would have named this PR:

"Backport PR #52075 on branch 2.0.x (BUG: Arrow setitem segfaults when len > 145 000)"

And apply the correct labels and milestones.

Congratulations — you did some good work! Hopefully your backport PR will be tested by the continuous integration and merged soon!

Remember to remove the Still Needs Manual Backport label once the PR gets merged.

If these instructions are inaccurate, feel free to suggest an improvement.

@mroeschke
Copy link
Member

Thanks @phofl

mroeschke pushed a commit to mroeschke/pandas that referenced this pull request Mar 28, 2023
* BUG: Arrow setitem segfaults when len > 145 000

* Add gh ref

* Address review

* Restrict to bool type

(cherry picked from commit 10000db)
mroeschke added a commit that referenced this pull request Mar 28, 2023
… len > 145 000) (#52259)

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

* BUG: Arrow setitem segfaults when len > 145 000

* Add gh ref

* Address review

* Restrict to bool type

(cherry picked from commit 10000db)

* _data

---------

Co-authored-by: Patrick Hoefler <[email protected]>
@phofl phofl deleted the 52059 branch March 29, 2023 14:01
@phofl
Copy link
Member Author

phofl commented Mar 29, 2023

thx for doing the backport

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Arrow pyarrow functionality
Projects
None yet
Development

Successfully merging this pull request may close these issues.

BUG: python crashes on filtering with .loc on boolean Series with dtype_backend=pyarrow on some dataframes.
5 participants