Skip to content

fix(function): expand regexp replacement captures - #28794

Merged
XuPeng-SH merged 8 commits into
mainfrom
codex/issue-28305-regexp-capture
Sep 13, 2026
Merged

XuPeng-SH merged 8 commits into
mainfrom
codex/issue-28305-regexp-capture

Conversation

@XuPeng-SH

@XuPeng-SH XuPeng-SH commented Sep 12, 2026 •

Copy link
Copy Markdown
Contributor

What type of PR is this?

  • BUG
  • API-change
  • Improvement
  • Documentation
  • Feature
  • Test and CI
  • Code Refactoring

Which issue(s) this PR fixes:

Partial follow-up for #28305 — capture replacement only. This PR targets main. It depends on the regexp match-type positioning support in #28787, so the current PR diff includes #28787's prerequisite commits as well. Once #28787 merges, rebase this branch onto updated main to remove those prerequisite commits from this PR's diff.

What this PR does / why we need it:

REGEXP_REPLACE currently uses literal-replacement APIs and directly appends the replacement for occurrence-specific calls. Thus MySQL/ICU capture references such as $2$1 are returned literally rather than expanded (REGEXP_REPLACE('ab', '(a)(b)', '$2$1') should return ba).

This change adds a bounded replacement-template parser for whole-match, numeric and supported named capture references, and backslash quoting. It preserves unmatched captures as empty and defers template errors until a match is actually selected, so a no-match result remains the original subject. The position-aware matcher exposes captures from the original pattern, and binary match offsets are mapped back to source bytes before captured content is appended.

Ordinary replacements without dollar-sign or backslash markers retain the existing optimized literal path. Template execution keeps only the current match, does not cache row-specific templates, checks the final byte length against the engine's 64 MiB blob limit, and returns an error instead of truncating oversized output. Large conservative bounds use a size-only pass before allocating; the streaming path allocates only after its first match and starts with a bounded capacity.

Scope and known limitations

This is intentionally only the capture-replacement part of #28305. It does not add ICU-only pattern constructs (lookaround, pattern backreferences, atomic groups, possessive quantifiers, or grapheme matching), does not replace RE2, and does not claim complete ICU replacement-template parity. Unicode-digit references and ICU-specific Unicode escape forms remain out of scope pending a pinned-version compatibility decision. #28305 should remain open until those pattern-dialect gaps are resolved or explicitly split/closed by the issue owner.

The runtime result limit is 64 MiB, matching MatrixOne's current maximum blob size. Results are never silently truncated. This PR does not alter the declared return-type formula.

Validation and merge gates

  • Added focused UT coverage for group swapping, whole match, greedy numeric references and fallback, leading zeroes, unmatched and named groups, escaping, invalid/lazy templates, occurrence/position/overlap/anchor/zero-width cases, binary NUL/invalid UTF-8 captures, binary case folding, and output-size boundaries.
  • Added quote-boundary regressions for trailing open \Q, escaped backslashes, the first \E after a quoted literal backslash, and noninitial-position REGEXP_INSTR/REGEXP_SUBSTR/REGEXP_REPLACE in text and binary modes.
  • Extended the existing func_regular_replace BVT with SQL-visible capture expansion, quote-boundary and position behavior, BINARY/VARBINARY byte preservation, and prepared replacement rebinding.
  • go test ./pkg/sql/plan/function -run '^Test_BuiltIn_Regexp' -count=1 passes on the current head; git diff --check passes.
  • golangci-lint run --timeout=10m ./pkg/sql/plan/function passed on the same PR diff before the latest base-only rebase; the diff fingerprint is unchanged.
  • The local distributed BVT and the full function-package UT were not run. Require exact-head full package UT, the func_regular_replace BVT, and incremental SCA before merge; no CI result is claimed here.

@qodo-code-review

Copy link
Copy Markdown

Qodo reviews are paused for this user.

Troubleshooting steps vary by plan Learn more →

On a Teams plan?
Reviews resume once this user has a paid seat and their Git account is linked in Qodo.
Link Git account →

Using GitHub Enterprise Server, GitLab Self-Managed, or Bitbucket Data Center?
These require an Enterprise plan - Contact us
Contact us →

@mergify

mergify Bot commented Sep 12, 2026

Copy link
Copy Markdown
Contributor

Tick the box to add this pull request to the merge queue (same as @mergifyio queue).

  • Queue this pull request

@XuPeng-SH XuPeng-SH left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed the complete five-file diff at exact head cd95ac06baa7b265571f2d3599553f8d089b3474. I found one correctness issue to fix before merge: the position-aware capture matcher appends the user pattern inside a wrapper that closes with ). A valid RE2 pattern ending in an unterminated \\Q quotes the rest of the expression, including that wrapper close. With all-occurrence replacement, the first match at offset 0 uses the original compiled regexp; the next search wraps/recompiles and fails with a missing ) after already finding a valid match. Repro: REGEXP_REPLACE("aa", "\\Qa", "[$0]") should replace both literal a matches, but the second search errors. Please make wrapper construction safe for a trailing quoted region (or avoid appending syntax into the user pattern) and add an all-occurrence capture-template regression. Also, the current head reports no executable UT/BVT/SCA checks—only summary/Mergify metadata and skipped Iceberg—and the PR body confirms those tests were not run; please get the stated exact-head validation before merge. This is a COMMENT only because this is my PR.

@XuPeng-SH
XuPeng-SH force-pushed the codex/issues-28304-28305-regexp branch from b2b2125 to 1f6bfb9 Compare September 12, 2026 16:23
@XuPeng-SH
XuPeng-SH force-pushed the codex/issue-28305-regexp-capture branch from 25d84e4 to a46dedc Compare September 12, 2026 16:24
@XuPeng-SH
XuPeng-SH force-pushed the codex/issues-28304-28305-regexp branch from 1f6bfb9 to 5860867 Compare September 12, 2026 16:31
@XuPeng-SH
XuPeng-SH force-pushed the codex/issue-28305-regexp-capture branch from a46dedc to de39ab3 Compare September 12, 2026 16:31
@XuPeng-SH
XuPeng-SH changed the base branch from codex/issues-28304-28305-regexp to main September 12, 2026 16:53

@XuPeng-SH XuPeng-SH left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed exact head de39ab3d2b2f1a4e9e58f8f4ddd6d35fc4aaf1c9. I found no code correctness blocker in the capture replacement parser, quoting boundary handling, byte-offset mapping, or output-size limit. The stated merge gates remain outstanding: exact-head full function-package UT, func_regular_replace BVT, and incremental SCA. Current checks show only Mergify bookkeeping and skipped Iceberg, without executable UT/BVT/SCA results. The PR also includes prerequisite commits from #28787, which is still open; the PR description says to rebase after it merges. COMMENT only because this is my PR.

@XuPeng-SH XuPeng-SH left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed the complete range cbe4daaff608..af91b54af5b6 at the current head. The root cause is confirmed: the original replacement path treats the replacement as literal text (ReplaceAllLiteralString / direct append), so capture references remain unexpanded. The new parser expands whole, numbered, and named captures only after a match is selected, while the binary path retains source-byte spans and output-size checks. The earlier trailing-\\Q wrapper issue is closed by terminating the quote before generated wrapper syntax; focused UT/BVT cases cover all-occurrence and position-aware matching. Exact-head CI is green for Ubuntu UT, arm64 SCA, enabled standalone/compose BVT, coverage, and shared build. No additional actionable blocker found. Scope note: this PR includes the documented #28787 prerequisite; rebase after that prerequisite merges.

@XuPeng-SH
XuPeng-SH merged commit d2a35c2 into main Sep 13, 2026
22 of 26 checks passed
@XuPeng-SH
XuPeng-SH deleted the codex/issue-28305-regexp-capture branch September 13, 2026 13:52
VioletQwQ-0 pushed a commit to VioletQwQ-0/matrixone that referenced this pull request Sep 17, 2026
## What type of PR is this?

- [x] BUG
- [ ] API-change
- [ ] Improvement
- [ ] Documentation
- [ ] Feature
- [ ] Test and CI
- [ ] Code Refactoring

## Which issue(s) this PR fixes:

Partial follow-up for matrixorigin#28305 — capture replacement only. This PR targets
`main`. It depends on the regexp match-type positioning support in
matrixorigin#28787, so the current PR diff includes matrixorigin#28787's prerequisite commits as
well. Once matrixorigin#28787 merges, rebase this branch onto updated `main` to
remove those prerequisite commits from this PR's diff.

## What this PR does / why we need it:

REGEXP_REPLACE currently uses literal-replacement APIs and directly
appends the replacement for occurrence-specific calls. Thus MySQL/ICU
capture references such as $2$1 are returned literally rather than
expanded (`REGEXP_REPLACE('ab', '(a)(b)', '$2$1')` should return `ba`).

This change adds a bounded replacement-template parser for whole-match,
numeric and supported named capture references, and backslash quoting.
It preserves unmatched captures as empty and defers template errors
until a match is actually selected, so a no-match result remains the
original subject. The position-aware matcher exposes captures from the
original pattern, and binary match offsets are mapped back to source
bytes before captured content is appended.

Ordinary replacements without dollar-sign or backslash markers retain
the existing optimized literal path. Template execution keeps only the
current match, does not cache row-specific templates, checks the final
byte length against the engine's 64 MiB blob limit, and returns an error
instead of truncating oversized output. Large conservative bounds use a
size-only pass before allocating; the streaming path allocates only
after its first match and starts with a bounded capacity.

## Scope and known limitations

This is intentionally only the capture-replacement part of matrixorigin#28305. It
does not add ICU-only pattern constructs (lookaround, pattern
backreferences, atomic groups, possessive quantifiers, or grapheme
matching), does not replace RE2, and does not claim complete ICU
replacement-template parity. Unicode-digit references and ICU-specific
Unicode escape forms remain out of scope pending a pinned-version
compatibility decision. matrixorigin#28305 should remain open until those
pattern-dialect gaps are resolved or explicitly split/closed by the
issue owner.

The runtime result limit is 64 MiB, matching MatrixOne's current maximum
blob size. Results are never silently truncated. This PR does not alter
the declared return-type formula.

## Validation and merge gates

- Added focused UT coverage for group swapping, whole match, greedy
numeric references and fallback, leading zeroes, unmatched and named
groups, escaping, invalid/lazy templates,
occurrence/position/overlap/anchor/zero-width cases, binary NUL/invalid
UTF-8 captures, binary case folding, and output-size boundaries.
- Added quote-boundary regressions for trailing open `\Q`, escaped
backslashes, the first `\E` after a quoted literal backslash, and
noninitial-position `REGEXP_INSTR`/`REGEXP_SUBSTR`/`REGEXP_REPLACE` in
text and binary modes.
- Extended the existing func_regular_replace BVT with SQL-visible
capture expansion, quote-boundary and position behavior,
BINARY/VARBINARY byte preservation, and prepared replacement rebinding.
- `go test ./pkg/sql/plan/function -run '^Test_BuiltIn_Regexp' -count=1`
passes on the current head; `git diff --check` passes.
- `golangci-lint run --timeout=10m ./pkg/sql/plan/function` passed on
the same PR diff before the latest base-only rebase; the diff
fingerprint is unchanged.
- The local distributed BVT and the full function-package UT were not
run. Require exact-head full package UT, the func_regular_replace BVT,
and incremental SCA before merge; no CI result is claimed here.
VioletQwQ-0 pushed a commit to VioletQwQ-0/matrixone that referenced this pull request Sep 17, 2026
## What type of PR is this?

- [x] BUG
- [ ] API-change
- [ ] Improvement
- [ ] Documentation
- [ ] Feature
- [ ] Test and CI
- [ ] Code Refactoring

## Which issue(s) this PR fixes:

Partial follow-up for matrixorigin#28305 — capture replacement only. This PR targets
`main`. It depends on the regexp match-type positioning support in
matrixorigin#28787, so the current PR diff includes matrixorigin#28787's prerequisite commits as
well. Once matrixorigin#28787 merges, rebase this branch onto updated `main` to
remove those prerequisite commits from this PR's diff.

## What this PR does / why we need it:

REGEXP_REPLACE currently uses literal-replacement APIs and directly
appends the replacement for occurrence-specific calls. Thus MySQL/ICU
capture references such as $2$1 are returned literally rather than
expanded (`REGEXP_REPLACE('ab', '(a)(b)', '$2$1')` should return `ba`).

This change adds a bounded replacement-template parser for whole-match,
numeric and supported named capture references, and backslash quoting.
It preserves unmatched captures as empty and defers template errors
until a match is actually selected, so a no-match result remains the
original subject. The position-aware matcher exposes captures from the
original pattern, and binary match offsets are mapped back to source
bytes before captured content is appended.

Ordinary replacements without dollar-sign or backslash markers retain
the existing optimized literal path. Template execution keeps only the
current match, does not cache row-specific templates, checks the final
byte length against the engine's 64 MiB blob limit, and returns an error
instead of truncating oversized output. Large conservative bounds use a
size-only pass before allocating; the streaming path allocates only
after its first match and starts with a bounded capacity.

## Scope and known limitations

This is intentionally only the capture-replacement part of matrixorigin#28305. It
does not add ICU-only pattern constructs (lookaround, pattern
backreferences, atomic groups, possessive quantifiers, or grapheme
matching), does not replace RE2, and does not claim complete ICU
replacement-template parity. Unicode-digit references and ICU-specific
Unicode escape forms remain out of scope pending a pinned-version
compatibility decision. matrixorigin#28305 should remain open until those
pattern-dialect gaps are resolved or explicitly split/closed by the
issue owner.

The runtime result limit is 64 MiB, matching MatrixOne's current maximum
blob size. Results are never silently truncated. This PR does not alter
the declared return-type formula.

## Validation and merge gates

