Skip to content

[Bug]: DECIMAL variance and standard deviation are rounded to the input scale #30179

Closed

Description

@Ariznawlll

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

Environment

Activity

  1. added this to the MO-202610 milestone on Oct 14, 2026
  2. added theissue type on Oct 14, 2026
  3. Ariznawlll commented on Oct 14, 2026

    @Ariznawlll
    CollaboratorAuthor

    当前本地 runtime 复核

    使用 issue 中同一组三个 DECIMAL(38,18) 值,在 MatrixOne 8.0.30-MatrixOne-v1.3.0(源码 revision 2ce4e58af6f03b943ee288ffad81012766cf830a)与隔离 MySQL 8.0.45 对照:

    WITH vals AS (
      SELECT CAST('0.000000000000000001' AS DECIMAL(38,18)) x UNION ALL
      SELECT CAST('0.000000000000000002' AS DECIMAL(38,18)) UNION ALL
      SELECT CAST('0.000000000000000003' AS DECIMAL(38,18))
    )
    SELECT VAR_POP(x), VAR_SAMP(x), STDDEV_POP(x), STDDEV_SAMP(x) FROM vals;

    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,两端相同。

  4. added
    ai-easy
    on Oct 15, 2026
  5. Ariznawlll commented on Oct 15, 2026

    @Ariznawlll
    CollaboratorAuthor

    测试版本

    • 分支:main
    • commit:17a6200f59e764729e5544908fffd5480ae88279
    • 修复:MatrixOne PR #30228,已合入上述官方 main
    • 部署方式:本地隔离 embedded MatrixOne,1 CN + 1 TN + 1 Log Service;独立数据库,无共享集群
    • 测试日期:2026-10-15(Asia/Shanghai)

    原问题验证

    沿用 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 覆盖

    1. 显式 CAST(v AS DOUBLE) 的方差对照也处于预期区间。
    2. PyMySQL 读取真实结果包:四个统计聚合的 wire type 均为 DOUBLE,数值与独立数学预期在 1e-12 相对误差内一致;不是只检查 SQL 字符串。
    3. CREATE TABLE AS SELECT 后四个结果列的数据类型均为 double。
    4. 有效边界:单行输入 VAR_POP=0、VAR_SAMP=NULL;仅 NULL 输入及空输入的两者均为 NULL。

    Unhappy Path 覆盖

    不适用:该 issue 为合法极小 DECIMAL 输入的数值精度问题,无错误输入拒绝或失败回滚契约。单行、仅 NULL、空输入已作为有效边界验证。

    白盒验证

    不适用:原问题可通过公开 SQL 和结果协议观察;本次未做内部故障注入或 -race。

    回归测试

    • MatrixOne 已有 BVT:test/distributed/cases/function/func_aggr_decimal_statistics.sql,包含分组、window、PREPARE 和统计值边界;其 PR 的 TKE 镜像构建检查失败,相关 BVT job 被跳过。
    • 已补 MOTR SQL 与协议场景:issue_30179_decimal_statistics.test、issue_30174_30179_aggregate_metadata.py,见 motr PR #240。本地各 3/3 通过;PR 的 4 项离线检查通过,但尚未合入、集成 MOTR CI 尚未验证。按本次要求先关闭 issue;持续回归正式生效仍待 PR 合入。
    • 不需 big-data、stability 或 chaos:三行极小 DECIMAL 已跨过原精度触发点。

    测试证据

    • MOTR PR #240 的数值及协议断言
    • 本地隔离运行:5 个 MOTR 定向 case × 3 轮,15/15 成功、147/147 SQL/场景语句成功;本 issue 的 SQL 与协议场景各 3/3 成功。

    测试结论

    测试通过。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

ai-easyarea/compatibilitykind/bug

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions