Repository navigation
Description
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
Ariznawlll commentedon Jul 22, 2026
补充测试场景与结果
c0486a2446c84c6dd6bf142494fe85243877ddc58.0.30-MatrixOne-v1.3.0执行过程:
EXPLAIN作为对照。EXPLAIN。LIMIT ... BY RANK、CTE + LEFT JOIN,并跨越索引删除和重建重复执行。实际结果:
Table Function on ivf_search;三种 JOIN 计划均退回Table Scan on vec_t+Sort。Table Function on hnsw_search;INNER/LEFT JOIN 计划同样退回Table Scan on vec_t+Sort。LIMIT ... BY RANK本身执行正确,并能在索引删除、重建后继续复用;CTE + JOIN 的结果也正确,但没有保留向量索引计划。结论:补充的 prepared/
BY RANK场景也会在 CTE Top-K 接 JOIN 后失去索引执行路径;查询结果正确,但执行计划发生变化。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 verift
Ariznawlll commentedon Aug 28, 2026
最新 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。结论:关闭。
Ariznawlll commentedon Aug 28, 2026
更正补充:验证基线为 MatrixOne main 28769ff;HNSW JOIN consumer 正反例连续三轮正确,现有 vector Top-K consumer BVT 覆盖 HNSW/IVFFLAT。
Vector join bug (matrixorigin#27057)
Vector join bug (matrixorigin#27057)
Vector join bug (matrixorigin#27057)