Skip to content

GH-39149: [Python] Validate max_replacements in ReplaceSubstringOptions - #53774

Closed
Asthenia0412 wants to merge 1 commit into
apache:mainfrom
Asthenia0412:fix/replace-substring-negative-max-replacements
Closed

Asthenia0412 wants to merge 1 commit into
apache:mainfrom
Asthenia0412:fix/replace-substring-negative-max-replacements

Conversation

@Asthenia0412

@Asthenia0412 Asthenia0412 commented Oct 14, 2026 •

Copy link
Copy Markdown

Background

Issue #39149 reports that pc.replace_substring_regex() (and related APIs) hang
indefinitely when passed a negative max_replacements value (other than -1 which
means unlimited). The root cause is that the C++ kernel decrements
max_replacements until 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 for max_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 raises ValueError when
max_replacements < -1. This provides immediate, clear feedback to the caller
rather 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_replacements in
python/pyarrow/tests/test_compute.py that verifies:

  • max_replacements=-2 and max_replacements=-3 raise ValueError
  • max_replacements=-1 and omitted (None) are still accepted as unlimited
  • Non-negative values (0, 5, etc.) continue to work as before

The test covers both the rejection path and the unchanged acceptance paths.

Fixes #39149

…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.
@github-actions

Copy link
Copy Markdown

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:

@github-actions github-actions Bot closed this Oct 14, 2026
@github-actions github-actions Bot added the awaiting review Awaiting review label Oct 14, 2026
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.

[C++][Python] replace_substring_regex hangs indefinitely for negative max_replacements

1 participant