Skip to content

Read a quantized IVF column's vectors from _ivf_vectors - #349

Open
MayCXC wants to merge 1 commit into
asg017:mainfrom
MayCXC:ivf-quantized-reads
Open

MayCXC wants to merge 1 commit into
asg017:mainfrom
MayCXC:ivf-quantized-reads

Conversation

@MayCXC

@MayCXC MayCXC commented Oct 23, 2026 •

Copy link
Copy Markdown

On an IVF column with quantizer=int8 or quantizer=binary, a point query reads past the end of the vector it returns, and a full scan or a KNN query returns the quantized bytes as the column's value.

create virtual table v using vec0(a float[16] indexed by ivf(nlist=2, quantizer=int8));
-- 10 inserts
select a from v where rowid = 3;   -- 64 bytes read from a 16-byte vector (ASan: heap-buffer-overflow in vec0Column_point)
select rowid, a from v;            -- 16-byte blobs, the int8 form of each float[16]
select rowid, a from v where a match :q and k = 10;  -- the same

A quantized column keeps each vector's int8 or binary form in its cell and the vector as it was inserted in _ivf_vectors, but ivf_get_vector_data() returned the cell's bytes, ivf_vec_size() of them, for every column. A quantized column's vectors are now read from _ivf_vectors, through a cached statement like the rowid-map lookup's, which is reset as soon as its row is copied out so a read-only query leaves no statement in progress.

Built with -DSQLITE_VEC_EXPERIMENTAL_IVF_ENABLE=1, on main none of 10 int8 or binary rows reads back as inserted from a point query, a full scan or a KNN query, before or after compute-centroids; here all 10 do from each, and an unquantized column reads back as before. Test: test_ivf_quantized_vectors_read_back in test-ivf-quantization.py, parametrized over int8 and binary; both fail on main and pass here. The Python suite passes (with the IVF define, 303 passed, 4 skipped; without it, 239 passed, 68 skipped), under ASan and UBSan as well, and both builds' warnings are main's.

#313 fixes the quantized paths of clear-centroids, set-centroid and assign-vectors and leaves this one as it is. This merges cleanly with it and with every other open PR that touches the IVF code, #343 and #347 included.

Origin. Found in the IVF port to photostructure/sqlite-vec, photostructure#12 (65817f4), which lists it among the port's differences from upstream; the change and its test are written here for asg017's main. No open or closed PR or issue here makes the same change.

A quantized IVF column keeps each vector's int8 or binary form in its
cell and the vector as it was inserted in _ivf_vectors, but
ivf_get_vector_data() returned the cell's bytes for every column. A
point query then read the column's float32 elements past the end of
the quantized vector, and a full scan or a KNN query returned the
quantized bytes as the column's value. A quantized column's vectors
are now read from _ivf_vectors.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant