Skip to content

[Bug]: AVG narrows integer capacity for DECIMAL(62-65) and rejects valid values #28776

Closed

Description

@Ariznawlll

问题

AVG 对 DECIMAL(62..65, scale) 生成结果类型时,将 precision 封顶为 65,但仍无条件把 scale 增加 4。结果类型的整数位容量比输入少 1~4 位,导致完全位于源列范围内的单个值也无法求平均。

一个单值集合的 AVG 必须等于该值;当前普通聚合、GROUP BY 和窗口 AVG 都会对合法的大值报 20301。

环境

  • 官方 main:1695c139956e9dea5a6b5ef4bd02367cec63b164
  • 2026-09-12,本地源码构建,2 CN / 1 TN / 1 Log service。
  • 对照:MySQL Community Server 8.3.0。

最小复现

SELECT AVG(v)
FROM (
  SELECT CAST(
    '99999999999999999999999999999999999.999999999999999999999999999999'
    AS DECIMAL(65,30)
  ) AS v
) q;

MatrixOne:

ERROR 20301 (HY000): invalid input:
99999999999999999999999999999999999.9999999999999999999999999999990000
beyond the range, can't be converted to Decimal256(65,34).

MySQL 8.3.0 返回:

99999999999999999999999999999999999.999999999999999999999999999999

同一输入的 AVG(v) OVER() 和按主键单行分组的 AVG(v) 也报相同错误;正负值行为一致。

边界扫描

扫描 precision=59~65、scale=0/1/5/10/20/30、正负最大值,以及 ordinary/grouped/window 三条执行路径,共 252 个组合/轮,双方各重复 3 轮:

MatrixOne 每轮:cases=252, wrong=0, errors=144
errorByPrecision={62=36, 63=36, 64=36, 65=36}

MySQL 每轮:cases=252, wrong=0, errors=0

precision=59/60/61 的全部组合返回精确原值;从 62 开始,每个 scale、正负号和三条路径都被拒绝。NULL 的普通和窗口 AVG 均返回 NULL。

表、视图、CTAS 和 PreparedStatement

对真实 DECIMAL(65,30) 列另外覆盖:

  • 普通、主键 GROUP BY、HAVING、窗口和视图读取合法最大值都报 20301;31 位整数的较小控制值可以返回。
  • CREATE TABLE bad_ctas AS SELECT AVG(d) 报错后,information_schema 中没有残留目标表;MySQL 创建目标并保存原值。
  • 对零值执行 CTAS,MO 生成 DECIMAL(65,34),MySQL 生成 DECIMAL(65,30)。这直接证明错误来源于结果域,而不是聚合输入先损坏。
  • SQL PREPARE 按 最大正值 -> 较小控制值 -> 最大负值 复用时表现为 error -> exact value -> error,错误后 statement 可继续使用。
  • 最小正负查询在两个 CN 上分别重复 3 轮;完整表/CTAS/prepared 脚本在 CN1 重建数据后重复 3 轮,结果一致。

代码定位

pkg/sql/colexec/aggexec/sumavg2.go 的 AvgReturnType 对 Decimal256 执行:

precision := min(typ.Width+4, 65)
scale := avgDecimalScale(typ.Scale) // input scale + 4
return DECIMAL256(precision, scale)

输入为 DECIMAL(65,30) 时,这会得到 DECIMAL(65,34),整数容量由 35 位缩到 31 位;输入为 DECIMAL(62,0) 时得到 DECIMAL(65,4),整数容量由 62 位缩到 61 位。因此 precision=62 是扫描中精确出现的首个失败点。

封顶时需要优先保留源整数容量,按剩余 precision 限制增加的 scale;MySQL 对 DECIMAL(65,30) 的 AVG 结果保持 DECIMAL(65,30)。

查重和回归方向

建议补 AvgReturnType 的 p=61/62/65 边界 UT,以及 ordinary/group/window/CTAS/prepared SQL 回归。无需 big-data 或 chaos 即可稳定触发。

Activity

  1. added this to the MO-202609 milestone on Sep 12, 2026
  2. added
    kind/bug
    on Sep 12, 2026
  3. added theissue type on Sep 12, 2026
  4. self-assigned this
    on Sep 12, 2026
  5. added a commit that references this issue on Sep 14, 2026
    b26b298
  6. Ariznawlll commented on Sep 14, 2026

    @Ariznawlll
    CollaboratorAuthor

    测试版本

    原问题验证

    • AVG(DECIMAL 62–65) 保留源整数容量。

    Happy Path 覆盖

    • DECIMAL(65,30) 最大合法值 AVG 精确返回,普通控制 AVG=1。

    Unhappy Path 覆盖

    • 覆盖 precision cap 边界,不再把合法结果缩窄后报错。

    白盒验证

    回归测试

    测试证据

    • 独立环境三轮输出字节一致;服务日志未见 panic/hang,测试后已清理服务。

    测试结论

    测试通过。

  7. added 3 commits that reference this issue on Sep 17, 2026
    bd2558f
    04db999
    c9e92f2
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