Repository navigation
Description
Description
DECIMAL256 multiplication rejects a product because the unscaled
intermediate coefficient is wider than 256 bits, even when the documented
output scale reduces the final value to a representable 65-digit result.
This is a base arithmetic-kernel contract gap exposed by PR #28825's correct
preservation of wide unquoted decimal literals.
Reproduction
The following direct kernel witness is on main
(f9a44447743fff827dab01300837d27a3d9811f0):
x, sx, _ := types.Parse256("0.32846287164921643232142372817438921749321")
y, sy, _ := types.Parse256("0.9999999999999999999999999999999999999")
err := d256Mul([]types.Decimal256{x}, []types.Decimal256{y}, result, sx, sy, nulls.NewWithSize(1))The scales are 41 and 37, so the result scale is 41 and the raw
coefficient is reduced by 10^37. The final value is representable, but the
current kernel returns:
invalid input: Decimal256 Mul overflow: 0.32846287164921643232142372817438921749321*0.9999999999999999999999999999999999999
The same public shape appears in PR #28825's Compose/PESS BVT at
test/distributed/cases/dtype/decimal.test rows 616, 617, and 636 after the
literal is correctly retained as Decimal256. The old Decimal128 result is not
a valid oracle because it was produced from a silently rounded literal.
Root cause
pkg/sql/plan/function/arith_decimal_fast.go::d256Mul computes the output
scale, but d256MulInline first requires the complete unscaled product to fit
in signed 256-bit storage. It reports overflow before applying the negative
scale adjustment. Raw intermediate overflow and final result overflow are
different conditions.
Expected behavior
When raw multiplication overflows but one target-scale reduction can produce a
value within the result domain, multiplication should round once at the target
scale and succeed. Genuine final overflow must still return the normal
DECIMAL out-of-range error.
Scope and constraints
- Preserve the existing fast paths and the integer
Decimal256.Mul256
contract; do not silently change integer multiplication semantics. - Cover vector/vector, constant/vector, vector/constant, signs, zero, NULL and
selection masks. - Cover raw-overflow/final-fit, raw-overflow/final-overflow, rounding carry,
and negative values with an independent high-precision oracle. - Use a bounded fixed-width (for example 512-bit limbs) fallback only on the
raw-overflow plus scale-down path; do not allocate an unboundedbig.Intper
row or regress normal Decimal256 multiplication.
Related: #28825 (wide literal preservation), #28584/#28629 (Decimal128
arithmetic widening). This issue is specifically the Decimal256 raw
intermediate/target-scale boundary and is not a duplicate of result-type
widening.
Activity
XuPeng-SH commentedon Sep 14, 2026
Fix queued in PR #28878: #28878. The implementation also closes related #28876 without duplicating the fixed-width arithmetic.
fix(decimal): handle Decimal256 wide multiplication scale reduction (#…
Ariznawlll commentedon Sep 15, 2026
测试版本
main:0c3a04f390adaf6281fd592a49778ea5b1155e67原问题验证
复现 raw product 一度溢出、但缩 scale 后结果仍可表示的 Decimal256 乘法;当前实现接受该结果。
Happy Path 覆盖
覆盖 raw-overflow 后缩 scale 可表示、边界 scale 对齐及高 scale 小值乘法。
Unhappy Path 覆盖
覆盖缩 scale 后仍不可表示的结果,确认仍返回受控溢出错误。
白盒验证
pkg/sql/plan/function中 Decimal256 缩 scale 乘法的定向测试连续 3 次通过,包含 raw-overflow 可恢复与不可恢复两类路径。回归测试
该问题为内部 Decimal256 算术内核路径;公开 SQL 没有
DECIMAL256类型声明,MOTR 不具备稳定的触发入口,因此保留在 MatrixOne 单元测试层回归。测试证据
定向 Go UT:3/3 通过;使用
TestD256MulScaledRawOverflow、TestD256MulScaledRawOverflowPreservesOverflow、TestD256MulScaledFallbackChunkBoundaries、TestD256MulHighScaleSmallValues。测试结论
测试通过。
fix(decimal): handle Decimal256 wide multiplication scale reduction (m…
fix(decimal): handle Decimal256 wide multiplication scale reduction (m…
fix(decimal): handle Decimal256 wide multiplication scale reduction (m…