Skip to content

[Bug]: BIT_COUNT rounds FLOAT and DOUBLE inputs before counting bits, unlike MySQL #30172

Closed

Description

@Ariznawlll

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 (version 8.0.30-MatrixOne-v)
  • Latest official main inspected: a53390c57de48ac280249ff0e88831e502e20fb0; pkg/sql/plan/function/func_unary.go is unchanged between the tested build and this main commit and still uses math.RoundToEven in 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) and BIT_COUNT(-3) return the same values on both servers (2 and 63).
  • Broader paths already checked locally: explicit FLOAT casts and SQL PREPARE/EXECUTE preserve 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

Activity

  1. added this to the MO-202610 milestone on Oct 14, 2026
  2. added
    kind/bug
    on Oct 14, 2026
  3. added theissue type on Oct 14, 2026
  4. Ariznawlll commented on Oct 14, 2026

    @Ariznawlll
    CollaboratorAuthor

    类型扩展复核

    扩展检查了 DECIMAL、负小数、边界 UNSIGNED、BIT 字面值和 NULL;这些路径当前均与 MySQL 8.0.45 一致:

    SELECT BIT_COUNT(CAST(3.9 AS DECIMAL(10,2))),
           BIT_COUNT(CAST(-3.9 AS DECIMAL(10,2))),
           BIT_COUNT(CAST(-0.9 AS DECIMAL(10,2)));
    -- 1, 62, 64
    
    SELECT BIT_COUNT(CAST('18446744073709551615' AS UNSIGNED)),
           BIT_COUNT(CAST(-1 AS SIGNED)),
           BIT_COUNT(CAST(0 AS UNSIGNED));
    -- 64, 64, 0
    
    SELECT BIT_COUNT(B'101'), BIT_COUNT(X'FF'), BIT_COUNT(CAST(NULL AS SIGNED));
    -- 2, 8, NULL

    MatrixOne 8.0.30-MatrixOne-v1.3.0 与 MySQL 8.0.45 的以上值相同。因此本次没有发现 DECIMAL/UNSIGNED/BIT 类型上的新增差异;当前已确认的差异仍限于 issue 中的 FLOAT/DOUBLE 小数转换路径。

  5. removed their assignment
    on Oct 16, 2026
  6. added
    ai-easy
    on Oct 16, 2026
  7. Ariznawlll commented on Oct 16, 2026

    @Ariznawlll
    CollaboratorAuthor

    测试版本

    • 分支:官方 main
    • commit:b24d1b311555e0b6f3d56eb37f9a2e9617e9a5b7(修复 PR #30307 已合入且为该 SHA 祖先)
    • 部署方式:本机隔离 worktree 自编译 mo-service,1 CN / 1 TN / 1 LogService,独立端口 38001;MySQL CLI 客户端
    • 测试日期:2026-10-16

    原问题验证

    原 SQL 的 DOUBLE(3.5, 3.9, -3.5) 三次均得到 2, 2, 63,整数对照 3, -3 为 2, 63;不再出现原来的 1, 1, 62。FLOAT 同组三值三次均为 2, 2, 63。

    Happy Path 覆盖

    1. DOUBLE/FLOAT 表列按 -3.5, 3.5, 3.9 逐行计算,两种类型均为 63, 2, 2。
    2. PREPARE ... BIT_COUNT(CAST(? AS DOUBLE)) 绑定 3.9 得 2;整数 3/-3 保持 2/63。
    3. 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 尚未验证,后续继续跟踪。

    测试证据

    • MOTR 用例及明确预期(提交 429916e1)
    • 原 SQL 本轮 3/3:DOUBLE=2,2,63、FLOAT=2,2,63;MOTR 定向结果 3/3、合并运行 12/12 用例通过。

    测试结论

    测试通过。

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

Metadata

Metadata

Assignees

Labels

ai-easyarea/compatibilitykind/bug

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions