Repository navigation
Description
问题
HNSW 的 cosine score 在零向量和极小非零向量边界不符合 MO cosine_distance 的 SQL 语义:
cosine_distance对零模向量采用距离1的约定,标量函数和 IVFFLAT 都遵循该语义;HNSW 使用零向量作为查询向量时,却把“零数据向量 × 零查询向量”的距离返回为0;- 极小但非零的向量会让 HNSW 返回
-Inf或负距离,而同一表达式的标量函数和 IVFFLAT 返回有限的[0,2]范围值。
因此启用 HNSW rewrite 会改变 Top-K 行成员,并且同一条 SQL 内直接投影的距离与嵌套标量表达式互相矛盾。
环境
- MatrixOne
main - commit:
01d60e1c4ded1b0f3fc1a4ecd75ce54e95e23b90 - 本地 2 CN / 1 TN / 1 LogService
- 同步 HNSW,
vector_cosine_ops
最小复现
set experimental_hnsw_index=1;
create database hnsw_cosine_zero;
use hnsw_cosine_zero;
create table t(id bigint primary key, v vecf32(3) not null);
insert into t values
(1, '[1,1,1]'),
(2, '[2,1,1]'),
(3, '[3,1,1]'),
(99, '[0,0,0]'),
(100, '[-0,0,-0]'),
(101, '[1e-20,1e-20,1e-20]');
create index hx using hnsw on t(v)
op_type 'vector_cosine_ops'
m=16 ef_construction=128 ef_search=256 max_index_capacity=100;
alter table t alter reindex hx hnsw force_sync;
select id,
cosine_distance(v,'[0,0,0]') direct_d,
cosine_distance(v,'[0,0,0]') + 0 scalar_d,
cosine_distance(v,'[0,0,0]') = 1 scalar_eq_one
from t
order by cosine_distance(v,'[0,0,0]')
limit 2;
select id, cosine_distance(v,'[0,0,0]') d
from t
order by cosine_distance(v,'[0,0,0]')
limit 2 by rank with option 'mode=force';
select id,
cosine_distance(v,'[1e-20,1e-20,1e-20]') direct_d,
cosine_distance(v,'[1e-20,1e-20,1e-20]') + 0 scalar_d
from t
order by cosine_distance(v,'[1e-20,1e-20,1e-20]')
limit 6;实际结果
HNSW 路径:
id direct_d scalar_d scalar_eq_one
100 0 1 1
99 0 1 1
强制精确路径:
id d
1 1
2 1
零查询向量与任意数据向量的标量 cosine_distance 都是 1。HNSW 把两个零数据向量的 score 改成 0,从而优先选入 Top-K。
极小非零查询向量下,id=101 的 HNSW direct_d 为 -Inf,同一行嵌套计算的 scalar_d 为 0。HNSW 对普通同方向向量还可返回负的有限距离;标量路径会将 cosine similarity 截断到 [-1,1],不会产生负距离或 -Inf。
预期结果
HNSW 对外返回的 score 应保持 MO cosine_distance 的 SQL 语义。使用索引不应把零向量间距离从 1 改成 0,也不应为有限输入返回 -Inf 或负 cosine distance;索引路径不应因此改变 Top-K 行成员。
稳定性与覆盖
vecf32、vecf64均复现;- 两个 CN 均复现;
- 连续 3 次独立删库、建表、建索引,共 12/12 个类型/CN/构建组合稳定复现;
- IVFFLAT 的相同用例返回
1,与标量路径一致; - 非零查询向量查询零数据向量时 HNSW 返回
1;问题限定在零查询向量与零数据向量的组合; - 全量 100 行集合比较中,HNSW 与精确路径的
(id, distance):UNION为 102 行、INTERSECT为 98 行,差异正是两个零向量的 score; - 极小向量矩阵覆盖 28 个 algorithm/type/CN/query 组合:IVFFLAT 28/28 与标量一致;HNSW 在两个 CN 上各有 12 个边界组合不一致;
- HNSW
vecf32在1e-20自比较时返回-Inf,vecf64在1e-160、1e-150、1e-100自比较时返回-Inf;输入值本身均为有限数; EXPLAIN确认异常查询使用hnsw_search。
定位
MO 标量实现 pkg/vectorindex/metric/distance_func.go 明确在余弦分母为零时返回距离 1.0,并将 similarity 截断到 [-1,1]。HNSW 使用 usearch 的 Cosine score,pkg/vectorindex/metric/types.go::DistanceTransformHnsw 将该 score 原样暴露,因为一般情况下 usearch Cosine 与 MO 都是 1 - cosine_similarity;但 usearch 的零/极小范数边界与 MO 不同,且返回值没有在暴露给 SQL 前按 MO 语义处理。
这与 #29050 不同:#29050 是非零向量 L2 score 的数值精度差异,行成员不变;本问题是 cosine 零分母语义不同,直接改变 Top-K 行成员。
Activity
[-][Bug]: HNSW cosine zero-vector query returns 0 instead of distance 1 and changes Top-K rows[/-][+][Bug]: HNSW cosine search returns wrong or -Inf scores for zero and tiny vectors[/+]Ariznawlll commentedon Sep 18, 2026
补充确认异常 score 会进入关系运算,不只影响展示值。
固定版本与拓扑:
01d60e1c4ded1b0f3fc1a4ecd75ce54e95e23b90,2 CN / 1 TN / 1 LogService。对
vecf32使用查询向量[1e-20,1e-20,1e-20],对vecf64使用[1e-150,1e-150,1e-150],将 HNSW Top-K 放入派生表后继续消费d:两个 CN 上结果一致:
vecf32返回 3 行负距离,其中 2 行为-Inf;精确路径返回 0 行;vecf64返回 3 行-Inf;精确路径返回 0 行;MIN(d):HNSW 为-Inf,精确路径为0;SUM(d < 0):HNSW 为3,精确路径为0;LIMIT 3 OFFSET 1的行成员也发生变化;(id,d)等值连接,7 行中只有 4 行能够匹配。因此当前 score 会影响派生表外层过滤、聚合、分页和等值连接。有限向量输入产生的
-Inf/负 cosine distance 已经改变 SQL 结果集,不能只按 ANN 近似召回或显示精度解释。fix: keep HNSW cosine boundary scores on exact path (#29136)
Lundomn commentedon Sep 20, 2026
QA 转测:
a1220dec751400a3a65d8ebedcd7e9ff61847eba(PR head87acd4db11fc25594c0b39dcb5cda27acb839632)。cosine_distance语义不一致,曾产生0、负距离、-Inf/NaN并改变 Top-K 结果;同时补上 brute-force 非有限距离的 top-1/top-K 处理。vecf32/vecf64的零向量、极小向量、同向/反向/正交查询,覆盖LIMIT 1、K>1、派生表过滤、聚合、分页和等值连接,并对比 HNSW 与精确路径的行集合和距离。-Inf/NaN或虚构 row id;零向量语义应与标量cosine_distance一致。若发现失败,请附测试拓扑、commit、SQL、实际/期望结果。请 QA 在完成上述验收后明确回复通过/失败及测试环境;未通过前不要关闭 Issue。
test: anchor cosine boundary regressions to scale invariance (#29175)
Ariznawlll commentedon Sep 21, 2026
Verified on current main 57c280e.\n\nA synchronous vector_cosine_ops HNSW index was built on normal, zero, signed-zero, and tiny vectors. The scalar and forced HNSW-rank paths were run three times:\n- zero-vector scores are 1.0, not -Inf;\n- tiny-vector self cosine distance is 0.0;\n- the forced rank query returns finite, SQL-consistent scores.\n\nMOTR coverage was added in matrixorigin/motr#193:\nsuites/14_issue_regression/t/issue_29082_hnsw_cosine_degenerate.test.\nThe regression uses ALTER ... REINDEX ... FORCE_SYNC and mode=force, and passed 3/3 runs.\n\nClosing as fixed.
fix(vectorindex): restore native cosine ANN for HNSW; reject degenera…
fix(vectorindex): standardize vector distances on the float32 domain (#…
Ariznawlll commentedon Sep 24, 2026
测试版本
maind7d940e93bd037e9aacdc0f1256b91e32df84642原问题验证
根据后续合入的 PR #29100,#29082 的验收边界调整为:
-Inf/NaN或错误 Top-K;原复现中的零/极小 HNSW cosine 查询现在会在产生 score 前 fail-fast,不再把负距离、
-Inf/NaN传入外层过滤、聚合、分页或等值连接。研发对极端存储向量的已知限制和 CDC 取舍见 #29100 决策记录。Happy Path 覆盖
[1,0,0],EXPLAIN包含hnsw_search,确认走 HNSW 索引;Top-3 结果为(1,0)、(4,0.292893)、(5,0.42265),score 全部有限且排序正确。cosine_distance均为1.0,与新契约一致。Unhappy Path 覆盖
hnsw cosine search requires a normalized (non-zero, non-subnormal) query vector,没有返回部分行或伪造 score。[1e-20,1e-20,1e-20]返回相同类型的 fail-fast 错误,不再出现原问题的负距离、-Inf或NaN。白盒验证
本次以 SQL 黑盒验收为主;PR #29100 已包含
CosineVectorL2Norm、HNSW planner/search 和 float32 数值域的定向 UT。回归测试
main已包含定向 BVT:vector_hnsw_cosine_boundary.sql,覆盖归一化查询的索引路径与结果,以及零/次正规查询 fail-fast 和标量对照。测试证据
main;上述回归链接固定到本次验证 commit。测试结论
测试通过。