Skip to content

fix: restore IVFFlat storage Top-K - #27494

Merged
mergify[bot] merged 6 commits into
matrixorigin:mainfrom
aunjgr:fix/ivfflat-storage-topk
Aug 23, 2026
Merged

mergify[bot] merged 6 commits into
matrixorigin:mainfrom
aunjgr:fix/ivfflat-storage-topk

Conversation

@aunjgr

@aunjgr aunjgr commented Aug 23, 2026

Copy link
Copy Markdown
Contributor

What type of PR is this?

  • API-change
  • BUG
  • Improvement
  • Documentation
  • Feature
  • Test and CI
  • Code Refactoring

Which issue(s) this PR fixes:

Fixes #27474

What this PR does / why we need it:

The optimizer-visible IVFFlat reader introduced by #27297 always used post-filter Top-K. Even an unfiltered search therefore read every entry vector selected by nprobe, calculated every distance in CN, and compacted only afterward. On the reported Wiki 10M workload this regressed the first l2_only case from about 20.25 QPS to 0.48 QPS.

This change restores storage-level vector Top-K only when the complete candidate set is known before ranking:

  • encode (index version, centroid ID) with the canonical composite-key serializer and use an exact prefix_in(__mo_cpkey_col, ...) predicate for range and block pruning;
  • pass a typed distance expression, query-vector literal, direction, and candidate limit to the storage reader;
  • consume the distance vector returned by storage instead of recomputing it in CN; and
  • retain filter-then-local-Top-K for membership filters, pushed predicates, bounded distance ranges, or missing centroid restrictions.

The implementation also propagates descending order to the local compactor. It does not change SQL planning, protobufs, index registration, on-disk data, or public APIs.

Validation:

  • the new fast-path regression fails on the unfixed revision because PostFilterTopOnly is unconditional, then passes with this change;
  • focused fast-path, fallback, direction, direct-relation, and reader-initialization tests pass;
  • full pkg/vectorindex/ivfflat and pkg/vm/engine/readutil suites pass;
  • affected packages pass go build and go vet with the repository CGo dependency contract; and
  • git diff --check passes.

The production Wiki 10M throughput confirmation remains an external nightly benchmark after this PR is deployed to that environment.

@qodo-code-review

Copy link
Copy Markdown

Qodo reviews are paused for this user.

Troubleshooting steps vary by plan Learn more →

On a Teams plan?
Reviews resume once this user has a paid seat and their Git account is linked in Qodo.
Link Git account →

Using GitHub Enterprise Server, GitLab Self-Managed, or Bitbucket Data Center?
These require an Enterprise plan - Contact us
Contact us →

@XuPeng-SH XuPeng-SH left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I reviewed exact head ed9a44d0bd6c65d738ad316adb5b7d9bb5fbba5e and found one blocking correctness issue.

canUseStorageTopK accepts DESC, and scanEntries forwards that flag to storage. However, objectio.TopNVector never consults IndexReaderTopOp.Desc: it always keeps the smallest distances (dist64 < DistHeap[0]). By the time compactRelationTop(..., desc=true) runs, storage has already discarded the farthest rows, so the local DESC sort cannot recover them.

Minimal exact-head witness: two vectors [1] and [10], query [0], L2sq, Limit=1, Desc=true. canUseStorageTopK(...) returns true, but TopNVector returns row 0 / distance 1 instead of row 1 / distance 100.

The SQL optimizer currently avoids ANN rewrite for DESC, but this PR explicitly adds the direction to the runtime fast-path contract and tests a DESC request there. Please either make DESC use the existing post-filter/local Top-K fallback (the small safe fix), or implement descending storage Top-K end to end, and add a test that exercises the actual storage Top-N selection rather than only checking flag propagation/local compaction.

@mergify

mergify Bot commented Aug 23, 2026 •

Copy link
Copy Markdown
Contributor

Merge Queue Status

  • ✅ Entered queue — 2026-08-23 19:22 UTC · Rule: main · triggered by @aunjgr with the merge queue checkbox
  • ✅ Checks skipped · PR is already up-to-date
  • ✅ Merged — 2026-08-23 19:23 UTC · at e7d2f282c349b488348db982ead53f8fe41cae39 · squash

This pull request spent 34 seconds in the queue, including 2 seconds running CI.

Required conditions to merge
  • #review-threads-unresolved = 0 [🛡 GitHub branch protection]
  • github-review-approved [🛡 GitHub branch protection]
  • any of [🛡 GitHub branch protection]:
    • check-success = Matrixone CI / UT Test on Ubuntu/x86
    • check-neutral = Matrixone CI / UT Test on Ubuntu/x86
    • check-skipped = Matrixone CI / UT Test on Ubuntu/x86
  • any of [🛡 GitHub branch protection]:
    • check-success = Matrixone CI / SCA Test on Linux/arm64
    • check-neutral = Matrixone CI / SCA Test on Linux/arm64
    • check-skipped = Matrixone CI / SCA Test on Linux/arm64
  • any of [🛡 GitHub branch protection]:
    • check-success = Matrixone Compose CI / multi cn e2e bvt test docker compose(PROXY)
    • check-neutral = Matrixone Compose CI / multi cn e2e bvt test docker compose(PROXY)
    • check-skipped = Matrixone Compose CI / multi cn e2e bvt test docker compose(PROXY)
  • any of [🛡 GitHub branch protection]:
    • check-success = Matrixone Utils CI / Coverage
    • check-neutral = Matrixone Utils CI / Coverage
    • check-skipped = Matrixone Utils CI / Coverage
  • any of [🛡 GitHub branch protection]:
    • check-success = Matrixone UT Coverage / UT Coverage on Ubuntu/x86
    • check-neutral = Matrixone UT Coverage / UT Coverage on Ubuntu/x86
    • check-skipped = Matrixone UT Coverage / UT Coverage on Ubuntu/x86
  • any of [🛡 GitHub branch protection]:
    • check-success = Matrixone Standlone CI / multi CN e2e BVT Test on Linux/x64(COMPOSE, PESSIMISTIC)
    • check-neutral = Matrixone Standlone CI / multi CN e2e BVT Test on Linux/x64(COMPOSE, PESSIMISTIC)
    • check-skipped = Matrixone Standlone CI / multi CN e2e BVT Test on Linux/x64(COMPOSE, PESSIMISTIC)

@mergify mergify Bot added the queued label Aug 23, 2026
@mergify
mergify Bot merged commit a4465b3 into matrixorigin:main Aug 23, 2026
52 of 54 checks passed
@aunjgr aunjgr mentioned this pull request Aug 25, 2026
2 of 7 tasks
XuPeng-SH pushed a commit that referenced this pull request Aug 25, 2026
## What type of PR is this?

- [ ] API-change
- [x] BUG
- [x] Improvement
- [ ] Documentation
- [ ] Feature
- [ ] Test and CI
- [ ] Code Refactoring

## Which issue(s) this PR fixes:

issue #27474

Related: #26241

## What this PR does / why we need it:

#27494 restored storage Top-K for unfiltered IVFFlat queries, but large
filtered mode=pre membership sets still take the exhaustive path. The
10M Wiki follow-up ran l2_filter + mode=pre at only about 0.37-0.39 QPS.

This change restores the former hybrid PRE policy in the new
VECTOR_INDEX_SCAN architecture:

- small membership sets (at most the existing exactPkFilterThreshold of
100 keys) retain the exact all-centroid, filter-first path;
- larger membership sets search only ranked nprobe centroids, use the
existing tapered candidate budget, run physical storage Top-K before
membership filtering, and pass the membership docfilter through
RelationScanRequest.FilterHint;
- the exact outer SEMI join remains the final SQL predicate verifier.

Filtered pre-mode is intentionally approximate only for larger
membership sets: a matching row may be excluded by the bounded candidate
stage, but a row violating the SQL filter cannot escape the final SEMI
join. Residual typed predicates, distance ranges, descending search,
empty membership, and malformed membership retain their existing safe
paths.

No protobuf, public API, threshold, or tuning option is added.

Validation:

- focused small-exact/large-bounded planner and IVFFlat regressions
- full tests: pkg/sql/plan, pkg/sql/colexec/vectorscan,
pkg/vectorindex/ivfflat, pkg/vectorindex/overfetch,
pkg/vm/engine/readutil
- build and vet for the same package set
- repository-pinned golangci-lint and molint on changed packages
- complete pre-push self-review: no blockers

CI follow-up:

- the first head applied the approximate path to every membership set
and caused existing selective/retry BVTs to under-fill;
- the correction restores the legacy exact path for sets of 100 keys or
fewer while keeping the large-set performance path bounded;
- newest main was merged before the update.

The 10M TKE acceptance check is: both 10,000-query rounds complete, and
filtered-pre QPS is at least 50% of the same-run unfiltered-pre control.

---------

Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
VioletQwQ-0 pushed a commit to VioletQwQ-0/matrixone that referenced this pull request Sep 17, 2026
The optimizer-visible IVFFlat reader introduced by matrixorigin#27297 always used post-filter Top-K. Even an unfiltered search therefore read every entry vector selected by `nprobe`, calculated every distance in CN, and compacted only afterward. On the reported Wiki 10M workload this regressed the first `l2_only` case from about 20.25 QPS to 0.48 QPS.

This change restores storage-level vector Top-K only when the complete candidate set is known before ranking:

- encode `(index version, centroid ID)` with the canonical composite-key serializer and use an exact `prefix_in(__mo_cpkey_col, ...)` predicate for range and block pruning;
- pass a typed distance expression, query-vector literal, direction, and candidate limit to the storage reader;
- consume the distance vector returned by storage instead of recomputing it in CN; and
- retain filter-then-local-Top-K for membership filters, pushed predicates, bounded distance ranges, or missing centroid restrictions.

The implementation also propagates descending order to the local compactor. It does not change SQL planning, protobufs, index registration, on-disk data, or public APIs.

Validation:

- the new fast-path regression fails on the unfixed revision because `PostFilterTopOnly` is unconditional, then passes with this change;
- focused fast-path, fallback, direction, direct-relation, and reader-initialization tests pass;
- full `pkg/vectorindex/ivfflat` and `pkg/vm/engine/readutil` suites pass;
- affected packages pass `go build` and `go vet` with the repository CGo dependency contract; and
- `git diff --check` passes.

The production Wiki 10M throughput confirmation remains an external nightly benchmark after this PR is deployed to that environment.

Approved by: @XuPeng-SH
VioletQwQ-0 pushed a commit to VioletQwQ-0/matrixone that referenced this pull request Sep 17, 2026
## What type of PR is this?

- [ ] API-change
- [x] BUG
- [x] Improvement
- [ ] Documentation
- [ ] Feature
- [ ] Test and CI
- [ ] Code Refactoring

## Which issue(s) this PR fixes:

issue matrixorigin#27474

Related: matrixorigin#26241

## What this PR does / why we need it:

matrixorigin#27494 restored storage Top-K for unfiltered IVFFlat queries, but large
filtered mode=pre membership sets still take the exhaustive path. The
10M Wiki follow-up ran l2_filter + mode=pre at only about 0.37-0.39 QPS.

This change restores the former hybrid PRE policy in the new
VECTOR_INDEX_SCAN architecture:

- small membership sets (at most the existing exactPkFilterThreshold of
100 keys) retain the exact all-centroid, filter-first path;
- larger membership sets search only ranked nprobe centroids, use the
existing tapered candidate budget, run physical storage Top-K before
membership filtering, and pass the membership docfilter through
RelationScanRequest.FilterHint;
- the exact outer SEMI join remains the final SQL predicate verifier.

Filtered pre-mode is intentionally approximate only for larger
membership sets: a matching row may be excluded by the bounded candidate
stage, but a row violating the SQL filter cannot escape the final SEMI
join. Residual typed predicates, distance ranges, descending search,
empty membership, and malformed membership retain their existing safe
paths.

No protobuf, public API, threshold, or tuning option is added.

Validation:

- focused small-exact/large-bounded planner and IVFFlat regressions
- full tests: pkg/sql/plan, pkg/sql/colexec/vectorscan,
pkg/vectorindex/ivfflat, pkg/vectorindex/overfetch,
pkg/vm/engine/readutil
- build and vet for the same package set
- repository-pinned golangci-lint and molint on changed packages
- complete pre-push self-review: no blockers

CI follow-up:

- the first head applied the approximate path to every membership set
and caused existing selective/retry BVTs to under-fill;
- the correction restores the legacy exact path for sets of 100 keys or
fewer while keeping the large-set performance path bounded;
- newest main was merged before the update.

