Repository navigation
GH-39149: [Python] Validate max_replacements in ReplaceSubstringOptions - #53774
Closed
Asthenia0412 wants to merge 1 commit into
Closed
Asthenia0412 wants to merge 1 commit into
Asthenia0412 wants to merge 1 commit into
Conversation
…gOptions Add a ValueError when max_replacements is less than -1 (other than the special value -1 which means unlimited), preventing an indefinite hang in the underlying C++ replace_substring_regex kernel. Also update the docstring to clarify the valid value range and add a regression test.
|
Thanks for opening a pull request! This pull request has been automatically closed because you currently have 4 open pull requests, which is more than the limit of 3. Due to the increase in pull requests opened by AI bots, and in order to keep the review queue manageable, Apache Arrow limits contributors without repository access to at most 3 concurrently open pull requests. This helps make sure each pull request gets the attention it needs and that work in progress does not go stale. Once one of your other open pull requests has been merged or closed, you are welcome to reopen this one. See also: |
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.
Background
Issue #39149 reports that
pc.replace_substring_regex()(and related APIs) hangindefinitely when passed a negative
max_replacementsvalue (other than -1 whichmeans unlimited). The root cause is that the C++ kernel decrements
max_replacementsuntil it reaches 0, but a value like -3 becomes -4, -5, ...forever.
The original fix attempt (PR #39264) targeted the C++ side but was closed before
merge. Adding validation on the Python side is a simpler, more defensive approach:
catch invalid input early, before it reaches the C++ layer.
Problem
ReplaceSubstringOptions.__init__accepted any integer formax_replacements,including values < -1. The sentinel value -1 means unlimited replacements, but
-2, -3, etc. are nonsensical and trigger an infinite loop in the C++ kernel.
Solution
Add a guard in
ReplaceSubstringOptions.__init__that raisesValueErrorwhenmax_replacements < -1. This provides immediate, clear feedback to the callerrather than hanging.
Also update the docstring to document the valid range explicitly (must be >= -1,
where -1 and None both mean unlimited).
Testing
Added
test_replace_substring_negative_max_replacementsinpython/pyarrow/tests/test_compute.pythat verifies:max_replacements=-2andmax_replacements=-3raiseValueErrormax_replacements=-1and omitted (None) are still accepted as unlimitedThe test covers both the rejection path and the unchanged acceptance paths.
Fixes #39149