Skip to content

[Bug]: JOIN consumer disables IVFFLAT and HNSW rewrite inside Top-K CTE #25974

Closed

Description

@Ariznawlll

Summary

Consuming a vector Top-K CTE with an INNER JOIN, LEFT JOIN, or RIGHT JOIN disables the IVFFLAT/HNSW index rewrite inside the CTE. The same Top-K CTE uses the vector index when selected directly.

Results remain correct because MatrixOne silently falls back to a full table scan, but the query loses the purpose of the vector index.

Environment

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

Reproduction

SET experimental_ivf_index = 1;

CREATE TABLE vec_t(
  id BIGINT PRIMARY KEY,
  owner_id INT,
  embedding VECF32(3)
);
INSERT INTO vec_t VALUES
  (1,1,'[1,1,1]'),(2,1,'[2,2,2]'),
  (3,2,'[3,3,3]'),(4,NULL,'[4,4,4]');
CREATE INDEX idx_vec USING IVFFLAT ON vec_t(embedding)
  LISTS=2 OP_TYPE 'vector_l2_ops';

CREATE TABLE owner_t(id INT PRIMARY KEY);
INSERT INTO owner_t VALUES (1),(2),(3);

-- Control: uses ivf_search.
EXPLAIN
WITH topv AS (
  SELECT id,owner_id
  FROM vec_t
  ORDER BY L2_DISTANCE(embedding,'[0,0,0]')
  LIMIT 3
)
SELECT * FROM topv;

-- The JOIN consumer disables the vector index inside topv.
EXPLAIN
WITH topv AS (
  SELECT id,owner_id
  FROM vec_t
  ORDER BY L2_DISTANCE(embedding,'[0,0,0]')
  LIMIT 3
)
SELECT topv.id,owner_t.id
FROM topv LEFT JOIN owner_t ON topv.owner_id=owner_t.id;

Replacing LEFT JOIN with INNER JOIN or expressing the same preserved-side operation as RIGHT JOIN produces the same regression. Replacing IVFFLAT with HNSW does as well.

Actual result

The control plan contains:

Table Function on ivf_search

The LEFT/INNER/RIGHT JOIN plans contain only:

Sort ... Limit: 3
  -> Table Scan on vec_t

No ivf_search remains. With HNSW, the control contains hnsw_search, while the JOIN consumer again leaves only a table scan.

The plan difference reproduced 3/3 times for all three JOIN types with IVFFLAT and 3/3 times for HNSW.

Expected result

The Top-K semantics are fully contained inside the CTE, so consuming its already-limited result with a relational JOIN must not prevent the inner IVFFLAT/HNSW rewrite.

White-box evidence

pkg/sql/plan/apply_indices.go dispatches vector index rewriting while processing a Project through applyIndicesForProject. buildVectorSortContext in pkg/sql/plan/apply_indices_vector.go recognizes a shallow Project/Sort/Scan shape, plus a special vector-provider JOIN shape. A normal relational JOIN consuming the CTE prevents that context from reaching the inner Top-K Project, leaving the underlying table scan unreplaced.

Impact

It is common to fetch nearest neighbors in a CTE and then join their IDs to metadata, permissions, owners, or dimension tables. This SQL shape silently turns indexed ANN retrieval into an exact full-table distance sort and can cause severe latency and resource regressions on large vector tables.

Related but distinct: #25967 covers an outer ORDER BY consumer; this issue requires no outer sort and is triggered by a JOIN consumer.

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. Ariznawlll commented on Jul 22, 2026

    @Ariznawlll
    CollaboratorAuthor

    补充测试场景与结果

    • 测试提交:c0486a2446c84c6dd6bf142494fe85243877ddc5
    • 运行环境:本地单节点 MatrixOne,8.0.30-MatrixOne-v1.3.0
    • 覆盖索引:IVFFLAT、HNSW
    • 覆盖消费者:INNER JOIN、LEFT JOIN、等价 RIGHT JOIN

    执行过程:

    1. 建立向量表、元数据表及对应向量索引。
    2. 对只读取 Top-K CTE 的查询执行 EXPLAIN 作为对照。
    3. 对同一 CTE 外接 INNER/LEFT/RIGHT JOIN 后再次执行 EXPLAIN。
    4. 另外使用参数化 prepared 语句覆盖向量目标、LIMIT ... BY RANK、CTE + LEFT JOIN,并跨越索引删除和重建重复执行。

    实际结果:

    • IVFFLAT 对照计划包含 Table Function on ivf_search;三种 JOIN 计划均退回 Table Scan on vec_t + Sort。
    • HNSW 对照计划包含 Table Function on hnsw_search;INNER/LEFT JOIN 计划同样退回 Table Scan on vec_t + Sort。
    • 参数化 LIMIT ... BY RANK 本身执行正确,并能在索引删除、重建后继续复用;CTE + JOIN 的结果也正确,但没有保留向量索引计划。
    • 上述计划差异重复 3/3。

    结论:补充的 prepared/BY RANK 场景也会在 CTE Top-K 接 JOIN 后失去索引执行路径;查询结果正确,但执行计划发生变化。

  5. added a commit that references this issue on Aug 15, 2026
  6. cpegeric commented on Aug 27, 2026

    @cpegeric
    Contributor

    please verift

  7. Ariznawlll commented on Aug 28, 2026

    @Ariznawlll
    CollaboratorAuthor

    最新 main 复测 3/3:HNSW Top-K CTE 接 INNER/LEFT/RIGHT JOIN 均保留 hnsw_search,结果与无索引对照一致;二维参数报 20301 后同 prepared 查询恢复正常。现有 vector Top-K consumer BVT 已覆盖 HNSW/IVFFLAT、JOIN 与外层消费者。无需 big-data、PITR/snapshot、stability 或 chaos。结论:关闭。

  8. Ariznawlll commented on Aug 28, 2026

    @Ariznawlll
    CollaboratorAuthor

    更正补充:验证基线为 MatrixOne main 28769ff;HNSW JOIN consumer 正反例连续三轮正确,现有 vector Top-K consumer BVT 覆盖 HNSW/IVFFLAT。

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