Skip to content

IVFFLAT 'mode=pre' drops extra distance predicates (residual filter not applied) → wrong results, order-dependent #25639

Closed

Description

@fengttt

Summary

An IVFFLAT vector search with ... by rank with option 'mode=pre' and more than one distance predicate on the indexed column can return incorrect rows: the index scan uses one distance predicate as its search bound and drops the others instead of keeping them as residual filters. Which predicate is kept vs. dropped depends on the order of the filters in the plan's FilterList, so the result is sensitive to filter ordering — a plan detail that should never affect correctness.

This was surfaced by PR #25616 (a sort.Slice → slices refactor that reordered equal-cost filters); the query planning change was reverted there, but the underlying index behavior is a latent bug and is the real defect.

Reproduce

CREATE TABLE mini_vector_data (id VARCHAR(64) NOT NULL, vec vecf32(8), PRIMARY KEY (id));
CREATE INDEX idx_vec USING ivfflat ON mini_vector_data (vec) lists = 16 op_type 'vector_l2_ops';
INSERT INTO mini_vector_data VALUES
('id1','[0.12,-0.33,0.51,0.27,-0.48,0.19,0.72,-0.11]'),
('id2','[0.44,0.09,-0.31,0.62,0.18,-0.55,0.21,0.33]'),
('id3','[-0.21,0.73,-0.12,0.51,-0.44,0.09,0.14,0.82]'),
('id4','[0.51,0.25,0.09,-0.33,0.74,-0.28,0.41,0.17]'),
('id5','[0.08,-0.41,0.66,0.12,0.53,-0.15,0.38,-0.22]'),
('id6','[0.32,0.71,-0.26,0.14,-0.03,0.66,-0.41,0.19]'),
('id7','[-0.55,0.12,0.44,-0.19,0.27,0.51,0.11,-0.03]'),
('id8','[0.23,-0.14,0.82,0.39,-0.52,0.08,0.26,0.73]'),
('id9','[0.61,0.33,-0.48,0.09,0.12,0.54,-0.29,0.41]'),
('id10','[0.19,-0.22,0.33,0.77,-0.04,0.12,0.58,-0.31]');

SELECT id FROM mini_vector_data
WHERE l2_distance(vec, '[0.1,-0.2,0.3,0.4,-0.1,0.2,0.0,0.5]') < 1.1
  AND l2_distance(vec, '[0.1,-0.2,0.3,0.4,-0.1,0.2,0.0,0.5]') < 1.2
ORDER BY l2_distance(vec, '[0.1,-0.2,0.3,0.4,-0.1,0.2,0.0,0.5]')
LIMIT 10 by rank with option 'mode=pre';

l2 < 1.1 AND l2 < 1.2 is logically just l2 < 1.1. True distances to the query vector (brute force):

id l2_distance < 1.1?
id8 0.776 ✅
id1 1.055 ✅
id10 1.072 ✅
id2 1.155 ❌
id3 1.183 ❌
id7 1.191 ❌
id5 1.198 ❌

Correct result: id8, id1, id10 (3 rows).

When the plan happens to place < 1.2 ahead of < 1.1 in the scan's FilterList, the IVF mode=pre scan probes with < 1.2 and drops the < 1.1 residual, returning 7 rows (id8, id1, id10, id2, id3, id7, id5) — rows with distance up to 1.198 that violate < 1.1.

Root cause (suspected)

For mode=pre, the IVF index scan appears to select a single distance predicate as the search radius / probe bound and does not re-apply the remaining distance predicate(s) as residual filters. The choice of "which predicate becomes the bound" is driven by FilterList order (filter-cost sort in pkg/sql/plan/stats.go), so a change in that order changes which predicate is enforced.

The existing test test/distributed/cases/vector/vector_ivf_mode.sql already encodes the intended invariant in a comment:

-- second upper bound must remain as a residual filter instead of replacing it.

so this contract is known; the implementation just doesn't hold it independent of filter order.

Expected

All distance predicates on the indexed column must be enforced for mode=pre (as index bound + residual filters), so the result is correct and independent of FilterList ordering. mode=post / mode=force should be checked for the same issue.

Impact

Silent wrong results (extra rows past the intended distance threshold) for mode=pre vector queries that carry more than one distance predicate on the indexed column. No error is raised.

Activity

  1. added
    kind/bug
    severity/s0
    on Jul 12, 2026
  2. added theissue type on Jul 12, 2026
  3. added this to the MO-202607 milestone on Jul 12, 2026
  4. fengttt commented on Jul 12, 2026

    @fengttt
    ContributorAuthor

    Related planner-side issue: #25646 — the filter-cost sort in stats.go produces the ill-defined FilterList order that this bug is sensitive to (non-strict cost1 <= cost2 comparator). The two are complementary: this issue is the IVF mode=pre scan dropping residual distance predicates (the order should not matter); #25646 is the planner emitting an undefined tie order in the first place. Resolving this one unblocks making the filter order strict/deterministic in #25646.

  5. added a commit that references this issue on Jul 12, 2026
    edd3c9f
  6. fengttt commented on Jul 12, 2026

    @fengttt
    ContributorAuthor

    Fixed in PR #25616 (commit edd3c9f): getDistRangeFromFilters now folds all same-side distance predicates into the tightest bound, so the IVF index enforces the intersection regardless of filter order. Verified: l2 < 1.1 AND l2 < 1.2 returns the correct 3 rows in both filter orders. Added a unit test (TestGetDistRangeFromFiltersKeepsTightestBound) covering both orders; vector/vector_ivf_mode BVT passes. Will auto-close on merge.

  7. assigned and unassigned on Jul 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

kind/bugseverity/s0

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions