Repository navigation
Description
Description
BIT_COUNT() returns incorrect bit counts for fractional FLOAT and DOUBLE expressions. MatrixOne rounds the value to the nearest even integer before counting bits, while MySQL truncates the fractional part toward zero for these inputs.
Environment
- MatrixOne runtime: local standalone, build commit
2ce4e58af6f03b943ee288ffad81012766cf830a(version8.0.30-MatrixOne-v) - Latest official
maininspected:a53390c57de48ac280249ff0e88831e502e20fb0;pkg/sql/plan/function/func_unary.gois unchanged between the tested build and this main commit and still usesmath.RoundToEvenin the float path. - MySQL reference: 8.0.46
- Verified: 2026-10-14
Steps to reproduce
SELECT BIT_COUNT(CAST(3.5 AS DOUBLE)) AS pos_half,
BIT_COUNT(CAST(3.9 AS DOUBLE)) AS pos_fraction,
BIT_COUNT(CAST(-3.5 AS DOUBLE)) AS neg_half,
BIT_COUNT(3) AS int_control,
BIT_COUNT(-3) AS signed_control;Actual behavior
MatrixOne returns:
1 1 62 2 63
MySQL 8.0.46 returns:
2 2 63 2 63
The mismatch also occurs with CAST(... AS FLOAT). Integer controls match. The fractional expression reproduced identically in three consecutive runs.
Expected behavior
BIT_COUNT() should count the bits of the MySQL-compatible integer conversion of the numeric input. For the examples above, the fractional part is truncated toward zero before counting; it should not be rounded to the nearest even integer.
Stability and controls
- Reproducer: 3/3 identical results on the local MatrixOne build and MySQL 8.0.46.
- Control:
BIT_COUNT(3)andBIT_COUNT(-3)return the same values on both servers (2and63). - Broader paths already checked locally: explicit
FLOATcasts and SQLPREPARE/EXECUTEpreserve the mismatch; a column filter and materialized generated-column/index path can persist or select on the incorrect count.
Code analysis
In pkg/sql/plan/function/func_unary.go, bitCountFromFloat applies math.RoundToEven before converting the value to the 64-bit pattern. This is consistent with MatrixOne's observed result and differs from MySQL's conversion for fractional inputs.
Regression coverage
After the behavior is corrected, add a SQL regression for positive and negative fractional FLOAT/DOUBLE inputs plus integer controls; include prepared execution and a row-based expression path so constant folding and vector evaluation remain aligned. No test file is added by this issue report.
Related
- [Bug]: BIT_COUNT exposes a parsing error instead of coercing nonnumeric strings #28219 covers
BIT_COUNTparsing/coercion of nonnumeric strings; it does not cover fractional numericFLOAT/DOUBLEinputs.
Activity
Ariznawlll commentedon Oct 14, 2026
类型扩展复核
扩展检查了 DECIMAL、负小数、边界 UNSIGNED、BIT 字面值和 NULL;这些路径当前均与 MySQL 8.0.45 一致:
MatrixOne
8.0.30-MatrixOne-v1.3.0与 MySQL 8.0.45 的以上值相同。因此本次没有发现 DECIMAL/UNSIGNED/BIT 类型上的新增差异;当前已确认的差异仍限于 issue 中的 FLOAT/DOUBLE 小数转换路径。Ariznawlll commentedon Oct 16, 2026
测试版本
mainb24d1b311555e0b6f3d56eb37f9a2e9617e9a5b7(修复 PR #30307 已合入且为该 SHA 祖先)mo-service,1 CN / 1 TN / 1 LogService,独立端口 38001;MySQL CLI 客户端原问题验证
原 SQL 的
DOUBLE(3.5, 3.9, -3.5)三次均得到2, 2, 63,整数对照3, -3为2, 63;不再出现原来的1, 1, 62。FLOAT同组三值三次均为2, 2, 63。Happy Path 覆盖
DOUBLE/FLOAT表列按-3.5, 3.5, 3.9逐行计算,两种类型均为63, 2, 2。PREPARE ... BIT_COUNT(CAST(? AS DOUBLE))绑定3.9得2;整数3/-3保持2/63。BIT_COUNT(CAST(NULL AS SIGNED))返回NULL。Unhappy Path 覆盖
本问题是合法输入错值,没有预期报错路径;负数和 NULL 的相关边界已在上面核对。本轮表数据和测试库均已清理。
白盒验证
修复 PR 调整
bitCountFromFloat并带有func_bit_count_test.go的类型覆盖;本轮以独立 SQL/协议复测为主,未单独重跑 Go UT。回归测试
已追加 MOTR 汇总 PR #237 的
suites/14_issue_regression/t/issue_30172_bit_count_fractional.test与配对 result:原复现、FLOAT/DOUBLE、整数/NULL、PREPARE、表列。定向 3/3,通过合并并行运行 3/3(13 条语句/轮)。该 PR 尚未合入,完整 suite/集成 CI 尚未验证,后续继续跟踪。测试证据
429916e1)DOUBLE=2,2,63、FLOAT=2,2,63;MOTR 定向结果 3/3、合并运行 12/12 用例通过。测试结论
测试通过。