Skip to content

Commit a4af6c6

Browse files
authored
Merge pull request #1637 from tursodatabase/vector-search-fix-error-handling
Vector search fix error handling
2 parents 56330c8 + 32d72e5 commit a4af6c6

File tree

4 files changed

+38
-6
lines changed

4 files changed

+38
-6
lines changed

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -214238,7 +214238,7 @@ int insertIndexParameters(sqlite3* db, const char *zDbSName, const char *zName,
214238214238
goto clear_and_exit;
214239214239
}
214240214240
rc = sqlite3_step(pStatement);
214241-
if( rc == SQLITE_CONSTRAINT ){
214241+
if( (rc&0xff) == SQLITE_CONSTRAINT ){
214242214242
rc = SQLITE_CONSTRAINT;
214243214243
}else if( rc != SQLITE_DONE ){
214244214244
rc = SQLITE_ERROR;
@@ -214546,7 +214546,10 @@ int vectorIndexCreate(Parse *pParse, const Index *pIdx, const char *zDbSName, co
214546214546
return CREATE_FAIL;
214547214547
}
214548214548
rc = insertIndexParameters(db, zDbSName, pIdx->zName, &idxParams);
214549-
if( rc == SQLITE_CONSTRAINT ){
214549+
214550+
// we must consider only lower bits because with sqlite3_extended_result_codes on
214551+
// we can recieve different subtypes of CONSTRAINT error
214552+
if( (rc&0xff) == SQLITE_CONSTRAINT ){
214550214553
// we are violating unique constraint here which means that someone inserted parameters in the table before us
214551214554
// taking aside corruption scenarios, this can be in case of loading dump (because tables and data are loaded before indices)
214552214555
// this case is valid and we must proceed with index creating but avoid index-refill step as it is already filled

libsql-ffi/bundled/src/sqlite3.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -214238,7 +214238,7 @@ int insertIndexParameters(sqlite3* db, const char *zDbSName, const char *zName,
214238214238
goto clear_and_exit;
214239214239
}
214240214240
rc = sqlite3_step(pStatement);
214241-
if( rc == SQLITE_CONSTRAINT ){
214241+
if( (rc&0xff) == SQLITE_CONSTRAINT ){
214242214242
rc = SQLITE_CONSTRAINT;
214243214243
}else if( rc != SQLITE_DONE ){
214244214244
rc = SQLITE_ERROR;
@@ -214546,7 +214546,10 @@ int vectorIndexCreate(Parse *pParse, const Index *pIdx, const char *zDbSName, co
214546214546
return CREATE_FAIL;
214547214547
}
214548214548
rc = insertIndexParameters(db, zDbSName, pIdx->zName, &idxParams);
214549-
if( rc == SQLITE_CONSTRAINT ){
214549+
214550+
// we must consider only lower bits because with sqlite3_extended_result_codes on
214551+
// we can recieve different subtypes of CONSTRAINT error
214552+
if( (rc&0xff) == SQLITE_CONSTRAINT ){
214550214553
// we are violating unique constraint here which means that someone inserted parameters in the table before us
214551214554
// taking aside corruption scenarios, this can be in case of loading dump (because tables and data are loaded before indices)
214552214555
// this case is valid and we must proceed with index creating but avoid index-refill step as it is already filled

libsql-server/tests/hrana/batch.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -379,3 +379,26 @@ fn reindex_statement() {
379379

380380
sim.run().unwrap();
381381
}
382+
383+
#[test]
384+
fn test_simulate_vector_index_load_from_dump() {
385+
let mut sim = turmoil::Builder::new()
386+
.simulation_duration(Duration::from_secs(1000))
387+
.build();
388+
sim.host("primary", super::make_standalone_server);
389+
sim.client("client", async {
390+
let db = Database::open_remote_with_connector("http://primary:8080", "", TurmoilConnector)?;
391+
let conn = db.connect()?;
392+
393+
conn.execute("CREATE TABLE t ( v FLOAT32(2) );", ()).await?;
394+
conn.execute("CREATE TABLE t_idx_shadow(index_key INTEGER , data BLOB, PRIMARY KEY (index_key));", ()).await?;
395+
conn.execute("CREATE TABLE libsql_vector_meta_shadow ( name TEXT PRIMARY KEY, metadata BLOB ) WITHOUT ROWID", ()).await?;
396+
conn.execute("INSERT INTO libsql_vector_meta_shadow VALUES ('t_idx', x'');", ()).await?;
397+
conn.execute("INSERT INTO t VALUES (vector('[1,2]')), (vector('[2,3]'));", ()).await?;
398+
conn.execute("CREATE INDEX t_idx ON t (libsql_vector_idx(v));", ()).await?;
399+
400+
Ok(())
401+
});
402+
403+
sim.run().unwrap();
404+
}

libsql-sqlite3/src/vectorIndex.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -629,7 +629,7 @@ int insertIndexParameters(sqlite3* db, const char *zDbSName, const char *zName,
629629
goto clear_and_exit;
630630
}
631631
rc = sqlite3_step(pStatement);
632-
if( rc == SQLITE_CONSTRAINT ){
632+
if( (rc&0xff) == SQLITE_CONSTRAINT ){
633633
rc = SQLITE_CONSTRAINT;
634634
}else if( rc != SQLITE_DONE ){
635635
rc = SQLITE_ERROR;
@@ -937,7 +937,10 @@ int vectorIndexCreate(Parse *pParse, const Index *pIdx, const char *zDbSName, co
937937
return CREATE_FAIL;
938938
}
939939
rc = insertIndexParameters(db, zDbSName, pIdx->zName, &idxParams);
940-
if( rc == SQLITE_CONSTRAINT ){
940+
941+
// we must consider only lower bits because with sqlite3_extended_result_codes on
942+
// we can recieve different subtypes of CONSTRAINT error
943+
if( (rc&0xff) == SQLITE_CONSTRAINT ){
941944
// we are violating unique constraint here which means that someone inserted parameters in the table before us
942945
// taking aside corruption scenarios, this can be in case of loading dump (because tables and data are loaded before indices)
943946
// this case is valid and we must proceed with index creating but avoid index-refill step as it is already filled

0 commit comments

Comments
 (0)