Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 32 additions & 1 deletion libsql-ffi/bundled/SQLite3MultipleCiphers/src/sqlite3.c
Original file line number Diff line number Diff line change
Expand Up @@ -217469,6 +217469,11 @@ int diskAnnSearch(
*pzErrMsg = sqlite3_mprintf("vector index(search): vector type differs from column type: %d != %d", pVector->type, pIndex->nNodeVectorType);
return SQLITE_ERROR;
}
if( k == 0 ){
pRows->nRows = 0;
pRows->nCols = pKey->nKeyColumns;
return SQLITE_OK;
}

rc = diskAnnSelectRandomShadowRow(pIndex, &nStartRowid);
if( rc == SQLITE_DONE ){
Expand All @@ -217482,8 +217487,9 @@ int diskAnnSearch(
}
rc = diskAnnSearchCtxInit(pIndex, &ctx, pVector, pIndex->searchL, k, DISKANN_BLOB_READONLY);
if( rc != SQLITE_OK ){
// on failure the init has already freed what it allocated, so ctx must not be deinitialized
*pzErrMsg = sqlite3_mprintf("vector index(search): failed to initialize search context");
goto out;
return rc;
}
rc = diskAnnSearchInternal(pIndex, &ctx, nStartRowid, pzErrMsg);
if( rc != SQLITE_OK ){
Expand Down Expand Up @@ -220050,6 +220056,7 @@ struct vectorVtab_cursor {
int nWrites; /* Number of row written to the storage backing virtual table */
VectorOutRows rows;
int iRow;
sqlite3_value *apArgs[3]; /* Arguments of the last xFilter, returned as the hidden columns */
};

/* Column numbers */
Expand All @@ -220058,6 +220065,14 @@ struct vectorVtab_cursor {
#define VECTOR_COLUMN_K 2
#define VECTOR_COLUMN_OFFSET 3

static void vectorVtabFreeArgs(vectorVtab_cursor *pCur){
int i;
for(i = 0; i < VECTOR_COLUMN_OFFSET; i++){
sqlite3_value_free(pCur->apArgs[i]);
pCur->apArgs[i] = NULL;
}
}

static int vectorVtabConnect(
sqlite3 *db,
void *pAux,
Expand Down Expand Up @@ -220110,6 +220125,7 @@ static int vectorVtabClose(sqlite3_vtab_cursor *cur){
vectorVtab_cursor *pCur = (vectorVtab_cursor*)cur;
vectorVtab *pVTab = (vectorVtab *)cur->pVtab;
vectorOutRowsFree(pVTab->db, &pCur->rows);
vectorVtabFreeArgs(pCur);
sqlite3_free(pCur);
return SQLITE_OK;
}
Expand All @@ -220131,6 +220147,12 @@ static int vectorVtabColumn(
int iCol /* Which column to return */
){
vectorVtab_cursor *pCur = (vectorVtab_cursor*)cur;
if( iCol < VECTOR_COLUMN_OFFSET ){
if( pCur->apArgs[iCol] != NULL ){
sqlite3_result_value(context, pCur->apArgs[iCol]);
}
return SQLITE_OK;
}
vectorOutRowsGet(context, &pCur->rows, pCur->iRow, iCol - VECTOR_COLUMN_OFFSET);
return SQLITE_OK;
}
Expand All @@ -220153,9 +220175,18 @@ static int vectorVtabFilter(
){
vectorVtab_cursor *pCur = (vectorVtab_cursor *)pVtabCursor;
vectorVtab *pVTab = (vectorVtab *)pVtabCursor->pVtab;
int i;
pCur->rows.aIntValues = NULL;
pCur->rows.ppValues = NULL;

vectorVtabFreeArgs(pCur);
for(i = 0; i < argc && i < VECTOR_COLUMN_OFFSET; i++){
pCur->apArgs[i] = sqlite3_value_dup(argv[i]);
if( pCur->apArgs[i] == NULL ){
return SQLITE_NOMEM_BKPT;
}
}

if( vectorIndexSearch(pVTab->db, argc, argv, &pCur->rows, &pCur->nReads, &pCur->nWrites, &pVTab->base.zErrMsg) != 0 ){
return SQLITE_ERROR;
}
Expand Down
33 changes: 32 additions & 1 deletion libsql-ffi/bundled/src/sqlite3.c
Original file line number Diff line number Diff line change
Expand Up @@ -217469,6 +217469,11 @@ int diskAnnSearch(
*pzErrMsg = sqlite3_mprintf("vector index(search): vector type differs from column type: %d != %d", pVector->type, pIndex->nNodeVectorType);
return SQLITE_ERROR;
}
if( k == 0 ){
pRows->nRows = 0;
pRows->nCols = pKey->nKeyColumns;
return SQLITE_OK;
}

rc = diskAnnSelectRandomShadowRow(pIndex, &nStartRowid);
if( rc == SQLITE_DONE ){
Expand All @@ -217482,8 +217487,9 @@ int diskAnnSearch(
}
rc = diskAnnSearchCtxInit(pIndex, &ctx, pVector, pIndex->searchL, k, DISKANN_BLOB_READONLY);
if( rc != SQLITE_OK ){
// on failure the init has already freed what it allocated, so ctx must not be deinitialized
*pzErrMsg = sqlite3_mprintf("vector index(search): failed to initialize search context");
goto out;
return rc;
}
rc = diskAnnSearchInternal(pIndex, &ctx, nStartRowid, pzErrMsg);
if( rc != SQLITE_OK ){
Expand Down Expand Up @@ -220050,6 +220056,7 @@ struct vectorVtab_cursor {
int nWrites; /* Number of row written to the storage backing virtual table */
VectorOutRows rows;
int iRow;
sqlite3_value *apArgs[3]; /* Arguments of the last xFilter, returned as the hidden columns */
};

/* Column numbers */
Expand All @@ -220058,6 +220065,14 @@ struct vectorVtab_cursor {
#define VECTOR_COLUMN_K 2
#define VECTOR_COLUMN_OFFSET 3

static void vectorVtabFreeArgs(vectorVtab_cursor *pCur){
int i;
for(i = 0; i < VECTOR_COLUMN_OFFSET; i++){
sqlite3_value_free(pCur->apArgs[i]);
pCur->apArgs[i] = NULL;
}
}

static int vectorVtabConnect(
sqlite3 *db,
void *pAux,
Expand Down Expand Up @@ -220110,6 +220125,7 @@ static int vectorVtabClose(sqlite3_vtab_cursor *cur){
vectorVtab_cursor *pCur = (vectorVtab_cursor*)cur;
vectorVtab *pVTab = (vectorVtab *)cur->pVtab;
vectorOutRowsFree(pVTab->db, &pCur->rows);
vectorVtabFreeArgs(pCur);
sqlite3_free(pCur);
return SQLITE_OK;
}
Expand All @@ -220131,6 +220147,12 @@ static int vectorVtabColumn(
int iCol /* Which column to return */
){
vectorVtab_cursor *pCur = (vectorVtab_cursor*)cur;
if( iCol < VECTOR_COLUMN_OFFSET ){
if( pCur->apArgs[iCol] != NULL ){
sqlite3_result_value(context, pCur->apArgs[iCol]);
}
return SQLITE_OK;
}
vectorOutRowsGet(context, &pCur->rows, pCur->iRow, iCol - VECTOR_COLUMN_OFFSET);
return SQLITE_OK;
}
Expand All @@ -220153,9 +220175,18 @@ static int vectorVtabFilter(
){
vectorVtab_cursor *pCur = (vectorVtab_cursor *)pVtabCursor;
vectorVtab *pVTab = (vectorVtab *)pVtabCursor->pVtab;
int i;
pCur->rows.aIntValues = NULL;
pCur->rows.ppValues = NULL;

vectorVtabFreeArgs(pCur);
for(i = 0; i < argc && i < VECTOR_COLUMN_OFFSET; i++){
pCur->apArgs[i] = sqlite3_value_dup(argv[i]);
if( pCur->apArgs[i] == NULL ){
return SQLITE_NOMEM_BKPT;
}
}

if( vectorIndexSearch(pVTab->db, argc, argv, &pCur->rows, &pCur->nReads, &pCur->nWrites, &pVTab->base.zErrMsg) != 0 ){
return SQLITE_ERROR;
}
Expand Down
8 changes: 7 additions & 1 deletion libsql-sqlite3/src/vectordiskann.c
Original file line number Diff line number Diff line change
Expand Up @@ -1478,6 +1478,11 @@ int diskAnnSearch(
*pzErrMsg = sqlite3_mprintf("vector index(search): vector type differs from column type: %d != %d", pVector->type, pIndex->nNodeVectorType);
return SQLITE_ERROR;
}
if( k == 0 ){
pRows->nRows = 0;
pRows->nCols = pKey->nKeyColumns;
return SQLITE_OK;
}

rc = diskAnnSelectRandomShadowRow(pIndex, &nStartRowid);
if( rc == SQLITE_DONE ){
Expand All @@ -1491,8 +1496,9 @@ int diskAnnSearch(
}
rc = diskAnnSearchCtxInit(pIndex, &ctx, pVector, pIndex->searchL, k, DISKANN_BLOB_READONLY);
if( rc != SQLITE_OK ){
// on failure the init has already freed what it allocated, so ctx must not be deinitialized
*pzErrMsg = sqlite3_mprintf("vector index(search): failed to initialize search context");
goto out;
return rc;
}
rc = diskAnnSearchInternal(pIndex, &ctx, nStartRowid, pzErrMsg);
if( rc != SQLITE_OK ){
Expand Down
25 changes: 25 additions & 0 deletions libsql-sqlite3/src/vectorvtab.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ struct vectorVtab_cursor {
int nWrites; /* Number of row written to the storage backing virtual table */
VectorOutRows rows;
int iRow;
sqlite3_value *apArgs[3]; /* Arguments of the last xFilter, returned as the hidden columns */
};

/* Column numbers */
Expand All @@ -51,6 +52,14 @@ struct vectorVtab_cursor {
#define VECTOR_COLUMN_K 2
#define VECTOR_COLUMN_OFFSET 3

static void vectorVtabFreeArgs(vectorVtab_cursor *pCur){
int i;
for(i = 0; i < VECTOR_COLUMN_OFFSET; i++){
sqlite3_value_free(pCur->apArgs[i]);
pCur->apArgs[i] = NULL;
}
}

static int vectorVtabConnect(
sqlite3 *db,
void *pAux,
Expand Down Expand Up @@ -103,6 +112,7 @@ static int vectorVtabClose(sqlite3_vtab_cursor *cur){
vectorVtab_cursor *pCur = (vectorVtab_cursor*)cur;
vectorVtab *pVTab = (vectorVtab *)cur->pVtab;
vectorOutRowsFree(pVTab->db, &pCur->rows);
vectorVtabFreeArgs(pCur);
sqlite3_free(pCur);
return SQLITE_OK;
}
Expand All @@ -124,6 +134,12 @@ static int vectorVtabColumn(
int iCol /* Which column to return */
){
vectorVtab_cursor *pCur = (vectorVtab_cursor*)cur;
if( iCol < VECTOR_COLUMN_OFFSET ){
if( pCur->apArgs[iCol] != NULL ){
sqlite3_result_value(context, pCur->apArgs[iCol]);
}
return SQLITE_OK;
}
vectorOutRowsGet(context, &pCur->rows, pCur->iRow, iCol - VECTOR_COLUMN_OFFSET);
return SQLITE_OK;
}
Expand All @@ -146,9 +162,18 @@ static int vectorVtabFilter(
){
vectorVtab_cursor *pCur = (vectorVtab_cursor *)pVtabCursor;
vectorVtab *pVTab = (vectorVtab *)pVtabCursor->pVtab;
int i;
pCur->rows.aIntValues = NULL;
pCur->rows.ppValues = NULL;

vectorVtabFreeArgs(pCur);
for(i = 0; i < argc && i < VECTOR_COLUMN_OFFSET; i++){
pCur->apArgs[i] = sqlite3_value_dup(argv[i]);
if( pCur->apArgs[i] == NULL ){
return SQLITE_NOMEM_BKPT;
}
}

if( vectorIndexSearch(pVTab->db, argc, argv, &pCur->rows, &pCur->nReads, &pCur->nWrites, &pVTab->base.zErrMsg) != 0 ){
return SQLITE_ERROR;
}
Expand Down
16 changes: 16 additions & 0 deletions libsql-sqlite3/test/libsql_vector_index.test
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,22 @@ do_execsql_test vector-update {
SELECT rowid FROM vector_top_k('t_update_idx', vector('[1,1,1]'), 2);
} {20 10}

do_execsql_test vector-top-k-zero {
CREATE TABLE t_top_k_zero( id INTEGER PRIMARY KEY, v FLOAT32(3) );
CREATE INDEX t_top_k_zero_idx ON t_top_k_zero( libsql_vector_idx(v) );
INSERT INTO t_top_k_zero VALUES (1, vector('[1,1,1]'));
INSERT INTO t_top_k_zero VALUES (2, vector('[-1,-1,-1]'));
SELECT count(*) FROM vector_top_k('t_top_k_zero_idx', vector('[1,1,1]'), 0);
} {0}

do_execsql_test vector-hidden-columns {
CREATE TABLE t_hidden( id INTEGER PRIMARY KEY, v FLOAT32(3) );
CREATE INDEX t_hidden_idx ON t_hidden( libsql_vector_idx(v) );
INSERT INTO t_hidden VALUES (10, vector('[1,1,1]'));
INSERT INTO t_hidden VALUES (20, vector('[-1,-1,-1]'));
SELECT idx, vector, k, id FROM vector_top_k('t_hidden_idx', '[1,1,1]', 2);
} {t_hidden_idx {[1,1,1]} 2 10 t_hidden_idx {[1,1,1]} 2 20}

do_execsql_test vector-simple {
CREATE TABLE t_simple( v FLOAT32(3));
CREATE INDEX t_simple_idx ON t_simple( libsql_vector_idx(v) );
Expand Down
Loading