Repository navigation
fix: restore IVFFlat storage Top-K - #27494
Conversation
Qodo reviews are paused for this user.Troubleshooting steps vary by plan Learn more → On a Teams plan? Using GitHub Enterprise Server, GitLab Self-Managed, or Bitbucket Data Center? |
XuPeng-SH
left a comment
There was a problem hiding this comment.
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.
Merge Queue Status
This pull request spent 34 seconds in the queue, including 2 seconds running CI. Required conditions to merge
|
## 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>
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
## 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>
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
## 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>
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
## 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>
What type of PR is this?
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 firstl2_onlycase 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:
(index version, centroid ID)with the canonical composite-key serializer and use an exactprefix_in(__mo_cpkey_col, ...)predicate for range and block pruning;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:
PostFilterTopOnlyis unconditional, then passes with this change;pkg/vectorindex/ivfflatandpkg/vm/engine/readutilsuites pass;go buildandgo vetwith the repository CGo dependency contract; andgit diff --checkpasses.The production Wiki 10M throughput confirmation remains an external nightly benchmark after this PR is deployed to that environment.