Skip to content

handle dynamic pattern columns in str.replace and str.replace_all#26874

Closed
moktamd wants to merge 2 commits intopola-rs:mainfrom
moktamd:fix/dynamic-str-replace-pattern
Closed

handle dynamic pattern columns in str.replace and str.replace_all#26874
moktamd wants to merge 2 commits intopola-rs:mainfrom
moktamd:fix/dynamic-str-replace-pattern

Conversation

@moktamd
Copy link

@moktamd moktamd commented Mar 10, 2026

Fixes #26789

When both pattern and value are column expressions (not scalar), str.replace and str.replace_all panic with dynamic pattern length in 'str.replace' expressions is not supported yet. Reproduces consistently with POLARS_MAX_THREADS=1 because single-threaded execution preserves multi-chunk layouts that the multi-threaded path rechunks into a single chunk.

The (1, len_val) arm handles scalar pattern + dynamic values, but there was no arm for (len_pat, len_val) where both are dynamic. Added element-wise iteration over source, pattern, and value columns for both replace_n and replace_all, compiling regex per-row via the existing cache.

Added test_replace_dynamic_pattern_column covering literal replacement, replace vs replace_all with repeated matches, regex patterns from columns, and a single-row regression guard.

when both pattern and value are column expressions rather than scalars,
the replace functions panicked with "dynamic pattern length not
supported". this happened regularly with POLARS_MAX_THREADS=1 because
single-threaded execution preserves multi-chunk column layouts that
get rechunked in the multi-threaded path.

added element-wise iteration over pattern, value, and source columns
for the (len_pat, len_val) match arm in both replace_n and replace_all,
compiling the regex per-row.
@Kevin-Patyk
Copy link
Contributor

@moktamd As a note, there is a recent, open PR for this same issue: #26807

@moktamd
Copy link
Author

moktamd commented Mar 10, 2026

Oh, missed that one. Closing in favor of #26807.

@moktamd moktamd closed this Mar 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Dynamic string replacement fails when POLARS_MAX_THREADS=1

2 participants