@@ -38,15 +38,15 @@ void vectorFunctionsHappyPath() {
38
38
PlanBuilder .ModifyPlan plan =
39
39
op .fromView ("vectors" , "persons" )
40
40
.bind (op .as ("sampleVector" , op .vec .vector (sampleVector )))
41
- .bind (op .as ("cosineSimilarity" , op .vec .cosineSimilarity (op .col ("embedding" ),op .col ("sampleVector" ))))
42
- .bind (op .as ("dotProduct" , op .vec .dotProduct (op .col ("embedding" ),op .col ("sampleVector" ))))
43
- .bind (op .as ("euclideanDistance" , op .vec .euclideanDistance (op .col ("embedding" ),op .col ("sampleVector" ))))
41
+ .bind (op .as ("cosineSimilarity" , op .vec .cosineSimilarity (op .col ("embedding" ), op .col ("sampleVector" ))))
42
+ .bind (op .as ("dotProduct" , op .vec .dotProduct (op .col ("embedding" ), op .col ("sampleVector" ))))
43
+ .bind (op .as ("euclideanDistance" , op .vec .euclideanDistance (op .col ("embedding" ), op .col ("sampleVector" ))))
44
44
.bind (op .as ("dimension" , op .vec .dimension (op .col ("sampleVector" ))))
45
45
.bind (op .as ("normalize" , op .vec .normalize (op .col ("sampleVector" ))))
46
46
.bind (op .as ("magnitude" , op .vec .magnitude (op .col ("sampleVector" ))))
47
47
.bind (op .as ("get" , op .vec .get (op .col ("sampleVector" ), op .xs .integer (2 ))))
48
- .bind (op .as ("add" , op .vec .add (op .col ("embedding" ),op .col ("sampleVector" ))))
49
- .bind (op .as ("subtract" , op .vec .subtract (op .col ("embedding" ),op .col ("sampleVector" ))))
48
+ .bind (op .as ("add" , op .vec .add (op .col ("embedding" ), op .col ("sampleVector" ))))
49
+ .bind (op .as ("subtract" , op .vec .subtract (op .col ("embedding" ), op .col ("sampleVector" ))))
50
50
.bind (op .as ("base64Encode" , op .vec .base64Encode (op .col ("sampleVector" ))))
51
51
.bind (op .as ("base64Decode" , op .vec .base64Decode (op .col ("base64Encode" ))))
52
52
.bind (op .as ("subVector" , op .vec .subvector (op .col ("sampleVector" ), op .xs .integer (1 ), op .xs .integer (1 ))))
@@ -64,15 +64,15 @@ void vectorFunctionsHappyPath() {
64
64
rows .forEach (row -> {
65
65
// Simple a sanity checks to verify that the functions ran. Very little concern about the actual return values.
66
66
double cosineSimilarity = row .getDouble ("cosineSimilarity" );
67
- assertTrue ((cosineSimilarity > 0 ) && (cosineSimilarity < 1 ),"Unexpected value: " + cosineSimilarity );
67
+ assertTrue ((cosineSimilarity > 0 ) && (cosineSimilarity < 1 ), "Unexpected value: " + cosineSimilarity );
68
68
double dotProduct = row .getDouble ("dotProduct" );
69
69
Assertions .assertTrue (dotProduct > 0 , "Unexpected value: " + dotProduct );
70
70
double euclideanDistance = row .getDouble ("euclideanDistance" );
71
71
Assertions .assertTrue (euclideanDistance > 0 , "Unexpected value: " + euclideanDistance );
72
72
assertEquals (3 , row .getInt ("dimension" ));
73
73
assertEquals (3 , ((ArrayNode ) row .get ("normalize" )).size ());
74
74
double magnitude = row .getDouble ("magnitude" );
75
- assertTrue ( magnitude > 0 , "Unexpected value: " + magnitude );
75
+ assertTrue (magnitude > 0 , "Unexpected value: " + magnitude );
76
76
assertEquals (3 , ((ArrayNode ) row .get ("add" )).size ());
77
77
assertEquals (3 , ((ArrayNode ) row .get ("subtract" )).size ());
78
78
assertFalse (row .getString ("base64Encode" ).isEmpty ());
@@ -89,7 +89,7 @@ void cosineSimilarity_DimensionMismatch() {
89
89
PlanBuilder .ModifyPlan plan =
90
90
op .fromView ("vectors" , "persons" )
91
91
.bind (op .as ("sampleVector" , op .vec .vector (twoDimensionalVector )))
92
- .bind (op .as ("cosineSimilarity" , op .vec .cosineSimilarity (op .col ("embedding" ),op .col ("sampleVector" ))))
92
+ .bind (op .as ("cosineSimilarity" , op .vec .cosineSimilarity (op .col ("embedding" ), op .col ("sampleVector" ))))
93
93
.select (op .col ("name" ), op .col ("summary" ), op .col ("cosineSimilarity" ));
94
94
Exception exception = assertThrows (FailedRequestException .class , () -> resultRows (plan ));
95
95
String actualMessage = exception .getMessage ();
@@ -102,7 +102,7 @@ void cosineSimilarity_InvalidVector() {
102
102
PlanBuilder .ModifyPlan plan =
103
103
op .fromView ("vectors" , "persons" )
104
104
.bind (op .as ("sampleVector" , invalidVector ))
105
- .bind (op .as ("cosineSimilarity" , op .vec .cosineSimilarity (op .col ("embedding" ),op .col ("sampleVector" ))))
105
+ .bind (op .as ("cosineSimilarity" , op .vec .cosineSimilarity (op .col ("embedding" ), op .col ("sampleVector" ))))
106
106
.select (op .col ("name" ), op .col ("summary" ), op .col ("cosineSimilarity" ));
107
107
Exception exception = assertThrows (FailedRequestException .class , () -> resultRows (plan ));
108
108
String actualMessage = exception .getMessage ();
@@ -111,20 +111,20 @@ void cosineSimilarity_InvalidVector() {
111
111
}
112
112
113
113
@ Test
114
- // As of 07/26/24, this test will fail with the ML12 develop branch.
115
- // However, it will succeed with the 12ea1 build.
116
- // See https://progresssoftware.atlassian.net/browse/MLE-15707
114
+ // As of 07/26/24, this test will fail with the ML12 develop branch.
115
+ // However, it will succeed with the 12ea1 build.
116
+ // See https://progresssoftware.atlassian.net/browse/MLE-15707
117
117
void bindVectorFromDocs () {
118
118
PlanBuilder .ModifyPlan plan =
119
119
op .fromSearchDocs (
120
- op .cts .andQuery (
121
- op .cts .documentQuery ("/optic/vectors/alice.json" ),
122
- op .cts .elementQuery (
123
- "person" ,
124
- op .cts .trueQuery ()
125
- )
126
- ))
127
- .bind (op .as ("embedding" , op .vec .vector (op .xpath ("doc" , "/person/embedding" ))));
120
+ op .cts .andQuery (
121
+ op .cts .documentQuery ("/optic/vectors/alice.json" ),
122
+ op .cts .elementQuery (
123
+ "person" ,
124
+ op .cts .trueQuery ()
125
+ )
126
+ ))
127
+ .bind (op .as ("embedding" , op .vec .vector (op .xpath ("doc" , "/person/embedding" ))));
128
128
List <RowRecord > rows = resultRows (plan );
129
129
assertEquals (1 , rows .size ());
130
130
}
@@ -138,4 +138,30 @@ void vecVectorWithCol() {
138
138
List <RowRecord > rows = resultRows (plan );
139
139
assertEquals (2 , rows .size ());
140
140
}
141
+
142
+ @ Test
143
+ void annTopK () {
144
+ PlanBuilder .ModifyPlan plan = op .fromView ("vectors" , "persons" )
145
+ .annTopK (10 , op .col ("embedding" ), op .vec .vector (sampleVector ), op .col ("distance" ), 0.5f );
146
+
147
+ List <RowRecord > rows = resultRows (plan );
148
+ assertEquals (2 , rows .size (), "Verifying that annTopK worked and returned both rows from the view." );
149
+
150
+ rows .forEach (row -> {
151
+ float distance = row .getFloat ("distance" );
152
+ assertTrue (distance > 0 , "Just verifying that annTopK both worked and put a valid value into the 'distance' column." );
153
+ });
154
+ }
155
+
156
+ @ Test
157
+ void dslAnnTopK () {
158
+ String query = "const qualityVector = vec.vector([ 1.1, 2.2, 3.3 ]);\n " +
159
+ "op.fromView('vectors', 'persons')\n " +
160
+ " .bind(op.as('myVector', op.vec.vector(op.col('embedding'))))\n " +
161
+ " .annTopK(2, op.col('myVector'), qualityVector, op.col('distance'), 0.5)" ;
162
+
163
+ RawQueryDSLPlan plan = rowManager .newRawQueryDSLPlan (new StringHandle (query ));
164
+ List <RowRecord > rows = resultRows (plan );
165
+ assertEquals (2 , rows .size (), "Just verifying that 'annTopK' works via the DSL and v1/rows." );
166
+ }
141
167
}
0 commit comments