The 10M TKE acceptance check is: both 10,000-query rounds complete, and
filtered-pre QPS is at least 50% of the same-run unfiltered-pre control.

---------

Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
VioletQwQ-0 pushed a commit to VioletQwQ-0/matrixone that referenced this pull request Sep 17, 2026
The optimizer-visible IVFFlat reader introduced by matrixorigin#27297 always used post-filter Top-K. Even an unfiltered search therefore read every entry vector selected by `nprobe`, calculated every distance in CN, and compacted only afterward. On the reported Wiki 10M workload this regressed the first `l2_only` case from about 20.25 QPS to 0.48 QPS.

This change restores storage-level vector Top-K only when the complete candidate set is known before ranking:

- encode `(index version, centroid ID)` with the canonical composite-key serializer and use an exact `prefix_in(__mo_cpkey_col, ...)` predicate for range and block pruning;
- pass a typed distance expression, query-vector literal, direction, and candidate limit to the storage reader;
- consume the distance vector returned by storage instead of recomputing it in CN; and
- retain filter-then-local-Top-K for membership filters, pushed predicates, bounded distance ranges, or missing centroid restrictions.

The implementation also propagates descending order to the local compactor. It does not change SQL planning, protobufs, index registration, on-disk data, or public APIs.

Validation:

- the new fast-path regression fails on the unfixed revision because `PostFilterTopOnly` is unconditional, then passes with this change;
- focused fast-path, fallback, direction, direct-relation, and reader-initialization tests pass;
- full `pkg/vectorindex/ivfflat` and `pkg/vm/engine/readutil` suites pass;
- affected packages pass `go build` and `go vet` with the repository CGo dependency contract; and
- `git diff --check` passes.

The production Wiki 10M throughput confirmation remains an external nightly benchmark after this PR is deployed to that environment.

Approved by: @XuPeng-SH
VioletQwQ-0 pushed a commit to VioletQwQ-0/matrixone that referenced this pull request Sep 17, 2026
## What type of PR is this?

- [ ] API-change
- [x] BUG
- [x] Improvement
- [ ] Documentation
- [ ] Feature
- [ ] Test and CI
- [ ] Code Refactoring

## Which issue(s) this PR fixes:

issue matrixorigin#27474

Related: matrixorigin#26241

## What this PR does / why we need it:

matrixorigin#27494 restored storage Top-K for unfiltered IVFFlat queries, but large
filtered mode=pre membership sets still take the exhaustive path. The
10M Wiki follow-up ran l2_filter + mode=pre at only about 0.37-0.39 QPS.

This change restores the former hybrid PRE policy in the new
VECTOR_INDEX_SCAN architecture:

- small membership sets (at most the existing exactPkFilterThreshold of
100 keys) retain the exact all-centroid, filter-first path;
- larger membership sets search only ranked nprobe centroids, use the
existing tapered candidate budget, run physical storage Top-K before
membership filtering, and pass the membership docfilter through
RelationScanRequest.FilterHint;
- the exact outer SEMI join remains the final SQL predicate verifier.

Filtered pre-mode is intentionally approximate only for larger
membership sets: a matching row may be excluded by the bounded candidate
stage, but a row violating the SQL filter cannot escape the final SEMI
join. Residual typed predicates, distance ranges, descending search,
empty membership, and malformed membership retain their existing safe
paths.

No protobuf, public API, threshold, or tuning option is added.

Validation:

- focused small-exact/large-bounded planner and IVFFlat regressions
- full tests: pkg/sql/plan, pkg/sql/colexec/vectorscan,
pkg/vectorindex/ivfflat, pkg/vectorindex/overfetch,
pkg/vm/engine/readutil
- build and vet for the same package set
- repository-pinned golangci-lint and molint on changed packages
- complete pre-push self-review: no blockers

CI follow-up:

- the first head applied the approximate path to every membership set
and caused existing selective/retry BVTs to under-fill;
- the correction restores the legacy exact path for sets of 100 keys or
fewer while keeping the large-set performance path bounded;
- newest main was merged before the update.

The 10M TKE acceptance check is: both 10,000-query rounds complete, and
filtered-pre QPS is at least 50% of the same-run unfiltered-pre control.

---------

Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
VioletQwQ-0 pushed a commit to VioletQwQ-0/matrixone that referenced this pull request Sep 17, 2026
The optimizer-visible IVFFlat reader introduced by matrixorigin#27297 always used post-filter Top-K. Even an unfiltered search therefore read every entry vector selected by `nprobe`, calculated every distance in CN, and compacted only afterward. On the reported Wiki 10M workload this regressed the first `l2_only` case from about 20.25 QPS to 0.48 QPS.

This change restores storage-level vector Top-K only when the complete candidate set is known before ranking:

- encode `(index version, centroid ID)` with the canonical composite-key serializer and use an exact `prefix_in(__mo_cpkey_col, ...)` predicate for range and block pruning;
- pass a typed distance expression, query-vector literal, direction, and candidate limit to the storage reader;
- consume the distance vector returned by storage instead of recomputing it in CN; and
- retain filter-then-local-Top-K for membership filters, pushed predicates, bounded distance ranges, or missing centroid restrictions.

The implementation also propagates descending order to the local compactor. It does not change SQL planning, protobufs, index registration, on-disk data, or public APIs.

Validation:

- the new fast-path regression fails on the unfixed revision because `PostFilterTopOnly` is unconditional, then passes with this change;
- focused fast-path, fallback, direction, direct-relation, and reader-initialization tests pass;
- full `pkg/vectorindex/ivfflat` and `pkg/vm/engine/readutil` suites pass;
- affected packages pass `go build` and `go vet` with the repository CGo dependency contract; and
- `git diff --check` passes.

The production Wiki 10M throughput confirmation remains an external nightly benchmark after this PR is deployed to that environment.

Approved by: @XuPeng-SH
VioletQwQ-0 pushed a commit to VioletQwQ-0/matrixone that referenced this pull request Sep 17, 2026
## What type of PR is this?

- [ ] API-change
- [x] BUG
- [x] Improvement
- [ ] Documentation
- [ ] Feature
- [ ] Test and CI
- [ ] Code Refactoring

## Which issue(s) this PR fixes:

issue matrixorigin#27474

Related: matrixorigin#26241

## What this PR does / why we need it:

matrixorigin#27494 restored storage Top-K for unfiltered IVFFlat queries, but large
filtered mode=pre membership sets still take the exhaustive path. The
10M Wiki follow-up ran l2_filter + mode=pre at only about 0.37-0.39 QPS.

This change restores the former hybrid PRE policy in the new
VECTOR_INDEX_SCAN architecture:

- small membership sets (at most the existing exactPkFilterThreshold of
100 keys) retain the exact all-centroid, filter-first path;
- larger membership sets search only ranked nprobe centroids, use the
existing tapered candidate budget, run physical storage Top-K before
membership filtering, and pass the membership docfilter through
RelationScanRequest.FilterHint;
- the exact outer SEMI join remains the final SQL predicate verifier.

Filtered pre-mode is intentionally approximate only for larger
membership sets: a matching row may be excluded by the bounded candidate
stage, but a row violating the SQL filter cannot escape the final SEMI
join. Residual typed predicates, distance ranges, descending search,
empty membership, and malformed membership retain their existing safe
paths.

No protobuf, public API, threshold, or tuning option is added.

Validation:

- focused small-exact/large-bounded planner and IVFFlat regressions
- full tests: pkg/sql/plan, pkg/sql/colexec/vectorscan,
pkg/vectorindex/ivfflat, pkg/vectorindex/overfetch,
pkg/vm/engine/readutil
- build and vet for the same package set
- repository-pinned golangci-lint and molint on changed packages
- complete pre-push self-review: no blockers

CI follow-up:

- the first head applied the approximate path to every membership set
and caused existing selective/retry BVTs to under-fill;
- the correction restores the legacy exact path for sets of 100 keys or
fewer while keeping the large-set performance path bounded;
- newest main was merged before the update.

The 10M TKE acceptance check is: both 10,000-query rounds complete, and
filtered-pre QPS is at least 50% of the same-run unfiltered-pre control.

---------

Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>

This branch was previously deployed

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

Labels

kind/bug Something isn't working kind/enhancement kind/test-ci size/M Denotes a PR that changes [100,499] lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: IVFFlat l2_only mode=pre recall regresses 46x and times out on 10M Wiki workload

3 participants