Skip to content

[Bug]: DECIMAL division truncates when scale-up falls back from Decimal128 to Decimal256 #28775

Closed

Description

@Ariznawlll

问题

DECIMAL128 除法在 scale-up 溢出并回退到 Decimal256 计算时,会把本应 round-half-up 的结果向零截断。相同数学比例走纯 Decimal256 慢路径时结果正确,说明不是输入或期望值问题。

环境

  • 官方 main:1695c139956e9dea5a6b5ef4bd02367cec63b164
  • 2026-09-12,本地源码构建,2 CN / 1 TN / 1 Log service。
  • 对照:MySQL Community Server 8.3.0;外层显式转换固定为 DECIMAL(65,6),避免只比较两边默认 division scale。

最小复现

SELECT CAST(
  CAST('123456789012345' AS DECIMAL(38,0)) /
  CAST('999999999999999.999999999999999999' AS DECIMAL(38,18))
  AS DECIMAL(65,6)
);
MatrixOne: 0.123456
MySQL 8.3: 0.123457

精确商约为 0.123456789...,保留 6 位小数应为 0.123457。将该表达式写入 DECIMAL(65,6) 列,MO 持久化 0.123456,MySQL 持久化 0.123457。

路径对照

把分子和分母同时扩大到超过 Decimal128 当前值范围、数学比例保持一致:

SELECT CAST(
  CAST('1234567890123450000000000000000000000000' AS DECIMAL(65,0)) /
  CAST('9999999999999999999999999999999999999999' AS DECIMAL(65,0))
  AS DECIMAL(65,6)
);

MatrixOne 和 MySQL 都返回 0.123457。因此错误只在输入适合 D128、但执行 scale-up 需要 Decimal256 fallback 的路径出现。

更广的 360 个除法组合扫描中,源值全部精确;去除 10 个另行报告的合法 D256 商误报 overflow 后,本条舍入偏差每轮稳定出现 1 次。最小复现与纯 D256 路径对照在两个 CN 上分别重复 3 轮;写入在 CN1 重建数据后重复 3 轮;MySQL 同脚本 3 轮返回 0.123457。

代码定位

pkg/sql/plan/function/arith_decimal_fast.go 的 d128DivOne 在 d128MulPow10 无法容纳 scale-adjusted numerator 时,转换到 Decimal256、调用 Div256,再收窄回 Decimal128。本例进入这个分支,但得到的是向零方向的 0.123456;让输入跳过 D128 路由、直接进入 D256 division 后得到正确的 0.123457。

这说明 D128 overflow fallback 与直接 D256 路径没有保持相同的最终舍入语义。修复时需要保留完整 remainder 并只在目标 scale 做一次 round-half-up。

查重和回归方向

建议补 d128DivOne overflow fallback 的半数下方/精确半数/上方及正负 UT,并补 SQL 写入回归。该问题不需要 big-data 或 chaos 才能触发。

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 #28775 (2026-09-15)

    Verification Environment

    • MatrixOne source and running binary were both verified at 2ea4ec2e6ef4b20321a235ef416228506813e39b (revert: restore single-runner race UT execution (#28887)), equal to fetched origin/main.
    • Topology: 2 CN / 1 TN / 1 Log service / Proxy.
    • Proxy 6001, CN1 16001, and CN2 16002 were reachable and each executed the SQL regression, not only a health check.
    • Client protocol: MySQL text protocol through the MySQL CLI. The Proxy, CN1, and CN2 SQL runs used isolated temporary databases and ran concurrently.

    What Was Covered

    • OK — The original DECIMAL(38,0) / DECIMAL(38,18) scale-up fallback expression returned 0.123457 on Proxy 6001, CN1 16001, and CN2 16002.
    • OK — The mathematically equivalent direct Decimal256 control returned 0.123457 on all three endpoints.
    • OK — The negative original expression returned -0.123457 on all three endpoints.
    • OK — Round-half-up boundaries returned 0.123456 below half and 0.123457 at and above half.
    • OK — Inserting the original positive and negative expressions into DECIMAL(65,6) persisted 0.123457 and -0.123457.
    • OK — The unhappy-path control DECIMAL / 0 returned NULL.
    • OK — The MOTR case recorded and replayed successfully (14/14).
    • Not covered — A direct MySQL 8.3 server comparison was not run locally. The direct Decimal256 control and exact fixed-scale expectations provide the local cross-path oracle.
    • Not covered — The product UT coverage for wide Decimal256 results and signed MinInt128 boundaries from the associated fix was not repeated as a public SQL test; those are distinct internal numeric-domain contracts.

    Findings

    The associated fix is #28814, merge commit 8130d012ebf5cb0b7d8a94699d981570e9420849, [Bug]: preserve DECIMAL division range and rounding. It is included in the verified main commit.

    The fix preserves the full Decimal256 quotient when the Decimal128 fast path cannot safely represent the scale-adjusted calculation, retaining final round-half-up semantics instead of narrowing through a truncated Decimal128 result.

    No large-data regression is required: this is scalar arithmetic with a deterministic value/scale boundary, independent of row count, block/batch boundaries, memory, spill, data skew, concurrency, or network transfer. The three endpoint SQL runs were concurrent for test efficiency; they do not claim or replace MatrixOne engine-parallel verification, which is out of scope for this issue.

    MOTR / BVT Coverage

    • MOTR source branch: test/issue-28775-decimal-division-rounding.
    • Source commit: 3cc0768c404ff6a55289b10ca32ce83933dc81b0 (test: add MOTR regression for #28775).
    • Added suites/14_issue_regression/t/issue_28775_decimal_division_rounding.test and suites/14_issue_regression/r/issue_28775_decimal_division_rounding.result.
    • The source branch was pushed and tracks origin/test/issue-28775-decimal-division-rounding.
    • Daily MOTR integration: matrixorigin/motr#177, cherry-pick commit e9c3534d33bc6d1555f9bee9e740be5cec2d4d60.

    Final Conclusion

    Fixed. The Decimal128 scale-up fallback now preserves correct round-half-up DECIMAL division results on Proxy 6001, CN1 16001, and CN2 16002. Large-data regression is not needed for the reasons above.

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