This is an automated email from the ASF dual-hosted git repository.
JingsongLi pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/paimon.git
The following commit(s) were added to refs/heads/master by this push:
new fb93f1a886 [core] Preserve cosine Top-K for finite scaled vectors
(#11328)
fb93f1a886 is described below
commit fb93f1a886df33290e9f27bc81c5d179b31284ee
Author: Jingsong Lee <[email protected]>
AuthorDate: Sun Nov 22 10:16:57 2026 +0800
[core] Preserve cosine Top-K for finite scaled vectors (#11328)
---
.../paimon/globalindex/VectorSearchMetric.java | 22 +++++++++++++++++-
.../table/source/VectorSearchBuilderTest.java | 26 ++++++++++++++++++++++
2 files changed, 47 insertions(+), 1 deletion(-)
diff --git
a/paimon-core/src/main/java/org/apache/paimon/globalindex/VectorSearchMetric.java
b/paimon-core/src/main/java/org/apache/paimon/globalindex/VectorSearchMetric.java
index 2caf81b5de..7a6c167d3b 100644
---
a/paimon-core/src/main/java/org/apache/paimon/globalindex/VectorSearchMetric.java
+++
b/paimon-core/src/main/java/org/apache/paimon/globalindex/VectorSearchMetric.java
@@ -94,7 +94,27 @@ public final class VectorSearchMetric {
storedNorm += stored[i] * stored[i];
}
float denominator = (float) (Math.sqrt(queryNorm) *
Math.sqrt(storedNorm));
- return denominator == 0 ? 0 : dot / denominator;
+ if (queryNorm >= Float.MIN_NORMAL
+ && storedNorm >= Float.MIN_NORMAL
+ && denominator >= Float.MIN_NORMAL
+ && Float.isFinite(denominator)
+ && Float.isFinite(dot)) {
+ return dot / denominator;
+ }
+
+ // Squaring float coordinates can overflow or erase a nonzero vector's
norm.
+ double wideDot = 0;
+ double wideQueryNorm = 0;
+ double wideStoredNorm = 0;
+ for (int i = 0; i < query.length; i++) {
+ double q = query[i];
+ double s = stored[i];
+ wideDot += q * s;
+ wideQueryNorm += q * q;
+ wideStoredNorm += s * s;
+ }
+ double wideDenominator = Math.sqrt(wideQueryNorm) *
Math.sqrt(wideStoredNorm);
+ return wideDenominator == 0 ? 0 : (float) (wideDot / wideDenominator);
}
private static float innerProduct(float[] query, float[] stored) {
diff --git
a/paimon-core/src/test/java/org/apache/paimon/table/source/VectorSearchBuilderTest.java
b/paimon-core/src/test/java/org/apache/paimon/table/source/VectorSearchBuilderTest.java
index 2cb2264841..239d0b83bc 100644
---
a/paimon-core/src/test/java/org/apache/paimon/table/source/VectorSearchBuilderTest.java
+++
b/paimon-core/src/test/java/org/apache/paimon/table/source/VectorSearchBuilderTest.java
@@ -537,6 +537,32 @@ public class VectorSearchBuilderTest extends TableTestBase
{
assertThat(TestVectorGlobalIndexer.metricCalls()).isGreaterThan(0);
}
+ @ParameterizedTest
+ @ValueSource(floats = {1.0e20f, 1.0e-23f})
+ public void testRawCosineTopKIsInvariantToVectorScale(float scale) throws
Exception {
+ Identifier id = identifier("scaled_raw_cosine");
+ catalog.createTable(
+ id,
+ vectorSchemaBuilder(VECTOR_FIELD_NAME)
+ .option(CoreOptions.VECTOR_INDEX_SEARCH_MODE.key(),
"full")
+ .option("test.vector.metric", "cosine")
+ .build(),
+ false);
+ FileStoreTable table = getTable(id);
+ writeVectors(table, new float[][] {{-scale, 0f}, {scale, 0f}, {0f,
scale}});
+
+ ScoredGlobalIndexResult result =
+ (ScoredGlobalIndexResult)
+ table.newVectorSearchBuilder()
+ .withVectorColumn(VECTOR_FIELD_NAME)
+ .withVector(new float[] {scale, 0f})
+ .withLimit(1)
+ .executeLocal();
+
+ assertThat(result.results()).containsExactly(1L);
+ assertThat(result.scoreGetter().score(1L)).isEqualTo(1f);
+ }
+
@ParameterizedTest
@CsvSource({
"global-index.test-vector-ann.index-columns,true",