Repository navigation
Description
Summary
Statistical aggregates over DECIMAL inputs expose a DECIMAL result with the input scale in MatrixOne, while MySQL returns DOUBLE. Variance is measured in squared units and can require more fractional digits than the input; the current MO result is rounded at the original input scale, changing the value rather than only its display.
Reproduction
CREATE TABLE t (d DECIMAL(38,18));
INSERT INTO t VALUES
(0.000000000000000001),
(0.000000000000000002),
(0.000000000000000003);
SELECT VAR_POP(d), VAR_SAMP(d), STDDEV_POP(d), STDDEV_SAMP(d) FROM t;The same SELECT was run three times with identical results:
MO: 0.000000000000000000 0.000000000000000000 0.000000000000000001 0.000000000000000001
MySQL: 6.666666666666664e-37 9.999999999999996e-37 8.164965809277258e-19 9.999999999999999e-19
MO result metadata reports DECIMAL with scale 18; MySQL reports DOUBLE. In MO, VAR_POP(CAST(d AS DOUBLE)) returns 6.666666666666664e-37, confirming the discrepancy is in the DECIMAL aggregate result domain/materialization. Grouped, window, and SQL PREPARE executions show the same scale-limited values.
With DECIMAL(38,18) values 1e-9, 2e-9, 3e-9, MO returns VAR_POP=1e-18 while MySQL returns approximately 6.666666666666665e-19.
Expected behavior
Variance and standard-deviation results for DECIMAL inputs should retain enough precision for the computed statistic and match the MySQL numeric result domain. The result must not be rounded to the input column scale before it is returned.
Duplicate / known-issue check
- [Bug]: VAR_POP / STDDEV_POP lose small variance under large numeric offsets #27543 concerns cancellation for values with a large common offset; this case uses tiny values without a large offset and is fixed by an explicit DOUBLE cast in MO.
- [Compatibility]: temporal VAR_POP differs from MySQL at DECIMAL(24,6) precision #28852 concerned temporal operands and a later DECIMAL cast; this case aggregates a native DECIMAL column and directly returns a scale-limited result.
- [Bug]: variance and stddev panic for DECIMAL256 inputs #28838 concerned a DECIMAL256 executor panic.
- The compatibility contract has no DECIMAL statistical-aggregate result-scale exemption.
Environment
- MatrixOne local runtime commit:
2ce4e58af6f03b943ee288ffad81012766cf830a. - Current
origin/maininspected ata53390c57de48ac280249ff0e88831e502e20fb0;pkg/sql/plan/function/list_agg.goandpkg/sql/colexec/aggexec/vardev2.goare unchanged from the tested runtime. The runtime itself was not rebuilt at that newer SHA. - MySQL: 8.0.46.
- MySQL 8.0 documentation specifies DOUBLE for variance and standard-deviation aggregates on numeric arguments: https://dev.mysql.com/doc/refman/8.0/en/aggregate-functions.html
Activity
Ariznawlll commentedon Oct 14, 2026
当前本地 runtime 复核
使用 issue 中同一组三个
DECIMAL(38,18)值,在 MatrixOne8.0.30-MatrixOne-v1.3.0(源码 revision2ce4e58af6f03b943ee288ffad81012766cf830a)与隔离 MySQL8.0.45对照:MySQL 返回
6.666666666666664e-37,9.999999999999996e-37,8.164965809277258e-19,9.999999999999999e-19;MatrixOne 返回0.000000000000000000,0.000000000000000000,0.000000000000000001,0.000000000000000001。本次仍可观察到方差按原输入 scale 表示的精度差异。邻近 controls:空输入四个结果均为 NULL;单行输入
VAR_POP/STDDEV_POP=0、VAR_SAMP/STDDEV_SAMP=NULL,两端相同。fix(agg): return DOUBLE for decimal variance and stddev (#30228)
Ariznawlll commentedon Oct 15, 2026
测试版本
main17a6200f59e764729e5544908fffd5480ae88279原问题验证
沿用 issue 中
DECIMAL(38,18)的1e-18, 2e-18, 3e-18三值。VAR_POP/VAR_SAMP乘1e36后分别落在0.66666666666666–0.66666666666668与0.99999999999999–1.00000000000001;STDDEV_POP/STDDEV_SAMP乘1e18后分别落在0.81649658092772–0.81649658092774与0.99999999999999–1.00000000000001。不再出现方差被舍入成零的原错误。原始数值路径和 MOTR 定向用例各 3 次通过。Happy Path 覆盖
CAST(v AS DOUBLE)的方差对照也处于预期区间。DOUBLE,数值与独立数学预期在1e-12相对误差内一致;不是只检查 SQL 字符串。CREATE TABLE AS SELECT后四个结果列的数据类型均为double。VAR_POP=0、VAR_SAMP=NULL;仅 NULL 输入及空输入的两者均为 NULL。Unhappy Path 覆盖
不适用:该 issue 为合法极小 DECIMAL 输入的数值精度问题,无错误输入拒绝或失败回滚契约。单行、仅 NULL、空输入已作为有效边界验证。
白盒验证
不适用:原问题可通过公开 SQL 和结果协议观察;本次未做内部故障注入或
-race。回归测试
test/distributed/cases/function/func_aggr_decimal_statistics.sql,包含分组、window、PREPARE 和统计值边界;其 PR 的 TKE 镜像构建检查失败,相关 BVT job 被跳过。issue_30179_decimal_statistics.test、issue_30174_30179_aggregate_metadata.py,见 motr PR #240。本地各 3/3 通过;PR 的 4 项离线检查通过,但尚未合入、集成 MOTR CI 尚未验证。按本次要求先关闭 issue;持续回归正式生效仍待 PR 合入。测试证据
15/15成功、147/147SQL/场景语句成功;本 issue 的 SQL 与协议场景各 3/3 成功。测试结论
测试通过。