Repository navigation
Description
Environment
- MatrixOne: latest
main, commitf0c31cd4b830be32442cf329e0a3fb08aa9c16c3 - 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
-100through-200; TINYINT,SMALLINT,INT, andBIGINTcolumns;- integer expressions and SQL
PREPARE/EXECUTEparameters; - CTAS, which persists the incorrect values.
Controls:
- positive signed integers are rounded correctly;
- negative
DECIMALandDOUBLEvalues are rounded correctly; - precision zero/positive,
NULL, andTRUNCATE()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
Ariznawlll commentedon Sep 9, 2026
补充极值边界:
ROUND(CAST(-9223372036854775808 AS SIGNED), -18)在 MatrixOne 返回 1690,而 MySQL 8.3 返回-9000000000000000000。它仍由正文所述的负整数余数分支导致;同一输入在-1精度下两端都会因结果超出 BIGINT 而报 1690,因此未作为独立问题拆分。fix(sql): correct signed ROUND and close speculative evaluation failu…
Ariznawlll commentedon Oct 2, 2026
测试版本
maind50a2d475a406829f67127fef7b2ad4abc1c1d3dmo-service,独立数据目录;MySQL 文本协议与 server-side PREPARE原问题验证
按 issue 原始语义在上述 commit 重新执行并重复 3 次。ROUND 对负整数负 precision 返回 -20、-30、-100、-200 等正确结果。
Happy Path 覆盖
覆盖正负、tie、非 tie 及多种负 precision。
Unhappy Path 覆盖
覆盖 BIGINT 最小值、结果溢出和 NULL;溢出保持 ERROR 1690。
白盒验证
关联修复 #29546 已进入当前
main。同时将本 issue 的可公开观察契约固化为独立回归,避免只依赖实现细节或共享测试间接判断。回归测试
suites/14_issue_regression/t/issue_recent_sql_semantics.test714/714条语句通过。测试证据
d50a2d475a406829f67127fef7b2ad4abc1c1d3d14_issue_regression中观察到的旧 GOROOT/历史 golden/并发锁冲突属于既有无关失败,以上新增用例单独重跑均稳定通过。测试结论
测试通过。