Skip to content

[Bug]: DECIMAL arithmetic does not widen results beyond Decimal128 #28584

Closed

Description

@Ariznawlll

Is there an existing issue for the same bug?

  • I have checked the existing issues, including open and closed issues.

Branch Name

main

Commit ID

fc621e3

Other Environment Information

  • MatrixOne: locally built from the commit above (8.0.30-MatrixOne-v1.3.0)
  • MySQL baseline: 8.0.46
  • Verification date: 2026-09-10

Actual Behavior

Arithmetic on valid DECIMAL operands remains in Decimal128 whenever the input type is DECIMAL(38, s), even when the expression result needs more than 38 digits and is representable by MatrixOne's Decimal256 type.

For example, MatrixOne reports Decimal128 Add overflow for the sum of two valid DECIMAL(38,0) values and Decimal128 Mul overflow for the square of a valid DECIMAL(20,0) value:

99999999999999999999999999999999999999
+ 99999999999999999999999999999999999999

ERROR 1690: Decimal128 Add overflow

99999999999999999999 * 99999999999999999999

ERROR 1690: Decimal128 Mul overflow

This is not a storage-domain overflow of either operand. Explicitly promoting the same operands to DECIMAL(65,0) returns the exact 39- and 40-digit results. MySQL 8.0.46 also returns both exact results without an explicit cast.

The behavior propagates into predicates, grouping, ordering, and CTAS expressions because evaluation fails before those operators can consume the value.

Boundary controls are consistent: DECIMAL(19,0) * DECIMAL(19,0) and DECIMAL(20,0) * 2 work because their results fit in Decimal128.

Expected Behavior

When the declared operand precision implies a result wider than Decimal128 but within MatrixOne's supported Decimal256 range, arithmetic type inference should promote the expression to Decimal256 and return the exact result.

Steps to Reproduce

DROP DATABASE IF EXISTS decimal_arithmetic_widening;
CREATE DATABASE decimal_arithmetic_widening;
USE decimal_arithmetic_widening;

CREATE TABLE t (
  id INT PRIMARY KEY,
  d38 DECIMAL(38,0),
  d38s DECIMAL(38,18),
  d20 DECIMAL(20,0),
  d19 DECIMAL(19,0)
);

INSERT INTO t VALUES
  (1,
   99999999999999999999999999999999999999,
   99999999999999999999.999999999999999999,
   99999999999999999999,
   9999999999999999999),
  (2,1,0.000000000000000001,2,2);

-- Decimal128 Add overflow; exact result needs 39 digits.
SELECT d38 + d38 FROM t WHERE id = 1;

-- The scaled DECIMAL(38,18) form has the same problem.
SELECT d38s + d38s FROM t WHERE id = 1;

-- Decimal128 Mul overflow; exact result needs 40 digits.
SELECT d20 * d20 FROM t WHERE id = 1;

-- Downstream relational paths inherit the expression failure.
SELECT id FROM t WHERE d38 + d38 > d38 ORDER BY id;
SELECT d38 + d38 AS k, COUNT(*) FROM t GROUP BY k ORDER BY k;
SELECT id, d38 + d38 AS k FROM t ORDER BY k;

-- Controls that fit in Decimal128.
SELECT d19 * d19 FROM t WHERE id = 1;
SELECT d20 * 2 FROM t WHERE id = 1;

-- Decimal256 proves that MatrixOne can represent both exact results.
SELECT
  CAST(d38 AS DECIMAL(65,0)) + CAST(d38 AS DECIMAL(65,0)),
  CAST(d20 AS DECIMAL(65,0)) * CAST(d20 AS DECIMAL(65,0))
FROM t WHERE id = 1;

Additional information

The arithmetic overload return-type functions in pkg/sql/plan/function/list_operator.go return DECIMAL128(38, scale) for Decimal128 +, -, and * operands. The execution functions in pkg/sql/plan/function/arithmetic.go consequently select the Decimal128 kernels. Decimal256 kernels already exist and work when an operand is explicitly typed as Decimal256.

The complete matrix above was reproduced three times on the same latest-main build and compared with MySQL 8.0.46.

Activity

  1. added this to the MO-202609 milestone on Sep 10, 2026
  2. added
    kind/bug
    on Sep 10, 2026
  3. added theissue type on Sep 10, 2026
  4. daviszhen commented on Sep 10, 2026

    @daviszhen
    Contributor

    A fix is available in #28629.

    Local verification used MatrixOne built and started inside an isolated Linux Docker container. DECIMAL(38,0) addition returns the exact 39-digit value, DECIMAL(20,0) multiplication returns the exact 40-digit value, and WHERE, GROUP BY, ORDER BY, and CTAS all consume the widened results without narrowing them back to Decimal128. CTAS derives DECIMAL(39,0) and DECIMAL(40,0). Full pkg/sql/plan and pkg/sql/plan/function tests pass.

  5. added a commit that references this issue on Sep 16, 2026
    c329160
  6. Ariznawlll commented on Sep 17, 2026

    @Ariznawlll
    CollaboratorAuthor

    测试版本

    • 分支:main
    • commit:8e8e1998ef02b1f6233ddb1c2d3208f3b70d2d83
    • 部署:隔离的单 CN / 单 TN / 单 LogService 本地集群,通过 MySQL 协议执行。
    • 日期:2026-09-17

    原问题验证

    以 Decimal256 边界量级执行加法、乘法、谓词过滤及 CTAS。结果与精确十进制预期一致,CTAS 后值保持完整,未出现中间窄化、溢出或精度截断。

    Happy Path 覆盖

    覆盖 Decimal256 加法、乘法、比较谓词和 CTAS 写入/读取。

    Unhappy Path 覆盖

    不适用:该问题是合法高精度运算的正确性;边界量级已经覆盖最易发生中间窄化的路径。

    白盒验证

    不适用:以精确算术输出与 CTAS 持久化结果进行端到端验收。

    回归测试

    已加入 MOTR PR #181:issue_28584_decimal256_arithmetic.test。本地连续 3 轮通过。

    测试证据

    motr --repeat 3 14_issue_regression.issue_28584_decimal256_arithmetic:3/3 通过,10 条 SQL/轮均成功。

    测试结论

    测试通过。

  7. added 2 commits that reference this issue on Sep 17, 2026
    5d7b0b3
    b85084f
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