Skip to content

[Bug]: DECIMAL256 division wrongly applies a DECIMAL128 result limit to valid quotients #28774

Closed

Description

@Ariznawlll

问题

DECIMAL256 除法在两个操作数的当前未缩放整数都能放进 Decimal128 时,会选择 D128 快速路径;但该路径同时用 Decimal128 的上限校验缩放后的商。最终结果明明能放进声明的 DECIMAL256 返回类型,也会被错误报为 Decimal128 Div overflow。

环境

  • 官方 main:1695c139956e9dea5a6b5ef4bd02367cec63b164
  • 2026-09-12,本地源码构建,2 CN / 1 TN / 1 Log service。
  • 对照:MySQL Community Server 8.3.0。

最小复现

SELECT CAST('1' AS DECIMAL(65,30)) /
       CAST('0.000000000000000003' AS DECIMAL(65,18));

MatrixOne:

ERROR 1690 (22003): data out of range: data type DECIMAL,
invalid input: Decimal128 Div overflow:
1.000000000000000000000000000000/0.000000000000000003

MySQL 8.3.0:

333333333333333333.333333333333333333333333333333

该结果是 18 位整数和 30 位小数,共 48 位有效数字,位于 DECIMAL(65,30) 范围内。

写入、原子性和语句复用

CREATE TABLE t(
 id INT PRIMARY KEY,
 a DECIMAL(65,30), b DECIMAL(65,18),
 result_value DECIMAL(65,30), note VARCHAR(20)
);
INSERT INTO t VALUES
 (1,1,0.000000000000000003,NULL,'original'),
 (2,0.000000000000000006,0.000000000000000003,NULL,'original');
UPDATE t SET result_value=a/b,note='changed';
SELECT id,result_value,note FROM t ORDER BY id;

MO 的 UPDATE 报同一错误,两行都仍为 NULL,'original',没有部分更新;MySQL 分别写入精确商和 2。SQL PREPARE 查询按照 id=1 -> id=2 -> id=1 执行时表现为 error -> 2 -> error,说明错误后的 statement 可以继续使用,但合法的 id=1 商仍被拒绝。

将左操作数仅改为 DECIMAL(65,0),MO 可以返回 333333333333333333.333333。这排除了除数为零、实际商超界和服务不可用;错误由声明 scale 触发的中间缩放路径决定。

最小查询在两个 CN 上分别重复 3 轮,均返回相同 overflow;UPDATE 后数据检查及 prepared reuse 在 CN1 重建数据后重复 3 轮,结果一致。MySQL 8.3.0 同脚本 3 轮均返回结果并完成写入。

代码定位

在 pkg/sql/plan/function/arith_decimal_fast.go:

  1. d256Div 只根据输入当前值是否适合 D128,调用 d256DivViaD128。
  2. d256DivViaD128 的目标切片是 Decimal256,但内部调用 d128DivOne。
  3. d128DivOne 的 D256 fallback 在计算商后仍要求高 128 位为 0,也就是要求商能重新装回 signed Decimal128;本例的合法 DECIMAL256 商在这里被拒绝。

路径选择需要考虑缩放后中间值和最终 DECIMAL256 返回域,或者让 D256 调用者的 fallback 保留 D256 结果,不能沿用 D128 返回值限制。

查重和回归方向

修复后需要补 D256 操作数值可放入 D128、但缩放后商只能放入 D256 的 UT;SQL/多 CN/PreparedStatement 路径适合 motr。该错误不依赖大数据量或内存压力。

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. added a commit that references this issue on Sep 13, 2026
    8130d01
  6. slyunnn commented on Sep 16, 2026

    @slyunnn

    Regression and extended verification for #28774 (2026-09-16)

    Verification Environment

    • Source and runtime: latest origin/main at 66b1672403e9b9efb1971d00b7ea87572891fe2a (fix: release opaque aggregate spill state on reuse (#28925)); the running binary reported Git commit 66b1672403.
    • Topology: local 1 Log service / 1 TN / 2 CN / Proxy. All validation used the MySQL wire protocol.
    • Endpoints: Proxy 127.0.0.1:6001, CN1 127.0.0.1:16001, and CN2 127.0.0.1:16002; all three were reachable. Original SQL ran through all three endpoints; Prepared Statement and JDBC BVT execution ran through Proxy.
    • Compatibility control: temporary local MySQL 8.3.0, also through the MySQL wire protocol, was used for the original wide quotient and signed-minimum-divisor boundary, then removed.
    • Related fix: 8130d012ebf5cb0b7d8a94699d981570e9420849, [Bug]: preserve DECIMAL division range and rounding (#28814); it fixes this issue and is an ancestor of the tested main commit.

    What Was Covered

    • OK Original reproduction on Proxy, CN1, and CN2: CAST('1' AS DECIMAL(65,30)) / CAST('0.000000000000000003' AS DECIMAL(65,18)) returned 333333333333333333.333333333333333333333333333333 without Decimal128 Div overflow.
    • OK Happy path: a two-row UPDATE stored the wide quotient for id=1 and 2.000000000000000000000000000000 for id=2.
    • OK Prepared Statement reuse: a Prepared UPDATE first rejected a zero divisor with 1365 / 22012; the same statement then successfully updated id=2 and id=1 with the expected values.
    • OK Atomicity/unhappy path: a three-row UPDATE containing one zero divisor failed with 1365 / 22012, and all rows retained NULL / original; no partial update was committed.
    • OK Boundary scenario: the signed Decimal128 minimum divisor path produced -0.000001 for the positive numerator, matching the corrected Decimal256 fallback contract.
    • OK Narrow-scale control: the DECIMAL(65,0) numerator path remained successful, proving the repaired DECIMAL(65,30) result no longer inherits the incorrect Decimal128 result limit.
    • OK Focused unit coverage: TestD256DivViaD128PreservesWideQuotient, TestD256DivViaD128MinInt128Divisor, and TestD128DivLargeDivisorHalfUp passed through the repository CGo wrapper.
    • OK Existing BVT: test/distributed/cases/dtype/decimal_256.test replayed twice through JDBC in normal comparison mode; both runs passed 16/16 with 0 failed/ignored/abnormal cases.
    • OK MySQL 8.3.0 comparison: the original wide quotient and signed-minimum-divisor boundary matched exactly.
    • Not covered MySQL div_precision_increment policy: the DECIMAL(65,0) control shows MySQL's default 4 fractional digits and MatrixOne's current 6; this is the separate existing compatibility scope of #28594, not the valid-wide-quotient overflow defect.
    • Not covered restart, mixed-version rollout, or large-scale execution: none is part of this scalar Decimal division contract.

    Findings

    The D128 fast path now preserves a valid DECIMAL256 quotient instead of requiring the final scaled result to fit back into Decimal128. It also correctly falls back for the signed Decimal128 minimum operand and preserves rounding behavior. The original legal 48-digit result now reaches SQL, DML, Prepared Statement, Proxy, CN1, and CN2 consumers correctly.

    MOTR / BVT Coverage

    No new MOTR case, branch, or commit is needed. The merged fix already added the canonical self-contained distributed BVT test/distributed/cases/dtype/decimal_256.test, covering #28774's original wide quotient, the signed-minimum-divisor boundary, and related Decimal division controls; its recorded result replayed twice successfully. Test execution was serial because it used a shared local topology; this issue has no engine-parallel execution contract.

    Large-data regression is not required. The defect is a scalar type-domain and scale-conversion decision; it does not depend on row count, block/batch boundaries, memory/spill, concurrency, skew, network-transfer volume, index state, or resource scale. Minimum rows cover the DML atomicity and Prepared Statement boundaries without unrelated CI cost.

    Final Conclusion

    Fixed. The merged fix is present in the tested latest main; original reproduction, multi-CN execution, DML atomicity, Prepared Statement reuse, signed-minimum boundary, focused unit tests, repeated JDBC BVT, and the relevant MySQL 8.3.0 compatibility comparison all passed.

  7. added 3 commits that reference this issue on Sep 17, 2026
    616ba24
    600ed2c
    e7849ac
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