Skip to content

[Bug]: DECIMAL modulo rejects representable results when scale alignment overflows #28789

Closed

Description

@Ariznawlll

问题描述

DECIMAL 取模在两个操作数 scale 不同时,会先把较低 scale 的内部系数乘以 10^scaleDiff 再求余。对于合法的 DECIMAL(65,*) 值,这个中间对齐值可能超出 Decimal256,MO 直接报 overflow;但余数本身完全可表示,MySQL 能返回精确结果。

环境

  • MatrixOne: main@46cf1d24a7d77e7be6060a17e4596da90743b59f
  • 本地 2 CN / 1 TN / 1 Log
  • 对照: MySQL 8.3.0

最小复现

SELECT
  CAST('99999999999999999999999999999999999999999999999999999999999999999'
       AS DECIMAL(65,0))
  %
  CAST('0.000000000000000000000000000007' AS DECIMAL(65,30)) AS r;

MO 返回:

ERROR 1690 (22003): Decimal256 Mod overflow:
999...999%0.000000000000000000000000000007

MySQL 8.3 返回:

0.000000000000000000000000000004

该余数只有 30 位小数,落在 SQL DECIMAL(65,30) 的合法范围内。a % b 与 b % a 都会受影响;左/右操作数的正负号组合也相同。scale 差为 11 时上述最大 65 位整数仍可运行,差为 12 起开始失败,说明失败由实现的中间对齐容量而非声明类型或结果容量决定。

扫描结果

以 10^n-1(n=1,10,18,19,20,30,38,39,46,47,50,60,65)和 7×10^-s(s=0,1,5,10,11,12,18,19,20,29,30)构造 1,144 个组合,覆盖:

  • 两种操作数顺序;
  • 四种正负号组合;
  • Java BigDecimal.remainder 作为结果 oracle。

MO: wrong=0, errors=112;MySQL: wrong=0, errors=0。MO 的全部 112 个错误都是有效结果被 Decimal256 Mod overflow 拒绝,而不是实际余数不同。

传播场景

该表达式在表列上同样导致 SELECT、过滤、GROUP BY、视图和 CTAS 失败;小数值控制行可在这些路径中正常运行。带零除数的 CTAS 另有严格模式下的标准零除数错误,已从本问题的对照结论中排除。

初步定位

pkg/container/types/decimal.go 的 Decimal256.Mod 在 scale 不等时调用 Scale 把一个完整系数对齐;pkg/sql/plan/function/arith_decimal_fast.go 的 Decimal256 慢路径随后传播该错误。取模可通过不物化超宽对齐系数的算法(例如基于缩放因子进行模约简)避免该中间溢出。

预期行为

只要输入值和数学余数均在 SQL DECIMAL 合法域内,%/MOD() 应返回余数;内部对齐中间值不应改变语义为 overflow。

Activity

  1. added this to the MO-202609 milestone on Sep 12, 2026
  2. added
    kind/bug
    on Sep 12, 2026
  3. added theissue type on Sep 12, 2026
  4. self-assigned this
    on Sep 12, 2026
  5. slyunnn commented on Sep 15, 2026

    @slyunnn

    Regression and extended verification for #28789 (2026-09-15)

    Verification Environment

    • MatrixOne source: latest local origin/main at 2ea4ec2e6ef4b20321a235ef416228506813e39b — revert: restore single-runner race UT execution (#28887).
    • Runtime binary was rebuilt from that commit, restarted, and reported Git commit ID: 2ea4ec2e6e.
    • Topology: 2 CN / 1 TN / 1 Log service / Proxy.
    • Endpoint connectivity was confirmed for Proxy 127.0.0.1:6001, CN1 127.0.0.1:16001, and CN2 127.0.0.1:16002.
    • Client protocol: MySQL text protocol through the mysql client. The original SQL and necessary extensions were executed independently and concurrently through all three endpoints.

    What Was Covered

    • OK — Original DECIMAL(65,0) % DECIMAL(65,30) reproduction returned 0.000000000000000000000000000004 through Proxy 6001, CN1 16001, and CN2 16002.
    • OK — Scale boundaries returned 0.00000000006 at scale=11 and 0.000000000004 at the formerly failing scale=12.
    • OK — Reverse operand order and dividend-sign controls preserved exact remainder and sign semantics.
    • OK — Decimal/integer compatibility control 7 % DECIMAL(10,1) returned 1.0.
    • OK — Table expression, filter, GROUP BY, view, CTAS, and SQL PREPARE / EXECUTE paths returned the expected remainder.
    • OK — Non-strict-session modulo-by-zero control returned NULL consistently on all three endpoints.
    • OK — The three endpoint validations ran in parallel with isolated client sessions and separate test databases.
    • Not covered — A local MySQL 8.3.0 instance was not available for a fresh side-by-side execution; the expected original remainder is the issue’s documented MySQL value and follows exact decimal arithmetic.
    • Not covered — Large-volume, spill, concurrency-pressure, skew, and network-scale tests are unrelated to this scalar precision/scale-alignment defect.

    Findings

    The associated fix is MatrixOne PR #28804, merge commit 67a9a4b130388956971888a67fc626784deade48 (fix(decimal): avoid DECIMAL256 MOD/DIV scale overflow). It is an ancestor of the verified origin/main.

    The fix computes the exact DECIMAL256 MOD remainder without materializing an overflowing scale-aligned coefficient, preserving valid result scale and sign semantics.

    MOTR / BVT Coverage

    Added and recorded/replayed:

    • suites/14_issue_regression/t/issue_28789_decimal_mod_scale.test
    • suites/14_issue_regression/r/issue_28789_decimal_mod_scale.result

    MOTR source commit: 2ca76d62d78350bfed1f6d4fee6db7baa122c3e7 on test/issue-28789-decimal-mod-scale. It was cherry-picked as b2b2f6d into daily PR matrixorigin/motr#177.

    Final Conclusion

    Fixed. The original representable DECIMAL modulo result is returned correctly through Proxy 6001, CN1 16001, and CN2 16002.

    Large-data regression is not required: this issue depends on scalar operand precision and scale difference, not on row count, block/batch boundaries, memory/spill, concurrency, data skew, network transfer, or cluster resource scale.

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

Metadata

Metadata

Assignees

Labels

kind/bug

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions