Skip to content

[Bug]: IVFFLAT applies unrecognized distance-range predicates after truncated Top-K #29077

Closed

Description

@Ariznawlll

Is there an existing issue for the same bug?

  • I have checked the existing issues.

Branch Name

main

Commit ID

01d60e1

Other Environment Information

  • Local deployment with 1 TN and 2 CNs (SQL ports 16201 and 16202)
  • Every repetition used a new database name; the result is independent of stale database/index metadata
  • Each IVFFLAT index used lists=1, so the result is not caused by approximate list recall

Actual Behavior

When the distance predicate is written with SQL BETWEEN, the optimizer still chooses Vector Index Scan but does not convert the predicate into the vector scan's DistRange. The index produces only the nearest LIMIT candidates first, then the remaining base-table filter rejects those candidates. Rows that satisfy the requested distance interval are never considered.

For example, the exact path returns IDs 490 through 501, while every indexed mode returns an empty set:

-- 12 rows: 490 ... 501
select id from t
where l2_distance(v, '[0,0]') between 489.5 and 501.5
order by l2_distance(v, '[0,0]')
limit 20 by rank with option 'mode=force';

-- 0 rows, although EXPLAIN contains Vector Index Scan
select id from t
where l2_distance(v, '[0,0]') between 489.5 and 501.5
order by l2_distance(v, '[0,0]')
limit 20 by rank with option 'mode=pre';

The equivalent predicate written as two comparisons is correct:

where l2_distance(v, '[0,0]') >= 489.5
  and l2_distance(v, '[0,0]') <= 501.5

The same problem reproduces for all five non-quantized IVFFLAT metric families:

  • l2_distance / vector_l2_ops
  • l2_distance_sq / vector_l2sq_ops
  • l1_distance / vector_l1_ops
  • cosine_distance / vector_cosine_ops
  • inner_product / vector_ip_ops

It also reproduces through server-side PREPARE / EXECUTE with parameterized BETWEEN ? AND ? bounds.

Coverage result: 3 fresh database generations x 5 metrics x 2 CNs x (pre, post, auto, include, prepared pre) = 150/150 indexed BETWEEN queries returned the wrong empty result. The equivalent >= AND <= control matched the exact scan in all 120 mode/CN/metric repetitions.

Expected Behavior

An indexed distance query using BETWEEN must return the rows in the requested interval, matching the equivalent pair of comparison predicates. The optimizer should either convert BETWEEN into DistRange before applying the vector Top-K limit, or avoid the vector rewrite when it cannot safely push the predicate.

Steps to Reproduce

set experimental_ivf_index = 1;
drop database if exists ivf_between_repro;
create database ivf_between_repro;
use ivf_between_repro;

create table t(id int primary key, v vecf32(2));
insert into t
select result, cast(concat('[', result, ',1]') as vecf32(2))
from generate_series(1, 1000) g;

create index ix using ivfflat on t(v)
lists = 1 op_type 'vector_l2_ops';
alter table t alter reindex ix ivfflat force_sync;

select id from t
where l2_distance(v, '[0,0]') between 489.5 and 501.5
order by l2_distance(v, '[0,0]')
limit 20 by rank with option 'mode=force';

select id from t
where l2_distance(v, '[0,0]') between 489.5 and 501.5
order by l2_distance(v, '[0,0]')
limit 20 by rank with option 'mode=pre';

explain select id from t
where l2_distance(v, '[0,0]') between 489.5 and 501.5
order by l2_distance(v, '[0,0]')
limit 20 by rank with option 'mode=pre';

Additional information

getDistRangeFromFilters in pkg/sql/plan/apply_indices_vector.go only recognizes binary <, <=, >, and >= expressions whose left argument is the distance function. A BETWEEN expression remains in the residual filter list, but the IVFFLAT rewrite still proceeds with a bounded candidate limit.

This is distinct from closed issue #29037. That report was closed because its original result depended on reusing stale hidden-index metadata. This report uses fresh database names for every repetition and isolates the missing BETWEEN range conversion.

Regression placement: planner UT for BETWEEN range extraction plus a small deterministic MOTR case covering literal and prepared bounds. Big-data and chaos coverage are not required for this planner correctness defect.

Activity

  1. added this to the MO-202609 milestone on Sep 18, 2026
  2. added
    kind/bug
    on Sep 18, 2026
  3. added theissue type on Sep 18, 2026
  4. changed the title [-][Bug]: IVFFLAT distance BETWEEN is applied after truncated Top-K and returns empty results[/-] [+][Bug]: IVFFLAT applies unrecognized distance-range predicates after truncated Top-K[/+] on Sep 18, 2026
  5. Ariznawlll commented on Sep 18, 2026

    @Ariznawlll
    CollaboratorAuthor

    Further scope isolation shows that this is a general SQL-equivalent distance-range recognition gap, not only a BETWEEN AST case.

    Using the same fresh-name, lists=1 dataset, the following predicates are all logically equivalent to d >= 489.5 AND d <= 501.5, where d = l2_distance(v, '[0,0]'):

    489.5 <= d AND d <= 501.5
    489.5 <= d AND 501.5 >= d
    NOT (d < 489.5 OR d > 501.5)
    NOT (d < 489.5) AND d <= 501.5

    The exact path returns IDs 490 through 501. With mode=pre, each form above uses Vector Index Scan but returns an empty set. The canonical form d >= 489.5 AND d <= 501.5 remains correct. The partially reversed control d >= 489.5 AND 501.5 >= d is also correct because the recognized lower bound prevents the premature nearest-candidate truncation from discarding the interval.

    NOT BETWEEN has the same unsafe residual-filter behavior. For:

    d NOT BETWEEN -1 AND 501.5

    the exact path returns IDs 502 through 521 for LIMIT 20, while the indexed path returns empty.

    This matrix reproduced identically in three fresh database generations on both CNs:

    • canonical and reversed-upper controls: 12/12 correct;
    • reversed lower/both, negated interval, negated lower, and NOT BETWEEN: 30/30 returned the wrong empty set.

    The common condition is that getDistRangeFromFilters does not recognize the semantically equivalent expression shape, yet the vector rewrite still applies its candidate limit before evaluating the residual distance predicate. A fix should therefore normalize supported equivalent forms or reject the vector rewrite whenever a residual predicate still depends on the ordered distance expression.

  6. added
    ai-easy
    on Oct 10, 2026
  7. Ariznawlll commented on Oct 12, 2026

    @Ariznawlll
    CollaboratorAuthor

    测试版本

    • 分支:main;commit:02a2823c2007d5e4fb4ada148e93743df337551a;日期:2026-10-12。
    • 部署:本地独立单 CN / 单 TN / 单 LogService;IVFFLAT lists=1,同步 REINDEX 完成后查询。

    原问题验证

    按 issue 的 1,000 行 L2 向量表和 BETWEEN 489.5 AND 501.5 查询,FORCE、PRE、POST、AUTO、INCLUDE 各 3 轮均返回 ID 490..501,不再出现 indexed PRE 返回空集。

    Happy Path 覆盖

    继续测同义形式:正向双比较、下界反写、上下界都反写、上界反写、NOT(d < lower OR d > upper)、NOT(d < lower) AND d <= upper;五模式各与 FORCE 的 12 行结果一致。另用 L2SQ、L1、inner-product、cosine 四类索引,分别选择不包含最近候选的有效距离区间,PRE/POST/AUTO 与各自 FORCE 非空结果一致。

    Unhappy Path 覆盖

    NOT BETWEEN -1 AND 501.5 的 PRE 返回 ID 502..521;完全不命中的区间返回空。二者分别排除残余过滤导致的假阴性和错误扩大结果集。每次 MOTR compare 运行独立建库、建索引,避免旧索引元数据影响。

    白盒验证

    使用单列表 IVFFLAT 与 lists=1 排除多列表近似召回的解释;对照 FORCE 检查的是过滤与 Top-K 顺序关系。未覆盖服务端 PREPARE 参数化区间及双 CN 的每个组合。

    回归测试

    已加入 MOTR PR #229 的五种 metric、同义谓词和 NOT BETWEEN 回归,compare 3/3。1,000 行已能稳定触发原错误,无需 big-data。

    测试证据

    测试结论

    测试通过。

  8. Ariznawlll commented on Oct 12, 2026

    @Ariznawlll
    CollaboratorAuthor

    回归归档:本 issue 在 MatrixOne main 02a2823c2007d5e4fb4ada148e93743df337551a 上的原问题、正常与边界/异常路径已有上方详细验证记录,MOTR 对应用例本地 compare 3/3 通过。回归 PR https://github.com/matrixorigin/motr/pull/229 已合入(merge commit 0ed4301b164aa42d0affa0dc27e17f7435b7bf04)。据此关闭本 issue;本次关闭未增加先前评论未覆盖的验收范围。

    测试通过。

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

Metadata

Metadata

Assignees

Labels

ai-easykind/bug

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions