Skip to content

[Bug]: Outer ORDER BY on vector Top-K CTE disables IVFFLAT and HNSW rewrite #25967

Closed

Description

@Ariznawlll

Summary

An outer ORDER BY on the distance column produced by a vector Top-K CTE disables the IVFFLAT/HNSW index rewrite. The logically equivalent query without the redundant outer sort uses the vector index normally.

The same regression occurs with a derived table, so this appears to be an optimizer plan-shape issue rather than a CTE materialization requirement.

Environment

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

Reproduction

SET experimental_ivf_index = 1;
SET experimental_hnsw_index = 1;

CREATE TABLE t_ivf(id BIGINT PRIMARY KEY, v VECF32(3));
INSERT INTO t_ivf VALUES
  (1,'[1,1,1]'),(2,'[2,2,2]'),(3,'[3,3,3]'),(4,'[4,4,4]');
CREATE INDEX idx_ivf USING IVFFLAT ON t_ivf(v)
  LISTS=2 OP_TYPE 'vector_l2_ops';

-- Uses ivf_search.
EXPLAIN
WITH topv AS (
  SELECT id,L2_DISTANCE(v,'[0,0,0]') d
  FROM t_ivf ORDER BY d LIMIT 2
)
SELECT id,d FROM topv;

-- Adding the equivalent outer ordering disables the vector index.
EXPLAIN
WITH topv AS (
  SELECT id,L2_DISTANCE(v,'[0,0,0]') d
  FROM t_ivf ORDER BY d LIMIT 2
)
SELECT id,d FROM topv ORDER BY d;

The same behavior can be reproduced after replacing IVFFLAT with an HNSW index and enabling experimental_hnsw_index.

Actual result

Without the outer ORDER BY, the plans contain:

Table Function on ivf_search

or:

Table Function on hnsw_search

After adding ORDER BY d to the outer query, both plans contain a pair of sort nodes followed by only:

Table Scan on t_ivf

No vector search table function remains. The plan difference reproduced 3/3 times for IVFFLAT and 3/3 times for HNSW.

FULLTEXT retains fulltext_index_scan under the analogous CTE plus outer-order shape, which further narrows the problem to the vector Top-K rewrite.

Expected result

The outer ordering should not prevent the inner Top-K from using its IVFFLAT/HNSW index. The outer sort may remain if the optimizer cannot prove it redundant, but the inner scan should still be rewritten to the vector search table function.

Impact

Applications commonly wrap Top-K retrieval in a CTE or derived table and order the final result for deterministic presentation. This harmless SQL shape silently changes indexed vector retrieval into a full table scan.

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. added a commit that references this issue on Aug 15, 2026
  5. cpegeric commented on Aug 27, 2026

    @cpegeric
    Contributor

    please verify

  6. Ariznawlll commented on Aug 28, 2026

    @Ariznawlll
    CollaboratorAuthor

    最新 main 复测:HNSW Top-K 外层 ORDER BY、参数异常后恢复与结果对照连续 3 轮正确;现有 / 已覆盖完整消费者计划与结果矩阵。IVFFLAT 消费者真实执行受本地 MOTR 集群缺少 30400 后端限制,未将该环境错误归因产品。无需 big-data、PITR/snapshot、stability 或 chaos。结论:关闭。

  7. Ariznawlll commented on Aug 28, 2026

    @Ariznawlll
    CollaboratorAuthor

    更正补充:验证基线为 MatrixOne main 28769ff。现有 test/distributed/cases/vector/vector_hnsw_topk_consumers.sql 和 vector_ivf_topk_consumers.sql 覆盖 Top-K consumer 计划与结果矩阵。

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