Skip to content

[Bug]: integer assignments bypass DECIMAL precision and persist out-of-range values #30367

Closed

Description

@Ariznawlll

Description

In strict SQL mode, assigning an integer to a DECIMAL(p,s) column can bypass the declared precision check. MatrixOne accepts 2 into DECIMAL(2,2) and persists 2.00, although the positive maximum for that type is 0.99. The equivalent decimal or string representation is correctly rejected. The same gap appears while copying existing integer rows through ALTER TABLE ... ALGORITHM=COPY.

Environment

  • Branch: main
  • Commit: 48dbe277b6d20216c1c513d03c5cbd681380a8a2
  • Deployment: isolated local MatrixOne 1 LogService / 1 TN / 2 CN (127.0.0.1:16041 and :16042); git_version()=48dbe277b6, version()=8.0.30-MatrixOne-v1.3.0
  • SQL mode: STRICT_TRANS_TABLES enabled
  • Comparator: isolated MySQL 8.0.45 with STRICT_TRANS_TABLES enabled
  • Date: 2026-10-16

Steps to reproduce

CREATE DATABASE decimal_integer_assignment_repro;
USE decimal_integer_assignment_repro;
CREATE TABLE t(id INT PRIMARY KEY, v DECIMAL(2,2));
INSERT INTO t VALUES (1, 0.99); -- valid boundary
INSERT INTO t VALUES (2, 2);    -- outside DECIMAL(2,2)
SELECT id, v FROM t ORDER BY id;

Actual behavior

MatrixOne accepts the second INSERT and returns:

id  v
1   0.99
2   2.00

It also accepts INSERT ... VALUES(10) into DECIMAL(3,2) and stores 10.00 (valid maximum is 9.99). A separate table copied from an INT source via ALTER TABLE ... ALGORITHM=COPY, MODIFY COLUMN rawv DECIMAL(2,2) likewise retained existing 2.00, 3.00, and 4.00 in the narrowed column.

Expected behavior

Strict assignment should reject the out-of-range integer and leave the table unchanged. MySQL 8.0.45 returns ERROR 1264 (22003): Out of range value for column 'v' at row 1 for the same INSERT and ALTER TABLE narrowing.

Stability and controls

  • MatrixOne: 3/3 independently created databases across both CNs accepted the integer 2 into DECIMAL(2,2) and read it back as 2.00 from a fresh connection on the other CN.
  • MySQL 8.0.45: 3/3 independent databases rejected the integer 2 with 1264 and retained only the valid 0.99 row.
  • MatrixOne correctly accepted valid 0.99 and -0.99 boundaries; a valid INSERT after the failed string control worked, so the connection remained usable.
  • MatrixOne rejected INSERT ... VALUES('2.00') with 20301: 2.00 beyond the range, can't be converted to Decimal64(2,2) in each database, left no row for that rejected statement, and also rejected numeric decimal literal 1.00. This isolates an integer-source conversion gap, not a missing decimal range rule generally.
  • On a cloned table with a stored generated column, UNIQUE key and CHECK, COPY ALTER from INT/BIGINT to DECIMAL(2,2) also succeeded and retained out-of-range rows. An independent duplicate-key UPDATE returned 1062, and an invalid CHECK INSERT returned 3819; source table and nonviolating rows remained unchanged.

Evidence

Exact SQL outputs, versions, comparison results and plan observations are in /private/tmp/mo-explore-run-20261016/checkpoint.md on the isolated local test host.

Code analysis

pkg/sql/plan/function/func_cast.go:4022-4042 (signedToDecimal64) scales the integer into the Decimal64 physical carrier and appends it without checking to.GetType().Width; it also discards the Scale error. The unsigned and wider-decimal integer conversions nearby follow a similar shape. By contrast, decimal/string paths call precision-aware parsing. This is a code-level hypothesis for the observed source-dependent difference; the issue does not claim that CLONE itself corrupts rows.

Existing TestCast and TestCastNumericToDecimal256Dispatcher Go UTs passed on this commit but do not assert the narrow target-precision boundary; they do not invalidate the SQL repro.

Regression coverage

