Skip to content

[Bug]: ROUND misrounds negative signed integers at negative precision #28490

Closed

Description

@Ariznawlll

Environment

  • MatrixOne: latest main, commit f0c31cd4b830be32442cf329e0a3fb08aa9c16c3
  • MySQL comparison: 8.3.0

Problem

ROUND() produces the wrong result when its first argument uses a signed integer overload, the value is negative, and the precision is negative. Values below the half-way point are rounded down instead of toward the nearest multiple, and values that are already exact multiples are moved down by a whole unit of the requested precision.

This affects TINYINT, SMALLINT, INT, and BIGINT, as well as integer expressions, prepared statements, and persisted CTAS results. The same values represented as DECIMAL or DOUBLE are calculated correctly.

Minimal reproduction

SELECT ROUND(-1, -1), ROUND(-10, -1), ROUND(-11, -1);
SELECT ROUND(-100, -2), ROUND(-101, -2);

MatrixOne returns:

-10, -20, -20
-200, -200

The nearest multiples, also returned by MySQL 8.3, are:

0, -10, -10
-100, -100

The exact-multiple cases are especially clear: ROUND(-10, -1) must not change -10, and ROUND(-100, -2) must not change -100.

Coverage

The behavior was reproduced three times through each relevant path:

  • constants around the rounding boundary: -1, -4, -5, -6, -9, -10, -11, -14, -15, -16, -19, and -20;
  • hundreds-scale boundaries from -100 through -200;
  • TINYINT, SMALLINT, INT, and BIGINT columns;
  • integer expressions and SQL PREPARE/EXECUTE parameters;
  • CTAS, which persists the incorrect values.

Controls:

  • positive signed integers are rounded correctly;
  • negative DECIMAL and DOUBLE values are rounded correctly;
  • precision zero/positive, NULL, and TRUNCATE() controls are correct;
  • the server remains healthy after every run.

Code analysis

The signed-integer branch in pkg/sql/plan/function/func_binary.go computes a negative remainder and compares it with a positive threshold:

step2 := x % scale
if step2 <= scale/2 {
    x = step1 - scale
}

For every negative remainder, including zero, step2 <= scale/2 is true. As a result, the implementation always selects the lower bucket for a negative integer. The comparison needs to use the magnitude of the remainder (or an equivalent sign-safe rule) and preserve exact multiples.

Activity

  1. added this to the MO-202609 milestone on Sep 9, 2026
  2. added theissue type on Sep 9, 2026
  3. Ariznawlll commented on Sep 9, 2026

    @Ariznawlll
    CollaboratorAuthor

    补充极值边界:ROUND(CAST(-9223372036854775808 AS SIGNED), -18) 在 MatrixOne 返回 1690,而 MySQL 8.3 返回 -9000000000000000000。它仍由正文所述的负整数余数分支导致;同一输入在 -1 精度下两端都会因结果超出 BIGINT 而报 1690,因此未作为独立问题拆分。

  4. added a commit that references this issue on Oct 1, 2026
    a09f3a3
  5. Ariznawlll commented on Oct 2, 2026

    @Ariznawlll
    CollaboratorAuthor

    测试版本

    • 分支:main
    • Commit:d50a2d475a406829f67127fef7b2ad4abc1c1d3d
    • 部署方式:本地全新单 CN mo-service,独立数据目录;MySQL 文本协议与 server-side PREPARE
    • 测试日期:2026-10-02

    原问题验证

    按 issue 原始语义在上述 commit 重新执行并重复 3 次。ROUND 对负整数负 precision 返回 -20、-30、-100、-200 等正确结果。

    Happy Path 覆盖

    覆盖正负、tie、非 tie 及多种负 precision。

    Unhappy Path 覆盖

    覆盖 BIGINT 最小值、结果溢出和 NULL;溢出保持 ERROR 1690。

    白盒验证

    关联修复 #29546 已进入当前 main。同时将本 issue 的可公开观察契约固化为独立回归,避免只依赖实现细节或共享测试间接判断。

    回归测试

    测试证据

    • 精确测试版本:d50a2d475a406829f67127fef7b2ad4abc1c1d3d
    • 当前结果:ROUND 对负整数负 precision 返回 -20、-30、-100、-200 等正确结果。
    • 回归代码与 golden result 已提交到 MOTR #209;本地全量 14_issue_regression 中观察到的旧 GOROOT/历史 golden/并发锁冲突属于既有无关失败,以上新增用例单独重跑均稳定通过。

    测试结论

    测试通过。

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

Metadata

Metadata

Assignees

Labels

area/compatibilitykind/bug

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions