Skip to content

Commit 9685212

Browse files
committed
more tests
1 parent 5b0251c commit 9685212

1 file changed

Lines changed: 72 additions & 0 deletions

File tree

tests/integration/index/vector_metrics_test.go

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,3 +298,75 @@ func TestVectorIndex_EuclideanOnLargeVectors_OrdersByDistance(t *testing.T) {
298298

299299
testUtils.ExecuteTestCase(t, test)
300300
}
301+
302+
// The dot product of two large vectors also exceeds a float32, so it needs the same float64 handling
303+
// as euclidean. Cosine cannot overflow because its vectors are normalised first, but it is covered
304+
// alongside so a future change to either metric is caught here.
305+
// https://github.com/sourcenetwork/defradb/issues/5220
306+
func TestVectorIndex_DotProductOnLargeVectors_OrdersByDistance(t *testing.T) {
307+
test := testUtils.TestCase{
308+
Actions: []any{
309+
&action.AddCollection{
310+
SDL: `type User {
311+
name: String
312+
vector: [Float32!] @index(vector: {dimensions: 1, hnsw: {metric: DOT}})
313+
}`,
314+
},
315+
&action.AddDoc{DocMap: map[string]any{"name": "near", "vector": []float32{4e19}}},
316+
&action.AddDoc{DocMap: map[string]any{"name": "mid", "vector": []float32{3e19}}},
317+
&action.AddDoc{DocMap: map[string]any{"name": "far", "vector": []float32{2e19}}},
318+
&action.WaitForIndexReady{CollectionID: 0},
319+
&action.Request{
320+
Request: `query {
321+
User(order: {_alias: {sim: DESC}}, limit: 3){
322+
name
323+
sim: SIMILARITY(vector: {vector: [1e20]})
324+
}
325+
}`,
326+
Results: map[string]any{
327+
"User": []map[string]any{
328+
{"name": "near", "sim": 4.0000000723660884e+39},
329+
{"name": "mid", "sim": 3.000000164225731e+39},
330+
{"name": "far", "sim": 2.0000000361830442e+39},
331+
},
332+
},
333+
},
334+
},
335+
}
336+
337+
testUtils.ExecuteTestCase(t, test)
338+
}
339+
340+
func TestVectorIndex_CosineOnLargeVectors_OrdersByDistance(t *testing.T) {
341+
test := testUtils.TestCase{
342+
Actions: []any{
343+
&action.AddCollection{
344+
SDL: `type User {
345+
name: String
346+
vector: [Float32!] @index(vector: {dimensions: 2, hnsw: {metric: COSINE}})
347+
}`,
348+
},
349+
&action.AddDoc{DocMap: map[string]any{"name": "aligned", "vector": []float32{2e19, 0}}},
350+
&action.AddDoc{DocMap: map[string]any{"name": "diagonal", "vector": []float32{2e19, 2e19}}},
351+
&action.AddDoc{DocMap: map[string]any{"name": "orthogonal", "vector": []float32{0, 2e19}}},
352+
&action.WaitForIndexReady{CollectionID: 0},
353+
&action.Request{
354+
Request: `query {
355+
User(order: {_alias: {sim: DESC}}, limit: 3){
356+
name
357+
sim: SIMILARITY(vector: {vector: [1e20, 0]})
358+
}
359+
}`,
360+
Results: map[string]any{
361+
"User": []map[string]any{
362+
{"name": "aligned", "sim": 1.0},
363+
{"name": "diagonal", "sim": 0.7071067811865476},
364+
{"name": "orthogonal", "sim": 0.0},
365+
},
366+
},
367+
},
368+
},
369+
}
370+
371+
testUtils.ExecuteTestCase(t, test)
372+
}

0 commit comments

Comments
 (0)