Repository navigation
Description
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
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
Ariznawlll commentedon Sep 10, 2026
补充确认了一个更直接的影响:相同的
DECIMAL(38,0)多重集合会因为扫描/累加顺序不同而得到不同执行结果。两张表包含完全相同的值,数学结果均为
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 次。daviszhen commentedon Sep 10, 2026
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.
fix: widen decimal SUM accumulation (#28624)
Ariznawlll commentedon Sep 16, 2026
测试版本
main8a4c84f4516d5098de9eef50b8afecc7524f37a2原问题验证
两行
DECIMAL(38,0)最大值执行SUM,返回精确的 39 位结果199999999999999999999999999999999999998,不再发生 Decimal128 overflow。Happy Path 覆盖
NULL分组保持NULL语义。Unhappy Path 覆盖
不适用;本轮未注入超出 DECIMAL(38) 可表示范围的非法值,避免将真正的溢出输入误判为修复目标。
白盒验证
不适用;以普通、分组、窗口和 CTAS 公开执行路径为准。
回归测试
已加入 motr PR #181 的
issue_28585_decimal_sum.test/.result。测试证据
最新 main 集群版本为
commit-8a4c84f;3/3 次边界 SUM 均返回精确值。测试结论
测试通过。
fix: widen decimal SUM accumulation (matrixorigin#28624)
fix: widen decimal SUM accumulation (matrixorigin#28624)