fix(v2): make ExactNearestNeighbor instantiable (unblocks exact search)#316
Open
Andrew Chen (chuenchen309) wants to merge 1 commit into
Open
Conversation
`ExactNearestNeighbor` inherits `BaseIndex`'s `@abstractmethod index_options` but never implements it, so it remains abstract: `ExactNearestNeighbor()` raises `TypeError: Can't instantiate abstract class ... without an implementation for abstract method 'index_options'`. This makes the documented brute-force / exact-search path unreachable. Its only consumer, `aapply_vector_index`, does `if isinstance(index, ExactNearestNeighbor): await self.adrop_vector_index(); return`, but no caller can ever construct such an instance to reach that branch. Sibling indexes (HNSWIndex/IVFFlatIndex) work because they implement `index_options`. Implement `index_options` returning an empty string. It is safe because `aapply_vector_index` short-circuits (drops the index and returns) before `index_options()` would be used to build DDL, so the value is never consumed; it exists only to make the class concrete. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
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.
Problem
ExactNearestNeighboris the intended way to switch aPGVectorStoretobrute-force / exact search —
apply_vector_indextreats it specially and dropsthe ANN index for it. But the class cannot be constructed:
BaseIndexdeclaresindex_optionsas an@abstractmethod, andExactNearestNeighbornever implements it, so it stays abstract:Because no instance can ever be created, the exact-search branch in
aapply_vector_indexis dead code:HNSWIndex/IVFFlatIndexwork because they implementindex_options.Fix
Implement
index_optionsonExactNearestNeighborreturning an empty string.This is safe:
aapply_vector_indexshort-circuits (drops the index and returns)before
index_options()is ever used to build DDL, so the returned value isnever consumed — it exists only to make the class concrete/instantiable.
Test
Added
test_exact_nearest_neighbor_indextotests/unit_tests/v2/test_indexes.py(pure-Python, no live Postgres): itconstructs
ExactNearestNeighbor()and checksindex_type/index_options().Fails before the change (
TypeError), passes after.ruff checkpasses on the changed files.Disclosure: I used AI assistance (Claude) to help find this
uninstantiable-class / dead-branch issue and write the test. I reviewed the
change, ran the tests, and take responsibility for its correctness.