- Added focused UT coverage for group swapping, whole match, greedy
numeric references and fallback, leading zeroes, unmatched and named
groups, escaping, invalid/lazy templates,
occurrence/position/overlap/anchor/zero-width cases, binary NUL/invalid
UTF-8 captures, binary case folding, and output-size boundaries.
- Added quote-boundary regressions for trailing open `\Q`, escaped
backslashes, the first `\E` after a quoted literal backslash, and
noninitial-position `REGEXP_INSTR`/`REGEXP_SUBSTR`/`REGEXP_REPLACE` in
text and binary modes.
- Extended the existing func_regular_replace BVT with SQL-visible
capture expansion, quote-boundary and position behavior,
BINARY/VARBINARY byte preservation, and prepared replacement rebinding.
- `go test ./pkg/sql/plan/function -run '^Test_BuiltIn_Regexp' -count=1`
passes on the current head; `git diff --check` passes.
- `golangci-lint run --timeout=10m ./pkg/sql/plan/function` passed on
the same PR diff before the latest base-only rebase; the diff
fingerprint is unchanged.
- The local distributed BVT and the full function-package UT were not
run. Require exact-head full package UT, the func_regular_replace BVT,
and incremental SCA before merge; no CI result is claimed here.
VioletQwQ-0 pushed a commit to VioletQwQ-0/matrixone that referenced this pull request Sep 17, 2026
## What type of PR is this?

- [x] BUG
- [ ] API-change
- [ ] Improvement
- [ ] Documentation
- [ ] Feature
- [ ] Test and CI
- [ ] Code Refactoring

## Which issue(s) this PR fixes:

Partial follow-up for matrixorigin#28305 — capture replacement only. This PR targets
`main`. It depends on the regexp match-type positioning support in
matrixorigin#28787, so the current PR diff includes matrixorigin#28787's prerequisite commits as
well. Once matrixorigin#28787 merges, rebase this branch onto updated `main` to
remove those prerequisite commits from this PR's diff.

## What this PR does / why we need it:

REGEXP_REPLACE currently uses literal-replacement APIs and directly
appends the replacement for occurrence-specific calls. Thus MySQL/ICU
capture references such as $2$1 are returned literally rather than
expanded (`REGEXP_REPLACE('ab', '(a)(b)', '$2$1')` should return `ba`).

This change adds a bounded replacement-template parser for whole-match,
numeric and supported named capture references, and backslash quoting.
It preserves unmatched captures as empty and defers template errors
until a match is actually selected, so a no-match result remains the
original subject. The position-aware matcher exposes captures from the
original pattern, and binary match offsets are mapped back to source
bytes before captured content is appended.

Ordinary replacements without dollar-sign or backslash markers retain
the existing optimized literal path. Template execution keeps only the
current match, does not cache row-specific templates, checks the final
byte length against the engine's 64 MiB blob limit, and returns an error
instead of truncating oversized output. Large conservative bounds use a
size-only pass before allocating; the streaming path allocates only
after its first match and starts with a bounded capacity.

## Scope and known limitations

This is intentionally only the capture-replacement part of matrixorigin#28305. It
does not add ICU-only pattern constructs (lookaround, pattern
backreferences, atomic groups, possessive quantifiers, or grapheme
matching), does not replace RE2, and does not claim complete ICU
replacement-template parity. Unicode-digit references and ICU-specific
Unicode escape forms remain out of scope pending a pinned-version
compatibility decision. matrixorigin#28305 should remain open until those
pattern-dialect gaps are resolved or explicitly split/closed by the
issue owner.

The runtime result limit is 64 MiB, matching MatrixOne's current maximum
blob size. Results are never silently truncated. This PR does not alter
the declared return-type formula.

## Validation and merge gates

- Added focused UT coverage for group swapping, whole match, greedy
numeric references and fallback, leading zeroes, unmatched and named
groups, escaping, invalid/lazy templates,
occurrence/position/overlap/anchor/zero-width cases, binary NUL/invalid
UTF-8 captures, binary case folding, and output-size boundaries.
- Added quote-boundary regressions for trailing open `\Q`, escaped
backslashes, the first `\E` after a quoted literal backslash, and
noninitial-position `REGEXP_INSTR`/`REGEXP_SUBSTR`/`REGEXP_REPLACE` in
text and binary modes.
- Extended the existing func_regular_replace BVT with SQL-visible
capture expansion, quote-boundary and position behavior,
BINARY/VARBINARY byte preservation, and prepared replacement rebinding.
- `go test ./pkg/sql/plan/function -run '^Test_BuiltIn_Regexp' -count=1`
passes on the current head; `git diff --check` passes.
- `golangci-lint run --timeout=10m ./pkg/sql/plan/function` passed on
the same PR diff before the latest base-only rebase; the diff
fingerprint is unchanged.
- The local distributed BVT and the full function-package UT were not
run. Require exact-head full package UT, the func_regular_replace BVT,
and incremental SCA before merge; no CI result is claimed here.

This branch was previously deployed

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

Labels

kind/bug Something isn't working size/XL Denotes a PR that changes [1000, 1999] lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants