Repository navigation
Description
Proposed consolidated tracking scope: 数值聚合返回类型与 DECIMAL 精度
These are independent reproductions grouped under one acceptance contract; a common immediate root cause is not assumed. Administrative consolidation does not claim that any defect is fixed. Original report bodies and discussions remain available at the links below.
Included reports
- #28131 — [Compatibility]: high-range DECIMAL VAR_POP errors instead of returning DOUBLE
Acceptance checklist
- Preserve existing MEDIAN overflow and SUM metadata regressions.
- Check VAR_POP, VAR_SAMP and VARIANCE return DOUBLE for the reported high-range DECIMAL input, with STDDEV, window and explicit-DOUBLE controls.
Additional consolidated scope
Numeric aggregate functions must preserve their documented result precision and metadata. MEDIAN value overflow and SUM result-type metadata are independent failures of that aggregate DECIMAL contract.
Additional acceptance criteria
- MEDIAN over valid DECIMAL(38,s) inputs, including singleton values and boundary scales, returns the correct value without intermediate overflow.
- SUM over INT columns exposes DECIMAL(32,0) result metadata to protocol clients and query consumers, with correct values.
- Keep MEDIAN value checks and SUM metadata checks independent; retain NULL, ordinary precision, and related aggregate controls.
Additional report
- #30792 — SUM over INT reports BIGINT instead of DECIMAL(32,0) in result metadata; original report and discussion remain on that issue.
问题
MEDIAN 声明支持 DECIMAL128,但对 DECIMAL(38,0) 和 DECIMAL(38,38) 的合法值无法可靠返回结果。即使过滤后聚合中只有一个值,也会报 Decimal128 scale overflow。
环境
- MatrixOne: official
main - commit:
e7bb0572235ec4bac81eb098eb0ad8900f0065ab
复现
create database median_decimal38_repro;
use median_decimal38_repro;
create table d0(v decimal(38,0));
insert into d0 values
(99999999999999999999999999999999999997),
(99999999999999999999999999999999999999);
select median(v) from d0;
select median(v) from d0
where v = 99999999999999999999999999999999999999;
create table d38(v decimal(38,38));
insert into d38 values
('0.10000000000000000000000000000000000000'),
('0.20000000000000000000000000000000000000');
select median(v) from d38;
select median(v) from d38 where v = 0.2;实际错误包括:
Decimal128 Add overflow: 99999999999999999999999999999999999997+99999999999999999999999999999999999999
Decimal128 scale overflow: 9999999999999999999999999999999999999.9
Decimal128 scale overflow: 2000000000000000000000000000000000000.0
单值中位数应等于该值,不需要加法,也不应溢出。
代码定位
pkg/sql/colexec/aggexec/median2.go 中:
MedianReturnType对所有 DECIMAL 输入返回DECIMAL(38, input_scale + 1),没有处理输入已经达到 precision/scale 38 的情况;- 奇数/单值路径仍调用
Scale(1); - 偶数路径的
averageMedianDecimal先执行v1.Add128(v2),可在数学平均值仍可表示时提前溢出。
期望
MEDIAN 对其声明支持的 DECIMAL(38,s) 输入返回可表示的精确结果;单值输入直接返回该值,偶数输入以避免中间加法溢出的方式计算平均值,并为 precision/scale 38 选择可承载结果的返回尺度。
以上场景重复执行 3 次,结果稳定。
Activity
Ariznawlll commentedon Sep 14, 2026
补充一个更窄的同根因场景(official
maincommitf9a44447743fff827dab01300837d27a3d9811f0):AVG(x)能返回可表示的精确结果:MEDIAN(x)却在计算中间和时直接报错:这说明问题不只发生在
DECIMAL(38,0)/DECIMAL(38,38)的返回尺度边界;即使输入为DECIMAL(38,6),且正确中位数明确可表示,averageMedianDecimal仍因先执行v1.Add128(v2)而产生不必要的中间溢出。相反符号及较小的同尺度数值均正常。以上复现连续执行 3 次,现象一致。[-][Bug]: MEDIAN overflows on valid DECIMAL(38,s) values including singleton input[/-][+][Bug]: numeric aggregates must preserve DECIMAL precision and result metadata[/+]Ariznawlll commentedon Oct 29, 2026
结论
测试不通过。当前 main 的
MEDIAN(DECIMAL(38,38))仍有本 issue 已记录的 scale 溢出;同时补充 SDK Arrow 消费边界:部分数值能由普通查询返回,但不合法的结果 scale 会使 Arrow 无法转成 Python 值。关键证据
main@c6683bd2f531dcaa80e153c0924dac619c49dc9d,同提交构建的隔离双 CN;3 个新建库,SDK 同步/异步、原生查询及同步 Arrow 入口。DECIMAL(38,38)上的普通偶数和大单值场景,12 次均出现本 issue 已记录的Decimal128 scale overflow。相邻DECIMAL(37,37)及普通 DECIMAL 控制能够正常返回;空集和全 NULL 对照也正常。(38,39)。各场景在 3 库 × 2 CN 共 18 次,Arrow 能构造decimal128(38, 39)表,但to_pylist()抛decimal.InvalidOperation: ConversionSyntax;表显示也提示 scale 无法格式化。sdk_median_scale7.json/.log;MEDIAN 返回类型实现 的 scale 推导已在原 issue 中记录。待解决点
fix(aggexec): widen AVG sum state to Decimal256 for DECIMAL(29..34) (#…
Fix SQL compatibility regressions across aggregates, casts, and prepa…
Ariznawlll commentedon Nov 29, 2026
结论
测试通过。MEDIAN 的 DECIMAL(38,s) 中间溢出、结果精度,以及合并范围内 SUM/VAR 元数据已通过;实际 SDK Arrow 消费也通过。
关键证据
版本与环境
922815d74fe664dc9f4ea7bfc4c4e8ac3b279cd0;本次 Linux CI 从官方 main 检出的实际版本为231b68c2d6421e9d212538866e0f825afd6c13e8,两处分别核对完整git_version()。CI 比本地多了 Release merge spill buffers on panic #32160,核对差异仅为 spill sinker 及其两个测试文件。以下 CI 的正式 SQL、SDK 和源码测试确实在 CI 版本上重新执行;不把 MOTR commit 当作 MO 版本,也不将 main 结果外推为 4.2-dev 通过。步骤、断言与实际结果
源码覆盖 scale=0/6/38、正负数、组间复用、distinct、错误路径和 spill/intermediate round-trip,执行后三轮对应内存计数回到基线。此次不是只成功创建 Arrow 表就判通过,已实际转换为 Python 值。
回归与 CI
qa-acceptance-main中版本、正式结果、原生测试和 SDK 日志。本轮整体正式 MOTR 63/63(21 个用例各三轮)、0 失败;63 次不全属于本 Issue。待解决点
本 Issue 上述原始问题与相关边界没有未通过项。该结论不扩展为其他 Issue、大数据性能或未执行的生产拓扑验收。MOTR 回归已经提交,按用户允许先验收关闭、随后合入 PR 的流程处理。
最终结论:测试通过。原始复现、上述扩展、对应 MOTR 回归及 Linux CI 均已完成;本 Issue 验收范围内未通过项:无。按授权关闭 #28230。