Skip to content

fix(function): recognize MySQL line terminators in REGEXP multiline mode - #31125

Merged
mergify[bot] merged 3 commits into
matrixorigin:mainfrom
gouhongshen:agent/issue-30210-main-d3d5f770
Oct 25, 2026
Merged

mergify[bot] merged 3 commits into
matrixorigin:mainfrom
gouhongshen:agent/issue-30210-main-d3d5f770

Conversation

@gouhongshen

@gouhongshen gouhongshen commented Oct 25, 2026 •

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:

Related to #30210

What this PR does / why we need it:

Root cause

MySQL 8.0 multiline (m) mode treats LF, CR, CRLF, NEL (U+0085), LINE SEPARATOR (U+2028) and PARAGRAPH SEPARATOR (U+2029) as line terminators, so ^b matches after any of them. MatrixOne's REGEXP_LIKE / REGEXP_INSTR / REGEXP_SUBSTR / REGEXP_REPLACE recognized only LF and CRLF: SELECT REGEXP_LIKE(CONCAT('a', CONVERT(0x0D USING utf8mb4), 'b'), '^b', 'm') returned 0 where MySQL returns 1 (same mismatch for INSTR/SUBSTR/REPLACE, in both directions of position and replacement-byte semantics). The Unix-only u flag was and must remain LF-only.

Exact changes

  1. Vendored backtracking engine pkg/sql/plan/function/internal/regexp2: byte-pinned fork github.com/XuPeng-SH/regexp2@718f43e031fe (= dlclark/regexp2 v1.10.0 + fastclock patch, MIT; attribution and pre-rename SHA-256 inventory in LICENSE/ATTRIB/PROVENANCE). MatrixOne-owned additions: linemode.go/linemode_test.go (wide line-terminator classification, CRLF kept atomic, \n boundaries preserved for mu) and errors.go/syntax/errors.go (errString, required by the repository err-check gate). Upstream files carry line-policy fields so ^/$ assertion candidates honor the wide terminator set only when the MatrixOne multiline bit is set.
  2. Engine classifier pkg/sql/plan/function/internal/regexpclass: routes each pattern to the pinned Go RE2 copy (internal/regexp) or to regexp2, with native line-policy fields; RE2-eligible patterns keep their existing engine and gain the wide terminator set through internal/regexp/lines.go. Anchored-prefix fast paths are preserved.
  3. Entry points pkg/sql/plan/function/func_builtin_regexp.go + regexp_icu.go: the four SQL functions extend m to the full MySQL terminator set with original character positions and replacement bytes preserved; mu stays Unix-only. The engine timeout message stays byte-identical (match timeout after %v on input %v, detected by regexp_icu.go).
  4. Policy conformance: vendored sources satisfy the repository's configured staticcheck/gofmt/ineffassign/unused policy, the err-check gate, and gofmt -s; .licenserc.yml exempts exactly the 23 upstream files individually (MIT retained, no Apache text stamped onto upstream sources).
  5. Design document: docs/design/20261024-30210-regexp-multiline-line-terminators.md (Rev 11, SHA-256 88b073910fb37bbc7cb7c5f931239e255116b348bb930a980a6d0d366763ec2d). Design-first review was distinct and complete before implementation review: Rev 1-9 received design-stage REQUEST_CHANGES from the independent local reviewer (each finding verified against primary sources and live ICU 77.1 measurement, addressed per revision in the document change log), and Rev 10 (SHA-256 5dcab7b28d863dbec65e003b5db3503fdbc3ee1c1374d4622e9ae737bf3315b4) received the design-stage PASS on 2026-10-25 with no remaining blocking finding. Rev 11 is a status-record-only revision (design content identical to Rev 10 outside the Status paragraph, the new "Design review record" section and the Rev 11 change-log entry) that publishes the approval chain inside the document. Implementation-stage local reviews re-verified the invariant map against the code each round, retained the design PASS, and ended in approval; its two non-blocking P3 findings are addressed at this head (vendored-copy sync procedure + maintained-change inventory in internal/regexp2/README.md/PROVENANCE, unseen-spelling cache-bound assertions in internal/regexpclass/review_fixes_round9_test.go).
  6. Tests: UT harness for the classifier/linemode mapping (28 tests), round 2-9 review regression suites, regexp_multiline_test.go, regexp_wide_linesplit_review_test.go, conformance suites against Go regexp/syntax; BVT rows added to test/distributed/cases/operator/regexp_operator.test[.result] and test/distributed/cases/function/func_regular_like.test[.result] covering all six separators x m/mu x all four entry points.

