You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[Bug]: Outer ORDER BY on vector Top-K CTE disables IVFFLAT and HNSW rewrite #25967
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;
CREATETABLEt_ivf(id BIGINTPRIMARY 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]');
CREATEINDEXidx_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 LIMIT2
)
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 LIMIT2
)
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
perf(plan/ivfflat): allow index-only scan when a consumer sits above …
test(vector): cover a VIEW as the Top-K consumer boundary (matrixorig…
Vector join bug (#27057)
cpegeric commentedon Aug 27, 2026
please verify
Ariznawlll commentedon Aug 28, 2026
最新 main 复测:HNSW Top-K 外层 ORDER BY、参数异常后恢复与结果对照连续 3 轮正确;现有 / 已覆盖完整消费者计划与结果矩阵。IVFFLAT 消费者真实执行受本地 MOTR 集群缺少 30400 后端限制,未将该环境错误归因产品。无需 big-data、PITR/snapshot、stability 或 chaos。结论:关闭。
Ariznawlll commentedon Aug 28, 2026
更正补充:验证基线为 MatrixOne main 28769ff。现有 test/distributed/cases/vector/vector_hnsw_topk_consumers.sql 和 vector_ivf_topk_consumers.sql 覆盖 Top-K consumer 计划与结果矩阵。
Vector join bug (matrixorigin#27057)
Vector join bug (matrixorigin#27057)
Vector join bug (matrixorigin#27057)