Skip to content

Commit 2857d61

Browse files
authored
Merge pull request #1677 from BillFarber/task/testOpticVectorFunctions
Added tests for the new Optic vector functions.
2 parents 7e0c909 + 394ee72 commit 2857d61

File tree

4 files changed

+177
-0
lines changed

4 files changed

+177
-0
lines changed
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
package com.marklogic.client.test.rows;
2+
3+
import com.fasterxml.jackson.databind.node.ArrayNode;
4+
import com.marklogic.client.FailedRequestException;
5+
import com.marklogic.client.expression.PlanBuilder;
6+
import com.marklogic.client.row.RowRecord;
7+
import com.marklogic.client.test.junit5.RequiresML12;
8+
import com.marklogic.client.type.ServerExpression;
9+
import com.marklogic.client.type.XsDoubleSeqVal;
10+
import org.junit.jupiter.api.Assertions;
11+
import org.junit.jupiter.api.BeforeEach;
12+
import org.junit.jupiter.api.Disabled;
13+
import org.junit.jupiter.api.Test;
14+
import org.junit.jupiter.api.extension.ExtendWith;
15+
16+
import java.util.List;
17+
18+
import static org.junit.jupiter.api.Assertions.*;
19+
20+
@ExtendWith(RequiresML12.class)
21+
class VectorTest extends AbstractOpticUpdateTest {
22+
23+
XsDoubleSeqVal sampleVector;
24+
XsDoubleSeqVal twoDimensionalVector;
25+
ServerExpression invalidVector;
26+
27+
@BeforeEach
28+
void beforeEach() {
29+
sampleVector = op.xs.doubleSeq(1.2, 3.4, 5.6);
30+
twoDimensionalVector = op.xs.doubleSeq(1.2, 3.4);
31+
invalidVector = op.xs.string("InvalidVector");
32+
rowManager.withUpdate(false);
33+
}
34+
35+
@Test
36+
void vectorFunctionsHappyPath() {
37+
PlanBuilder.ModifyPlan plan =
38+
op.fromView("vectors", "persons")
39+
.bind(op.as("sampleVector", sampleVector))
40+
.bind(op.as("cosineSimilarity", op.vec.cosineSimilarity(op.col("embedding"),op.col("sampleVector"))))
41+
.bind(op.as("dotProduct", op.vec.dotProduct(op.col("embedding"),op.col("sampleVector"))))
42+
.bind(op.as("euclideanDistance", op.vec.euclideanDistance(op.col("embedding"),op.col("sampleVector"))))
43+
.bind(op.as("dimension", op.vec.dimension(op.col("sampleVector"))))
44+
.bind(op.as("normalize", op.vec.normalize(op.col("sampleVector"))))
45+
.bind(op.as("magnitude", op.vec.magnitude(op.col("sampleVector"))))
46+
.bind(op.as("get", op.vec.get(op.col("sampleVector"), op.xs.integer(2))))
47+
.bind(op.as("add", op.vec.add(op.col("embedding"),op.col("sampleVector"))))
48+
.bind(op.as("subtract", op.vec.subtract(op.col("embedding"),op.col("sampleVector"))))
49+
.bind(op.as("base64Encode", op.vec.base64Encode(op.col("sampleVector"))))
50+
.bind(op.as("base64Decode", op.vec.base64Decode(op.col("base64Encode"))))
51+
.bind(op.as("subVector", op.vec.subvector(op.col("sampleVector"), op.xs.integer(1), op.xs.integer(1))))
52+
.select(
53+
op.col("cosineSimilarity"), op.col("dotProduct"), op.col("euclideanDistance"),
54+
op.col("name"), op.col("dimension"), op.col("normalize"),
55+
op.col("magnitude"), op.col("get"), op.col("add"), op.col("subtract"),
56+
op.col("base64Encode"), op.col("base64Decode"), op.col("subVector")
57+
)
58+
.limit(5);
59+
List<RowRecord> rows = resultRows(plan);
60+
assertEquals(2, rows.size());
61+
rows.forEach(row -> {
62+
// Simple a sanity checks to verify that the functions ran. Very little concern about the actual return values.
63+
double cosineSimilarity = row.getDouble("cosineSimilarity");
64+
assertTrue((cosineSimilarity > 0) && (cosineSimilarity < 1),"Unexpected value: " + cosineSimilarity);
65+
double dotProduct = row.getDouble("dotProduct");
66+
Assertions.assertTrue(dotProduct > 0, "Unexpected value: " + dotProduct);
67+
double euclideanDistance = row.getDouble("euclideanDistance");
68+
Assertions.assertTrue(euclideanDistance > 0, "Unexpected value: " + euclideanDistance);
69+
assertEquals(3, row.getInt("dimension"));
70+
assertEquals(3, ((ArrayNode) row.get("normalize")).size());
71+
double magnitude = row.getDouble("magnitude");
72+
assertTrue( magnitude > 0, "Unexpected value: " + magnitude);
73+
assertEquals(3, ((ArrayNode) row.get("add")).size());
74+
assertEquals(3, ((ArrayNode) row.get("subtract")).size());
75+
assertFalse(row.getString("base64Encode").isEmpty());
76+
assertEquals(3, ((ArrayNode) row.get("base64Decode")).size());
77+
assertEquals(5.6, row.getDouble("get"));
78+
assertEquals(1, ((ArrayNode) row.get("subVector")).size());
79+
});
80+
}
81+
82+
@Test
83+
void cosineSimilarity_DimensionMismatch() {
84+
PlanBuilder.ModifyPlan plan =
85+
op.fromView("vectors", "persons")
86+
.bind(op.as("sampleVector", twoDimensionalVector))
87+
.bind(op.as("cosineSimilarity", op.vec.cosineSimilarity(op.col("embedding"),op.col("sampleVector"))))
88+
.select(op.col("name"), op.col("summary"), op.col("cosineSimilarity"));
89+
Exception exception = assertThrows(FailedRequestException.class, () -> resultRows(plan));
90+
String actualMessage = exception.getMessage();
91+
assertTrue(actualMessage.contains("Server Message: VEC-DIMMISMATCH"), "Unexpected message: " + actualMessage);
92+
assertTrue(actualMessage.contains("Mismatched dimension: Vector dimensions must be equal"), "Unexpected message: " + actualMessage);
93+
}
94+
95+
@Test
96+
void cosineSimilarity_InvalidVector() {
97+
PlanBuilder.ModifyPlan plan =
98+
op.fromView("vectors", "persons")
99+
.bind(op.as("sampleVector", invalidVector))
100+
.bind(op.as("cosineSimilarity", op.vec.cosineSimilarity(op.col("embedding"),op.col("sampleVector"))))
101+
.select(op.col("name"), op.col("summary"), op.col("cosineSimilarity"));
102+
Exception exception = assertThrows(FailedRequestException.class, () -> resultRows(plan));
103+
String actualMessage = exception.getMessage();
104+
assertTrue(actualMessage.contains("Server Message: XDMP-ARGTYPE"), "Unexpected message: " + actualMessage);
105+
assertTrue(actualMessage.contains("arg2 is not of type vec:vector"), "Unexpected message: " + actualMessage);
106+
}
107+
108+
@Test
109+
@Disabled("See ticket MLE-15508")
110+
void vectorScore() {
111+
PlanBuilder.ModifyPlan plan =
112+
op.fromView("vectors", "persons")
113+
.bind(op.as("vectorScore", op.vec.vectorScore(op.xs.unsignedInt(1), op.xs.doubleVal(2))))
114+
.select(op.col("vectorScore"))
115+
.limit(5);
116+
List<RowRecord> rows = resultRows(plan);
117+
assertEquals(2, rows.size());
118+
rows.forEach(row -> {
119+
double vectorScore = row.getDouble("vectorScore");
120+
assertTrue(vectorScore > 0, "Unexpected value: " + vectorScore);
121+
});
122+
}
123+
124+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"person": {
3+
"name": "Alice",
4+
"summary": "About Alice",
5+
"embedding": [
6+
1.1,
7+
2.2,
8+
3.3
9+
]
10+
}
11+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"person": {
3+
"name": "Bob",
4+
"summary": "Below Bob",
5+
"embedding": [
6+
9.9,
7+
8.8,
8+
7.7
9+
]
10+
}
11+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<template xmlns="http://marklogic.com/xdmp/tde">
2+
<context>/person</context>
3+
<directories><directory>/optic/vectors/</directory></directories>
4+
<rows>
5+
<row>
6+
<schema-name>vectors</schema-name>
7+
<view-name>persons</view-name>
8+
<columns>
9+
<column>
10+
<name>name</name>
11+
<scalar-type>string</scalar-type>
12+
<val>name</val>
13+
</column>
14+
15+
<column>
16+
<name>summary</name>
17+
<scalar-type>string</scalar-type>
18+
<val>summary</val>
19+
</column>
20+
21+
<column>
22+
<name>embedding</name>
23+
<scalar-type>vector</scalar-type>
24+
<val>vec:vector(embedding)</val>
25+
<dimension>3</dimension>
26+
<invalid-values>reject</invalid-values>
27+
</column>
28+
</columns>
29+
</row>
30+
</rows>
31+
</template>

0 commit comments

Comments
 (0)