Proof (issue -> test)

  • Baseline red / fixed green: the 28-test harness fails on merge-base 6fb6531432aa and passes on this tree (same tests, same commands).
  • Live SQL behavior: 178/178 recorded checks on a running instance over all six separators (CR, LF, CRLF, NEL, LS, PS) x {m, mu} x {LIKE, INSTR, SUBSTR, REPLACE}, verifying exact positions and replacement bytes (m=1 for CR/NEL/LS/PS, mu=0 for them, LF/CRLF behavior unchanged).
  • Independent oracle: 169 ICU 77.1 reference checks replayed by the local reviewer without disagreement.
  • BVT (mo-tester against the patch build): all 66 failing cases in the broader run match the normalized baseline exactly; the new regexp cases pass.
  • Regression/consistency: full pkg/sql/plan/function suite, regexpclass/regexp/regexp2 suites, and race-detector runs are green; scaling benchmarks on >1 MiB inputs and anchored-prefix patterns show no regression vs main.
  • Delivery gates: configured golangci-lint run -c .golangci.yml on the affected closure reports 0 issues; make err-check passes; repo-wide license-eye -c .licenserc.yml header check reports 0 invalid files.

Test commands

  • go test ./pkg/sql/plan/function -count=1 (cgo env) — full function suite incl. the issue harness.
  • go test ./pkg/sql/plan/function/internal/regexp/... ./pkg/sql/plan/function/internal/regexp2/... ./pkg/sql/plan/function/internal/regexpclass/... -count=1.
  • go test -race on the same closure (race belt) and package-level race runs for regexpclass.
  • golangci-lint run -v -c .golangci.yml <4 packages> with the repository CGO_CFLAGS (golangci-lint 2.14.0, go1.27.1): 0 issues.
  • make err-check: passes ("Does not find 'fmt.Errorf()', 'errors.New()','context.WithTimeout' and 'context.WithDeadline'").
  • license-eye -c .licenserc.yml header check: 6877 files, 0 invalid.
  • mo-tester BVT: regexp_operator and func_regular_like cases green; whole-run failures byte-identical to baseline after path normalization.

Residual risks / notes

  • The vendored engine is a maintained fork-in-tree: PROVENANCE pins pristine upstream hashes and enumerates every local change class; README.md documents the synchronization procedure so a re-vendor cannot silently drop the line-policy fields, assertion changes, or policy corrections.
  • Behaviors outside the tested 6-separator x 2-mode x 4-entry-point matrix (exotic flag combinations, unusual encodings already routed to ICU) keep their existing semantics; the ICU oracle found no disagreement on the sampled space.
  • Two molint informational observations remain (upstream Debug-only fmt.Printf in runner.go); the tool exits 0 and the Makefile line is non-gating. Kept deliberately to minimize upstream divergence.
  • Open PR compile/plan: fix identifier escaping in ALTER COPY DROP, lenient integer CAST, MySQL multiline regexp terminators (#30550 #30417 #30210) #30646 upstream supplies only a partial fix (regexp2-only paths); this PR covers all four entry points and the RE2 path as well.

Rebase notes (2026-10-25)

main merged #31079 (perf(regexp): skip terminal any-rune loops at EOF), which restructured the unanchored loop in internal/regexp/backtrack.go (required-prefix admission before bitState.reset, prefix re-scan at the loop bottom, markEndTextRepeats EOF classification) and conflicted with this PR's CRLF-interior start suppression. The conflict was resolved semantically: upstream's loop structure is kept, and this PR's guard (lineMask && crlfBetween(i.context(pos))) now wraps the candidate match attempt inside the restructured loop — the same form as the NFA-path guard in exec.go. The upstream optimization is orthogonal: it classifies only InstRuneAny/InstRuneAnyNotNL loops that accept at EmptyEndText, and the wide line modes change line assertions only, never consuming instructions, so both changes compose without interaction.

Post-rebase validation on the delivered head 379e87f1e0 (base 9dcd26be43, includes #31079): go build ./pkg/sql/plan/function/... exit 0; go test -count=1 green for internal/regexp (7.962s, incl. upstream #31079 backtrack_test.go and this PR's lines_test.go), internal/regexpclass (4.709s) and internal/regexp2 (2.995s); go test -count=1 -run 'Regexp|REGEXP' ./pkg/sql/plan/function/ green (15.940s, incl. upstream regexp_regular_engine_test.go and this PR's multiline/line-split/review-round suites).

MatrixOne's REGEXP_LIKE/INSTR/SUBSTR/REPLACE multiline ('m') mode only
treated LF and CRLF as line boundaries, while MySQL 8.0 also recognizes
CR, NEL (U+0085), LINE SEPARATOR (U+2028) and PARAGRAPH SEPARATOR
(U+2029) as line terminators. The 'mu' (Unix-only) mode was and stays
LF-only.

Vendor the dlclark/regexp2 engine (fork github.com/XuPeng-SH/regexp2
@718f43e031fe = v1.10.0 + fastclock patch, MIT) under
pkg/sql/plan/function/internal/regexp2 with attribution in README /
ATTRIB / LICENSE / PROVENANCE, add the MatrixOne-owned linemode
classifier for the wide line-terminator set (with atomic CRLF), add the
pkg/sql/plan/function/internal/regexpclass engine classifier that routes
patterns between the pinned Go RE2 copy and regexp2, and wire the wide
terminator set through both engines and the four SQL entry points
(REGEXP_LIKE/INSTR/SUBSTR/REPLACE) behind the ' m' flag while keeping
'mu' Unix-only. Vendored sources are reconciled with the repository's
license-header, err-check and configured staticcheck policies;
.licenserc.yml exempts the 23 upstream files individually.

Design: docs/design/20261024-30210-regexp-multiline-line-terminators.md

Related to matrixorigin#30210.
@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 →

@XuPeng-SH

XuPeng-SH commented Oct 25, 2026 •

Copy link
Copy Markdown
Contributor

mo-bot review · mo-bot-local · done

Commit: 379e87f1e0749af895190a2a3a273e03e0af5019

Review completed. Read the assessment.

@XuPeng-SH XuPeng-SH left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

mo-bot review · mo-bot-local · review

Commit: 379e87f1e0749af895190a2a3a273e03e0af5019

Assessment: request_changes

The mandatory design-review gate remains open. This change introduces a substantial pattern-classification subsystem shared by two regex engines, but the supplied evidence does not establish approval of the implemented design revision. Full implementation approval is deferred.

Model: gpt-6-astra / xhigh

Session: 01a13dcf-1517-7f90-b3f9-c5f9f9e7031b · slot 0 · generation 1

  • P2 docs/design/20261024-30210-regexp-multiline-line-terminators.md:3: Validation requirement: this architectural change adds 3,458 lines of non-vendored classifier production code and changes candidate-start policy across RE2 and regexp2, triggering the repository's design-first review contract.
    Impact: Implementation approval requires a traceable design approval decision. Provide the distinct design-review record for the exact implemented revision, resolve any blockers, and reconcile the pending status before resuming implementation review.
    Evidence: Rev 10 explicitly says 'pending design review'. Its SHA-256, 5dcab7b28d863dbec65e003b5db3503fdbc3ee1c1374d4622e9ae737bf3315b4, matches the PR body. The body says it was independently reviewed locally but supplies no approval decision or review record; evidence.json contains no reviews or local_validation gates.

Validation:

  • Verified the assigned head, merge-base, unchanged checkout, 77-file inventory and exact design hash.

  • Design audit exited 0; checks/1792947942110056164-4.json matches design-gate-audit.log and the frozen source fingerprint.

  • Frozen CI reports successful build, UT, SCA, coverage and selected BVT checks. These do not establish design approval.

  • Audit details: /home/xupeng/.local/state/mo-bot/jobs/da9075f02ec07249c6606269/attempt-1/review-record.json.

Limitations:

  • The repository contract at .agents/skills/mo-dev/references/feature-design-review.md requires a traceable 'reviewed revision and approval decision' and directs reviewers to 'stop the implementation approval path' when the gate fails.

  • Full implementation/test review, native provenance reconciliation and behavioral/performance validation remain outstanding. No fresh product tests were run and no runtime defect is asserted.

…trixorigin#31125)

The design-stage review of design Rev 10 (sha256 5dcab7b2...) closed with
decision PASS on 2026-10-25, after Rev 1-9 each received design-stage
REQUEST_CHANGES whose findings were verified and addressed per revision.
The document Status header still read "pending design review" and the PR
carried no traceable approval record, which the external review on
PR matrixorigin#31125 flagged as an open design-review gate.

Rev 11 records the complete decision chain in a new "Design review
record" section and updates the Status header. It is a status-record-only
revision: outside the Status paragraph, the new section and the Rev 11
change-log entry, the document is identical to the approved Rev 10, so
the PASS applies unchanged to the implemented revision. Rev 11 sha256:
88b073910fb37bbc7cb7c5f931239e255116b348bb930a980a6d0d366763ec2d.
@gouhongshen

Copy link
Copy Markdown
Contributor Author

Addressing the design-gate finding (P2, docs/design/20261024-30210-regexp-multiline-line-terminators.md:3).

Decision: the design-review gate is satisfied; the traceability gap is now closed in-tree. The finding is correct on the publication side: the Status header still read "pending design review" and the body cited no review record, so the approval was not traceable from the PR. The design-stage review itself was distinct, complete, and keyed to the exact implemented revision:

  • The design-first phase ran 9 design-stage review rounds against the versioned document: Rev 1-9 each received REQUEST_CHANGES; every finding was verified against primary sources and live ICU 77.1 measurement and addressed in the next revision (per-revision resolutions are the document's change log, Rev 2-10 entries).
  • Rev 10 (SHA-256 5dcab7b28d863dbec65e003b5db3503fdbc3ee1c1374d4622e9ae737bf3315b4, the same hash the review verified against the PR body) closed the design stage with decision: PASS on 2026-10-25, no remaining blocking finding. Only after that decision did implementation review begin.
  • Every implementation-stage review round re-verified the invariant map against the implemented code and retained the Rev 10 design PASS unchanged, ending in implementation approval. Its two non-blocking P3 findings are addressed at the current head: the vendored-copy synchronization procedure and maintained-change inventory in pkg/sql/plan/function/internal/regexp2/README.md ("Sync procedure (preserving the maintained local changes)" + divergence classes + PROVENANCE pristine-hash inventory vs. local-change classes), and the previously-unseen-spelling cache-bound assertions in pkg/sql/plan/function/internal/regexpclass/review_fixes_round9_test.go (unseen := []string{"LETTER", "l_e_t_t_e_r", "A_N_Y", "GREEK", "g-r-e-e-k", "Gr eek"} with cardinality assertions).

Publication fix delivered in 843ef39b18: the document is now Rev 11 (SHA-256 88b073910fb37bbc7cb7c5f931239e255116b348bb930a980a6d0d366763ec2d), a status-record-only revision. The Status paragraph records the PASS, a new "Design review record" section carries the complete decision chain in the contract's compact review-record form (change scope, trigger, reviewed revision, blocking findings, decision log, decision, implementation deviations: none), and a Rev 11 change-log entry documents the reconciliation. Outside those three regions the document is byte-identical to the approved Rev 10 (verifiable: the commit's diff contains only those hunks), so the Rev 10 PASS applies unchanged to the implemented revision. The PR body item 5 now cites Rev 11 with its hash and the full chain.

No product code, tests, or validation semantics changed in 843ef39b18; the earlier build/UT/BVT evidence on 379e87f1e0 remains the implementation evidence of record, and CI on this head covers the documentation-only delta. Implementation review can resume against the traceable record above.

@XuPeng-SH

XuPeng-SH commented Oct 25, 2026 •

Copy link
Copy Markdown
Contributor

mo-bot review · mo-bot-local · done

Commit: 843ef39b189218318da277ce78e943f70ecb1057

Review completed. Read the assessment.

@XuPeng-SH XuPeng-SH left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

mo-bot review · mo-bot-local · review

Commit: 843ef39b189218318da277ce78e943f70ecb1057

Assessment: approve

Reviewed the complete delta, linked issue, review concerns, callers and validation. The design appropriately keeps line assertions in the engines and shares compile-time classification, preserving original subject offsets and removing admission-inflating anchor rewrites. The published design approval matches the prior document revision. No actionable introduced defects remain; the measured wide-line performance cost is an accepted correctness tradeoff.

Model: gpt-6-astra / xhigh

Session: 01a13dcf-1517-7f90-b3f9-c5f9f9e7031b · slot 0 · generation 1

Validation:

  • Verified clean frozen HEAD and supplied patch, 200 native build inputs, and all 27 vendored upstream provenance hashes. Final audit passed: checks/1792955861453676165-5.json and audit-review-final.log.

  • Engine and classifier suites passed; classifier and regexp2 race tests passed. Receipts: 1792954960327164749-3.json and 1792955732686901503-3.json.

  • Built native prerequisites through the repository Makefile using exact candidate inputs. SQL regexp, operator and wide-line tests passed, covering offsets, replacement, routing and admission boundaries. Receipts: 1792955548199700155-4.json and 1792955845423684145-4.json.

  • Independent ICU 76.1 comparison passed 17,530 RE2 cases. Regexp2 discrepancies were investigated with pinned-upstream controls and attributed to existing lazy-empty-loop behavior, not counted as a parity pass.

  • Five-sample base/head benchmarks measured approximately 30–31% slower long wide-line scans, 12% slower short wide-line matching, and 12–31% higher wide compilation time. Sampled Unix-line and prefix-rejection paths remained near base; warm matching added no allocations.

  • Command, source, exit and log reconciliation is retained in /home/xupeng/.local/state/mo-bot/jobs/30a82aee1f27ba74878f0bc9/attempt-1/review-record.json.

Limitations:

  • Independent oracle execution used ICU 76.1, not the author's ICU 77.1 environment; it does not establish universal ICU equivalence.

  • No fresh live-server BVT run. Frozen matching-head CI metadata reports successful UT, native builds and BVT, but raw CI logs and author artifacts were unavailable.

  • Initial oracle timeout and native setup failures were diagnostic failures, followed by successful bounded validation. Benchmarks are local microbenchmarks, not end-to-end SQL throughput measurements.

@gouhongshen

Copy link
Copy Markdown
Contributor Author

@Mergifyio queue

@mergify

mergify Bot commented Oct 25, 2026 •

Copy link
Copy Markdown
Contributor

Merge Queue Status

  • ✅ Entered queue — 2026-10-25 19:18 UTC · Rule: main · triggered by @gouhongshen with the @mergifyio queue command
  • ✅ Checks passed · in-place
  • ✅ Merged — 2026-10-25 20:36 UTC · at b14d67a29041cb24b5c4e3029e20980580346c42 · squash

This pull request spent 1 hour 18 minutes in the queue, including 55 minutes 51 seconds running CI.

Required conditions to merge

@mergify mergify Bot added the queued label Oct 25, 2026
@mergify
mergify Bot merged commit b14d67a into matrixorigin:main Oct 25, 2026
44 checks passed
@mergify mergify Bot removed the queued label Oct 25, 2026

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/XXL Denotes a PR that changes 2000+ lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants