Repository navigation
fix(function): expand regexp replacement captures - #28794
Conversation
Qodo reviews are paused for this user.Troubleshooting steps vary by plan Learn more → On a Teams plan? Using GitHub Enterprise Server, GitLab Self-Managed, or Bitbucket Data Center? |
707472e to
cd95ac0
Compare
|
Tick the box to add this pull request to the merge queue (same as
|
XuPeng-SH
left a comment
There was a problem hiding this comment.
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.
b2b2125 to
1f6bfb9
Compare
25d84e4 to
a46dedc
Compare
1f6bfb9 to
5860867
Compare
a46dedc to
de39ab3
Compare
XuPeng-SH
left a comment
There was a problem hiding this comment.
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
left a comment
There was a problem hiding this comment.
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.
## 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.
## 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.
## 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.
What type of PR is this?
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 updatedmainto 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 returnba).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
\Q, escaped backslashes, the first\Eafter a quoted literal backslash, and noninitial-positionREGEXP_INSTR/REGEXP_SUBSTR/REGEXP_REPLACEin text and binary modes.go test ./pkg/sql/plan/function -run '^Test_BuiltIn_Regexp' -count=1passes on the current head;git diff --checkpasses.golangci-lint run --timeout=10m ./pkg/sql/plan/functionpassed on the same PR diff before the latest base-only rebase; the diff fingerprint is unchanged.