Skip to content

Commit ac6a89b

Browse files
committed
small fixes
1 parent 93caa27 commit ac6a89b

7 files changed

Lines changed: 30 additions & 52 deletions

File tree

libsql-sqlite3/src/vector.c

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ size_t vectorDataSize(VectorType type, VectorDims dims){
4242
case VECTOR_TYPE_FLOAT64:
4343
return dims * sizeof(double);
4444
case VECTOR_TYPE_1BIT:
45+
assert( dims > 0 );
4546
return (dims + 7) / 8;
4647
default:
4748
assert(0);
@@ -252,7 +253,7 @@ static int vectorParseSqliteText(
252253
return -1;
253254
}
254255

255-
int vectorParseSqliteBlob(
256+
int vectorParseSqliteBlobWithType(
256257
sqlite3_value *arg,
257258
Vector *pVector,
258259
char **pzErrMsg
@@ -362,14 +363,14 @@ int detectVectorParameters(sqlite3_value *arg, int typeHint, int *pType, int *pD
362363
}
363364
}
364365

365-
int vectorParse(
366+
int vectorParseWithType(
366367
sqlite3_value *arg,
367368
Vector *pVector,
368369
char **pzErrMsg
369370
){
370371
switch( sqlite3_value_type(arg) ){
371372
case SQLITE_BLOB:
372-
return vectorParseSqliteBlob(arg, pVector, pzErrMsg);
373+
return vectorParseSqliteBlobWithType(arg, pVector, pzErrMsg);
373374
case SQLITE_TEXT:
374375
return vectorParseSqliteText(arg, pVector, pzErrMsg);
375376
default:
@@ -531,7 +532,7 @@ static void vectorFuncHintedType(
531532
if( pVector==NULL ){
532533
return;
533534
}
534-
if( vectorParse(argv[0], pVector, &pzErrMsg) != 0 ){
535+
if( vectorParseWithType(argv[0], pVector, &pzErrMsg) != 0 ){
535536
sqlite3_result_error(context, pzErrMsg, -1);
536537
sqlite3_free(pzErrMsg);
537538
goto out_free_vec;
@@ -581,7 +582,7 @@ static void vectorExtractFunc(
581582
if( pVector==NULL ){
582583
return;
583584
}
584-
if( vectorParse(argv[0], pVector, &pzErrMsg)<0 ){
585+
if( vectorParseWithType(argv[0], pVector, &pzErrMsg)<0 ){
585586
sqlite3_result_error(context, pzErrMsg, -1);
586587
sqlite3_free(pzErrMsg);
587588
goto out_free;
@@ -636,12 +637,12 @@ static void vectorDistanceCosFunc(
636637
if( pVector2==NULL ){
637638
goto out_free;
638639
}
639-
if( vectorParse(argv[0], pVector1, &pzErrMsg)<0 ){
640+
if( vectorParseWithType(argv[0], pVector1, &pzErrMsg)<0 ){
640641
sqlite3_result_error(context, pzErrMsg, -1);
641642
sqlite3_free(pzErrMsg);
642643
goto out_free;
643644
}
644-
if( vectorParse(argv[1], pVector2, &pzErrMsg)<0 ){
645+
if( vectorParseWithType(argv[1], pVector2, &pzErrMsg)<0 ){
645646
sqlite3_result_error(context, pzErrMsg, -1);
646647
sqlite3_free(pzErrMsg);
647648
goto out_free;

libsql-sqlite3/src/vector1bit.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,11 @@ void vector1BitDump(const Vector *pVec){
4141

4242
assert( pVec->type == VECTOR_TYPE_1BIT );
4343

44+
printf("f1bit: [");
4445
for(i = 0; i < pVec->dims; i++){
45-
printf("%d ", ((elems[i / 8] >> (i & 7)) & 1) ? +1 : -1);
46+
printf("%s%d", i == 0 ? "" : ", ", ((elems[i / 8] >> (i & 7)) & 1) ? +1 : -1);
4647
}
47-
printf("\n");
48+
printf("]\n");
4849
}
4950

5051
/**************************************************************************

libsql-sqlite3/src/vectorIndex.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ int vectorInRowAlloc(sqlite3 *db, const UnpackedRecord *pRecord, VectorInRow *pV
261261
vectorInitFromBlob(pVectorInRow->pVector, sqlite3_value_blob(pVectorValue), sqlite3_value_bytes(pVectorValue));
262262
} else if( sqlite3_value_type(pVectorValue) == SQLITE_TEXT ){
263263
// users can put strings (e.g. '[1,2,3]') in the table and we should process them correctly
264-
if( vectorParse(pVectorValue, pVectorInRow->pVector, pzErrMsg) != 0 ){
264+
if( vectorParseWithType(pVectorValue, pVectorInRow->pVector, pzErrMsg) != 0 ){
265265
rc = SQLITE_ERROR;
266266
goto out;
267267
}
@@ -982,7 +982,7 @@ int vectorIndexSearch(
982982
rc = SQLITE_NOMEM_BKPT;
983983
goto out;
984984
}
985-
if( vectorParse(argv[1], pVector, pzErrMsg) != 0 ){
985+
if( vectorParseWithType(argv[1], pVector, pzErrMsg) != 0 ){
986986
rc = SQLITE_ERROR;
987987
goto out;
988988
}

libsql-sqlite3/src/vectorInt.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ struct Vector {
4242
size_t vectorDataSize(VectorType, VectorDims);
4343
Vector *vectorAlloc(VectorType, VectorDims);
4444
void vectorFree(Vector *v);
45-
int vectorParse(sqlite3_value *, Vector *, char **);
45+
int vectorParseWithType(sqlite3_value *, Vector *, char **);
4646
void vectorInit(Vector *, VectorType, VectorDims, void *);
4747

4848
/*
@@ -97,7 +97,7 @@ void vectorSerializeWithType(sqlite3_context *, const Vector *);
9797
/*
9898
* Parses Vector content from the blob; vector type and dimensions must be filled already
9999
*/
100-
int vectorParseSqliteBlob (sqlite3_value *, Vector *, char **);
100+
int vectorParseSqliteBlobWithType(sqlite3_value *, Vector *, char **);
101101

102102
void vectorF32DeserializeFromBlob(Vector *, const unsigned char *, size_t);
103103
void vectorF64DeserializeFromBlob(Vector *, const unsigned char *, size_t);

libsql-sqlite3/src/vectordiskann.c

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@
4747
** diskAnnInsert() Insert single new(!) vector in an opened index
4848
** diskAnnDelete() Delete row by key from an opened index
4949
*/
50-
#include "vectorInt.h"
5150
#ifndef SQLITE_OMIT_VECTOR
5251

5352
#include "math.h"
@@ -84,7 +83,8 @@ typedef struct VectorPair VectorPair;
8483
typedef struct DiskAnnSearchCtx DiskAnnSearchCtx;
8584
typedef struct DiskAnnNode DiskAnnNode;
8685

87-
// VectorPair represents single vector where pNode is an exact representation and pEdge - compressed representation (always NULL if pNodeType == pEdgeType)
86+
// VectorPair represents single vector where pNode is an exact representation and pEdge - compressed representation
87+
// (pEdge pointer always equals to pNode if pNodeType == pEdgeType)
8888
struct VectorPair {
8989
int nodeType;
9090
int edgeType;
@@ -966,15 +966,13 @@ static int diskAnnSearchCtxInit(const DiskAnnIndex *pIndex, DiskAnnSearchCtx *pC
966966
pCtx->nUnvisited = 0;
967967
pCtx->blobMode = blobMode;
968968
if( initVectorPair(pIndex->nNodeVectorType, pIndex->nEdgeVectorType, pIndex->nVectorDims, &pCtx->query) != 0 ){
969-
goto out_oom;
969+
return SQLITE_NOMEM_BKPT;
970970
}
971971
loadVectorPair(&pCtx->query, pQuery);
972972

973-
if( pCtx->aDistances == NULL || pCtx->aCandidates == NULL || pCtx->aTopDistances == NULL || pCtx->aTopCandidates == NULL ){
974-
goto out_oom;
973+
if( pCtx->aDistances != NULL && pCtx->aCandidates != NULL && pCtx->aTopDistances != NULL && pCtx->aTopCandidates != NULL ){
974+
return SQLITE_OK;
975975
}
976-
return SQLITE_OK;
977-
out_oom:
978976
if( pCtx->aDistances != NULL ){
979977
sqlite3_free(pCtx->aDistances);
980978
}
@@ -987,6 +985,7 @@ static int diskAnnSearchCtxInit(const DiskAnnIndex *pIndex, DiskAnnSearchCtx *pC
987985
if( pCtx->aTopCandidates != NULL ){
988986
sqlite3_free(pCtx->aTopCandidates);
989987
}
988+
deinitVectorPair(&pCtx->query);
990989
return SQLITE_NOMEM_BKPT;
991990
}
992991

libsql-sqlite3/src/vectorfloat32.c

Lines changed: 3 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,11 @@ void vectorF32Dump(const Vector *pVec){
4141

4242
assert( pVec->type == VECTOR_TYPE_FLOAT32 );
4343

44+
printf("f32: [");
4445
for(i = 0; i < pVec->dims; i++){
45-
printf("%f ", elems[i]);
46+
printf("%s%f", i == 0 ? "" : ", ", elems[i]);
4647
}
47-
printf("\n");
48+
printf("]\n");
4849
}
4950

5051
/**************************************************************************
@@ -94,34 +95,6 @@ size_t vectorF32SerializeToBlob(
9495
return sizeof(float) * pVector->dims;
9596
}
9697

97-
void vectorF32Serialize(
98-
sqlite3_context *context,
99-
const Vector *pVector
100-
){
101-
float *elems = pVector->data;
102-
unsigned char *pBlob;
103-
size_t nBlobSize;
104-
105-
assert( pVector->type == VECTOR_TYPE_FLOAT32 );
106-
assert( pVector->dims <= MAX_VECTOR_SZ );
107-
108-
nBlobSize = vectorDataSize(pVector->type, pVector->dims);
109-
110-
if( nBlobSize == 0 ){
111-
sqlite3_result_zeroblob(context, 0);
112-
return;
113-
}
114-
115-
pBlob = sqlite3_malloc64(nBlobSize);
116-
if( pBlob == NULL ){
117-
sqlite3_result_error_nomem(context);
118-
return;
119-
}
120-
121-
vectorF32SerializeToBlob(pVector, pBlob, nBlobSize);
122-
sqlite3_result_blob(context, (char*)pBlob, nBlobSize, sqlite3_free);
123-
}
124-
12598
#define SINGLE_FLOAT_CHAR_LIMIT 32
12699
void vectorF32MarshalToText(
127100
sqlite3_context *context,

libsql-sqlite3/src/vectorfloat64.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,14 @@
3838
void vectorF64Dump(const Vector *pVec){
3939
double *elems = pVec->data;
4040
unsigned i;
41+
42+
assert( pVec->type == VECTOR_TYPE_FLOAT64 );
43+
44+
printf("f64: [");
4145
for(i = 0; i < pVec->dims; i++){
42-
printf("%lf ", elems[i]);
46+
printf("%s%lf", i == 0 ? "" : ", ", elems[i]);
4347
}
44-
printf("\n");
48+
printf("]\n");
4549
}
4650

4751
/**************************************************************************

0 commit comments

Comments
 (0)