Skip to content

fix: align INSERT/UPDATE string width enforcement with MySQL sql_mode (#25200) (cherry-pick #25262) - #25312

Merged
mergify[bot] merged 44 commits into
matrixorigin:mainfrom
ck89119:issue-25200-main
Jul 27, 2026
Merged

mergify[bot] merged 44 commits into
matrixorigin:mainfrom
ck89119:issue-25200-main

Conversation

@ck89119

@ck89119 ck89119 commented Jul 1, 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:

issue #25200

What this PR does / why we need it:

Cherry-pick of PR #25262 from 3.0-dev to main.

Rework CHAR/VARCHAR width enforcement on DML assignment paths so it matches MySQL's sql_mode semantics:

  • sql_mode gating (runtime): add a new cast_assign function used by DML assignment paths for CHAR/VARCHAR targets. It reads sql_mode at runtime (strict -> reject 1406, non-strict -> truncate), avoiding plan-cache staleness.
  • trailing-space exemption: over-length values whose excess is only trailing spaces are accepted (truncated) even under strict mode, matching MySQL.
  • INSERT IGNORE downgrade: under IGNORE, over-length writes are truncated instead of rejected.

Notes

🤖 Generated with Claude Code

@ck89119
ck89119 requested review from aunjgr and ouyuanning as code owners July 1, 2026 07:23
@ck89119
ck89119 requested a review from a user July 1, 2026 07:23
@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 →

@matrix-meow matrix-meow added the size/XXL Denotes a PR that changes 2000+ lines label Jul 1, 2026
@matrix-meow matrix-meow removed the size/XXL Denotes a PR that changes 2000+ lines label Jul 1, 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.

Requesting changes on 432e06217e7ecd552251d0e89b8da7ef185d36b2.

The earlier remote sql_mode and function-ID findings are fixed, but the latest head still has four correctness/compatibility blockers:

  1. [P1] Cast warnings are dropped at the first real pipeline-process boundary.

    newCast captures warnings through proc.GetSession() (pkg/sql/plan/function/func_cast.go:950-957), but Process.Session is a field on the individual Process, while NewNoContextChildProc / NewContextChildProc copy only Base (pkg/vm/process/process2.go:107-147). Consequently the processes that execute pipeline expressions have a nil session, and addCastWarningToSession silently returns. A decoded remote process has no frontend session either, so the same loss happens across CNs.

    Concrete failure: an over-width non-strict/IGNORE assignment is truncated successfully, but the OK packet reports zero warnings and SHOW WARNINGS is empty. I reproduced this by assigning a warning-capable session to a top process, executing NewAssignCast on top.NewContextChildProc(0), and observing the expected value "abc" with no 1265 recorded. The existing warning unit test sets Session directly on the same process used for evaluation, so it does not exercise the production boundary.

    Please put warning collection on state shared by child processes and close the remote-result aggregation path (or explicitly defer/remove the partial warning feature until that end-to-end path exists).

  2. [P1] SELECT responses still advertise zero warnings.

    castModeExplicit now records ER_TRUNCATED_WRONG_VALUE, but the *tree.Select branch in respStreamResultRow still calls WriteEOFOrOK(0, ...) (pkg/frontend/result_row_stmt.go:297-302). Thus even after warning collection is fixed, SELECT CAST(...) sends a zero warning count in its final EOF/OK packet. The added TestStreamResultResponseCarriesWarnings uses SHOW WARNINGS, which takes the default branch and misses this case. I reproduced it with a tree.Select response carrying one stored warning: the writer received 0.

    Pass ses.WarningCount() in the SELECT branch and cover the final result-set terminator (including CLIENT_DEPRECATE_EOF).

  3. [P1] The protocol-v4 compatibility plan now returns a DDL error for DML.

    During a mixed-version rollout, assignmentCastFunctionName intentionally emits cast_strict (pkg/sql/plan/build_constraint_util.go:1094-1106). However, this PR changed NewStrictCast width failures to NewErrInvalidDefault in the allowTrailingSpaceTrim == false path (pkg/sql/plan/function/func_cast.go:7007-7010, with equivalent branches for other sources). Therefore the new binary executing the v4-compatible DML plan returns 1067/ErrInvalidDefault, while the old binary executing the same cast_strict plan returns the legacy DML/internal cast error. Client-visible behavior now depends on which CN executes the plan.

    A focused execution of NewStrictCast(TEXT('abcd') -> VARCHAR(3)) on this head returned ErrInvalidDefault; the same path on the base returns ErrInternal. Please keep cast_strict's stable contract and use a DDL-specific operator/mode/context for DEFAULT validation.

  4. [P1] Persisting cast_assign in generated-column metadata breaks a 5→4 rollback.

    At protocol v5, buildGeneratedExpr stores cast_assign (ID 554) directly in the catalog expression (pkg/sql/plan/build_util.go:488-516). The previous binary has FUNCTION_END_NUMBER = 554, no cast_assign registration, and expression execution resolves the encoded ID rather than ObjName; it therefore fails with function overload id not found. applyGeneratedColumnAssignmentCast only protects planners running the new code and cannot help a rolled-back CN reading the catalog.

    Concrete sequence: upgrade and set protocol 5 → create a VARCHAR generated column → set protocol back to 4/roll a CN back → INSERT/UPDATE planned on that old CN cannot execute the stored generated expression. Since this PR explicitly handles and tests protocol rollback for cached plans, catalog state needs the same closure. Keep the persisted expression on the stable cast_strict ID and rewrite it to cast_assign/cast_ignore only while building an execution plan.

Focused existing tests for function semantics, protocol-gated planning, generated-column rewriting, prepared-plan/cache invalidation, and frontend warning responses pass. The boundary regressions above are not covered by those tests.

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

Requesting changes on 9c1c54c8b99043cf72ab44b0a86fd09ed3f59e14.

The earlier cast-strict contract and generated-column rollback findings are fixed, and the focused cast/planner/protocol tests pass. Three warning-path correctness gaps remain:

  1. [P1] Warning collection still stops at the first production Process boundary and has no remote return path.

newCast captures its sink from proc.GetSession() (pkg/sql/plan/function/func_cast.go:948-957), but Process.Session is outside the shared BaseProcess (pkg/vm/process/types.go:421-430). NewNoContextChildProc and NewContextChildProc copy only Base (pkg/vm/process/process2.go:109-147), so the child processes used to evaluate pipeline expressions have a nil session. Remote execution is also incomplete: remoterunServer.newCompile reconstructs a top process and restores only Base.SessionInfo (pkg/sql/compile/remoterunServer.go:669-685), and neither the pipeline protocol nor the terminal envelope carries warnings back to the initiating CN.

Concrete failures: SELECT CAST('abcd' AS CHAR(3)), a non-strict over-width INSERT/UPDATE, or INSERT/UPDATE IGNORE can truncate successfully while the client sees warning_count=0 and SHOW WARNINGS is empty; the same loss is guaranteed when the cast executes remotely. TestAssignmentCastTypedSourceAndWarnings sets proc.Session directly on the exact evaluator process, so it bypasses this production boundary.

Please put the warning sink/counter on state shared by child processes and close the remote aggregation path (or defer the warning feature until that end-to-end path exists). Add a child-process regression and a multi-CN remote regression.

  1. [P1] Ordinary SELECT still writes a zero warning count in its final EOF/OK packet.

The *tree.Select branch still calls WriteEOFOrOK(0, ...) at pkg/frontend/result_row_stmt.go:296-302. Thus even after collection is fixed, explicit CAST warnings are hidden from the protocol response. TestStreamResultResponseCarriesWarnings uses tree.ShowWarnings, which reaches the default branch and does not exercise SELECT.

Pass ses.WarningCount() in the SELECT branch and test both EOF and CLIENT_DEPRECATE_EOF terminators for a warning-producing SELECT.

  1. [P2] Protocol warning_count is derived from the bounded display buffer, so bulk warnings are under-counted.

AddWarning stores warnings in errInfo, whose default max is 64, while WarningCount returns len(ses.warnings.codes) (pkg/frontend/session.go:1934-1962). errInfo.push also uses len > maxCnt rather than >= (pkg/frontend/session.go:711-717), so the buffer stabilizes at 65 entries. A statement that truncates 100 rows therefore advertises 65 warnings, not 100. The storage limit should bound SHOW WARNINGS retention, not the statement total used by the protocol; track the total separately (capped at uint16 for the wire) and cover a batch larger than the retention limit.

Validation on this head: the newly added focused tests in pkg/common/moerr, pkg/vm/process, pkg/sql/plan/function, pkg/sql/plan, and pkg/frontend all pass with the repository CGo wrapper. CI is green except for the still-running arm64 SCA job.

@ck89119

ck89119 commented Jul 27, 2026

Copy link
Copy Markdown
Contributor Author

@XuPeng-SH Thanks for the review. The three remaining findings are all related to warning collection, propagation, protocol warning counts, and SHOW WARNINGS behavior. MatrixOne does not currently support an end-to-end warning mechanism, so warning-related features and change requests are explicitly out of scope for this PR and will not be addressed here. The previously reported non-warning issues—the cast_strict compatibility contract and generated-column rollback compatibility—have been fixed.

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

Re-review on unchanged head 9c1c54c8b99043cf72ab44b0a86fd09ed3f59e14: the requested changes remain.

Calling warning behavior out of scope does not resolve the blockers because this PR itself adds warning production (newCast), storage/counting (Session), SHOW WARNINGS, and protocol warning counts. Either complete that behavior end to end or remove/defer those warning-facing changes from this PR.

  1. [P1] Warning collection is still lost at real process/CN boundaries. Process.Session is outside shared BaseProcess; NewNoContextChildProc/NewContextChildProc copy only Base, so evaluator child processes have no warning sink. Remote execution also has no terminal warning aggregation back to the initiating CN. The current cast test attaches the session directly to the evaluator and bypasses both boundaries.
  2. [P1] Ordinary SELECT still advertises zero warnings. The *tree.Select branch in respStreamResultRow calls WriteEOFOrOK(0, ...); the added test uses tree.ShowWarnings, which takes a different branch.
  3. [P2] Protocol warning_count is the retained-buffer length, not the statement total. errInfo.push retains at most 65 entries with the current len > maxCnt condition, and WarningCount returns that retained length, so 100 truncations are reported as 65.

The focused cast/frontend warning tests pass on this head, but they confirm only the bypassing unit-test paths above. CI is green; these are uncovered correctness gaps, not CI failures.

@ck89119

ck89119 commented Jul 27, 2026

Copy link
Copy Markdown
Contributor Author

@XuPeng-SH Thanks for the clarification. I removed the warning-facing changes introduced by this PR instead of extending MatrixOne's currently incomplete warning mechanism.

The cast warning production/context, session warning storage/counting, SHOW WARNINGS-specific state, protocol warning propagation, and warning-only tests have been removed. String-width behavior remains unchanged: strict assignment rejects over-width values, non-strict/IGNORE truncates them, and explicit CAST returns the truncated value.

Validation:

  • Full tests passed for pkg/sql/plan/function, pkg/frontend, pkg/sql/plan, and pkg/vm/process.
  • Read-only build and vet passed for the changed package closure.
  • Focused string-width BVT passed: 138/138.
  • make static-check remains blocked by the repository's existing go.mod / vendor/modules.txt inconsistency, unrelated to these files.

Fixed in bb5fb4133.

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

Re-reviewed the latest head bb5fb41. The incomplete warning pipeline that caused the remaining blockers has been fully removed, while the core strict/non-strict/IGNORE string-width behavior remains intact. I also rechecked remote sql_mode propagation, function-ID/protocol-version gating, plan-cache invalidation, and generated-column rolling-upgrade compatibility. Local full-package tests passed for pkg/common/moerr, pkg/vm/process, pkg/sql/plan/function, pkg/sql/plan, and pkg/frontend.

@mergify

mergify Bot commented Jul 27, 2026 •

Copy link
Copy Markdown
Contributor

Merge Queue Status

  • ✅ Entered queue — 2026-07-27 11:56 UTC · Rule: main · triggered by rule Automatic queue on approval for main
  • ❌ Checks failed · in-place
  • 🚫 Left the queue — 2026-07-27 15:23 UTC · at 115abf7933a5a1067647449d91a663f6edfd6749

This pull request spent 3 hours 27 minutes 10 seconds in the queue, with no time running CI.

Waiting for
  • any of: [🛡 GitHub branch protection]
    • check-neutral = Matrixone Standlone CI / e2e BVT Test on Linux/x64(LAUNCH, PESSIMISTIC)
    • check-skipped = Matrixone Standlone CI / e2e BVT Test on Linux/x64(LAUNCH, PESSIMISTIC)
    • check-success = Matrixone Standlone CI / e2e BVT Test on Linux/x64(LAUNCH, PESSIMISTIC)
  • any of: [🛡 GitHub branch protection]
    • check-neutral = Matrixone CI / UT Test on Ubuntu/x86
    • check-skipped = Matrixone CI / UT Test on Ubuntu/x86
    • check-success = Matrixone CI / UT Test on Ubuntu/x86
  • any of: [🛡 GitHub branch protection]
    • check-neutral = Matrixone CI / SCA Test on Linux/arm64
    • check-skipped = Matrixone CI / SCA Test on Linux/arm64
    • check-success = Matrixone CI / SCA Test on Linux/arm64
  • any of: [🛡 GitHub branch protection]
    • check-neutral = Matrixone Compose CI / multi cn e2e bvt test docker compose(PROXY)
    • check-skipped = Matrixone Compose CI / multi cn e2e bvt test docker compose(PROXY)
    • check-success = Matrixone Compose CI / multi cn e2e bvt test docker compose(PROXY)
  • any of: [🛡 GitHub branch protection]
    • check-neutral = Matrixone Utils CI / Coverage
    • check-skipped = Matrixone Utils CI / Coverage
    • check-success = Matrixone Utils CI / Coverage
  • any of: [🛡 GitHub branch protection]
    • check-neutral = Matrixone UT Coverage / UT Coverage on Ubuntu/x86
    • check-skipped = Matrixone UT Coverage / UT Coverage on Ubuntu/x86
    • check-success = Matrixone UT Coverage / UT Coverage on Ubuntu/x86
All conditions
  • any of [🛡 GitHub branch protection]:
    • check-neutral = Matrixone Standlone CI / e2e BVT Test on Linux/x64(LAUNCH, PESSIMISTIC)
    • check-skipped = Matrixone Standlone CI / e2e BVT Test on Linux/x64(LAUNCH, PESSIMISTIC)
    • check-success = Matrixone Standlone CI / e2e BVT Test on Linux/x64(LAUNCH, PESSIMISTIC)
  • any of [🛡 GitHub branch protection]:
    • check-neutral = Matrixone CI / UT Test on Ubuntu/x86
    • check-skipped = Matrixone CI / UT Test on Ubuntu/x86
    • check-success = Matrixone CI / UT Test on Ubuntu/x86
  • any of [🛡 GitHub branch protection]:
    • check-neutral = Matrixone CI / SCA Test on Linux/arm64
    • check-skipped = Matrixone CI / SCA Test on Linux/arm64
    • check-success = Matrixone CI / SCA Test on Linux/arm64
  • any of [🛡 GitHub branch protection]:
    • check-neutral = Matrixone Compose CI / multi cn e2e bvt test docker compose(PROXY)
    • check-skipped = Matrixone Compose CI / multi cn e2e bvt test docker compose(PROXY)
    • check-success = Matrixone Compose CI / multi cn e2e bvt test docker compose(PROXY)
  • any of [🛡 GitHub branch protection]:
    • check-neutral = Matrixone Utils CI / Coverage
    • check-skipped = Matrixone Utils CI / Coverage
    • check-success = Matrixone Utils CI / Coverage
  • any of [🛡 GitHub branch protection]:
    • check-neutral = Matrixone UT Coverage / UT Coverage on Ubuntu/x86
    • check-skipped = Matrixone UT Coverage / UT Coverage on Ubuntu/x86
    • check-success = Matrixone UT Coverage / UT Coverage on Ubuntu/x86
  • #review-threads-unresolved = 0 [🛡 GitHub branch protection]
  • github-review-approved [🛡 GitHub branch protection] (documentation)
  • github-review-decision = APPROVED [🛡 GitHub branch protection] (documentation)

Reason

The merge conditions cannot be satisfied due to failing checks

Failing checks:

Hint

You may have to fix your CI before adding the pull request to the queue again.
If you update this pull request, to fix the CI, it will automatically be requeued once the queue conditions match again.
If you think this was a flaky issue instead, you can requeue the pull request, without updating it, by posting a @mergifyio queue comment.

Requeued — the merge queue status continues in this comment ↓.

@mergify

mergify Bot commented Jul 27, 2026 •

Copy link
Copy Markdown
Contributor

Merge Queue Status

  • ✅ Entered queue — 2026-07-27 16:36 UTC · Rule: main · triggered by rule Automatic queue on approval for main
  • ✅ Checks passed · in-place
  • ✅ Merged — 2026-07-27 17:21 UTC · at a8f79ac6c03f9550a0355db9d16444fd2a0d650c · squash

This pull request spent 45 minutes 21 seconds in the queue, including 35 minutes 30 seconds running CI.

Required conditions to merge
  • #review-threads-unresolved = 0 [🛡 GitHub branch protection]
  • github-review-approved [🛡 GitHub branch protection] (documentation)
  • github-review-decision = APPROVED [🛡 GitHub branch protection] (documentation)
  • any of [🛡 GitHub branch protection]:
    • check-success = Matrixone Standlone CI / e2e BVT Test on Linux/x64(LAUNCH, PESSIMISTIC)
    • check-neutral = Matrixone Standlone CI / e2e BVT Test on Linux/x64(LAUNCH, PESSIMISTIC)
    • check-skipped = Matrixone Standlone CI / e2e BVT Test on Linux/x64(LAUNCH, PESSIMISTIC)
  • any of [🛡 GitHub branch protection]:
    • check-success = Matrixone CI / UT Test on Ubuntu/x86
    • check-neutral = Matrixone CI / UT Test on Ubuntu/x86
    • check-skipped = Matrixone CI / UT Test on Ubuntu/x86
  • any of [🛡 GitHub branch protection]:
    • check-success = Matrixone CI / SCA Test on Linux/arm64
    • check-neutral = Matrixone CI / SCA Test on Linux/arm64
    • check-skipped = Matrixone CI / SCA Test on Linux/arm64
  • any of [🛡 GitHub branch protection]:
    • check-success = Matrixone Compose CI / multi cn e2e bvt test docker compose(PROXY)
    • check-neutral = Matrixone Compose CI / multi cn e2e bvt test docker compose(PROXY)
    • check-skipped = Matrixone Compose CI / multi cn e2e bvt test docker compose(PROXY)
  • any of [🛡 GitHub branch protection]:
    • check-success = Matrixone Utils CI / Coverage
    • check-neutral = Matrixone Utils CI / Coverage
    • check-skipped = Matrixone Utils CI / Coverage
  • any of [🛡 GitHub branch protection]:
    • check-success = Matrixone UT Coverage / UT Coverage on Ubuntu/x86
    • check-neutral = Matrixone UT Coverage / UT Coverage on Ubuntu/x86
    • check-skipped = Matrixone UT Coverage / UT Coverage on Ubuntu/x86

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/enhancement kind/test-ci size/XXL Denotes a PR that changes 2000+ lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants