Skip to content

Commit 92890f0

Browse files
committed
NULL vectors are now correctly skipped during scanning
1 parent 41af197 commit 92890f0

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

src/sqlite-vector.c

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1018,6 +1018,8 @@ void vector_context_free (void *p) {
10181018
}
10191019

10201020
table_context *vector_context_lookup (vector_context *ctx, const char *table_name, const char *column_name) {
1021+
if ((table_name == NULL) || (column_name == NULL)) return NULL;
1022+
10211023
for (int i=0; i<ctx->table_count; ++i) {
10221024
// tname and cname can be NULL after adding vector_cleanup function
10231025
const char *tname = ctx->tables[i].t_name;
@@ -1177,12 +1179,15 @@ static int vector_rebuild_quantization (sqlite3_context *context, const char *ta
11771179
rc = sqlite3_step(vm);
11781180
if (rc == SQLITE_DONE) {rc = SQLITE_OK; break;}
11791181
else if (rc != SQLITE_ROW) break;
1182+
if (sqlite3_column_type(vm, 1) == SQLITE_NULL) continue;
11801183

11811184
const void *blob = (float *)sqlite3_column_blob(vm, 1);
1185+
if (!blob) continue;
1186+
11821187
int blob_size = sqlite3_column_bytes(vm, 1);
11831188
size_t need_bytes = (size_t)dim * (size_t)vector_type_to_size(type);
1184-
if (!blob || blob_size < need_bytes) {
1185-
context_result_error(context, SQLITE_ERROR, "Invalid or missing vector blob found at rowid %lld.", (long long)sqlite3_column_int64(vm, 0));
1189+
if (blob_size < need_bytes) {
1190+
context_result_error(context, SQLITE_ERROR, "Invalid vector blob found at rowid %lld.", (long long)sqlite3_column_int64(vm, 0));
11861191
rc = SQLITE_ERROR;
11871192
goto vector_rebuild_quantization_cleanup;
11881193
}
@@ -1246,9 +1251,12 @@ static int vector_rebuild_quantization (sqlite3_context *context, const char *ta
12461251
rc = sqlite3_step(vm);
12471252
if (rc == SQLITE_DONE) {rc = SQLITE_OK; break;}
12481253
else if (rc != SQLITE_ROW) break;
1254+
if (sqlite3_column_type(vm, 1) == SQLITE_NULL) continue;
12491255

12501256
int64_t rowid = (int64_t)sqlite3_column_int64(vm, 0);
12511257
const void *blob = sqlite3_column_blob(vm, 1);
1258+
if (!blob) continue;
1259+
12521260
if (n_processed == 0) min_rowid = rowid;
12531261
VECTOR_PRINT((void *)blob, type, dim);
12541262

@@ -1716,6 +1724,7 @@ static int vCursorFilterCommon (sqlite3_vtab_cursor *cur, int idxNum, const char
17161724
} else {
17171725
vector = (const void *)sqlite3_value_blob(argv[2]);
17181726
vsize = sqlite3_value_bytes(argv[2]);
1727+
if (!vector) return sqlite_vtab_set_error(&vtab->base, "%s: input vector cannot be NULL.", fname);
17191728
}
17201729
VECTOR_PRINT((void*)vector, t_ctx->options.v_type, t_ctx->options.v_dim);
17211730

@@ -1933,8 +1942,11 @@ static int vFullScanRun (sqlite3 *db, vFullScanCursor *c, const void *v1, int v1
19331942
rc = sqlite3_step(vm);
19341943
if (rc == SQLITE_DONE) {rc = SQLITE_OK; goto cleanup;}
19351944
if (rc != SQLITE_ROW) goto cleanup;
1945+
if (sqlite3_column_type(vm, 1) == SQLITE_NULL) continue;
19361946

19371947
float *v2 = (float *)sqlite3_column_blob(vm, 1);
1948+
if (v2 == NULL) continue;
1949+
19381950
float distance = distance_fn((const void *)v1, (const void *)v2, dimension);
19391951
if (nearly_zero_float32(distance)) distance = 0.0;
19401952
VECTOR_PRINT((void*)v2, vt, dimension);

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.8"
27+
#define SQLITE_VECTOR_VERSION "0.9.9"
2828

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

0 commit comments

Comments
 (0)