After a fix, add a focused cast/assignment UT plus public SQL regression for integer literal, prepared integer, and INT-column COPY ALTER into DECIMAL(2,2) and DECIMAL(3,2), with strict error, unchanged rows/schema on failure, valid boundary and same-scale string/decimal controls. No repository tests or PR were added during this exploration.

Related

Activity

  1. added this to the MO-202610 milestone on Oct 16, 2026
  2. added
    kind/bug
    on Oct 16, 2026
  3. added theissue type on Oct 16, 2026
  4. Ariznawlll commented on Oct 16, 2026

    @Ariznawlll
    CollaboratorAuthor

    补充同一问题的执行路径边界(固定 main commit 48dbe277b6d20216c1c513d03c5cbd681380a8a2,隔离双 CN;MySQL 8.0.45 对照;两端均启用 STRICT_TRANS_TABLES):

    1. SQL PREPARE/EXECUTE 也会写入越界整数。 在三个独立库各建 t(id INT PRIMARY KEY, v DECIMAL(2,2)),执行:

      PREPARE ins FROM 'INSERT INTO t(id,v) VALUES(?,?)';
      SET @id=5,@v=5;
      EXECUTE ins USING @id,@v;
      SELECT id,v FROM t WHERE id=5;

      MO 在两个 CN 上均成功写入 (5,5.00)(3/3);MySQL 均报 1264 / 22003: Out of range value for column 'v' at row 1(3/3),且没有写入 id=5。

    2. 同一 prepared statement 的输入类型切换与错误恢复。 在 MO 的同一连接中,把 @v 改成字符串 '2.00' 后 EXECUTE 报 20301: 2.00 beyond the range, can't be converted to Decimal64(2,2),表未增加该行;再改成 -0.99 后复用同一 ins 成功写入 (4,-0.99)。MySQL 对整数 2 和字符串 '2.00' 均拒绝,随后复用该语句写入 -0.99 成功。这说明连接和 prepared statement 在错误后可用,缺口集中在整数值到目标 DECIMAL 精度的转换。

    3. 相关控制。 CAST(6 AS UNSIGNED) 写入 DECIMAL(2,2) 时,MO 也接受并保存 6.00,MySQL 报 1264(当前该变体仅执行 1 轮)。相反,原列已是 DECIMAL(3,2) 且值为 2.00 时,ALTER TABLE ... ALGORITHM=COPY, MODIFY COLUMN v DECIMAL(2,2) 在 MO 与 MySQL 均被拒绝;MO 错误后仍保留原 DECIMAL(3,2) 定义及原行,随后可继续正常写入。先前 issue 正文中的 INT/BIGINT 来源 COPY ALTER 却可成功保留 2.00 等越界值,进一步指向整数来源的 cast 路径,而不是所有 COPY ALTER 都跳过检查。

    白盒对应 pkg/sql/plan/function/func_cast.go 的 signedToDecimal64 / unsignedToDecimal64:当前按目标 scale 写入物理 Decimal64,但未使用目标 width 验证精度。这里是代码与现象一致的定位线索,不是声称所有 DECIMAL 转换路径均有此问题。本轮没有增加仓库用例或 PR。

  5. XuPeng-SH commented on Oct 22, 2026

    @XuPeng-SH
    Contributor

    PR #30639 review update (head 7134ffe6d66c9a9e6d7d4f3459e95a357e760055)

    XuPeng-SH submitted a current-head CHANGES_REQUESTED review on 2026-10-22 identifying an unresolved compatibility case in pkg/sql/plan/catalog_decimal_compat.go: persisted generated-column expressions using four-argument IN_RANGE are not included in the legacy implicit-decimal migration. The review reports that a base-authored stored expression over BIGINT and TIME(0) remains Decimal64(18,0) after upgrade; writing 10^18-1 succeeds, while 10^18 fails the new width check, although fresh head binding widens to Decimal128(19,0). It requests migrating this IN_RANGE form and adding an old-catalog/new-reader regression.

    This is reviewer-provided parser/planner/catalog/executor evidence, not an execution I performed in this run. The report notes frozen CI statuses pass but does not provide raw CI logs; the persisted-expression compatibility case remains open. No duplicate issue opened. Issue remains open and assigned to daviszhen pending a fix and validation.

  6. XuPeng-SH commented on Oct 22, 2026

    @XuPeng-SH
    Contributor

    PR #30639 current-head performance findings (head a1027b586d37b067ab0b67e0187de8fe28503c0e)

    The latest review reports two P2 performance regressions in the fix path. These numbers are from the reviewer’s benchmark report; I have not rerun those benchmarks in this QA run.

    1. Unchanged table-resolution allocations — pkg/sql/plan/catalog_decimal_compat.go:61-65 copies each non-nil column (owned := *col) before knowing whether any legacy DECIMAL expression changed. The review reports 100-column resolution rising from 2 allocations / 1,344 bytes to 102 / 36,544 bytes, and NULL-default resolution from 102 to 202 allocations (+35,200 bytes). Consider cloning the column only after a default, on-update, or generated expression actually changes.

    2. Per-row DECIMAL256 bound construction — pkg/sql/plan/function/func_cast.go:4080-4099 reconstructs the width bound with repeated wide multiplications inside checkIntDecimal256Width; integer-to-DECIMAL vector conversion invokes this validation for each row. The review reports 4,096-row serial median latency of 14.29→95.80 μs for DECIMAL256 and 14.54→30.13 μs for DECIMAL128. Consider computing the invariant bound once per vector/batch or precomputing it, while preserving strict-width boundary tests.

    Please retain both findings in the fix/verification scope for #30367. Static inspection here confirms the eager column copy and repeated bound construction at the cited exact-head locations; no runtime benchmark or MatrixOne integration test was run in this environment.

    Current-head verification checkpoint (2026-10-22)

    PR #30639 is now at exact head 78c61bd8309c613c616cd59d72592064beed538e. Its current-head APPROVED review states that the earlier correctness, persisted-catalog compatibility, and performance findings are addressed; it reports eight targeted test roots run three times, with retained artifacts/receipts reconciled. This is reviewer-reported evidence, not a test run by this QA automation. The review also notes that no live rolling-upgrade test was performed.

    Issue #30367 remains open. It is already assigned to tester Ariznawlll, so no duplicate assignment was made. The PR is not merged yet; the issue remains open under the repository workflow.

  7. XuPeng-SH commented on Oct 23, 2026

    @XuPeng-SH
    Contributor

    QA handoff checkpoint (2026-10-22): linked fix PR #30639 is at 78c61bd8309c613c616cd59d72592064beed538e. XuPeng-SH submitted an APPROVED review on this exact head; the prior precision, BIT comparison, persisted catalog compatibility, and performance findings have fixes with resolved review threads. The exact-head MatrixOne ALL CI, Iceberg Connector E2E, and Connector/J Pool Reset E2E workflows all completed successfully. This environment did not run MatrixOne locally; the test details are from the review/CI evidence. Transferring this issue to Ariznawlll for tester verification; issue remains open.

  8. XuPeng-SH commented on Oct 23, 2026

    @XuPeng-SH
    Contributor

    PR #30639 current-head DUMP/LOAD 阻塞(head 78c61bd8309c613c616cd59d72592064beed538e)

    aptend 在该 head 的最新 CHANGES_REQUESTED review 中报告了一个尚未覆盖的目录兼容性回归:对旧版本创建并持久化的 CHECK / STORED generated-column 表达式,DUMP 路径绕过编译期 resolver/migration,LOAD 对有效的旧 bound-expression payload 也可能因当前绑定校验拒绝而失败。

    最小例子:

    CREATE TABLE t(i BIGINT, tm TIME(0), CONSTRAINT ck CHECK(i > tm));

    评审报告称,base planner 生成的旧 TableDef 在新 head 上执行 DUMP 会报 table dump cannot verify bound check expression ck;同类 STORED generated-column 表达式也失败。LOAD 旧备份中有效 CHECK payload 的验证同样失败。评审称其用 base 生成的 protobuf 在 head 重放确认,并通过 fresh-table / explicit-CAST controls;这是 review 提供的验证,我本轮没有独立重跑。

    请修复 DUMP 和 LOAD 的旧表达式绑定处理,并增加旧 TableDef / 旧 payload 的回归覆盖;需继续保留严格赋值精度、显式 CAST 与 payload 完整性校验。该问题尚未修复并验证,本 issue 暂不转测;目前仍 open、assigned to Ariznawlll。

  9. Ariznawlll commented on Oct 26, 2026

    @Ariznawlll
    CollaboratorAuthor

    测试版本

    • 分支:官方 main
    • commit:28e8760905a7f39dba2ce436357ea75492f30512;隔离 worktree 从该 SHA 构建的实际服务返回 git_version()=28e8760905,两个 CN 均核对。
    • 修复:MatrixOne PR #30639 已 MERGED,merge commit 3de2ee56e840fedb8b9994002a4fcbcb049c37e0 经 git merge-base --is-ancestor 确认在该 main 祖先链上。
    • 部署方式:本机隔离 2 CN / 1 TN / 1 LogService;通过两个实际 MySQL 协议端口分别写入、读回。未使用共享 TKE。
    • 测试日期:2026-10-26。

    原问题验证

    每轮新建独立数据库,共 3/3 轮。在 STRICT_TRANS_TABLES 下向 DECIMAL(2,2) 插入整数 2,三轮均返回 20301: 2 beyond the range, can't be converted to Decimal64(2,2),没有写入原 issue 的错误值 2.00。另一 CN 读回原有三行始终为 (1,0.99),(2,-0.99),(3,0.00)。将含 0,2,3 的 INT 列用 ALGORITHM=COPY 改为 DECIMAL(2,2),三轮均因 2 越界拒绝,另一 CN 核对行仍为 0,2,3 且列类型仍是 int。

    Happy Path 覆盖

    1. DECIMAL(2,2) 的 0.99、-0.99 和 0 均正确存储;DECIMAL(3,2) 的 9.99 正确存储。3/3。
    2. 两 CN 跨端读回值、列类型及测试库删除后不存在均核对通过。3/3。

    Unhappy Path 覆盖

    1. 整数 2、-2、1,字符串 '2.00' 与小数 1.00 写入 DECIMAL(2,2) 均被拒绝;每次失败后再次从另一 CN 读回,原三行未变。3/3。
    2. INSERT SELECT 从 INT 源列写入越界值以及 SQL 服务端 PREPARE/EXECUTE 绑定整数 2,均返回 20301,目标表无部分行。3/3。
    3. 整数 10 写入 DECIMAL(3,2) 被拒绝,合法 9.99 仍在。3/3。
    4. COPY ALTER 失败后源表数据和 schema 均保持原状。3/3。

    白盒验证

    合入后的完整 main 上,TestIntToDecimalWidthContract、TestIntLiteralIntoColumnKeepsDecimalWidth、TestTableDumpHistoricalDecimalBoundPayload、TestTableDumpPersistedDecimalBindings 使用 -race -count=3 定向通过;TestDecimalHistoricalDumpRoundtrip 也用实际 SQL 集成路径 -race -count=3 通过。历史 DUMP/LOAD 的序列化夹具得到覆盖,但本轮未执行真实旧二进制到新二进制的滚动升级。

    回归测试

    • 已按约定添加到 MOTR PR #278:issue_30367_integer_decimal_width.test 与配对 .result。隔离服务上录制后定向重放 3/3、共 90/90 SQL 语句通过,未把错误值写成成功 golden。
    • PR #278 离线 CI 的 4 个 job 均通过;修复 PR #30639 的 CI 中 required、普通/race UT、coverage、arm64 SCA 和双多 CN BVT 均通过。本轮未重跑 MOTR 全部 631 个 issue 回归用例或完整 MO BVT,不宣称已执行。

    测试证据

    上述 MOTR 提交的 SQL/期望结果可直接复跑;本轮隔离脚本及 3 轮实际值保留在 /private/tmp/mo-six-qa-20261026/qa-current/verify_30367.py。修复前原 issue 的 3/3 复现记录保留在 issue 正文。

    测试结论

    测试通过。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

kind/bug

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions