Skip to content

Commit 26c49a8

Browse files
authored
Merge pull request #1660 from tursodatabase/vector-search-fix-null
vector search: fix delete of rows with NULL vector value
2 parents a5db99b + c135391 commit 26c49a8

File tree

4 files changed

+33
-8
lines changed

4 files changed

+33
-8
lines changed

libsql-ffi/bundled/SQLite3MultipleCiphers/src/sqlite3.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213389,7 +213389,12 @@ int diskAnnDelete(
213389213389
DiskAnnTrace(("diskAnnDelete started: rowid=%lld\n", nodeRowid));
213390213390

213391213391
rc = blobSpotCreate(pIndex, &pNodeBlob, nodeRowid, pIndex->nBlockSize, DISKANN_BLOB_WRITABLE);
213392-
if( rc != SQLITE_OK ){
213392+
if( rc == DISKANN_ROW_NOT_FOUND ){
213393+
// as we omit rows with NULL values during insert, it can be the case that there is nothing to delete in the index, while row exists in the base table
213394+
// so, we must simply silently stop delete process as there is nothing to delete from index
213395+
rc = SQLITE_OK;
213396+
goto out;
213397+
}else if( rc != SQLITE_OK ){
213393213398
*pzErrMsg = sqlite3_mprintf("vector index(delete): failed to create blob for node row");
213394213399
goto out;
213395213400
}

libsql-ffi/bundled/src/sqlite3.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213389,7 +213389,12 @@ int diskAnnDelete(
213389213389
DiskAnnTrace(("diskAnnDelete started: rowid=%lld\n", nodeRowid));
213390213390

213391213391
rc = blobSpotCreate(pIndex, &pNodeBlob, nodeRowid, pIndex->nBlockSize, DISKANN_BLOB_WRITABLE);
213392-
if( rc != SQLITE_OK ){
213392+
if( rc == DISKANN_ROW_NOT_FOUND ){
213393+
// as we omit rows with NULL values during insert, it can be the case that there is nothing to delete in the index, while row exists in the base table
213394+
// so, we must simply silently stop delete process as there is nothing to delete from index
213395+
rc = SQLITE_OK;
213396+
goto out;
213397+
}else if( rc != SQLITE_OK ){
213393213398
*pzErrMsg = sqlite3_mprintf("vector index(delete): failed to create blob for node row");
213394213399
goto out;
213395213400
}

libsql-sqlite3/src/vectordiskann.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1633,7 +1633,12 @@ int diskAnnDelete(
16331633
DiskAnnTrace(("diskAnnDelete started: rowid=%lld\n", nodeRowid));
16341634

16351635
rc = blobSpotCreate(pIndex, &pNodeBlob, nodeRowid, pIndex->nBlockSize, DISKANN_BLOB_WRITABLE);
1636-
if( rc != SQLITE_OK ){
1636+
if( rc == DISKANN_ROW_NOT_FOUND ){
1637+
// as we omit rows with NULL values during insert, it can be the case that there is nothing to delete in the index, while row exists in the base table
1638+
// so, we must simply silently stop delete process as there is nothing to delete from index
1639+
rc = SQLITE_OK;
1640+
goto out;
1641+
}else if( rc != SQLITE_OK ){
16371642
*pzErrMsg = sqlite3_mprintf("vector index(delete): failed to create blob for node row");
16381643
goto out;
16391644
}

libsql-sqlite3/test/libsql_vector_index.test

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -131,13 +131,23 @@ do_execsql_test vector-empty {
131131

132132

133133
do_execsql_test vector-null {
134-
CREATE TABLE t_null( v FLOAT32(3));
134+
CREATE TABLE t_null( v FLOAT32(2));
135135
CREATE INDEX t_null_idx ON t_null( libsql_vector_idx(v) );
136-
INSERT INTO t_null VALUES(vector('[1,2,3]'));
136+
INSERT INTO t_null VALUES(vector('[1,-1]'));
137137
INSERT INTO t_null VALUES(NULL);
138-
INSERT INTO t_null VALUES(vector('[2,3,4]'));
139-
SELECT * FROM vector_top_k('t_null_idx', '[1,2,3]', 2);
140-
} {1 3}
138+
INSERT INTO t_null VALUES(vector('[-2,1]'));
139+
SELECT * FROM vector_top_k('t_null_idx', '[1,1]', 2);
140+
UPDATE t_null SET v = vector('[1,1]') WHERE rowid = 2;
141+
SELECT rowid FROM vector_top_k('t_null_idx', vector('[1,1]'), 3);
142+
UPDATE t_null SET v = NULL WHERE rowid = 3;
143+
SELECT rowid FROM vector_top_k('t_null_idx', vector('[1,1]'), 3);
144+
UPDATE t_null SET v = NULL;
145+
SELECT rowid FROM vector_top_k('t_null_idx', vector('[1,1]'), 3);
146+
} {
147+
1 3
148+
2 1 3
149+
2 1
150+
}
141151

142152
do_execsql_test vector-sql {
143153
CREATE TABLE t_sql( v FLOAT32(3));

0 commit comments

Comments
 (0)