Skip to content

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

Closed
ck89119 wants to merge 7 commits into
matrixorigin:3.0-devfrom
ck89119:issue-25200-3.0-dev
Closed

ck89119 wants to merge 7 commits into
matrixorigin:3.0-devfrom
ck89119:issue-25200-3.0-dev

Conversation

@ck89119

@ck89119 ck89119 commented Jun 30, 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:

Follow-up to #25162. It aligns CHAR/VARCHAR width enforcement on DML assignment
paths (INSERT projection / INSERT ... SELECT / INSERT VALUES rowset / INSERT ...
ON DUPLICATE KEY UPDATE / UPDATE) with MySQL's sql_mode semantics, while
keeping DDL DEFAULT / ON UPDATE validation strict — MySQL enforces those at
the DDL layer (error 1067), independent of sql_mode.

Items 1–3 of the issue are addressed. Item 4 (the 1265 Data truncated warning)
is intentionally out of scope; truncation is silent for now.

1. sql_mode gating on assignment paths (runtime)

Adds a new internal cast_assign function used by DML assignment paths for
CHAR/VARCHAR targets. It reads sql_mode at runtime:

  • strict (STRICT_TRANS_TABLES / STRICT_ALL_TABLES) → reject (1406);
  • non-strict → truncate.

The gating is done at runtime rather than at plan-build time on purpose: MO
caches plans (pkg/frontend/plan_cache.go), so baking the cast vs cast_strict
choice into the plan would go stale after SET sql_mode. This mirrors how the
LOAD DATA path already resolves strictness at runtime. The existing
cast_strict is retained for DDL DEFAULT / ON UPDATE validation
(makePlan2AssignmentCastExpr) and stays always-strict.

2. Trailing-space exemption

Over-length values whose excess runes are only trailing spaces are accepted
(truncated) even under strict mode, matching MySQL. The exemption applies to
cast_assign; the DDL cast_strict does not apply it.

3. INSERT IGNORE downgrade

Under INSERT IGNORE, over-length writes are truncated instead of rejected.
INSERT IGNORE is part of the SQL text (parsed as OnDuplicateUpdate == [nil]),
so it is gated at plan time (cache-safe) via a QueryBuilder.isInsertIgnore flag.

INSERT VALUES constant path

INSERT VALUES string literals are constant-folded through
util.SetInsertValueString (not a cast function). The same
sql_mode / trailing-space / INSERT IGNORE resolution is applied there via a
new function.ResolveAssignStringWidth helper, with the IGNORE flag threaded
through MakeInsertValueConstExpr.

Tests

  • UT: pkg/sql/plan/function/func_cast_width_test.go (strToStr width matrix,
    isStrictSqlMode, NewAssignCast, ResolveAssignStringWidth) and plan-level
    DML / IGNORE / DDL cast assertions in pkg/sql/plan/build_test.go.
  • BVT: test/distributed/cases/dml/insert/insert_string_width_sqlmode.
  • No regression on the existing update_text_coalesce_cast and
    load_data_string_width BVTs.

Validation:

  • go test ./pkg/sql/plan/function ./pkg/sql/plan ./pkg/sql/util ./pkg/sql/colexec/external
  • make build
  • mo-tester genrs + run on the new and existing BVTs (100%)

🤖 Generated with Claude Code

…matrixorigin#25200)

Rework CHAR/VARCHAR width enforcement on DML assignment paths so it matches
MySQL's sql_mode semantics, while keeping DDL DEFAULT/ON UPDATE validation
strict (MySQL enforces those at the DDL layer, independent of sql_mode).

- 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 the plan-cache
  staleness a plan-time choice would introduce. `cast_strict` is kept for DDL
  DEFAULT/ON UPDATE validation and remains always-strict.
- trailing-space exemption: over-length values whose excess is only trailing
  spaces are accepted (truncated) even under strict mode, matching MySQL. The
  exemption applies to `cast_assign` but not to the DDL `cast_strict`.
- INSERT IGNORE downgrade: under IGNORE, over-length writes are truncated
  instead of rejected. Detected at plan time (cache-safe) via the
  QueryBuilder.isInsertIgnore flag.

INSERT VALUES string literals are constant-folded through
util.SetInsertValueString (not a cast function), so the same
sql_mode/trailing-space/IGNORE resolution is applied there via the new
function.ResolveAssignStringWidth helper.

Item 4 from the issue (1265 truncation warning) is intentionally out of scope;
truncation is silent.

Tests: new func_cast width UTs and plan-level DML/IGNORE/DDL assertions; new BVT
test/distributed/cases/dml/insert/insert_string_width_sqlmode. Existing
update_text_coalesce_cast and load_data_string_width BVTs unchanged.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@ck89119
ck89119 requested a review from a user June 30, 2026 10:21
@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 →

The function was orphaned when DML assignment tests moved from asserting
cast_strict to cast_assign. The DDL path (cast_strict) has its own dedicated
test helper in build_util_test.go, so this helper is no longer needed.

Co-Authored-By: Claude <[email protected]>

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

I found several correctness blockers here.

  1. INSERT IGNORE still rejects non-literal VALUES expressions.

The IGNORE downgrade is only threaded into the tree.NumVal fast path. For non-literal VALUES expressions such as repeat('x', 100), planning falls through to forceCastExpr2(...), which hardwires cast_assign for CHAR/VARCHAR. So in strict mode INSERT IGNORE still throws 1406 instead of truncating.

Suggested fix: thread isInsertIgnore through the non-literal VALUES path as well, or replace forceCastExpr2 there with an assignment-cast helper that can choose lenient cast under IGNORE.

  1. Prepared INSERT ... VALUES ('literal') still freezes sql_mode at prepare time.

The constant-literal path resolves truncation vs rejection during planning via SetInsertValueString(...), so the decision is baked into the prepared plan. That means changing sql_mode after PREPARE does not reliably affect literal INSERT VALUES statements, which is exactly the stale-plan problem this PR is trying to avoid elsewhere with runtime cast_assign.

Suggested fix: defer width enforcement for literal VALUES strings to execution time as well, instead of materializing the post-truncation value during planning.

  1. The new constant-path helper now relaxes VARBINARY / non-CHAR targets too.

SetInsertValueString applies ResolveAssignStringWidth(...) to every non-TEXT / non-DATALINK / non-BINARY string literal, not just CHAR/VARCHAR. That includes VARBINARY, but the runtime assignment logic only grants sql_mode/trailing-space leniency to CHAR/VARCHAR. So overflow behavior can now depend on whether the source is a literal VALUES constant or an expression / INSERT ... SELECT.

Suggested fix: restrict ResolveAssignStringWidth in SetInsertValueString to CHAR/VARCHAR targets only.

  1. The fallback planner path never sets isInsertIgnore.

builder.isInsertIgnore is initialized in bindInsert, but not in buildInsert. When planning falls back from bindInsert to buildInsert on ErrUnsupportedDML, the fallback code still consults builder.isInsertIgnore in its cast/constant helpers, but the flag is always false. So INSERT IGNORE semantics become planner-path dependent.

Suggested fix: initialize builder.isInsertIgnore in buildInsert the same way as in bindInsert, and add a regression that forces the fallback path.

Coverage is otherwise fairly good, but these are code-level blockers rather than just test gaps.

…n function tests

CHAR/VARCHAR INSERT VALUES now routes through runtime cast_assign,
changing error format from 'Can't cast X to Y type' to
'Can't cast X from VARCHAR type to Y type'.
Matches the format already updated in insert_string_width_sqlmode.result.

Co-Authored-By: Claude <[email protected]>

This branch was previously deployed

1 inactive deployment
ci — 7d44b582 Deployed Jul 1, 2026 by ck89119 via Matrixone Utils CI (3.0) / Coverage #39508
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/L Denotes a PR that changes [500,999] lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants