Skip to content

fix(regexp): separate operand decoding from charset compatibility - #28534

Closed
ck89119 wants to merge 16 commits into
matrixorigin:mainfrom
ck89119:issue-27217-main
Closed

ck89119 wants to merge 16 commits into
matrixorigin:mainfrom
ck89119:issue-27217-main

Conversation

@ck89119

@ck89119 ck89119 commented Sep 9, 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:

Fixes #27217

What this PR does / why we need it:

Separate three REGEXP decisions that cannot share an OR-of-IsBin shortcut:

  • Static compatibility: distinguish binary casts/literals from BINARY/BLOB columns and runtime variables. Reject incompatible fixed binary operands at PREPARE, including typed NULL and nested REGEXP results.
  • Input decoding: subject and pattern decode independently; a binary pattern does not turn text subject positions into byte positions. Preserve invalid-text prefix behavior and the homogeneous binary matcher.
  • Result encoding: bare binary variables retain binary results, while SQL/COM_STMT markers retain text results even when the current execution sends BLOB. Keep marker result bounds stable across reuse without changing executor input casts or shared parameter conversion.

Also correct SUBSTR/REPLACE terminal-position handling and the independently verified occurrence boundary rules.

Validation

  • MySQL 8.4.8 oracle for static classification, independent operand roles, multibyte/invalid bytes, output encoding, and boundary behavior.
  • make -j8 passed.
  • Full pkg/sql/plan/function, pkg/sql/plan, and pkg/frontend tests passed with the repository CGo wrapper, -count=1, and coverage instrumentation.
  • Real-client TestRegexpPreparedProtocolSources passed: actual COM_STMT type descriptors 252/254, COM PREPARE error 3995, fresh-versus-reused statements, metadata stability, invalid pattern recovery, and typed NULL followed by text/BLOB reuse.
  • Existing prepared BIT64, numeric overload, JSON modification, and ordinary string/NULL recovery integration tests passed.
  • Changed-statement coverage after review fixes: 319/350 = 91.14%; every changed production file exceeds 75%. Duplicate -coverpkg blocks across test binaries are merged before counting.
  • StringSource is not treated as UTF-8 validity provenance: VARCHAR columns and every expression source preserve MySQL's text conversion behavior. Text decoding follows MySQL's lead-byte input-width rule before validating continuation bytes: C2–DF require 2 bytes, E0–EF require 3, and F0–FF require 4. Insufficient input truncates at the valid prefix; a complete but ill-formed continuation, overlong, surrogate, or out-of-range sequence returns MySQL error 3854 (utf8mb4 to utf16le).
  • REGEXP_INSTR validates only its rebased suffix at pos, so malformed/truncatable bytes before pos cannot affect matching. INSTR/SUBSTR preserve malformed-pattern 3854 even with NULL subjects. SUBSTR then lets NULL position/occurrence determine NULL before rejecting non-positive position and processing NULL subject/pattern. REGEXP_REPLACE follows observable precedence: compile non-NULL pattern, return NULL for NULL position/occurrence, reject non-positive position, validate replacement in its original text domain, apply remaining nullable arguments, then validate subject. A text replacement for binary BLOB-column matching is converted only after validation. Predicate/INSTR/SUBSTR/REPLACE reject ill-formed sequences even when an early regexp match precedes them. Without independent validity provenance this requires validating the relevant complete text value/suffix; the PR no longer claims that source ownership proves validity or that every large early match can skip conversion validation.
  • mo-tester generated and verified both changed result files. Nine related BVT scripts passed consecutively on one fresh instance; the final source matrix (63/63) and INSTR suite (187/187) passed with zero database residue. The permissive-VARCHAR case changes session sql_mode only around INSERT, restores it before querying, and the following eight scripts verify no state leakage.

Scope notes

  • SQL rows and statements in the real-client suite are closed by defers registered immediately after successful QueryContext/PrepareContext; loop bodies use per-operation helpers so defers do not accumulate. Conversion errors preserve printable source bytes, escape invalid bytes as uppercase \\xNN, preserve DEL 0x7F, and follow MySQL's first-six-source-bytes plus ... formatting.
  • No protobuf/wire-format changes or generic parameter-conversion changes.
  • General provenance transport remains outside [Subtask]: Separate REGEXP static operands, user variables, and parameter markers #27217: MySQL materializes scalar/derived aliases as text, whereas MO currently forwards marker source metadata through them. The retained legacy scalar/derived cases check MO's transparent-transport contract against direct operand semantics, not MySQL materialization compatibility.
  • MO's existing unbounded TEXT representation is retained; MySQL reports LONGTEXT for REPLACE's expansion bound. This PR fixes text-versus-binary result semantics, not global large-text protocol mapping.
  • The existing INSTR result file was regenerated by mo-tester; this also updates its result-header formatting.

Versioned design and separate design decision

The project user explicitly approved r1 in the development conversation on 2026-09-12, then authorized delivery. This is a separately recorded design decision, not a claim that the design preceded the existing implementation or that the GitHub reviewer has approved it. The record maps the implementation to the design and preserves the mixed-version, resource-budget, materialized-alias and large-text exclusions.

This update publishes design artifacts and synchronizes the upstream base; it makes no REGEXP implementation changes. Earlier runtime/coverage evidence is historical and is not claimed as an exact-head pass after the upstream merge. The PR remains draft pending implementation acceptance.

@matrix-meow matrix-meow added size/XXL Denotes a PR that changes 2000+ lines and removed size/XL Denotes a PR that changes [1000, 1999] lines labels Sep 11, 2026
@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 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.

Reviewed target bff80fa30846707eaab4de137cf49d4c2d39ed35 against main@a0770f5dc4a296f8407bbebf6a2664bac9972e53.

[P1] Supply and approve the versioned design for the REGEXP domain refactor before implementation acceptance. The complete change materially restructures the core contract across binder classification, prepared specialization, operand decoding, result encoding and error precedence: new StringDomainCheck modes, the regexpStringParameter conversion abstraction, marker-specific return-type derivation, and consumer-local source rewriting. It is approximately 700 changed non-generated production lines across the error/planner/function boundaries. This triggers the repository's design-first requirement for a major core-abstraction/compatibility refactor, both by size and by the cross-boundary contract changes; the BUG label does not exempt the actual scope.

The implementation PR, comments/reviews, issue #27217 and linked preceding #26907/#25299 provide requirements and extensive implementation/testing descriptions, but do not link an exact stable versioned design with a traceable design approval. An issue acceptance matrix or coverage percentage does not replace that mandatory artifact.

Suggested closure: add/link a design revision covering the separate static/decode/result matrices, source ownership through folding and prepared reuse, typed NULL/error precedence, charset conversion and offset/anchor invariants, alternatives, compatibility across CN versions, bounded conversion/cache/output costs, and the explicit exclusions for materialized aliases and large-text metadata. Record a distinct design review decision, then check implementation alignment against that revision. No RFC format or redundant blanket test rerun is required.

Review scope/limits: inventoried all 16 changed files and inspected production changes sufficiently to establish the trigger and critical boundaries. The design gate stops implementation approval; I am not claiming a completed runtime correctness/performance PASS or a reproduced runtime defect. Author-reported MySQL 8.4.8 oracle, package tests, real COM_STMT tests and BVT/coverage evidence are substantial and may be reused after the design gate; this finding is not based on optional evidence being absent. I did not run native tests or wait for CI. Exact live head/base rechecked before posting.

@fengttt fengttt 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.

Just like the other PR, #28428 , I don't understand WHY this param binding/cast has anything to do with one, or several particular functions. I don't understand all the stringdomains, why are they necessary.

Regexp is a strange animal, so the param may need some handling, sure, but why isn't this happen in the functions execution time? Handling depends on whether the param comes from session var or prepared statement, is really strange.

// SQL EXECUTE can substitute a literal before this consumer binds.
// Preserve the marker owner on this REGEXP argument only, rather
// than changing the shared parameter used by unrelated consumers.
boundArgs[i] = DeepCopyExpr(boundArgs[i])

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.

What is this DeepCopy doing? Why?

@aptend aptend 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.

Reviewed exact head e925266a4e17841c581c7d176682a9497220ffc2 against exact base 4f9d170a22deaabc2eee6b239a51a8cb40a39f9f.

The design gate is still blocking. This change establishes a cross-boundary REGEXP contract across binder item classification, prepared-plan specialization, vector provenance, operand decoding, result encoding, error precedence, and release routing. The immutable r1 itself still says “待独立设计审批;实现验收仍阻塞” at lines 3/7 and leaves the approver/SHA/link pending at lines 127-133. The newly added decision document does not supply independently reviewable approval: it transcribes two phrases from an unidentified private development conversation, gives no source link, and explicitly says it is not GitHub reviewer approval. This is material because the decision purportedly accepts the function-specific planner/rebind architecture, an operational same-version-CN routing constraint with no enforcement mechanism, and explicit scope exclusions. A current owner review still has the architecture question open.

Closure: have the appropriate design owner review an immutable r1 (or a new revision) in a traceable venue and record the approver identity, exact design SHA, decision link, and explicit acceptance/rejection of the planner coupling, release constraint, resource tradeoff, and exclusions. Then update the r1 status/approval record and perform the promised implementation-alignment review, including resolution of the current architecture thread. A PR-local transcript written by the implementation side cannot replace that independent decision.

I inspected the complete 18-file diff, all review/inline history and the unresolved thread, issue #27217, static/execute-time domain matrices, prepared reuse/source isolation (including the DeepCopy path), per-operand conversion, invalid-UTF-8/NULL/error ordering, offsets/anchors, output domains, cache and failure paths. Exact-head focused tests passed for pkg/sql/plan/function, pkg/sql/plan, pkg/frontend, pkg/common/moerr, and the real TestRegexpPreparedProtocolSources protocol test; diff-check and gofmt are clean. The current CI failures reproduce only the unrelated pkg/vectorindex TestAppendColumnBuffer_AllTypes/json failure. I am not asserting an additional runtime defect beyond this blocking design/ownership issue.

- 日期:2026-09-12。
- 设计:[r1 固定版本](https://github.com/ck89119/matrixone/blob/931f646eb986221e34285e14f35ce85dc515e2a0/docs/design/CLAUDE_REGEXP_DOMAINS_27217_r1.md)。
- 文档内容 SHA-256:`bdb3dc057ebf620b17b899dd39b5eeff51e9303b9e9d097ba194b231ba3d1ba2`。
- 审批来源:项目用户在开发会话中明确回复「确认 r1 设计」,随后回复「go ahead」授权交付。

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.

[P1] This is not an independently verifiable approval record.

Trigger/reproduction: follow the claimed approval in lines 8-10. It points only to two quoted phrases in a private development conversation, without an approver identity or source link, while the immutable r1 still says approval is pending and requires an approver, exact SHA, and link. The record also explicitly says it is not GitHub reviewer approval.

Impact: these lines currently purport to authorize the new function-specific binder/rebind architecture, same-version routing release constraint, resource tradeoff, and exclusions. Without a reviewable decision from the appropriate design owner, reviewers/operators cannot establish who accepted those contracts, and the implementation-side transcript can self-close the very independent gate it is meant to satisfy.

Please replace this with a traceable independent approval (identity, exact immutable design SHA, decision URL, and explicit disposition of those material choices), update the r1 pending status, and complete implementation alignment after that decision.

This branch had an error being deployed

1 failed 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.

[Subtask]: Separate REGEXP static operands, user variables, and parameter markers

5 participants