Skip to content

Commit 7cafb80

Browse files
committed
use float1bit instead of 1bit everywhere in the code in index settings
1 parent db15413 commit 7cafb80

6 files changed

Lines changed: 44 additions & 34 deletions

File tree

libsql-sqlite3/src/vector.c

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ size_t vectorDataSize(VectorType type, VectorDims dims){
4141
return dims * sizeof(float);
4242
case VECTOR_TYPE_FLOAT64:
4343
return dims * sizeof(double);
44-
case VECTOR_TYPE_1BIT:
44+
case VECTOR_TYPE_FLOAT1BIT:
4545
return (dims + 7) / 8;
4646
default:
4747
assert(0);
@@ -114,7 +114,7 @@ float vectorDistanceCos(const Vector *pVector1, const Vector *pVector2){
114114
return vectorF32DistanceCos(pVector1, pVector2);
115115
case VECTOR_TYPE_FLOAT64:
116116
return vectorF64DistanceCos(pVector1, pVector2);
117-
case VECTOR_TYPE_1BIT:
117+
case VECTOR_TYPE_FLOAT1BIT:
118118
return vector1BitDistanceHamming(pVector1, pVector2);
119119
default:
120120
assert(0);
@@ -278,7 +278,7 @@ static int vectorParseMeta(const unsigned char *pBlob, size_t nBlobSize, int *pT
278278
}
279279
*pDims = nBlobSize / sizeof(double);
280280
*pDataSize = nBlobSize;
281-
}else if( *pType == VECTOR_TYPE_1BIT ){
281+
}else if( *pType == VECTOR_TYPE_FLOAT1BIT ){
282282
if( nBlobSize == 0 || nBlobSize % 2 != 0 ){
283283
*pzErrMsg = sqlite3_mprintf("vector: 1bit vector blob length must be divisible by 2 and not be empty (excluding 'type'-byte): length=%d", nBlobSize);
284284
return SQLITE_ERROR;
@@ -328,7 +328,7 @@ int vectorParseSqliteBlobWithType(
328328
case VECTOR_TYPE_FLOAT64:
329329
vectorF64DeserializeFromBlob(pVector, pBlob, nDataSize);
330330
return 0;
331-
case VECTOR_TYPE_1BIT:
331+
case VECTOR_TYPE_FLOAT1BIT:
332332
vector1BitDeserializeFromBlob(pVector, pBlob, nDataSize);
333333
return 0;
334334
default:
@@ -426,7 +426,7 @@ void vectorDump(const Vector *pVector){
426426
case VECTOR_TYPE_FLOAT64:
427427
vectorF64Dump(pVector);
428428
break;
429-
case VECTOR_TYPE_1BIT:
429+
case VECTOR_TYPE_FLOAT1BIT:
430430
vector1BitDump(pVector);
431431
break;
432432
default:
@@ -457,7 +457,7 @@ static int vectorMetaSize(VectorType type, VectorDims dims){
457457
return 0;
458458
}else if( type == VECTOR_TYPE_FLOAT64 ){
459459
return 1;
460-
}else if( type == VECTOR_TYPE_1BIT ){
460+
}else if( type == VECTOR_TYPE_FLOAT1BIT ){
461461
nDataSize = vectorDataSize(type, dims);
462462
nMetaSize++; // one byte which specify amount of leftover bits
463463
if( nDataSize % 2 == 0 ){
@@ -477,10 +477,10 @@ static void vectorSerializeMeta(const Vector *pVector, size_t nDataSize, unsigne
477477
assert( nDataSize % 2 == 0 );
478478
assert( nBlobSize == nDataSize + 1 );
479479
pBlob[nBlobSize - 1] = VECTOR_TYPE_FLOAT64;
480-
}else if( pVector->type == VECTOR_TYPE_1BIT ){
480+
}else if( pVector->type == VECTOR_TYPE_FLOAT1BIT ){
481481
assert( nBlobSize % 2 == 1 );
482482
assert( nBlobSize >= 3 );
483-
pBlob[nBlobSize - 1] = VECTOR_TYPE_1BIT;
483+
pBlob[nBlobSize - 1] = VECTOR_TYPE_FLOAT1BIT;
484484
pBlob[nBlobSize - 2] = 8 * (nBlobSize - 1) - pVector->dims;
485485
}else{
486486
assert( 0 );
@@ -517,7 +517,7 @@ void vectorSerializeWithMeta(
517517
case VECTOR_TYPE_FLOAT64:
518518
vectorF64SerializeToBlob(pVector, pBlob, nDataSize);
519519
break;
520-
case VECTOR_TYPE_1BIT:
520+
case VECTOR_TYPE_FLOAT1BIT:
521521
vector1BitSerializeToBlob(pVector, pBlob, nDataSize);
522522
break;
523523
default:
@@ -533,7 +533,7 @@ size_t vectorSerializeToBlob(const Vector *pVector, unsigned char *pBlob, size_t
533533
return vectorF32SerializeToBlob(pVector, pBlob, nBlobSize);
534534
case VECTOR_TYPE_FLOAT64:
535535
return vectorF64SerializeToBlob(pVector, pBlob, nBlobSize);
536-
case VECTOR_TYPE_1BIT:
536+
case VECTOR_TYPE_FLOAT1BIT:
537537
return vector1BitSerializeToBlob(pVector, pBlob, nBlobSize);
538538
default:
539539
assert(0);
@@ -562,7 +562,7 @@ static void vectorConvertFromF32(const Vector *pFrom, Vector *pTo){
562562
for(i = 0; i < pFrom->dims; i++){
563563
dstF64[i] = src[i];
564564
}
565-
}else if( pTo->type == VECTOR_TYPE_1BIT ){
565+
}else if( pTo->type == VECTOR_TYPE_FLOAT1BIT ){
566566
dst1Bit = pTo->data;
567567
for(i = 0; i < pFrom->dims; i += 8){
568568
dst1Bit[i / 8] = 0;
@@ -594,7 +594,7 @@ static void vectorConvertFromF64(const Vector *pFrom, Vector *pTo){
594594
for(i = 0; i < pFrom->dims; i++){
595595
dstF32[i] = src[i];
596596
}
597-
}else if( pTo->type == VECTOR_TYPE_1BIT ){
597+
}else if( pTo->type == VECTOR_TYPE_FLOAT1BIT ){
598598
dst1Bit = pTo->data;
599599
for(i = 0; i < pFrom->dims; i += 8){
600600
dst1Bit[i / 8] = 0;
@@ -618,7 +618,7 @@ static void vectorConvertFrom1Bit(const Vector *pFrom, Vector *pTo){
618618

619619
assert( pFrom->dims == pTo->dims );
620620
assert( pFrom->type != pTo->type );
621-
assert( pFrom->type == VECTOR_TYPE_1BIT );
621+
assert( pFrom->type == VECTOR_TYPE_FLOAT1BIT );
622622

623623
src = pFrom->data;
624624
if( pTo->type == VECTOR_TYPE_FLOAT32 ){
@@ -656,7 +656,7 @@ void vectorConvert(const Vector *pFrom, Vector *pTo){
656656
vectorConvertFromF32(pFrom, pTo);
657657
}else if( pFrom->type == VECTOR_TYPE_FLOAT64 ){
658658
vectorConvertFromF64(pFrom, pTo);
659-
}else if( pFrom->type == VECTOR_TYPE_1BIT ){
659+
}else if( pFrom->type == VECTOR_TYPE_FLOAT1BIT ){
660660
vectorConvertFrom1Bit(pFrom, pTo);
661661
}else{
662662
assert( 0 );
@@ -739,7 +739,7 @@ static void vector1BitFunc(
739739
int argc,
740740
sqlite3_value **argv
741741
){
742-
vectorFuncHintedType(context, argc, argv, VECTOR_TYPE_1BIT);
742+
vectorFuncHintedType(context, argc, argv, VECTOR_TYPE_FLOAT1BIT);
743743
}
744744

745745
/*

libsql-sqlite3/src/vector1bit.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ void vector1BitDump(const Vector *pVec){
3939
u8 *elems = pVec->data;
4040
unsigned i;
4141

42-
assert( pVec->type == VECTOR_TYPE_1BIT );
42+
assert( pVec->type == VECTOR_TYPE_FLOAT1BIT );
4343

4444
printf("f1bit: [");
4545
for(i = 0; i < pVec->dims; i++){
@@ -61,7 +61,7 @@ size_t vector1BitSerializeToBlob(
6161
u8 *pPtr = pBlob;
6262
unsigned i;
6363

64-
assert( pVector->type == VECTOR_TYPE_1BIT );
64+
assert( pVector->type == VECTOR_TYPE_FLOAT1BIT );
6565
assert( pVector->dims <= MAX_VECTOR_SZ );
6666
assert( nBlobSize >= (pVector->dims + 7) / 8 );
6767

@@ -108,8 +108,8 @@ int vector1BitDistanceHamming(const Vector *v1, const Vector *v2){
108108
int i, len8, len32, offset8;
109109

110110
assert( v1->dims == v2->dims );
111-
assert( v1->type == VECTOR_TYPE_1BIT );
112-
assert( v2->type == VECTOR_TYPE_1BIT );
111+
assert( v1->type == VECTOR_TYPE_FLOAT1BIT );
112+
assert( v2->type == VECTOR_TYPE_FLOAT1BIT );
113113

114114
len8 = (v1->dims + 7) / 8;
115115
len32 = v1->dims / 32;
@@ -131,7 +131,7 @@ void vector1BitDeserializeFromBlob(
131131
){
132132
u8 *elems = pVector->data;
133133

134-
assert( pVector->type == VECTOR_TYPE_1BIT );
134+
assert( pVector->type == VECTOR_TYPE_FLOAT1BIT );
135135
assert( 0 <= pVector->dims && pVector->dims <= MAX_VECTOR_SZ );
136136
assert( nBlobSize >= (pVector->dims + 7) / 8 );
137137

libsql-sqlite3/src/vectorIndex.c

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -382,8 +382,8 @@ static struct VectorColumnType VECTOR_COLUMN_TYPES[] = {
382382
{ "F32_BLOB", VECTOR_TYPE_FLOAT32 },
383383
{ "FLOAT64", VECTOR_TYPE_FLOAT64 },
384384
{ "F64_BLOB", VECTOR_TYPE_FLOAT64 },
385-
{ "FLOAT1BIT", VECTOR_TYPE_1BIT },
386-
{ "F1BIT_BLOB", VECTOR_TYPE_1BIT },
385+
{ "FLOAT1BIT", VECTOR_TYPE_FLOAT1BIT },
386+
{ "F1BIT_BLOB", VECTOR_TYPE_FLOAT1BIT },
387387
};
388388

389389
/*
@@ -399,10 +399,11 @@ struct VectorParamName {
399399
};
400400

401401
static struct VectorParamName VECTOR_PARAM_NAMES[] = {
402-
{ "type", VECTOR_INDEX_TYPE_PARAM_ID, 0, "diskann", VECTOR_INDEX_TYPE_DISKANN },
403-
{ "metric", VECTOR_METRIC_TYPE_PARAM_ID, 0, "cosine", VECTOR_METRIC_TYPE_COS },
404-
{ "metric", VECTOR_METRIC_TYPE_PARAM_ID, 0, "l2", VECTOR_METRIC_TYPE_L2 },
405-
{ "compress_neighbors", VECTOR_COMPRESS_NEIGHBORS_PARAM_ID, 0, "1bit", VECTOR_TYPE_1BIT },
402+
{ "type", VECTOR_INDEX_TYPE_PARAM_ID, 0, "diskann", VECTOR_INDEX_TYPE_DISKANN },
403+
{ "metric", VECTOR_METRIC_TYPE_PARAM_ID, 0, "cosine", VECTOR_METRIC_TYPE_COS },
404+
{ "metric", VECTOR_METRIC_TYPE_PARAM_ID, 0, "l2", VECTOR_METRIC_TYPE_L2 },
405+
{ "compress_neighbors", VECTOR_COMPRESS_NEIGHBORS_PARAM_ID, 0, "float1bit", VECTOR_TYPE_FLOAT1BIT },
406+
{ "compress_neighbors", VECTOR_COMPRESS_NEIGHBORS_PARAM_ID, 0, "float32", VECTOR_TYPE_FLOAT32 },
406407
{ "alpha", VECTOR_PRUNING_ALPHA_PARAM_ID, 2, 0, 0 },
407408
{ "search_l", VECTOR_SEARCH_L_PARAM_ID, 1, 0, 0 },
408409
{ "insert_l", VECTOR_INSERT_L_PARAM_ID, 1, 0, 0 },
@@ -962,7 +963,7 @@ int vectorIndexSearch(
962963
rc = SQLITE_ERROR;
963964
goto out;
964965
}
965-
assert( type == VECTOR_TYPE_FLOAT32 || type == VECTOR_TYPE_FLOAT64 || type == VECTOR_TYPE_1BIT );
966+
assert( type == VECTOR_TYPE_FLOAT32 || type == VECTOR_TYPE_FLOAT64 || type == VECTOR_TYPE_FLOAT1BIT );
966967

967968
pVector = vectorAlloc(type, dims);
968969
if( pVector == NULL ){

libsql-sqlite3/src/vectorInt.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,9 @@ typedef u32 VectorDims;
4242
/*
4343
* Enumerate of supported vector types (0 omitted intentionally as we can use zero as "undefined" value)
4444
*/
45-
#define VECTOR_TYPE_FLOAT32 1
46-
#define VECTOR_TYPE_FLOAT64 2
47-
#define VECTOR_TYPE_1BIT 3
45+
#define VECTOR_TYPE_FLOAT32 1
46+
#define VECTOR_TYPE_FLOAT64 2
47+
#define VECTOR_TYPE_FLOAT1BIT 3
4848

4949
#define VECTOR_FLAGS_STATIC 1
5050

libsql-sqlite3/src/vectordiskann.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -505,7 +505,7 @@ int diskAnnCreateIndex(
505505
}
506506
}
507507
neighbours = vectorIdxParamsGetU64(pParams, VECTOR_COMPRESS_NEIGHBORS_PARAM_ID);
508-
if( neighbours == VECTOR_TYPE_1BIT && metric != VECTOR_METRIC_TYPE_COS ){
508+
if( neighbours == VECTOR_TYPE_FLOAT1BIT && metric != VECTOR_METRIC_TYPE_COS ){
509509
*pzErrMsg = "1-bit compression available only for cosine metric";
510510
return SQLITE_ERROR;
511511
}
@@ -1749,7 +1749,7 @@ int diskAnnOpenIndex(
17491749
if( compressNeighbours == 0 ){
17501750
pIndex->nEdgeVectorType = pIndex->nNodeVectorType;
17511751
pIndex->nEdgeVectorSize = pIndex->nNodeVectorSize;
1752-
}else if( compressNeighbours == VECTOR_TYPE_1BIT ){
1752+
}else if( compressNeighbours == VECTOR_TYPE_FLOAT1BIT ){
17531753
pIndex->nEdgeVectorType = compressNeighbours;
17541754
pIndex->nEdgeVectorSize = vectorDataSize(compressNeighbours, pIndex->nVectorDims);
17551755
}else{

libsql-sqlite3/test/libsql_vector_index.test

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ do_execsql_test vector-transaction {
275275

276276
do_execsql_test vector-1bit {
277277
CREATE TABLE t_1bit( v FLOAT32(3) );
278-
CREATE INDEX t_1bit_idx ON t_1bit( libsql_vector_idx(v, 'compress_neighbors=1bit') );
278+
CREATE INDEX t_1bit_idx ON t_1bit( libsql_vector_idx(v, 'compress_neighbors=float1bit') );
279279
INSERT INTO t_1bit VALUES (vector('[-1,-1,1]'));
280280
INSERT INTO t_1bit VALUES (vector('[-1,1,-1.5]'));
281281
INSERT INTO t_1bit VALUES (vector('[1,-1,-1]'));
@@ -285,7 +285,7 @@ do_execsql_test vector-1bit {
285285

286286
do_execsql_test vector-all-params {
287287
CREATE TABLE t_all_params ( emb FLOAT32(2) );
288-
CREATE INDEX t_all_params_idx ON t_all_params(libsql_vector_idx(emb, 'type=diskann', 'metric=cos', 'alpha=1.2', 'search_l=200', 'insert_l=70', 'max_neighbors=6', 'compress_neighbors=1bit'));
288+
CREATE INDEX t_all_params_idx ON t_all_params(libsql_vector_idx(emb, 'type=diskann', 'metric=cos', 'alpha=1.2', 'search_l=200', 'insert_l=70', 'max_neighbors=6', 'compress_neighbors=float1bit'));
289289
INSERT INTO t_all_params VALUES (vector('[1,2]')), (vector('[3,4]'));
290290
SELECT * FROM vector_top_k('t_all_params_idx', vector('[1,2]'), 2);
291291
} {1 2}
@@ -336,6 +336,15 @@ do_execsql_test vector-1bit-index {
336336
SELECT * FROM vector_top_k('t_1bit_table_idx', vector1bit('[10,-10,-20,20]'), 4);
337337
} {3 1 2}
338338

339+
do_execsql_test vector-f64-compress-f32 {
340+
CREATE TABLE t_f64_f32( v FLOAT64(4) );
341+
CREATE INDEX t_f64_f32_idx ON t_f64_f32( libsql_vector_idx(v, 'compress_neighbors=float32') );
342+
INSERT INTO t_f64_f32 VALUES ( vector64('[1,-1,1,-1]') );
343+
INSERT INTO t_f64_f32 VALUES ( vector64('[-1,1,1,-1]') );
344+
INSERT INTO t_f64_f32 VALUES ( vector64('[1,-1,-1,1]') );
345+
SELECT * FROM vector_top_k('t_f64_f32_idx', vector64('[10,-10,-20,20]'), 4);
346+
} {3 1 2}
347+
339348
proc error_messages {sql} {
340349
set ret ""
341350
catch {

0 commit comments

Comments
 (0)