Skip to content

[Bug]: FLOAT and DOUBLE secondary-index equality omits SQL-equal signed zero #28695

Closed

Description

@Ariznawlll

问题

FLOAT/DOUBLE 二级索引把 -0.0 与 +0.0 编码为不同的索引键,但 SQL 比较、分组和唯一键均把它们视为相等。因此等值谓词使用二级索引时漏行:结果随访问路径变化。

环境

  • MatrixOne main:7013fbbd6e152ba31da2d3dbba7012ac7f8b8462,新启动的 2 CN 环境。
  • 对照:MySQL 8.3。

复现

create table d(id int primary key, v double, key idx_v(v));
insert into d values
  (1, cast('-0.0' as double)),
  (2, cast('0.0' as double)),
  (3, 1.0);

select group_concat(id order by id) from d
where v = cast('-0.0' as double);
-- MO: 1

select group_concat(id order by id) from d
where v = cast('0.0' as double);
-- MO: 2

select group_concat(id order by id) from d ignore index(idx_v)
where v = cast('-0.0' as double);
-- MO: 1,2

explain select id from d where v = cast('-0.0' as double);
-- Index Table Scan on d.idx_v

同样的 FLOAT 表得到相同行为。

MO 中 -0.0 = +0.0 和 -0.0 <=> +0.0 都为 true;GROUP BY v 合并为一组,且向 DOUBLE UNIQUE KEY 插入 -0.0 后插入 +0.0 会正确报重复键。因此索引谓词的结果与同一实例的 SQL 相等语义不一致。

在 MySQL 8.3,DOUBLE/FLOAT 的索引访问和 IGNORE INDEX 扫描对正、负零谓词均返回 1,2。

稳定性与范围

  • DOUBLE 使用三个独立表重复三轮,每轮 -0.0 索引谓词返回 1、+0.0 返回 2,对应扫描均为 1,2。
  • FLOAT 使用相同数据与二级索引,索引和扫描差分相同。
  • SQL PREPARE/EXECUTE 复用这一索引路径时也只返回一行;无索引表返回两行。

代码线索

pkg/container/types/compare.go 已将 signed zero 定义为 SQL ORDER BY peer;pkg/common/hashmap/keycodec/keycodec.go 的 CanonicalFloat32Bits / CanonicalFloat64Bits 也会将两种零归一化。相对地,二级索引的物理元组键保留浮点原始位表示(Float32TupleAscCompare / Float64TupleAscCompare 的注释明确说明会区分 signed zero)。当前 prefix_eq(__mo_index_idx_col) 没有将 SQL 的零等值谓词扩展到两个物理键,导致漏行。

预期

二级索引只能作为访问路径,不能改变 SQL 等值语义。FLOAT/DOUBLE 对 -0.0 或 +0.0 的 =, <=>, IN 等谓词走索引时应覆盖两种物理零表示,或索引键应在写入/查找时按 SQL 等值规则归一化。

Activity

  1. added this to the MO-202609 milestone on Sep 11, 2026
  2. added
    kind/bug
    on Sep 11, 2026
  3. added theissue type on Sep 11, 2026
  4. Ariznawlll commented on Sep 11, 2026

    @Ariznawlll
    CollaboratorAuthor

    补充谓词范围:

    • v <=> -0.0 / v <=> +0.0 在本轮返回两行,说明该形式没有触发同样的漏行路径。
    • v IN (-0.0)、v IN (+0.0) 仍各只返回一种物理零表示。
    • v >= 0.0 AND v <= 0.0 仅返回 +0.0;以 -0.0 为上下界仅返回 -0.0。
    • v BETWEEN -0.0 AND +0.0 返回两行,因为物理范围恰好覆盖两种编码。

    FLOAT 与 DOUBLE 均一致;MySQL 对上述所有谓词均返回两行。问题不仅限于 =,还影响走索引的单值 IN 与退化零范围。

  5. self-assigned this
    on Sep 12, 2026
  6. added a commit that references this issue on Sep 20, 2026
    c2a1d63
  7. Ariznawlll commented on Sep 20, 2026

    @Ariznawlll
    CollaboratorAuthor

    测试版本

    • 分支:main
    • commit:22d43911d3f6c51872c1852340265a7d3f206925
    • 环境:隔离单机 Log/TN/CN
    • 日期:2026-09-20

    原问题验证

    建立 FLOAT、DOUBLE 二级索引后分别写入 -0.0、+0.0 和 1.0。= 的正零/负零谓词、IN (-0.0),以及参数化 PREPARE/EXECUTE 都返回 1,2;结果与表扫描的 SQL 等值语义一致,未再出现按物理 signed-zero 位模式漏行。

    覆盖范围

    • DOUBLE:=-0.0、=+0.0、IN (-0.0);
    • FLOAT:=-0.0、=+0.0、IN (-0.0);
    • DOUBLE 参数化等值谓词;
    • 所有查询均显式指定对应二级索引,确认访问路径不会改变结果。

    回归测试

    已加入 MOTR PR #193:

    • suites/14_issue_regression/t/issue_28695_signed_zero_secondary_index.test
    • suites/14_issue_regression/r/issue_28695_signed_zero_secondary_index.result

    在上述 commit 录制基线后连续执行 3 轮,54/54 statements 通过。

    测试证据

    修复合入提交 c2a1d63ee08d31c09cc30099af54b7a51168e236 已包含在本次验证的 main 中;回归提交为 MOTR faf895aa。

    测试结论

    测试通过。

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