From 88e592d3f8b866e4d08240a281c36b69183355fa Mon Sep 17 00:00:00 2001 From: Taewoon Kim Date: Fri, 30 Oct 2026 13:11:17 +0900 Subject: [PATCH] tests: make the PQ approximate-search tests ask for every row (#149) TestFindNearestApproximate asserted the first name of find_nearest_approximate with k=1 and k=3 on a four-row index. The approximate path has no re-rank (it returns the PQ scores of what the walk collected), so the only run-to-run variable the test left open was how much of the four-node graph a small k collects. Both tests now ask for k = number of rows, so the answer is the PQ ranking of all four rows, and a smaller k only asserts count and order. Repetition (pytest-repeat --count 200, both tests, taskset -c 4,5, Python 3.14), before and after: JVM wheel 0 of 400 failed before, 0 of 400 after; native wheel 0 of 400 before, 0 of 400 after. The flake did not reproduce on this host, so the before numbers do not prove the cause. Ruled out: jvector's KMeansPlusPlusClusterer on the globally centered four rows gave four distinct centroids in 20000 of 20000 runs (unweighted, anisotropic 0.2 and 0.5), and 1200 fresh index builds returned the same ranking and scores. Co-Authored-By: Claude Opus 5.5 --- tests/test_vector_index_introspection.py | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/tests/test_vector_index_introspection.py b/tests/test_vector_index_introspection.py index 3777b0b..d3083b2 100644 --- a/tests/test_vector_index_introspection.py +++ b/tests/test_vector_index_introspection.py @@ -182,23 +182,34 @@ def _pq_index(db, rows): def test_returns_the_nearest_record_and_its_distance(self, temp_db): index = self._pq_index(temp_db, PQ_VECTORS) - results = index.find_nearest_approximate([0.9, 0.1, 0.0], k=1) + # k = every row: the walk keeps all four nodes, so the ranking is the PQ scores of the four rows and does + # not depend on how a k=1 beam happens to travel the graph (#149) + results = index.find_nearest_approximate([0.9, 0.1, 0.0], k=len(PQ_VECTORS)) - assert len(results) == 1 + assert len(results) == len(PQ_VECTORS) record, distance = results[0] assert record.get("name") == "x" assert isinstance(distance, float) + # k=1 still returns exactly one result with a float distance + ((_, only_distance),) = index.find_nearest_approximate([0.9, 0.1, 0.0], k=1) + assert isinstance(only_distance, float) + def test_k_results_come_back_nearest_first(self, temp_db): index = self._pq_index(temp_db, PQ_VECTORS) - results = index.find_nearest_approximate([0.9, 0.1, 0.0], k=3) + results = index.find_nearest_approximate([0.9, 0.1, 0.0], k=len(PQ_VECTORS)) assert [r.get("name") for r, _ in results][0] == "x" - assert len(results) == 3 + assert len(results) == len(PQ_VECTORS) distances = [d for _, d in results] assert distances == sorted(distances) + # a smaller k returns that many, sorted + top3 = index.find_nearest_approximate([0.9, 0.1, 0.0], k=3) + assert len(top3) == 3 + assert [d for _, d in top3] == sorted(d for _, d in top3) + def test_allowed_rids_restrict_the_answer(self, temp_db): index = self._pq_index(temp_db, PQ_VECTORS) rids = {