Skip to content

[Bug]: Decimal256 modulo narrowing returns wrong remainder or panics at magnitude boundaries #29610

Closed

Description

@XuPeng-SH

Observed failure

Fixed by PR #29618, merged into main. The PR body references this issue with Fixes #29610.

Systematic QA for #29249 found two reachable Decimal256 modulo defects. Both reproduce through the actual SQL parser, planner and expression executor with valid DECIMAL(65,0) operands and a three-row scan (1, 2, NULL), preventing constant folding from bypassing the batch owner.

Tested branch: 76f2cd1617d823dc929e1c3fc1a72426a24f10a6, including main 21f711a7b8. The four affected owner bodies (Decimal128.Mod128, d256ModViaD128, d128ModOne, d128ModDiffScaleYPow10) are byte-identical to that main commit. This is separate from #29607's scale-difference-above-38 defect: these witnesses use equal scales.

Minimal SQL shape

CREATE TABLE mod_boundary_probe(i INT);
INSERT INTO mod_boundary_probe VALUES (1), (2), (NULL);

SELECT (CAST('-170141183460469231731687303715884105728' AS DECIMAL(65,0))
        + CAST(i - 1 AS DECIMAL(65,0)))
       % CAST('18446744073709551616' AS DECIMAL(65,0))
FROM mod_boundary_probe;
-- expected: 0, -18446744073709551615, NULL
-- first evaluated row actually returns -170141183460469231731687303715884105728

SELECT CAST(i AS DECIMAL(65,0))
       % CAST('-18446744073709551616' AS DECIMAL(65,0))
FROM mod_boundary_probe;
-- expected: 1, 2, NULL
-- actual executor panic: runtime error: integer divide by zero

DROP TABLE mod_boundary_probe;

The executed reproduction binds the equivalent expressions to the existing mock optimizer's nation.n_nationkey scan, supplies actual int32 vectors and evaluates real expression executors. These are executed parser/planner/executor failures, not yet a live-service BVT claim. Independent literal coefficient and NULL assertions are used.

Root causes and ownership

  1. Decimal128.Mod128 is used for absolute magnitudes by signed modulo owners, but its wide-divisor comparisons call signed Less. The magnitude of the minimum signed coefficient is 2^127; treating that bit pattern as negative skips reduction. D128's own SQL precision limit does not make this unreachable: legal D256 operands narrow to the same carrier.
  2. d256ModViaD128 independently admits divisors using an OR reduction of B64_127 + 1. It accepts -2^64, whose low limb is zero and whose absolute magnitude does not fit uint64. The resulting narrowed divisor is zero at bits.Div64. The existing canonical d128AbsFits64 predicate already excludes exactly this boundary; the D256 path duplicates an incomplete rule.

Required closure

Restore absolute-magnitude comparison at the existing remainder owner, and reuse the existing divisor-domain predicate across narrowing consumers. Preserve signed results, factor/fallback contracts, NULL/mask ordering and strict/permissive zero behavior. Verify nearest positive/negative boundaries, broadcasting and late wide divisors; avoid adding a separate execution path or full-batch allocation. Validate actual public SQL/BVT and common-path cost before publication.

Baseline private probes: two raw and two public children fail for the intended wrong result/panic; command exits 1. No production source was changed for reproduction. Refs #29249 and #29608.

Activity

  1. self-assigned this
    on Oct 4, 2026
  2. added
    kind/bug
    severity/s0
    on Oct 6, 2026
  3. removed their assignment
    on Oct 6, 2026
  4. XuPeng-SH commented on Oct 6, 2026

    @XuPeng-SH
    ContributorAuthor

    Fixed by #29618 (merged into main). The regression tests cover both minimum D128 magnitude remainder and the negative 2^64 divisor panic witness.

  5. Ariznawlll commented on Oct 8, 2026

    @Ariznawlll
    Collaborator

    main 服务级复核结果

    测试版本:bab4b3286a0dd5683a9b291763817722233e586c,包含修复 PR #29618 的 merge commit 856e9ddbdd69b52d18f727c763ecd963e66644de。使用该提交重新编译的本地单 CN/TN 服务执行原 issue 的两组 DECIMAL(65,0) SQL。

    连续 3 轮结果一致:

    (-2^127 + (i-1)) % 2^64
      i=1    -> 0
      i=2    -> -18446744073709551615
      i=NULL -> NULL
    
    CAST(i AS DECIMAL(65,0)) % (-2^64)
      i=1    -> 1
      i=2    -> 2
      i=NULL -> NULL
    

    负 2^64 除数不再触发 integer divide by zero panic;每轮之后连接保持可用,测试表仍可正常读取。当前 main 已覆盖原 issue 的错误余数、负宽除数和 NULL 路径,#29610 描述的问题已由 #29618 修复。

  6. Ariznawlll commented on Oct 8, 2026

    @Ariznawlll
    Collaborator

    测试版本

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

    原问题验证

    原 (-2^127+(i-1)) % 2^64 在 i=1/2 分别为 0、-18446744073709551615,不再错算。

    正常场景覆盖

    DECIMAL(65,0) 宽除数 % (-2^64) 在 i=1/2 返回 1、2。

    边界与异常场景覆盖

    NULL 行保持 NULL;负宽除数不触发 divide-by-zero panic,后续 SQL 正常。

    白盒验证

    修复 PR #29618 的底层 modulo 测试已在 main;本轮补真实 SQL 的双边界行验证。

    回归测试

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

    测试证据

    MOTR PR #218 中同名 SQL/golden;两条 modulo 路径及会话存活均有断言。

    测试结论

    测试通过。

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