Skip to content

[Compatibility]: inner_product() 返回负内积(与 Milvus 的 IP 语义相反) #30475

Open

Description

@daviszhen

环境:matrixorigin/matrixone:latest(arm64/linux),VERSION() = 8.0.30-MatrixOne-v4.2.5;对照 Milvus v2.6.14。

现象:inner_product() 返回的是负的内积

SELECT inner_product('[1,2,3]', '[1,2,4]');
-- MatrixOne 返回: -17
-- 真实内积 1*1 + 2*2 + 3*4 = 17

同一组向量上其余三个函数数值均正确(与 numpy 逐位一致):

SELECT l2_distance('[1,2,3]','[1,2,4]');         -- 1            (正确)
SELECT cosine_similarity('[1,2,3]','[1,2,4]');   -- 0.9914601445 (正确)
SELECT cosine_distance('[1,2,3]','[1,2,4]');     -- 0.0085398657 (正确)
SELECT inner_product('[1,2,3]','[1,2,4]');       -- -17          (真实值 17)

因此这是 inner_product 单点的符号问题,不是整体度量实现问题。

影响

排序本身不受影响(升序负内积 = 降序内积),实测 vector_ip_ops 索引 + ORDER BY inner_product(...) 的召回与 numpy 基准一致(force 1.000 / post 1.000)。真正的影响在"读值"的场景:

  1. 与 Milvus 语义相反:Milvus 的 IP 度量返回原始内积(越大越相似)。从 Milvus 迁移的应用如果直接读回该值做阈值判断(if score > 0.8)、打分展示或混合排序权重,会得到反号的值,且不会有任何报错。
  2. 与直觉相反:函数名为 inner_product,使用者预期拿到内积本身。
  3. 容易被漏掉:因为排序结果是正确的,只有在"用这个数值"时才会暴露。

建议

明确二者之一并在文档中写清:

  • 方案 A:inner_product() 返回原始内积(与 Milvus 及函数名一致),另提供 negative_inner_product() 或文档说明用 ORDER BY inner_product(...) DESC;
  • 方案 B:保持当前"距离语义"(返回负内积),但在文档中显著标注该函数返回负值、排序需升序,并说明与 Milvus 的差异,便于迁移方改造读值逻辑。

若为兼容 Milvus 而设计,建议优先方案 A。

关联

Activity

  1. added
    priority/p2
    kind/compatibility
    on Oct 17, 2026
  2. XuPeng-SH commented on Oct 31, 2026

    @XuPeng-SH
    Contributor

    合并范围核对:#31896 已合入 main(5a67e453a7dd85dc1101ea87f9b9c01238c33abb),新增 docs/sql/vector-distance-functions.md,明确 inner_product = -dot、升序距离排序及量化/精度边界;没有把生产函数改为正内积。

    本 issue 暂保留研发:合并文档覆盖了建议方案 B 的符号和排序部分,但尚未显式说明与 Milvus 正内积读值的差异、迁移时阈值/展示/混合打分需要转换;因此不把相关文档合并当作本项完整修复或验收。后续应明确剩余迁移说明范围,或由维护者明确缩小本项范围,再交 QA。这里只做合并内容与 issue 要求的远端核对,本轮没有运行 SQL/GPU/Milvus 对照测试。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area/compatibilitykind/compatibilitypriority/p2

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions