Skip to content

[C++][Python] split_pattern_regex hangs forever on a pattern that matches the empty string #55842

Backlog

Description

@arunsahabytes

Describe the bug, including details regarding any error messages, version, and platform.

pc.split_pattern_regex never returns when the pattern can match an empty string, e.g. "" or "a*". It loops forever and memory keeps growing.

import pyarrow as pa
import pyarrow.compute as pc

pc.split_pattern_regex(pa.array(["abc"]), pattern="")    # hangs
pc.split_pattern_regex(pa.array(["bbb"]), pattern="a*")  # hangs

With max_splits set, it returns, but shows the position never moves forward:

>>> pc.split_pattern_regex(pa.array(["abc"]), pattern="", max_splits=2)
[["", "", "abc"]]

Binary input (pa.array([b"abc"]), pattern="b*") hangs too.

Cause: SplitRegexFinder::Find in cpp/src/arrow/compute/kernels/scalar_string_ascii.cc can return an empty match at the current position. The split loop then continues from the end of that match, which is the same position, so it matches the same empty spot again forever.

For comparison, split_pattern with pattern="" raises ArrowInvalid: Empty separator, replace_substring_regex handles "a*" fine, and Python's re.split("", "abc") gives ['', 'a', 'b', 'c', ''].

Possible fixes: after an empty match, advance by one character (like Python's re.split), or raise an error like split_pattern does. I'm happy to send a PR for whichever is preferred.

Tested on pyarrow 26.0.1 (latest), Windows 11, Python 3.12. The same code is on main.

Component(s)

C++

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions