Skip to content

[Bug]: Decimal256 vector alignment and modulo panic beyond 38-digit scale differences #29607

Closed

Description

@XuPeng-SH

Problem

Discovered while consolidating exact arithmetic test oracles for #29249. Decimal256 addition/subtraction can panic for vector scale alignment above 38 digits. Modulo has the same failure because it admits D128 narrowing using coefficient size without checking the scale domain.

Latest main checked: 3eab55f2ab. Its five affected owner bodies are byte-identical to the reproduced failing baseline: scalePow10Factors, d256AddDiffScale, d256SubDiffScale, d256Mod, d256ModViaD128.

Reproduction

create table decimal_alignment_repro (id int primary key, x int);
insert into decimal_alignment_repro values (1,1),(2,2);
select cast(x as decimal(20,0)) + cast('3e-39' as decimal(65,39))
from decimal_alignment_repro order by id;
select cast(x as decimal(20,0)) - cast('3e-39' as decimal(65,39))
from decimal_alignment_repro order by id;
select cast(x as decimal(20,0)) % cast('3e-39' as decimal(65,39))
from decimal_alignment_repro order by id;
drop table decimal_alignment_repro;

Actual parser → planner → expression-executor tests with two real scan rows reproduce all three failures at scale39. The scale38 controls return exact results. Kernel probes additionally distinguish scalar/vector shapes: existing scalar-upscale addition/subtraction paths work; vector-upscale paths panic. Two scan rows suffice for these witnesses.

Root cause and required behavior

The shared two-factor helper represents only exponents 0..38. For39 it accesses Pow10[39-19], index20 of a20-element array. Eight fused Decimal256 Add/Sub preparations can reach it outside that domain. Modulo's narrowing admission proves that coefficients fit D128 but does not prove that scale difference is at most38.

Alignment should compute the exact coefficient with existing checked chunking, or return the established overflow error. Masked/NULL rows must bypass arithmetic. Modulo must retain existing recovery when intermediate alignment overflows but the final remainder is representable; simply rejecting all wide-scale inputs is incorrect.

SQL declared precision65 and the raw signed256 coefficient domain must remain distinct. For example, at scale65, -1 + 3e-65 and 1 - 3e-65 fit, while 1 + 3e-65 must return the declared-precision error.

Validation of the local fix

The fix preserves fused loops, hoisted common factors and zero allocations, reuses checked chunked multiplication above38, and adds the scale-domain check at modulo's existing narrowing owner.

On the branch rebased onto the above main: complete types/function/plan normal tests and complete types/function race tests pass. The prior #29592 failure also passes using its already-merged dependency correction. Exact SQL BVT passes twice on one test-owned CN with32MB CN/TN memory caches, with metadata/error/continuation and teardown assertions. Independent four-mutation QA rejects omitted scaling, narrowing admission, masks and late overflow failures. Matched pre-rebase measurements of the unchanged Decimal256 alignment owners cover96 Add/Sub and32 modulo cases, with no material regression and zero allocations. Whole-query and whole-CI performance were not measured. Final incremental SCA is being completed before publishing the PR.

Related closed #28876 concerns multiplication scale reduction; this issue concerns addition/subtraction alignment and modulo narrowing. Existing modulo alignment-overflow recovery from #28789 must remain intact. The whole #29249 task remains ongoing.

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 4, 2026
    c3fbe9c
  4. Ariznawlll commented on Oct 8, 2026

    @Ariznawlll
    Collaborator

    main 服务级复核结果

    测试版本:bab4b3286a0dd5683a9b291763817722233e586c,包含 #29608 的 merge commit c3fbe9ce744aa877ec944c86258916fc5e45b63b。环境为本地单 CN/TN,由该提交重新编译并使用独立数据目录启动。

    按 issue 中的真实 SQL 路径创建三行表(x=1、x=2、x=NULL),对 scale 差 39 的加法、减法和 modulo 连续执行 3 轮:

    • DECIMAL(20,0) + DECIMAL(65,39) 分别得到 1...003、2...003、NULL;
    • DECIMAL(20,0) - DECIMAL(65,39) 分别得到 0...997、1...997、NULL;
    • % 3e-39 分别得到 1e-39、2e-39、NULL;
    • 3 轮结果完全一致,没有 panic、连接中断或残留状态。

    相邻边界也符合修复后的声明精度规则:

    • scale 差 38 的加、减、模结果精确,NULL 继续传播;
    • -1 + 3e-65 和 1 - 3e-65 返回可表示的精确值;
    • 1 + 3e-65 返回 data out of range ... exceeds DECIMAL(65,65),没有进程 panic。

    因此 #29607 描述的 scale>38 向量对齐越界和 modulo panic 在当前 main 上已由 #29608 修复;本轮同时覆盖了原始 scale39、相邻 scale38、完整 scale65、NULL 和重复执行路径。

  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 离线检查均通过。

    原问题验证

    原 scale 差 39 的 Decimal256 向量加、减、模在当前 main 均返回精确值,不再 panic。

    正常场景覆盖

    i=1/2 的 +3e-39、-3e-39、%3e-39 均与十进制精确期望一致。

    边界与异常场景覆盖

    第三行 NULL 正确传播;scale 65 的可表示负值正常返回、超出声明精度的正值报 1690,连接继续可用。

    白盒验证

    #29608 的缩放/模运算修复已包含于当前 main;真实三行扫描验证向量路径而非纯常量折叠。

    回归测试

    MOTR 14_issue_regression.issue_29607_29610_29615_decimal256_boundaries,3/3 通过。

    测试证据

    MOTR PR #218 的输入 SQL 和 golden 结果。

    测试结论

    测试通过。

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