Repository navigation
Description
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
fix: consolidate decimal expression contracts and historical bug regr…
Ariznawlll commentedon Oct 8, 2026
修复后交叉覆盖:DDL/DML 原子性、prepared 恢复、聚合与窗口
测试版本:
mainbab4b3286a0dd5683a9b291763817722233e586c,包含 #29618;本地单 CN/TN。使用原 issue 的 MinInt256 边界表达式:
当前行为均为明确的
exceeds DECIMAL(65,12)错误,没有发布 77 位系数:同一 prepared statement 按“越界 →
-1 + -0.5→ NULL →2 + 0.25”顺序重绑定:第一次返回精度错误,之后分别得到-1.500000000000、NULL、2.250000000000;连接和 prepared 状态可继续使用。进一步使用
DECIMAL(65,0)正负最大值交叉聚合:SUM和AVG在正负值抵消后返回 0;无 ORDER 的窗口 SUM 对每行返回 0;beyond the range ... Decimal256(65,0);这些场景说明 #29618 的声明精度保护已经传播到直接表达式、物化、生成列、prepared、批量写入及聚合最终输出,并保持失败原子性。
Ariznawlll commentedon Oct 8, 2026
测试版本
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 重绑与聚合/窗口交叉测试记录仍有效。
测试结论
测试通过。