Skip to content

[Bug]: Decimal256 arithmetic precision guard lets MinInt256 escape the declared SQL domain #29615

Closed

Description

@XuPeng-SH

Problem

The common Decimal256 arithmetic result precision guard can publish a coefficient outside the declared SQL DECIMAL domain when the physical coefficient equals MinInt256.

Reproduction

Reproduced with the binder and expression executor on main c3fbe9ce744aa877ec944c86258916fc5e45b63b:

select cast('-57896044618658097711785492504343953926634992332820282019728792003' as decimal(65,0))
     + cast('-0.956564819968' as decimal(65,12));

Both operands are legal in their declared types. Actual result type is DECIMAL(65,12), but its coefficient is -57896044618658097711785492504343953926634992332820282019728792003956564819968 (-2^255), a 77-digit coefficient. Expected: OutOfRange rather than publication under a precision-65 type.

Nearest controls: changing the fractional input to -0.956564819967 correctly rejects at the declared precision guard; the positive counterpart correctly rejects at the physical kernel. Native function tests additionally reproduce the missing rejection for vector/vector, scalar/vector and vector/scalar input paths.

Root cause and scope

decimal256BatchArith in pkg/sql/plan/function/arithmetic.go owns the post-kernel declared precision check shared by addition, subtraction, multiplication and division. d256Abs(MinInt256) leaves the sign bit set because the unsigned magnitude is 2^255. The subsequent signed Decimal256.Less(limit) incorrectly treats it as a negative number below the positive limit.

This is distinct from primitive rounded division: the failing boundary is publication of a physical arithmetic coefficient under a narrower SQL precision.

Proposed repair and regression

Keep validation at the existing common owner. After absolute-value normalization, a remaining sign bit proves the coefficient exceeds any constrained precision limit; reject it before the signed comparison. Retain internal width-76 behavior and NULL/selection semantics.

Unify existing multiplication declared precision tests with common arithmetic precision cases; preserve independent division rounding/scale tests. Cover exact positive/negative logical boundaries, legal-input MinInt256 across VV/SV/VS, NULL/filtered rows, and a public SQL binder/executor regression. Verify incremental static analysis, normal and race tests. No SQL throughput claim is made from these checks.

Activity

  1. self-assigned this
    on Oct 4, 2026
  2. added
    kind/bug
    severity/s0
    on Oct 4, 2026
  3. added a commit that references this issue on Oct 5, 2026
    856e9dd
  4. Ariznawlll commented on Oct 8, 2026

    @Ariznawlll
    Collaborator

    修复后交叉覆盖:DDL/DML 原子性、prepared 恢复、聚合与窗口

    测试版本:main bab4b3286a0dd5683a9b291763817722233e586c,包含 #29618;本地单 CN/TN。

    使用原 issue 的 MinInt256 边界表达式:

    CAST('-57896044618658097711785492504343953926634992332820282019728792003' AS DECIMAL(65,0))
    + CAST('-0.956564819968' AS DECIMAL(65,12))

    当前行为均为明确的 exceeds DECIMAL(65,12) 错误,没有发布 77 位系数:

    • 直接 SELECT;
    • CTAS:语句拒绝后目标表不存在;
    • INSERT…SELECT:目标表原有行保持不变;
    • STORED 生成列 INSERT:没有插入失败行;
    • STORED 生成列 UPDATE:原行三个列值均保持不变;
    • 三组双行 INSERT(第一行合法、第二行越界):每组均未留下第一行,语句原子性保持。

    同一 prepared statement 按“越界 → -1 + -0.5 → NULL → 2 + 0.25”顺序重绑定:第一次返回精度错误,之后分别得到 -1.500000000000、NULL、2.250000000000;连接和 prepared 状态可继续使用。

    进一步使用 DECIMAL(65,0) 正负最大值交叉聚合:

    • 全局 SUM 和 AVG 在正负值抵消后返回 0;无 ORDER 的窗口 SUM 对每行返回 0;
    • 分组 SUM 以及有序累计窗口在首个分组/前缀达到 66 位时返回 beyond the range ... Decimal256(65,0);
    • 聚合错误后连接保持可用,源表 5 行未改变。

    这些场景说明 #29618 的声明精度保护已经传播到直接表达式、物化、生成列、prepared、批量写入及聚合最终输出,并保持失败原子性。

  5. Ariznawlll commented on Oct 8, 2026

    @Ariznawlll
    Collaborator

    测试版本

    MatrixOne 最新 main 1a0088790c8823bd3fe824ad9ff2ef1ce130eb31;本地独立数据目录、单 CN/TN 服务;MOTR PR #218 已推送,Python 3.10/3.12/3.13 与 Go 离线检查均通过。

    原问题验证

    原 MinInt256 算式现在返回 1690(exceeds DECIMAL(65,12)),不再把 77 位系数作为 DECIMAL(65,12) 返回。

    正常场景覆盖

    同组 SQL 覆盖合法的 -1 + 3e-65,得到精确的负小数;查询表仍保有 3 行。

    边界与异常场景覆盖

    正向 1 + 3e-65 与原 MinInt256 越界都报范围错误;错误后 SELECT 1 成功,NULL 行保持 NULL。

    白盒验证

    既有 #29618 修复及 MatrixOne 算术回归覆盖声明精度检查;本轮用真实 SQL/向量行核对对外行为。

    回归测试

    MOTR 14_issue_regression.issue_29607_29610_29615_decimal256_boundaries,3/3 通过(13 条语句/轮)。

    测试证据

    MOTR 用例与 golden;原 issue 此前 DDL/DML 原子性、prepared 重绑与聚合/窗口交叉测试记录仍有效。

    测试结论

    测试通过。

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

Metadata

Metadata

Assignees

Labels

kind/bugseverity/s0

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions