[python] Stream batch vector refinement candidates - #9806
Open
TheR1sing3un wants to merge 1 commit into
Open
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Purpose
Batch ANN refinement reads the union of candidate vectors into a complete Arrow table, expands every vector into Python lists, and retains a row-ID-to-vector dictionary while scoring each query. Large candidate unions cause substantial memory amplification.
Stream candidate Arrow batches and score only the rows belonging to each query, updating bounded Top-K heaps as each batch arrives. Candidate membership is shared across split workers, existing table read parallelism is retained, and each worker owns one streaming reader with bounded scoring blocks. Snapshot selection, score calculations, null handling, and row-ID tie breaking remain unchanged.
Tests
python -m pytest -q pypaimon/tests/batch_vector_streaming_refine_test.py pypaimon/tests/vector_search_filter_test.py pypaimon/tests/vector_scoring_test.py pypaimon/tests/batch_vector_raw_scan_test.py pypaimon/tests/vector_metric_consistency_test.py: 119 passed.git diff --checkpassed.Benchmark
macOS 26.4.1 arm64, Python 3.9.6, NumPy 2.0.2, PyArrow 19.0.1. Each query has 512 candidates and returns Top-K=64 with refine factor 8. The temporary harness reads real, uncompressed local Parquet data in 1,024-row batches and measures candidate reading, conversion, scoring, and Top-K selection. ANN candidate generation and Paimon scan planning are excluded. Each variant runs in three fresh processes with warmed filesystem pages and one read worker; values are medians and RSS is peak process resident memory.
The materialized-Arrow ablation keeps all candidate Arrow data resident but uses the new Arrow scoring path. The streaming variant additionally reads and releases bounded batches.
For the large disjoint candidate workload, the final implementation is 8.14x faster and reduces peak RSS by 81.1%. The ablation shows that avoiding Python vector expansion supplies most of the speedup, while streaming further reduces retained Arrow memory. Small or heavily overlapping candidate sets have smaller memory benefits. All variants returned identical row IDs and score bytes.
End-to-end gains depend on candidate overlap, vector dimensions, storage latency, batch size, and read parallelism. Multiple read workers retain one batch and per-query heaps per worker.