Skip to content

[Bug]: SUM does not widen DECIMAL(38) accumulation beyond Decimal128 #28585

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

SUM keeps a DECIMAL(38,s) input in a Decimal128 accumulator. Two individually valid values whose exact sum needs one additional integer digit therefore produce Decimal128 Add overflow, although the result is representable by MatrixOne's Decimal256 type.

The same overflow occurs in ordinary aggregate, grouped aggregate, window aggregate, and CTAS paths. For the same rows, AVG is already promoted to Decimal256 and returns the correct value.

Examples:

SUM(DECIMAL(38,0))  -> ERROR 1690: Decimal128 Add overflow
AVG(DECIMAL(38,0))  -> 99999999999999999999999999999999999999.0000

SUM(CAST(value AS DECIMAL(65,0)))
                     -> 199999999999999999999999999999999999998

MySQL 8.0.46 returns the exact 39-digit sum for DECIMAL(38,0) and the exact scaled result for DECIMAL(38,18). Its CTAS result for the former uses DECIMAL(60,0).

Controls are consistent: single-row SUM, SUM(DECIMAL(18,0)), SUM(DECIMAL(30,10)), empty aggregates, and SUM(DISTINCT) over duplicate inputs all work. Current BIGINT and BIGINT UNSIGNED SUM/AVG widening also works, so this is distinct from closed issue #25485.

Expected Behavior

SUM should use a Decimal256 result and accumulator when the input precision plus aggregate headroom exceeds Decimal128, so representable sums do not fail merely because the input column is stored as Decimal128.

Steps to Reproduce

DROP DATABASE IF EXISTS decimal_sum_widening;
CREATE DATABASE decimal_sum_widening;
USE decimal_sum_widening;

CREATE TABLE src (
  id INT PRIMARY KEY,
  g INT,
  d38 DECIMAL(38,0),
  d38s DECIMAL(38,18)
);

INSERT INTO src VALUES
  (1,1,
   99999999999999999999999999999999999999,
   99999999999999999999.999999999999999999),
  (2,1,
   99999999999999999999999999999999999999,
   99999999999999999999.999999999999999999),
  (3,2,NULL,NULL);

-- Ordinary aggregate.
SELECT SUM(d38), AVG(d38) FROM src WHERE g = 1;
SELECT SUM(d38s), AVG(d38s) FROM src WHERE g = 1;

-- Grouped and window aggregate paths.
SELECT g, SUM(d38) FROM src GROUP BY g ORDER BY g;
SELECT id, SUM(d38) OVER (
  ORDER BY id ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
) FROM src WHERE g = 1 ORDER BY id;

-- CTAS inherits the same overflow.
CREATE TABLE dst AS
SELECT SUM(d38) AS sd, AVG(d38) AS ad FROM src WHERE g = 1;

-- Decimal256 control returns the exact result.
SELECT
  SUM(CAST(d38 AS DECIMAL(65,0))),
  AVG(CAST(d38 AS DECIMAL(65,0)))
FROM src WHERE g = 1;

Additional information

SumReturnType in pkg/sql/colexec/aggexec/sumavg2.go returns DECIMAL128(38, input_scale) for every Decimal128 input. makeSumAvgExec then selects newSumDecimal128FastExec for SUM, while the adjacent AVG path explicitly promotes wide Decimal128 input to a Decimal256 executor. This explains the consistent SUM/AVG split.

The complete ordinary/group/window/CTAS/control matrix was reproduced three times on the same latest-main build and compared with MySQL 8.0.46. Closed #24430 concerns CTAS aggregate column-definition inference, not runtime aggregate overflow; closed #25485 concerns BIGINT UNSIGNED accumulation and current main passes that control.

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. Ariznawlll commented on Sep 10, 2026

    @Ariznawlll
    CollaboratorAuthor

    补充确认了一个更直接的影响:相同的 DECIMAL(38,0) 多重集合会因为扫描/累加顺序不同而得到不同执行结果。

    CREATE TABLE t_positive_first(id INT PRIMARY KEY, d DECIMAL(38,0));
    INSERT INTO t_positive_first VALUES
      (1,  99999999999999999999999999999999999999),
      (2,  99999999999999999999999999999999999999),
      (3, -99999999999999999999999999999999999999),
      (4, -99999999999999999999999999999999999999);
    
    CREATE TABLE t_alternating LIKE t_positive_first;
    INSERT INTO t_alternating VALUES
      (1,  99999999999999999999999999999999999999),
      (2, -99999999999999999999999999999999999999),
      (3,  99999999999999999999999999999999999999),
      (4, -99999999999999999999999999999999999999);
    
    SELECT SUM(d) FROM t_positive_first;
    SELECT SUM(d) FROM t_alternating;

    两张表包含完全相同的值,数学结果均为 0。当前 main 上,前者在两个正最大值相加时产生 Decimal128 Add overflow,后者返回 0。显式使用 SUM(CAST(d AS DECIMAL(65,0))) 时两种顺序都返回 0;MySQL 8.0.46 的原始 DECIMAL 查询也都返回 0。

    此外,SUM(DISTINCT d) 对两个不同的 38 位大值同样在 Decimal128 累加阶段溢出,因此问题不局限于普通 SUM 的重复值路径。

    以上顺序、抵消、DISTINCT 和 Decimal256 对照均连续复现 3 次。

  5. daviszhen commented on Sep 10, 2026

    @daviszhen
    Contributor

    A fix is available in #28624.

    Local Docker-isolated verification covered ordinary, grouped, window, CTAS, DISTINCT, and cancellation-order SUM paths. DECIMAL(38,0) produces the exact 39-digit result, both accumulation orders return zero for the cancellation case, and CTAS derives DECIMAL(60,0). The aggregate executor package tests also pass.

  6. added a commit that references this issue on Sep 15, 2026
    793d65e
  7. Ariznawlll commented on Sep 16, 2026

    @Ariznawlll
    CollaboratorAuthor

    测试版本

    • 分支:main
    • commit:8a4c84f4516d5098de9eef50b8afecc7524f37a2
    • 部署方式:1 Log Service / 1 DN / 2 CN,MySQL 协议
    • 测试日期:2026-09-16

    原问题验证

    两行 DECIMAL(38,0) 最大值执行 SUM,返回精确的 39 位结果 199999999999999999999999999999999999998,不再发生 Decimal128 overflow。

    Happy Path 覆盖

    1. 普通 SUM、分组 SUM、窗口 SUM 均返回精确结果。
    2. CTAS 继承的 SUM 结果正确。
    3. NULL 分组保持 NULL 语义。
    4. 3/3 次独立执行一致。

    Unhappy Path 覆盖

    不适用;本轮未注入超出 DECIMAL(38) 可表示范围的非法值,避免将真正的溢出输入误判为修复目标。

    白盒验证

    不适用;以普通、分组、窗口和 CTAS 公开执行路径为准。

    回归测试

    已加入 motr PR #181 的 issue_28585_decimal_sum.test/.result。

    测试证据

    最新 main 集群版本为 commit-8a4c84f;3/3 次边界 SUM 均返回精确值。

    测试结论

    测试通过。

  8. added 2 commits that reference this issue on Sep 17, 2026
    05906c5
    b707c9b
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