Skip to content

Commit 6f0a9ed

Browse files
committed
Added better error message for when vector_quantize_scan is used without first calling vector_quantize.
1 parent bda5b4e commit 6f0a9ed

File tree

2 files changed

+18
-5
lines changed

2 files changed

+18
-5
lines changed

src/sqlite-vector.c

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -832,10 +832,14 @@ static char *generate_memory_quant_table (const char *table_name, const char *co
832832
return sqlite3_snprintf(STATIC_SQL_SIZE, sql, "SELECT SUM(LENGTH(data)) FROM vector0_%q_%q;", table_name, column_name);
833833
}
834834

835-
static char * generate_insert_quant_table (const char *table_name, const char *column_name, char sql[STATIC_SQL_SIZE]) {
835+
static char *generate_insert_quant_table (const char *table_name, const char *column_name, char sql[STATIC_SQL_SIZE]) {
836836
return sqlite3_snprintf(STATIC_SQL_SIZE, sql, "INSERT INTO vector0_%q_%q (rowid1, rowid2, counter, data) VALUES (?, ?, ?, ?);", table_name, column_name);
837837
}
838838

839+
static char *generate_quant_table_name (const char *table_name, const char *column_name, char sql[STATIC_SQL_SIZE]) {
840+
return sqlite3_snprintf(STATIC_SQL_SIZE, sql, "vector0_%q_%q", table_name, column_name);
841+
}
842+
839843
// MARK: - Vector Context and Options -
840844

841845
void *vector_context_create (void) {
@@ -1513,7 +1517,7 @@ static void vector_convert_i8 (sqlite3_context *context, int argc, sqlite3_value
15131517

15141518
// MARK: - Modules -
15151519

1516-
static int vCursorFilterCommon (sqlite3_vtab_cursor *cur, int idxNum, const char *idxStr, int argc, sqlite3_value **argv, const char *fname, vcursor_run_callback run_callback, vcursor_sort_callback sort_callback) {
1520+
static int vCursorFilterCommon (sqlite3_vtab_cursor *cur, int idxNum, const char *idxStr, int argc, sqlite3_value **argv, const char *fname, vcursor_run_callback run_callback, vcursor_sort_callback sort_callback, bool check_quant) {
15171521

15181522
vFullScanCursor *c = (vFullScanCursor *)cur;
15191523
vFullScan *vtab = (vFullScan *)cur->pVtab;
@@ -1562,6 +1566,15 @@ static int vCursorFilterCommon (sqlite3_vtab_cursor *cur, int idxNum, const char
15621566
vsize = sqlite3_value_bytes(argv[2]);
15631567
}
15641568

1569+
if (check_quant) {
1570+
char buffer[STATIC_SQL_SIZE];
1571+
char *name = generate_quant_table_name(table_name, column_name, buffer);
1572+
if (!name || !sqlite_table_exists(vtab->db, name)) {
1573+
sqlite_vtab_set_error(&vtab->base, "Quantization table not found for table '%s' and column '%s'. Ensure that vector_quantize() has been called before using vector_quantize_scan().");
1574+
return SQLITE_ERROR;
1575+
}
1576+
}
1577+
15651578
int k = sqlite3_value_int(argv[3]);
15661579

15671580
// nothing needs to be returned
@@ -1785,7 +1798,7 @@ static int vFullScanRun (sqlite3 *db, vFullScanCursor *c, const void *v1, int v1
17851798
}
17861799

17871800
static int vFullScanCursorFilter (sqlite3_vtab_cursor *cur, int idxNum, const char *idxStr, int argc, sqlite3_value **argv) {
1788-
return vCursorFilterCommon(cur, idxNum, idxStr, argc, argv, "vector_full_scan", vFullScanRun, vFullScanSortSlots);
1801+
return vCursorFilterCommon(cur, idxNum, idxStr, argc, argv, "vector_full_scan", vFullScanRun, vFullScanSortSlots, false);
17891802
}
17901803

17911804
// MARK: -
@@ -1897,7 +1910,7 @@ static int vQuantRun (sqlite3 *db, vFullScanCursor *c, const void *v1, int v1siz
18971910

18981911

18991912
static int vQuantCursorFilter (sqlite3_vtab_cursor *cur, int idxNum, const char *idxStr, int argc, sqlite3_value **argv) {
1900-
return vCursorFilterCommon(cur, idxNum, idxStr, argc, argv, "vector_quantize_scan", vQuantRun, vFullScanSortSlots);
1913+
return vCursorFilterCommon(cur, idxNum, idxStr, argc, argv, "vector_quantize_scan", vQuantRun, vFullScanSortSlots, true);
19011914
}
19021915

19031916
static sqlite3_module vFullScanModule = {

src/sqlite-vector.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
extern "C" {
2525
#endif
2626

27-
#define SQLITE_VECTOR_VERSION "0.9.0"
27+
#define SQLITE_VECTOR_VERSION "0.9.1"
2828

2929
SQLITE_VECTOR_API int sqlite3_vector_init (sqlite3 *db, char **pzErrMsg, const sqlite3_api_routines *pApi);
3030

0 commit comments

Comments
 (0)