Skip to content

[Bug]: HNSW vector_ip_ops returns incorrect INNER_PRODUCT score #25965

Done

Description

@Ariznawlll

Summary

An HNSW index created with OP_TYPE 'vector_ip_ops' returns the correct nearest row but exposes an incorrect INNER_PRODUCT value. The HNSW plan returns a score that is exactly 1 greater than the scalar/full-scan result in the minimal case below.

The issue affects both VECF32 and VECF64.

Environment

  • Branch: main
  • Commit: 3046f14d94b3f1a25839eeeb65729b64f85ac647
  • Local standalone CN/TN/logservice cluster

Reproduction

SET experimental_hnsw_index = 1;

CREATE TABLE t (
  id BIGINT PRIMARY KEY,
  v VECF64(3)
);
INSERT INTO t VALUES
  (1, '[1,1,1]'),
  (2, '[2,2,2]'),
  (3, '[-3,-3,-3]'),
  (4, '[4,4,4]');

CREATE INDEX idx_v USING HNSW ON t(v)
  M=4 EF_CONSTRUCTION=32 EF_SEARCH=16
  OP_TYPE 'vector_ip_ops' MAX_INDEX_CAPACITY 1000000;

-- Scalar value.
SELECT id, INNER_PRODUCT(v, '[1,1,1]') AS d
FROM t
WHERE id = 4;

-- Secondary key prevents the HNSW rewrite.
SELECT id, INNER_PRODUCT(v, '[1,1,1]') AS d
FROM t
ORDER BY d, id
LIMIT 1;

-- Uses hnsw_search.
SELECT id, INNER_PRODUCT(v, '[1,1,1]') AS d
FROM t
ORDER BY d
LIMIT 1;

Actual result

Scalar WHERE id=4:     4    -12
Full table scan:       4    -12
HNSW index plan:       4    -11

The same difference reproduced 3/3 times with VECF32(3) and 3/3 times with VECF64(3).

Expected result

The HNSW rewrite must preserve the SQL value of INNER_PRODUCT. The indexed query should return 4, -12, matching direct scalar evaluation and the full table scan.

White-box evidence

  • The indexed plan replaces the ORDER BY expression with the score emitted by hnsw_search.
  • pkg/vectorindex/hnsw/search.go calls metric.DistanceTransformHnsw before returning that score.
  • DistanceTransformHnsw currently transforms only the L2sq-to-L2 case and otherwise returns the underlying USearch distance unchanged.

This is consistent with the observed fixed offset, but the issue is reported based on the SQL-level result mismatch rather than assuming a specific implementation fix.

Activity

  1. added this to the MO-202607 milestone on Jul 21, 2026
  2. added
    kind/bug
    on Jul 21, 2026
  3. added theissue type on Jul 21, 2026
  4. cpegeric commented on Aug 12, 2026

    @cpegeric
    Contributor

    The value is round off from usearch. We will not recompute the score again and use directly from HNSW usearch. So, nothing to fix here.

  5. cpegeric commented on Aug 19, 2026

    @cpegeric
    Contributor

    please verify

  6. slyunnn commented on Aug 20, 2026

    @slyunnn

    Regression and extended verification for #25965 (2026-08-20)

    Verification Environment

    MatrixOne origin/main: 621782d4634a36865bdb18c9fe3853bf70f43f1c — fix variables (#26762).

    Related fix: 99d03a52f78b9b42243c169e6d554fafd32f73c4 / PR #27044, fix: vector_l1_ops and vector_l2sq_ops indexes were unusable and hnsw inner product score transform (#27044).

    Topology: 1 Log + 1 TN + 2 CN + 1 Proxy.

    Endpoints: CN1 127.0.0.1:16001, CN2 127.0.0.1:16002, Proxy 127.0.0.1:6009.

    Client protocol: MOTR SQL runner using PyMySQL/MySQL text protocol.

    What Was Covered

    • OK — scalar INNER_PRODUCT result for the known row is -12.
    • OK — secondary-sort full-scan control returns id=4 and -12.
    • OK — HNSW vector_ip_ops top-k query returns id=4 and -12.
    • OK — Proxy MOTR record and replay passed with 11/11 statements.
    • OK — CN1 and CN2 replay passed with 11/11 statements each.
    • Not covered — VECF32 parity, larger vector dimensions, concurrent build/query stress, and other index algorithms.

    Findings

    The old HNSW score exposed USearch's 1 - a·b value, while SQL INNER_PRODUCT uses -a·b; this caused a fixed +1 error. The current implementation transforms the HNSW score correctly and preserves SQL semantics.

    Large-data regression is not required: the defect is a metric-value transformation error, independent of rows, block boundaries, memory, spill, concurrency, or resource scale.

    MOTR / BVT Coverage

    MOTR branch: test/issue-25965-hnsw-inner-product.

    MOTR commit: bc668a0 test: add MOTR regression for #25965.

    Covered files:

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

    The independent MOTR branch has been pushed. No issue-specific PR was created.

    Final Conclusion

    Fixed. HNSW vector_ip_ops now returns the exact SQL INNER_PRODUCT score on Proxy, CN1, and CN2.

  7. Ariznawlll commented on Nov 29, 2026

    @Ariznawlll
    CollaboratorAuthor

    结论

    测试通过。本轮“HNSW inner_product 分数与精确计算一致”已完成下列原始复现、相关扩展及对应 Linux CI;回归已提交至现有 MOTR PR,按授权关闭。

    关键证据

    版本与修复范围

    实际执行步骤与结果

    1. 在当前 main 验证 VECF32/VECF64,维度分别为 3、128、768、1536;生成元素全为 1、2、-3、4 的四个精确向量。
    2. 查询全 1 向量时最近 id=4,inner_product 分数为 -4×维度;查询全 -1 时 id=3,分数为 -3×维度;零向量各项分数为 0。
    3. 比较标量计算、无索引精确排序、HNSW 查询及第二 CN 查询的 id/score。实际计划包含 Vector Index Scan on idx_v [inner_product],没有把未走索引的成功当作 HNSW 通过。
    4. 3 维原始容量设置为 1,000,000;其余维度用小容量覆盖分数转换,不宣称大数据召回率或吞吐验收。
    5. MOTR 三轮通过;已有 TestDistanceTransformHnsw 三轮通过。最初 Go 测试遇到本机 libmo 动态库路径问题,增加绝对 rpath 后重跑通过,未修改产品代码。

    回归与 CI

    • 已追加现有 MOTR #374,用例:script/14_issue_regression/issue_25965_hnsw_score_parity.py。
    • Linux CI 46284168418 已完成 SUCCESS,下载 artifact 核对实际 MO SHA 为 e75a4d2ca470b35c2bf254cfcf5e3069f51be90a,不是仅依据分支名称。六组正式 MOTR × 3 轮,共 18/18(0 SKIP);plan、blockio、fulltext、vector metric 定向源码测试,以及 frontend native prepared SHOW 和 sqlintegration 角色规则测试均 -count=3 通过。
    • 每日调度须等相应 PR 合入才生效,本次是已执行的 PR CI,不声称已在默认分支每日运行。
    • 次数按脚本完整轮次统计,不把 MOTR 报告的一个 script statement 当成内部 SQL 条数。

    待解决点

    本 Issue 所列验收范围内无待解决的产品失败。MOTR PR 尚待合入;已获得“回归提交后可先关闭”的授权。本结论不外推到未涉及的性能、大数据或其他 Issue。

    最终结论:测试通过。上述原始与扩展场景、对应分支回归及 Linux CI 均通过;未通过项:无。回归已提交现有 MOTR PR,本次关闭 #25965。

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