mirror of
https://github.com/asg017/sqlite-vec.git
synced 2026-04-25 08:46:49 +02:00
Complete vec0 DELETE: zero data, reclaim empty chunks, fix metadata rc bug
When a row is deleted from a vec0 virtual table, the rowid slot in _chunks.rowids and vector data in _vector_chunksNN.vectors are now zeroed out (previously left as stale data, tracked in #54). When all rows in a chunk are deleted (validity bitmap all zeros), the chunk and its associated vector/metadata shadow table rows are reclaimed. - Add vec0Update_Delete_ClearRowid to zero the rowid blob slot - Add vec0Update_Delete_ClearVectors to zero all vector blob slots - Add vec0Update_Delete_DeleteChunkIfEmpty to detect and delete fully-empty chunks from _chunks, _vector_chunksNN, _metadatachunksNN - Fix missing rc check in ClearMetadata loop (bug: errors were silently ignored) - Fix vec0_new_chunk to explicitly set _rowid_ on shadow table INSERTs (SHADOW_TABLE_ROWID_QUIRK: "rowid PRIMARY KEY" without INTEGER type is not a true rowid alias, causing blob_open failures after chunk delete+recreate cycles) - Add 13 new tests covering rowid/vector zeroing, chunk reclamation, metadata/auxiliary/partition/text-PK/int8/bit variants, and page_count shrinkage verification - Add vec0-delete-completeness fuzz target - Update snapshots for new delete zeroing behavior Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
parent
b669801d31
commit
56707c4c09
6 changed files with 732 additions and 24 deletions
114
tests/fuzz/vec0-delete-completeness.c
Normal file
114
tests/fuzz/vec0-delete-completeness.c
Normal file
|
|
@ -0,0 +1,114 @@
|
|||
#include <stdint.h>
|
||||
#include <stddef.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include "sqlite-vec.h"
|
||||
#include "sqlite3.h"
|
||||
#include <assert.h>
|
||||
|
||||
int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
|
||||
if (size < 6) return 0;
|
||||
|
||||
int rc;
|
||||
sqlite3 *db;
|
||||
sqlite3_stmt *stmtInsert = NULL;
|
||||
sqlite3_stmt *stmtDelete = NULL;
|
||||
sqlite3_stmt *stmtScan = NULL;
|
||||
sqlite3_stmt *stmtCount = NULL;
|
||||
|
||||
rc = sqlite3_open(":memory:", &db);
|
||||
assert(rc == SQLITE_OK);
|
||||
rc = sqlite3_vec_init(db, NULL, NULL);
|
||||
assert(rc == SQLITE_OK);
|
||||
|
||||
rc = sqlite3_exec(db,
|
||||
"CREATE VIRTUAL TABLE v USING vec0(emb float[4], chunk_size=4)",
|
||||
NULL, NULL, NULL);
|
||||
if (rc != SQLITE_OK) { sqlite3_close(db); return 0; }
|
||||
|
||||
sqlite3_prepare_v2(db,
|
||||
"INSERT INTO v(rowid, emb) VALUES (?, ?)", -1, &stmtInsert, NULL);
|
||||
sqlite3_prepare_v2(db,
|
||||
"DELETE FROM v WHERE rowid = ?", -1, &stmtDelete, NULL);
|
||||
sqlite3_prepare_v2(db,
|
||||
"SELECT rowid FROM v", -1, &stmtScan, NULL);
|
||||
|
||||
if (!stmtInsert || !stmtDelete || !stmtScan) goto cleanup;
|
||||
|
||||
size_t i = 0;
|
||||
while (i + 2 <= size) {
|
||||
uint8_t op = data[i++] % 3;
|
||||
uint8_t rowid_byte = data[i++];
|
||||
int64_t rowid = (int64_t)(rowid_byte % 16) + 1;
|
||||
|
||||
switch (op) {
|
||||
case 0: {
|
||||
// INSERT
|
||||
float vec[4] = {0.0f, 0.0f, 0.0f, 0.0f};
|
||||
for (int j = 0; j < 4 && i < size; j++, i++) {
|
||||
vec[j] = (float)((int8_t)data[i]) / 10.0f;
|
||||
}
|
||||
sqlite3_reset(stmtInsert);
|
||||
sqlite3_bind_int64(stmtInsert, 1, rowid);
|
||||
sqlite3_bind_blob(stmtInsert, 2, vec, sizeof(vec), SQLITE_TRANSIENT);
|
||||
sqlite3_step(stmtInsert);
|
||||
break;
|
||||
}
|
||||
case 1: {
|
||||
// DELETE
|
||||
sqlite3_reset(stmtDelete);
|
||||
sqlite3_bind_int64(stmtDelete, 1, rowid);
|
||||
sqlite3_step(stmtDelete);
|
||||
break;
|
||||
}
|
||||
case 2: {
|
||||
// Full scan
|
||||
sqlite3_reset(stmtScan);
|
||||
while (sqlite3_step(stmtScan) == SQLITE_ROW) {}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Delete all remaining rows
|
||||
sqlite3_exec(db, "DELETE FROM v", NULL, NULL, NULL);
|
||||
|
||||
// Assert all shadow tables are empty after full deletion
|
||||
sqlite3_prepare_v2(db,
|
||||
"SELECT count(*) FROM v_rowids", -1, &stmtCount, NULL);
|
||||
if (stmtCount) {
|
||||
rc = sqlite3_step(stmtCount);
|
||||
assert(rc == SQLITE_ROW);
|
||||
assert(sqlite3_column_int(stmtCount, 0) == 0);
|
||||
sqlite3_finalize(stmtCount);
|
||||
stmtCount = NULL;
|
||||
}
|
||||
|
||||
sqlite3_prepare_v2(db,
|
||||
"SELECT count(*) FROM v_chunks", -1, &stmtCount, NULL);
|
||||
if (stmtCount) {
|
||||
rc = sqlite3_step(stmtCount);
|
||||
assert(rc == SQLITE_ROW);
|
||||
assert(sqlite3_column_int(stmtCount, 0) == 0);
|
||||
sqlite3_finalize(stmtCount);
|
||||
stmtCount = NULL;
|
||||
}
|
||||
|
||||
sqlite3_prepare_v2(db,
|
||||
"SELECT count(*) FROM v_vector_chunks00", -1, &stmtCount, NULL);
|
||||
if (stmtCount) {
|
||||
rc = sqlite3_step(stmtCount);
|
||||
assert(rc == SQLITE_ROW);
|
||||
assert(sqlite3_column_int(stmtCount, 0) == 0);
|
||||
sqlite3_finalize(stmtCount);
|
||||
stmtCount = NULL;
|
||||
}
|
||||
|
||||
cleanup:
|
||||
sqlite3_finalize(stmtInsert);
|
||||
sqlite3_finalize(stmtDelete);
|
||||
sqlite3_finalize(stmtScan);
|
||||
sqlite3_close(db);
|
||||
return 0;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue