Skip to content

fix(function): support regexp match_type overloads - #28787

Closed
XuPeng-SH wants to merge 0 commit into
mainfrom
codex/issues-28304-28305-regexp
Closed

XuPeng-SH wants to merge 0 commit into
mainfrom
codex/issues-28304-28305-regexp

Conversation

@XuPeng-SH

Copy link
Copy Markdown
Contributor

What type of PR is this?

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

Which issue(s) this PR fixes:

Fixes #28304

What this PR does / why we need it:

Adds the missing MySQL-compatible match_type overloads for REGEXP_INSTR, REGEXP_SUBSTR, and REGEXP_REPLACE. The flags are applied to both initial and position-aware matching, including the iterator's wrapped-pattern compilation. Row-dependent modes are parsed per row; existing overload IDs and signatures remain unchanged.

The implementation preserves existing binary byte-position/result behavior. For explicit case-insensitive matching on binary operands, high bytes use the existing Windows-1252 facade.

Validation

  • gofmt -d and git diff --check: passed.
  • Incremental golangci-lint against base 46cf1d24a7d77e7be6060a17e4596da90743b59f: passed, 0 issues.
  • GPT-6 medium source review: no confirmed code blocker.
  • Focused UTs, the owning function package, and the four changed BVT cases were not run locally. The machine has 1.3 GiB free and this worktree lacks thirdparties/install and cgo/libmo.dylib; the controlled CGo test wrapper rejected the missing native-artifact provenance. No test pass is claimed.

Merge gate: run the affected UTs and owning package through the controlled CGo workflow, plus normal-comparison BVT for func_regular_like, func_regular_instr, func_regular_substr, and func_regular_replace. Do not merge unless these pass on the PR head.

Performance and scope

Existing call signatures keep their prior execution path. The new overloads add per-row flag parsing and reuse the bounded regexp cache. Explicit binary i matching on non-ASCII bytes performs linear conversion and temporary allocation; no benchmark was run.

This PR accepts u using Go RE2's existing LF-only line behavior; it does not implement the full MySQL ICU dialect. Capture-group replacement and the other ICU-only constructs in #28305 are intentionally out of scope. PR #28534 (#27217) separately touches the regex executor/domain area; verify the combined behavior before merge.

@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 added the kind/bug Something isn't working label Sep 12, 2026
@matrix-meow matrix-meow added the size/L Denotes a PR that changes [500,999] lines label Sep 12, 2026
@XuPeng-SH
XuPeng-SH force-pushed the codex/issues-28304-28305-regexp branch from 08bf9bf to b2b2125 Compare September 12, 2026 15:19
@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 added a commit that referenced this pull request Sep 13, 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 #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.
@XuPeng-SH XuPeng-SH closed this Sep 13, 2026
@XuPeng-SH
XuPeng-SH force-pushed the codex/issues-28304-28305-regexp branch from 00a222d to 4d18bee Compare September 13, 2026 18:10
@matrix-meow matrix-meow added size/XS Denotes a PR that changes [1, 9] lines and removed size/L Denotes a PR that changes [500,999] lines labels Sep 13, 2026
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/XS Denotes a PR that changes [1, 9] lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Compatibility]: REGEXP match_type support is incomplete across the function family

4 participants