Repository navigation
Conversation
…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]>
Qodo reviews are paused for this user.Troubleshooting steps vary by plan Learn more → On a Teams plan? Using GitHub Enterprise Server, GitLab Self-Managed, or Bitbucket Data Center? |
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
left a comment
There was a problem hiding this comment.
I found several correctness blockers here.
INSERT IGNOREstill rejects non-literalVALUESexpressions.
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.
- Prepared
INSERT ... VALUES ('literal')still freezessql_modeat 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.
- The new constant-path helper now relaxes
VARBINARY/ non-CHARtargets 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.
- 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]>
What type of PR is this?
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_modesemantics, whilekeeping DDL
DEFAULT/ON UPDATEvalidation strict — MySQL enforces those atthe DDL layer (error 1067), independent of
sql_mode.Items 1–3 of the issue are addressed. Item 4 (the
1265 Data truncatedwarning)is intentionally out of scope; truncation is silent for now.
1.
sql_modegating on assignment paths (runtime)Adds a new internal
cast_assignfunction used by DML assignment paths forCHAR/VARCHAR targets. It reads
sql_modeat runtime:STRICT_TRANS_TABLES/STRICT_ALL_TABLES) → reject (1406);The gating is done at runtime rather than at plan-build time on purpose: MO
caches plans (
pkg/frontend/plan_cache.go), so baking thecastvscast_strictchoice into the plan would go stale after
SET sql_mode. This mirrors how theLOAD DATApath already resolves strictness at runtime. The existingcast_strictis retained for DDLDEFAULT/ON UPDATEvalidation(
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 DDLcast_strictdoes not apply it.3.
INSERT IGNOREdowngradeUnder
INSERT IGNORE, over-length writes are truncated instead of rejected.INSERT IGNOREis part of the SQL text (parsed asOnDuplicateUpdate == [nil]),so it is gated at plan time (cache-safe) via a
QueryBuilder.isInsertIgnoreflag.INSERT VALUES constant path
INSERT VALUES string literals are constant-folded through
util.SetInsertValueString(not a cast function). The samesql_mode/ trailing-space /INSERT IGNOREresolution is applied there via anew
function.ResolveAssignStringWidthhelper, with the IGNORE flag threadedthrough
MakeInsertValueConstExpr.Tests
pkg/sql/plan/function/func_cast_width_test.go(strToStr width matrix,isStrictSqlMode,NewAssignCast,ResolveAssignStringWidth) and plan-levelDML / IGNORE / DDL cast assertions in
pkg/sql/plan/build_test.go.test/distributed/cases/dml/insert/insert_string_width_sqlmode.update_text_coalesce_castandload_data_string_widthBVTs.Validation:
go test ./pkg/sql/plan/function ./pkg/sql/plan ./pkg/sql/util ./pkg/sql/colexec/externalmake build🤖 Generated with Claude Code