From ae23ff317bd6cfefd80b60ec08614bf712704274 Mon Sep 17 00:00:00 2001 From: meiravgri Date: Wed, 11 Mar 2026 13:46:52 +0000 Subject: [PATCH 1/4] api for VecSimParams_GetQueryBlobSize --- src/VecSim/vec_sim.cpp | 12 ++++++++++++ src/VecSim/vec_sim.h | 13 +++++++++++++ tests/unit/test_common.cpp | 19 +++++++++++++++++++ 3 files changed, 44 insertions(+) diff --git a/src/VecSim/vec_sim.cpp b/src/VecSim/vec_sim.cpp index 1cc8ea8b0..ceb64502c 100644 --- a/src/VecSim/vec_sim.cpp +++ b/src/VecSim/vec_sim.cpp @@ -207,6 +207,18 @@ extern "C" void VecSim_Normalize(void *blob, size_t dim, VecSimType type) { } } +extern "C" size_t VecSimParams_GetQueryBlobSize(VecSimType type, size_t dim, VecSimMetric metric) { + // Assert all supported types are covered + assert(type == VecSimType_FLOAT32 || type == VecSimType_FLOAT64 || + type == VecSimType_BFLOAT16 || type == VecSimType_FLOAT16 || type == VecSimType_INT8 || + type == VecSimType_UINT8); + size_t blobSize = VecSimType_sizeof(type) * dim; + if (metric == VecSimMetric_Cosine && (type == VecSimType_INT8 || type == VecSimType_UINT8)) { + blobSize += sizeof(float); // For the norm + } + return blobSize; +} + extern "C" size_t VecSimIndex_IndexSize(VecSimIndex *index) { return index->indexSize(); } extern "C" VecSimResolveCode VecSimIndex_ResolveParams(VecSimIndex *index, VecSimRawParam *rparams, diff --git a/src/VecSim/vec_sim.h b/src/VecSim/vec_sim.h index 56110a900..4958a0a79 100644 --- a/src/VecSim/vec_sim.h +++ b/src/VecSim/vec_sim.h @@ -98,6 +98,19 @@ double VecSimIndex_GetDistanceFrom_Unsafe(VecSimIndex *index, size_t label, cons */ void VecSim_Normalize(void *blob, size_t dim, VecSimType type); +/** + * @brief Returns the required blob size for a query vector that will be normalized. + * + * For INT8/UINT8 vectors with Cosine metric, VecSim_Normalize appends the norm (a float) + * at the end of the blob, so the required size is larger than just dim * sizeof(type). + * + * @param type vector element type. + * @param dim vector dimension. + * @param metric distance metric. + * @return required blob size in bytes. + */ +size_t VecSimParams_GetQueryBlobSize(VecSimType type, size_t dim, VecSimMetric metric); + /** * @brief Return the number of vectors in the index. * @param index the index whose size is requested. diff --git a/tests/unit/test_common.cpp b/tests/unit/test_common.cpp index 082be6ee6..8d51fc868 100644 --- a/tests/unit/test_common.cpp +++ b/tests/unit/test_common.cpp @@ -883,6 +883,25 @@ TEST_P(CommonTypeMetricTests, TestInitialSizeEstimationHNSW) { this->test_initial_size_estimation(); } +TEST_P(CommonTypeMetricTests, TestGetQueryBlobSize) { + size_t dim = 4; + VecSimType type = std::get<0>(GetParam()); + VecSimMetric metric = std::get<1>(GetParam()); + + // Call the API function + size_t actual = VecSimParams_GetQueryBlobSize(type, dim, metric); + + // Calculate expected blob size + size_t expected = dim * VecSimType_sizeof(type); + if (metric == VecSimMetric_Cosine && (type == VecSimType_INT8 || type == VecSimType_UINT8)) { + expected += sizeof(float); // For the norm + } + + ASSERT_EQ(actual, expected); + // We don't need to create an index for this test, set to nullptr to avoid cleanup issues + this->index = nullptr; +} + class CommonTypeMetricTieredTests : public CommonTypeMetricTests { protected: virtual void TearDown() override {} From 227bb8a0f5672fd014e82a7ea057c9f674a8736b Mon Sep 17 00:00:00 2001 From: meiravgri Date: Wed, 11 Mar 2026 14:15:50 +0000 Subject: [PATCH 2/4] fix dtor --- tests/unit/test_common.cpp | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/tests/unit/test_common.cpp b/tests/unit/test_common.cpp index 8d51fc868..9f4e1a5c1 100644 --- a/tests/unit/test_common.cpp +++ b/tests/unit/test_common.cpp @@ -839,7 +839,11 @@ class CommonTypeMetricTests : public testing::TestWithParam void test_initial_size_estimation(); - virtual void TearDown() { VecSimIndex_Free(index); } + virtual void TearDown() { + if (index) { + VecSimIndex_Free(index); + } + } VecSimIndex *index; }; From 3b22c61bc4fc34d55e493f14699a5fc5743b1286 Mon Sep 17 00:00:00 2001 From: meiravgri Date: Wed, 11 Mar 2026 14:39:22 +0000 Subject: [PATCH 3/4] move index-> null to the test start --- tests/unit/test_common.cpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/tests/unit/test_common.cpp b/tests/unit/test_common.cpp index 9f4e1a5c1..c03b126aa 100644 --- a/tests/unit/test_common.cpp +++ b/tests/unit/test_common.cpp @@ -888,6 +888,9 @@ TEST_P(CommonTypeMetricTests, TestInitialSizeEstimationHNSW) { } TEST_P(CommonTypeMetricTests, TestGetQueryBlobSize) { + // We don't need to create an index for this test, set to nullptr to avoid cleanup issues + this->index = nullptr; + size_t dim = 4; VecSimType type = std::get<0>(GetParam()); VecSimMetric metric = std::get<1>(GetParam()); @@ -902,8 +905,7 @@ TEST_P(CommonTypeMetricTests, TestGetQueryBlobSize) { } ASSERT_EQ(actual, expected); - // We don't need to create an index for this test, set to nullptr to avoid cleanup issues - this->index = nullptr; + } class CommonTypeMetricTieredTests : public CommonTypeMetricTests { From e657913a52df3debc59d241d9881dbabe7714e91 Mon Sep 17 00:00:00 2001 From: meiravgri Date: Wed, 11 Mar 2026 15:30:20 +0000 Subject: [PATCH 4/4] format --- tests/unit/test_common.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/unit/test_common.cpp b/tests/unit/test_common.cpp index c03b126aa..5e4cb95ed 100644 --- a/tests/unit/test_common.cpp +++ b/tests/unit/test_common.cpp @@ -905,7 +905,6 @@ TEST_P(CommonTypeMetricTests, TestGetQueryBlobSize) { } ASSERT_EQ(actual, expected); - } class CommonTypeMetricTieredTests : public CommonTypeMetricTests {