Repository navigation
Fix replaceRegexpAll replacing only the first match when the pattern starts with an optional ^ - #126111
Merged
alexey-milovidov merged 4 commits intoOct 16, 2026
Conversation
…fied
RegexpFunctionRewritePass turns `replaceRegexpAll` (and `regexp_replace`)
into `replaceRegexpOne` when the constant pattern starts with `^`, on the
assumption that such a pattern can only match at offset 0. It decided that
from the first byte alone. A quantifier that allows zero repetitions (`^?`,
`^*`, `^{0,n}` and their lazy forms) makes the anchor optional, so the
pattern matches at any offset and every match after the first one was
dropped: `replaceRegexpAll('xfx', '^?x', 'Z')` returned `Zfx` instead of
`ZfZ`. The same happens when the quantifier binds to the `^` through an
empty `\Q\E` or an inline flag group, as in `^\Q\E?x` and `^(?i)?x`.
Decide the caret side with the tokenizer that already decides the trailing
`$` side: `RegexpAtom` gets an `is_caret` flag, set for a bare `^` and
cleared by any quantifier exactly like `is_dollar`, and the tokenizer's
existing give-up on a quantifier after a no-element construct covers the
binding cases. A pattern the tokenizer cannot analyze now declines the
rewrite, which only costs the optimization; the ClickBench pattern
`^https?://(?:www\.)?([^/]+)/.*$` keeps it.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
…t least once
A `^` repeated at least once (`^+`, `^{1,3}`) still anchors the pattern at offset 0,
and re2 does not mark such a program as start-anchored, so declining the rewrite costs
a full rescan per row. Only a quantifier that allows zero repetitions now declines.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
The comments now say that only a leading `^` whose quantifier allows zero repetitions is optional. The test pins `enable_analyzer` so it also runs in the old-analyzer jobs of release branches. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Drop the example line from the startsWithCaretAnchor comment; the call site already says when a quantified caret stops anchoring. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Collaborator
Author
Internal second-model review (rounds 1-3): 1 major and 3 nits agreed and fixed, 1 major declined with measurement; round 3 found nothing
|
Collaborator
Author
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-slot-41:20261015-184400 |
Contributor
|
Workflow [PR], commit [a338323] Summary: ✅
AI ReviewSummaryThe PR prevents Missing context / blind spots
Final VerdictStatus: ✅ Approve. No code changes requested. |
Collaborator
Author
|
cc @yakov-olkhovskiy @vdimir, could you review this? |
Merged
via the queue into
ClickHouse:master
with commit Oct 16, 2026
c01c33a
525 of 529 checks passed
clickhouse-gh Bot
added a commit
that referenced
this pull request
Nov 3, 2026
Backport #126111 to 26.10: Fix `replaceRegexpAll` replacing only the first match when the pattern starts with an optional `^`
leshikus
pushed a commit
that referenced
this pull request
Nov 13, 2026
…first match when the pattern starts with an optional `^`
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.
Closes: #121494
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixed
replaceRegexpAllandregexp_replacereplacing only the first match when the pattern starts with an optional^, such as^?,^*or^{0,3}:replaceRegexpAll('xfx', '^?x', 'Z')returnedZfxinstead ofZfZ. Closes #121494.Description
What breaks. On 26.8.18.2 and 26.3.42.3,
replaceRegexpAll(materialize('xfx'), '^?x', 'Z')returnsZfxinstead ofZfZ. Withoptimize_rewrite_regexp_functions = 0it is correct.Root cause.
RegexpFunctionRewritePassturnsreplaceRegexpAllintoreplaceRegexpOnewhen the pattern starts with^, because then it can only match at offset 0. It checked only the first byte. A quantifier that allows zero repetitions makes the^optional, so the pattern matches anywhere. The quantifier can also bind to the^through an empty\Q\Eor a flag group (^\Q\E?x,^(?i)?x).Change. The
^side now uses the tokenizer that already decides the trailing$side: the leading^counts as an anchor unless its quantifier allows zero repetitions (?,*,{0,n}). The tokenizer already gives up on a quantifier after\Q\Eor a flag group, so those are declined too. The ClickBench pattern^https?://(?:www\.)?([^/]+)/.*$keeps the rewrite.Also declined now, with unchanged results: patterns the tokenizer does not analyze, such as a literal
{after^(no measurable cost).Validation. New test
05401_replace_regexp_all_quantified_caretfails on master and passes with the fix, 50/50 randomized runs. Over 36^-led patterns the results with the rewrite on now equal the results with it off, and the existing regexp-rewrite tests pass.Workflow [PR]
Sync PR [sync-upstream/pr/126111]
Version info
26.11.1.28-master(included in26.11and later)