Skip to content

[Bug]: DECIMAL DIV rejects a representable integer quotient when scale alignment overflows #28790

Closed

Description

@Ariznawlll

问题描述

DECIMAL DIV 为补偿两个操作数的小数位数,会将被除数或除数的内部系数按 scale 差放大。合法输入在该中间步骤超出 Decimal256 时,MO 返回 overflow,即使最终整数商是可表示的 0。

环境

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

最小复现

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

数学结果和 MySQL 8.3 结果均为 0。MO 在两个 CN 上返回:

ERROR 1690 (22003): Decimal256 IntDiv overflow:
0.000000000000000000000000000007 DIV 999...999

覆盖情况

  • scale 差 11 的边界可运行;65 位整数与 scale 差 12、30 的组合被错误拒绝;
  • 左右符号组合均复现;
  • 更小的 46 位整数控制值可运行;
  • 表列表达式 b DIV a、过滤、视图和 SQL PREPARE/EXECUTE 都进入相同路径;
  • 反向的“巨大整数 DIV 微小数”因商确实超出 BIGINT 而报错,是合理控制,未计入本问题。

初步定位与预期

d256IntDiv 的慢路径在 scaleAdj < 0 时先对除数执行 Scale(-scaleAdj),随后才做截断整数除法。对于绝对值远小于除数的输入,应能在不物化超宽对齐系数的情况下直接得出 0。内部对齐溢出不应让一个可表示的整数商变成 SQL 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 #28790 (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.
    • Endpoints: Proxy 127.0.0.1:6001, CN1 127.0.0.1:16001, and CN2 127.0.0.1:16002; all were reachable before execution.
    • Client protocol: MySQL text protocol through the mysql client. The original and necessary extended SQL cases were run independently and concurrently across all three endpoints.

    What Was Covered

    • OK — Original DECIMAL(65,30) DIV DECIMAL(65,0) reproduction returned the representable quotient 0 on Proxy 6001, CN1 16001, and CN2 16002.
    • OK — Scale boundary coverage: both scale=11 and the formerly overflowing scale=12 alignment case returned 0.
    • OK — Sign controls preserved truncation toward zero for negative numerator and denominator combinations.
    • OK — Ordinary decimal and integer controls returned 6 for 12.9 DIV 2 and 3 for 7 DIV DECIMAL(10,1).
    • OK — Table expression, filter, view, and SQL PREPARE / EXECUTE paths returned the same 0 result.
    • OK — The reverse DECIMAL(65,0) DIV DECIMAL(65,30) case still returned the valid final-range error 1690 / 22003 when the quotient exceeded BIGINT.
    • OK — The three endpoint checks ran in parallel with isolated client sessions and separate test databases for table-based paths.
    • Not covered — A local MySQL 8.3.0 instance was not available for a fresh side-by-side execution; expected values follow exact integer division semantics and the issue’s documented MySQL comparison.
    • Not covered — Large-volume, spill, concurrency-pressure, skew, and network-scale tests are unrelated to this scalar decimal 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 avoids materializing an overflowing scale-aligned coefficient for DECIMAL256 DIV, while preserving truncation, signs, null/zero behavior, and final BIGINT range validation.

    MOTR / BVT Coverage

    Added and recorded/replayed:

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

    MOTR source commit: 37a02803c0b7e35c4e8051b1b095997fb87cd3c1 on test/issue-28790-decimal-div-scale. It was cherry-picked as f4e7e0f into daily PR matrixorigin/motr#177.

    Final Conclusion

    Fixed. The original representable-quotient overflow no longer occurs through Proxy 6001, CN1 16001, or CN2 16002; valid final quotient overflow remains correctly reported as 1690.

    Large-data regression is not required: behavior depends on operand precision, scale difference, and scalar quotient range, 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