Skip to content

[Bug]: binary prepared DECIMAL arithmetic rejects representable values with trailing zeros #28771

Closed

Description

@Ariznawlll

问题

二进制 PreparedStatement 的 DECIMAL 参数参与算术运算时,数值相同、仅尾部零数量不同的 BigDecimal 会出现不同结果。1.2 正常;1.200000000000000000000000000000 被报成无法转换为 Decimal64(2,1)。该值本身完全可以表示,不是真实数值越界。

环境

  • 官方 main:10ce23e7423d690ece19098e39a78b17236aa771
  • 2026-09-12,本地源码构建,etc/launch-multi-cn/launch.toml,2 CN / 1 TN / 1 Log service。
  • Connector/J 8.0.33、8.4.0;useServerPrepStmts=true&cachePrepStmts=false。
  • 8.4.0 同时用 useServerPrepStmts=false 做同驱动对照;本条没有将 MySQL 服务端作为实测基线。

最小复现

下面的 connection 是已建立的 JDBC 连接,URL 启用 useServerPrepStmts=true。

try (PreparedStatement ps = connection.prepareStatement(
        "SELECT ? + CAST(0 AS DECIMAL(65,30))")) {
    for (String v : new String[]{
            "1.2", "1.200000000000000000000000000000", "2.3"}) {
        ps.setBigDecimal(1, new java.math.BigDecimal(v));
        try (ResultSet rs = ps.executeQuery()) {
            rs.next();
            System.out.println(rs.getBigDecimal(1).toPlainString());
        } catch (SQLException e) {
            System.out.println(e.getErrorCode() + ": " + e.getMessage());
        }
    }
}

实际输出:

1.200000000000000000000000000000
20301: invalid input: 1200000000000000000000000000000e-30 beyond the range, can't be converted to Decimal64(2,1).
2.300000000000000000000000000000

期望三个值分别为 1.2、1.2、2.3,不应因为无意义的尾部零报溢出。

影响范围与对照

  • 8.0.33 的原始 JDBC 复现连续 3 次一致;8.4.0 的完整矩阵连续 3 次一致。
  • ? + decimal、decimal + ?、减法、乘法、负值及 COALESCE 都覆盖到同类异常;DECIMAL64/128/256 的另一侧操作数均可触发。
  • 对 1.2 逐步增加小数位:实测 scale=1/6/16/17 正常;18/19/29/30/37/38/39/64/65/75 报错;76/77/80 又恢复正常。该边界与表示形式有关,不随实际数值增加。
  • SELECT ? 可以精确返回该参数,元数据为 DECIMAL(31,30)。
  • SELECT CAST(? AS DECIMAL(65,30)) + CAST(0 AS DECIMAL(65,30)) 正常。
  • 同一驱动改为客户端预编译,最小加法例子连续 3 次正常。
  • 原始算术矩阵中用 setString 传入同一文本也正常。
  • 出错后同一个 PreparedStatement 再传 2.3 能继续执行。

写入同样受影响:

CREATE TABLE t(id INT PRIMARY KEY,v DECIMAL(65,30),note VARCHAR(30));
INSERT INTO t VALUES(1,2.3,'original');
-- JDBC setBigDecimal(1, new BigDecimal("1.200000000000000000000000000000"))
UPDATE t SET v=?+CAST(0 AS DECIMAL(65,30)),note='changed' WHERE id=1;

服务端预编译报 20301;检查行仍为 2.3, original,未观察到部分更新。客户端预编译写入 1.2, changed。两条路径均重复 3 次。

代码分析

以下是由代码与边界实验支持的原因定位,未修改产品代码:

  1. PreparedDecimalRuntimeDomains 同时产生去尾零的 normalized 类型和保留 scale 的 visible 类型,但 canonical 文本通常保留 visible 的完整系数。
  2. 对本例得到 normalized Decimal64(2,1),canonical 却为 1200000000000000000000000000000e-30;binaryProtocolPrepareParamDomains 将二者带入参数替换。直接结果路径另外使用 visible 类型,所以 SELECT ? 正常。
  3. Parse64 在读到指数补偿前,已经因为整数系数超过 18 位而报错。
  4. visible 超过内部可表示范围时,canonical 反而回退到 normalized 系数,解释了 scale=76/77/80 又正常的现象。

