Repository navigation
Description
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
16001and16002
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, andinclude. - Matrix result: 48/48 boundary checks produced the same wrong membership (
>false positive and<=false negative). l2_distance_sq: all 168 corresponding checks matchedmode=force.l2_distancebounds shifted by+1e-5or-1e-5: all controls matchedmode=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
- Source exploration: [Bug]: v4.2.1 multi-CN IVF post search returns nondeterministic candidates due to unordered LIMIT merge #28943
- Separate cache-function issue: [Bug]: IVFFLAT cache keeps the first L2 function and misfilters later l2/l2sq ranges #29038
Activity
Ariznawlll commentedon Sep 17, 2026
补充 5 种 IVFFLAT metric 与两种向量宽度的边界矩阵。固定 commit
01d60e1c4ded1b0f3fc1a4ecd75ce54e95e23b90,每张表 80 个固定随机向量、lists=1,三个独立数据库 × 两个 CN:l2_distancel2_distance_sql1_distancecosine_distanceinner_product对每轮第一条 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。Ariznawlll commentedon Sep 18, 2026
补充确认:这个精度差异不仅改变距离范围边界,还会直接破坏集合运算、等值连接和分组语义。
固定版本:
main/01d60e1c4ded1b0f3fc1a4ecd75ce54e95e23b90,本地 2 CN / 1 TN / 1 LogService。复现方法是让同一张
VECF32表、同一查询向量、同一 Top-K 分别通过 IVFFLAT 和mode=force计算,再消费两支结果。两支的 40 个id完全相同,排除了近似召回和 tie 的影响;唯一差异是相同l2_distance表达式的返回精度。实际结果:
UNIONINTERSECTIVFFLAT EXCEPT forceid和距离等值连接UNION ALL后按id, distance分组,单例组数量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和对应输出中。fix(vectorindex): keep float32-domain rounding out of distance prunin…
Merge upstream/main into bug_29040; revert matrixorigin#29143 Stable*…
test(function): restore func_binary_test.go tests botched by the main…
fix(vectorindex): standardize vector distances on the float32 domain (#…
Ariznawlll commentedon Sep 24, 2026
测试版本
maind7d940e93bd037e9aacdc0f1256b91e32df84642原问题验证
使用原问题的
VECF32(4)数据、查询向量和精确边界4.02731990814209创建lists=1的 L2 IVFFLAT 索引,独立建库重复 3 次。标量路径仍确认
id=29的距离为4.02731990814209,且distance > boundary为 false。当前 IVFFLAT 与标量路径的成员关系一致:>:仅返回id=99;<=:仅返回id=29。Happy Path 覆盖
force/post/pre/auto/include五种模式的>查询均与标量路径一致。force/post/pre/auto/include五种模式的<=查询均与标量路径一致。l2_distance的一致值,不再出现 IVFFLAT score 边界漂移。Unhappy Path 覆盖
id=29。id=29。白盒验证
不适用。该问题可通过公开 SQL 对索引路径与
mode=force的成员关系进行黑盒差分验证。回归测试
已加入 matrixorigin/motr#204:
suites/14_issue_regression/t/issue_29040_ivfflat_l2_boundary.testsuites/14_issue_regression/r/issue_29040_ivfflat_l2_boundary.result新增用例本地重复 3 次全部通过。完整
14_issue_regression套件中本用例通过;套件的其他既有协议/基线失败与本用例无关。测试证据
99089c8b测试结论
测试通过。
fix(vector): reject VECF32 l1_norm/l2_norm that overflow the element …
fix(vectorindex/metric): apply finite-extreme contract in arm64 NEON …