Skip to content

[Bug]: IVFFLAT l2_distance range predicates use a different boundary value than scalar evaluation #29040

Closed

Description

@Ariznawlll

Description

An IVFFLAT scan evaluates l2_distance range predicates with a different numeric value from the public SQL function. At an exact boundary, the index path can return a row for which the same WHERE predicate evaluates to false, or omit a row that satisfies an inclusive predicate.

Environment

  • Branch: main
  • Commit: 01d60e1c4ded1b0f3fc1a4ecd75ce54e95e23b90
  • Deployment: local multi-CN launch, 2 CN / 1 TN / 1 LogService; SQL endpoints 16001 and 16002

Steps to reproduce

SET experimental_ivf_index = 1;
SET probe_limit = 1;

DROP DATABASE IF EXISTS ivf_l2_boundary_min;
CREATE DATABASE ivf_l2_boundary_min;
USE ivf_l2_boundary_min;

CREATE TABLE t(id INT PRIMARY KEY, v VECF32(4));
INSERT INTO t VALUES
  (29, '[2.189,-1.676,2.08,-1.578]'),
  (99, '[10,10,10,10]');

CREATE INDEX i USING IVFFLAT ON t(v)
  LISTS=1 OP_TYPE 'vector_l2_ops';

-- The public function says row 29 is exactly on the boundary and `>` is false.
SELECT id,
       l2_distance(v,'[1.125,-0.625,0.375,1.75]') AS d,
       l2_distance(v,'[1.125,-0.625,0.375,1.75]') > 4.02731990814209 AS satisfies
FROM t WHERE id=29;

-- Exact table-scan control excludes row 29.
SELECT id, l2_distance(v,'[1.125,-0.625,0.375,1.75]') AS d
FROM t
WHERE l2_distance(v,'[1.125,-0.625,0.375,1.75]') > 4.02731990814209
ORDER BY l2_distance(v,'[1.125,-0.625,0.375,1.75]')
LIMIT 10 BY RANK WITH OPTION 'mode=force';

-- IVFFLAT incorrectly includes row 29.
SELECT id, l2_distance(v,'[1.125,-0.625,0.375,1.75]') AS d
FROM t
WHERE l2_distance(v,'[1.125,-0.625,0.375,1.75]') > 4.02731990814209
ORDER BY l2_distance(v,'[1.125,-0.625,0.375,1.75]')
LIMIT 10 BY RANK WITH OPTION 'mode=post';

Actual behavior

The scalar evaluation is:

id  d                 satisfies
29  4.02731990814209  0

mode=force excludes row 29. The IVFFLAT path returns it with a different score:

id  d
29  4.027320069947357
99  18.771237971961252

The opposite boundary is also wrong: l2_distance(...) <= 4.02731990814209 includes row 29 under mode=force, but all tested IVFFLAT modes omit it.

Expected behavior

An index access path must preserve the public SQL predicate semantics. A row for which the scalar l2_distance(...) > bound expression is false must not be returned by the same indexed query, and inclusive bounds must not lose qualifying rows.

Stability and controls

  • Reproducer: 3 independent indexes on each of 2 CN endpoints, 3/3 per endpoint.
  • Affected modes: post, pre, auto, and include.
  • Matrix result: 48/48 boundary checks produced the same wrong membership (> false positive and <= false negative).
  • l2_distance_sq: all 168 corresponding checks matched mode=force.
  • l2_distance bounds shifted by +1e-5 or -1e-5: all controls matched mode=force.
  • Equality, <, and >= controls at the same bound matched in this fixture.
  • No panic, hang, restart, or data mutation was observed.

Evidence

The full differential reproducer is evidence/ivfflat_distance_boundary_differential.py in the local validation worktree. It compares exact row membership rather than only query completion.

Code analysis

The scalar l2_distance implementation returns the square root cast back to the vector element type. For VECF32, metric.L2Distance[T] therefore rounds the result to float32 before the SQL function exposes it as float64 (pkg/vectorindex/metric/distance_func_amd64.go).

IVFFLAT stores/searches squared L2 and converts it through DistanceTransformIvfflat, which applies math.Sqrt and retains the float64 result (pkg/vectorindex/metric/types.go, called by pkg/vectorindex/ivfflat/search.go). filterEntryDistanceRange compares that transformed value directly with the peeled SQL bound (pkg/vectorindex/ivfflat/relation_search.go), so the original predicate is not re-evaluated with the public function's value.

For the reproducer row those paths produce 4.02731990814209 and 4.027320069947357, respectively, which changes boundary membership.

Regression coverage

After the fix, add a vector regression that compares IVFFLAT and mode=force for exact >, >=, <, and <= boundaries under post, pre, auto, and include, with both l2_distance and l2_distance_sq.

Related

Activity

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

    @Ariznawlll
    CollaboratorAuthor

    补充 5 种 IVFFLAT metric 与两种向量宽度的边界矩阵。固定 commit 01d60e1c4ded1b0f3fc1a4ecd75ce54e95e23b90,每张表 80 个固定随机向量、lists=1,三个独立数据库 × 两个 CN:

    向量类型 l2_distance l2_distance_sq l1_distance cosine_distance inner_product
    VECF32 80/80 行的索引 score 与标量值不同 0/80 0/80 0/80 0/80
    VECF64 0/80 0/80 0/80 0/80 0/80

    对每轮第一条 VECF32 L2 差异行,以标量函数返回值作为精确边界,> 均出现 false positive,<= 均出现 false negative;FORCE 的成员关系与该行直接标量布尔表达式一致。其余九种“类型 × metric”组合的公开 SQL 值与 IVFFLAT score 逐行完全相等。

    因此当前 issue 可明确限定为 VECF32 + l2_distance 的 sqrt 精度路径;不是 IVFFLAT 所有 metric 的通用 score 漂移。l2_distance_sq、L1、cosine、inner product 以及 VECF64 L2 是有效控制组。

    复现:evidence/ivfflat_metric_boundary_scope.py。

  5. Ariznawlll commented on Sep 18, 2026

    @Ariznawlll
    CollaboratorAuthor

    补充确认:这个精度差异不仅改变距离范围边界,还会直接破坏集合运算、等值连接和分组语义。

    固定版本:main / 01d60e1c4ded1b0f3fc1a4ecd75ce54e95e23b90,本地 2 CN / 1 TN / 1 LogService。

    复现方法是让同一张 VECF32 表、同一查询向量、同一 Top-K 分别通过 IVFFLAT 和 mode=force 计算,再消费两支结果。两支的 40 个 id 完全相同,排除了近似召回和 tie 的影响;唯一差异是相同 l2_distance 表达式的返回精度。

    实际结果:

    消费方式 正确结果 实际结果
    UNION 40 行 80 行
    INTERSECT 40 行 0 行
    IVFFLAT EXCEPT force 0 行 40 行
    按 id 和距离等值连接 40 行 0 行
    UNION ALL 后按 id, distance 分组,单例组数量 0 行 80 行

    pre、post、include、auto 四种模式都相同。三个独立数据库 × 两个 CN,共 24/24 组复现;每组 EXPLAIN 都确认只有 IVFFLAT 分支使用 Vector Index Scan。

    控制组:

    • VECF32 + l2_distance_sq:6/6 组的 UNION/INTERSECT/EXCEPT/JOIN/GROUP BY 均保持正确语义;
    • VECF64 + l2_distance:6/6 组均保持正确语义。

    因此这里不是 ANN 候选差异,而是 #29040 已定位的 VECF32 + l2_distance 两条精度路径泄漏到关系运算。修复时需要保证索引 score 与公开 SQL 函数值一致,否则即使候选行完全正确,集合、等值和分组仍会得到错误结果。

    本地完整复现保存在 evidence/ivfflat_mixed_path_relational_semantics.py 和对应输出中。

  6. Ariznawlll commented on Sep 24, 2026

    @Ariznawlll
    CollaboratorAuthor

    测试版本

    • 分支:main
    • commit:d7d940e93bd037e9aacdc0f1256b91e32df84642
    • 部署方式:本地分布式,1 LogService / 1 TN / 1 CN;全新数据目录
    • 测试日期:2026-09-24

    原问题验证

    使用原问题的 VECF32(4) 数据、查询向量和精确边界 4.02731990814209 创建 lists=1 的 L2 IVFFLAT 索引,独立建库重复 3 次。

    标量路径仍确认 id=29 的距离为 4.02731990814209,且 distance > boundary 为 false。当前 IVFFLAT 与标量路径的成员关系一致:

    • >:仅返回 id=99;
    • <=:仅返回 id=29。

    Happy Path 覆盖

    1. force/post/pre/auto/include 五种模式的 > 查询均与标量路径一致。
    2. force/post/pre/auto/include 五种模式的 <= 查询均与标量路径一致。
    3. 所有模式均返回公开 SQL l2_distance 的一致值,不再出现 IVFFLAT score 边界漂移。

    Unhappy Path 覆盖

    1. 精确边界的排他条件未错误包含 id=29。
    2. 精确边界的包含条件未丢失 id=29。
    3. 三轮共 30 个 mode/operator 检查结果一致;未观察到 panic、hang、重复行或数据修改。

    白盒验证

    不适用。该问题可通过公开 SQL 对索引路径与 mode=force 的成员关系进行黑盒差分验证。

    回归测试

    已加入 matrixorigin/motr#204:

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

    新增用例本地重复 3 次全部通过。完整 14_issue_regression 套件中本用例通过;套件的其他既有协议/基线失败与本用例无关。

    测试证据

    • 回归 PR:matrixorigin/motr#204
    • 回归提交:99089c8b
    • 本地测试汇总:目标三项合计重复 9/9 通过,177/177 statements 成功。

    测试结论

    测试通过。

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