类型与 canonical 数值的物化契约需要一致;单纯缩小类型而不对应规范化系数,会把可表示值变成中间解析溢出。

与已有问题的区别

建议修复后补二进制协议回归和 normalized/visible/canonical 配对 UT。本轮只保留探索脚本,没有添加 BVT。

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. Ariznawlll commented on Sep 12, 2026

    @Ariznawlll
    CollaboratorAuthor

    补充 MySQL 服务端对照:本地启动 MySQL Community Server 8.3.0,使用同一份 JDBC 程序、Connector/J 8.4.0、useServerPrepStmts=true&cachePrepStmts=false 连续执行 3 轮。

    SELECT ? + CAST(0 AS DECIMAL(65,30)) 依次绑定 BigDecimal:

    参数                              MySQL 8.3.0 实际结果
    1.2                               1.200000000000000000000000000000
    1.200000000000000000000000000000    1.200000000000000000000000000000
    2.3                               2.300000000000000000000000000000
    2.300000000000000000000000000000    2.300000000000000000000000000000
    

    三轮全部保持精确数值,没有 20301。MatrixOne 同一官方 main 10ce23e7423d690ece19098e39a78b17236aa771 在两个带 30 位小数尾零的参数处报 20301。

    这补齐了 issue 创建时尚未运行的 MySQL 服务端对照:差异在 MO 的二进制 DECIMAL 参数物化路径,不是 BigDecimal 本身数值越界,也不是 Connector/J 对尾零的通用限制。

  5. self-assigned this
    on Sep 12, 2026
  6. Ariznawlll commented on Sep 12, 2026

    @Ariznawlll
    CollaboratorAuthor

    补充确认该参数类型问题不只影响投影算术,也影响表列过滤。以下均在 main@1695c139956e9dea5a6b5ef4bd02367cec63b164、Connector/J 8.4、服务端二进制 PreparedStatement 下复现 3/3:

    CREATE TABLE t(id INT PRIMARY KEY, d DECIMAL(65,30));
    INSERT INTO t VALUES (1, CAST('1.2' AS DECIMAL(65,30)));
    SELECT id FROM t WHERE d = ?;

    绑定 BigDecimal("1.200000000000000000000000000000") 时,MO 返回:

    20301 invalid input: 1200000000000000000000000000000e-30 beyond the range,
    can't be converted to Decimal64(2,1)
    

    同一数值使用 BigDecimal("1.2") 或在 SQL 中显式 CAST(? AS DECIMAL(65,30)) 均可正确匹配。问题与索引选择无关:普通堆表等值/范围过滤以及 FORCE INDEX、USE INDEX、IGNORE INDEX 都有相同的错误。MySQL 8.3 对两种 scale 的 BigDecimal 均匹配同一行。

    另外,首次转换报错后继续复用同一个 PreparedStatement 会触发独立的计划生命周期问题;该问题另行提交,避免与本 issue 的数值类型选择根因混合。

  7. Ariznawlll commented on Sep 12, 2026

    @Ariznawlll
    CollaboratorAuthor

    远端 main 在本轮验证期间前进到 46cf1d24a7d77e7be6060a17e4596da90743b59f。完成重新构建并在全新启动的 2-CN 环境上复核后,现象保持不变:CN1 3/3、CN2 3/3;堆表等值/范围过滤及各索引提示路径均一致,重新 prepare 后恢复。issue 环境信息已更新为该提交。

  8. added a commit that references this issue on Sep 14, 2026
    00eab40
  9. Ariznawlll commented on Sep 14, 2026

    @Ariznawlll
    CollaboratorAuthor

    测试版本

    原问题验证

    • 二进制 prepared DECIMAL 算术会规范化尾随零,不再把可表示值拒绝。

    Happy Path 覆盖

    • 1.200000000000000000000000000000 可进入 DECIMAL(65,30) 算术。

    Unhappy Path 覆盖

    • 覆盖尾随零的 binary NEWDECIMAL 参数物化,避免 20301。

    白盒验证

    • pkg/frontend TestInitExecuteStmtParamAcceptsPaddedBinaryDecimal 连续 3 次通过。

    回归测试

    测试证据

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

    测试结论

    测试通过。

  10. added 3 commits that reference this issue on Sep 17, 2026
    aadb5ab
    f9dc68f
    187421b
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