SIGN IN SIGN UP

libsql-sqlite3: Break vector search distance ties by rowid deterministically

The DiskANN top-k buffer ordered candidates by distance using a strict
less-than comparison (distanceBufferInsertIdx), so when two vectors are
exactly equidistant from the query their relative order in the result fell
out of the search/visit order. That visit order depends on float rounding in
the distance computation, which varies with compiler/build flags -- e.g. the
same sources built as testfixture vs the sqlite3 shell resolved such ties
differently, and the Makefile.in changes in the 3.47.0 merge were enough to
flip the testfixture build's result for vector-index-v2-query-4 (query vector
[-1,1,1,1] is exactly equidistant from rows 'b' and 'c').

Add topCandidateInsertIdx(), which breaks exact-distance ties in the top
candidates buffer by rowid (ascending). Equidistant results now come back in
a stable, reproducible order independent of build flags or how the search
happened to reach them. Non-tied results are unaffected (the distance
comparison still decides first).

Update vector-index-v2-query-4 to assert the deterministic ordering ({d a b},
'b' being the smaller rowid of the tied pair) instead of the build-dependent
value it previously hard-coded.
P
Pekka Enberg committed
d8bcfeb1764c5083e29aebf4ab8dba03e1ad4f76
Parent: ebd1563