Skip to content

Commit a62cdde

Browse files
committed
dot product and arrayConcat fix
1 parent eb5a815 commit a62cdde

File tree

5 files changed

+73
-117
lines changed

5 files changed

+73
-117
lines changed

google-cloud-firestore/pom.xml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,15 @@
312312
<descriptorRef>jar-with-dependencies</descriptorRef>
313313
</descriptorRefs>
314314
</configuration>
315+
<executions>
316+
<execution>
317+
<id>make-assembly</id>
318+
<phase>package</phase>
319+
<goals>
320+
<goal>single</goal>
321+
</goals>
322+
</execution>
323+
</executions>
315324
</plugin>
316325
<plugin>
317326
<groupId>org.apache.maven.plugins</groupId>
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@
2121
import com.google.common.collect.Lists;
2222

2323
@BetaApi
24-
public final class DotProductDistance extends Function {
24+
public final class DotProduct extends Function {
2525
@InternalApi
26-
DotProductDistance(Expr vector1, Expr vector2) {
26+
DotProduct(Expr vector1, Expr vector2) {
2727
super("dot_product", Lists.newArrayList(vector1, vector2));
2828
}
2929
}

google-cloud-firestore/src/main/java/com/google/cloud/firestore/pipeline/expressions/Expr.java

Lines changed: 19 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
import com.google.api.core.BetaApi;
2222
import java.util.Arrays;
23+
import java.util.List;
2324

2425
/**
2526
* Represents an expression that can be evaluated to a value within the execution of a {@link
@@ -450,25 +451,7 @@ default Not notInAny(Object... other) {
450451
// Array Functions
451452

452453
/**
453-
* Creates an expression that concatenates an array expression with one or more other arrays.
454-
*
455-
* <p>Example:
456-
*
457-
* <pre>{@code
458-
* // Combine the 'items' array with another array field.
459-
* Field.of("items").arrayConcat(Field.of("otherItems"));
460-
* }</pre>
461-
*
462-
* @param elements The array expressions to concatenate.
463-
* @return A new {@code Expr} representing the concatenated array.
464-
*/
465-
@BetaApi
466-
default ArrayConcat arrayConcat(Expr... elements) {
467-
return new ArrayConcat(this, Arrays.asList(elements));
468-
}
469-
470-
/**
471-
* Creates an expression that concatenates an array expression with one or more other arrays.
454+
* Creates an expression that concatenates an array expression with another array.
472455
*
473456
* <p>Example:
474457
*
@@ -477,12 +460,12 @@ default ArrayConcat arrayConcat(Expr... elements) {
477460
* Field.of("tags").arrayConcat(Arrays.asList("newTag1", "newTag2"), Field.of("otherTag"));
478461
* }</pre>
479462
*
480-
* @param elements The array expressions or values to concatenate.
463+
* @param array The array of constants or expressions to concat with.
481464
* @return A new {@code Expr} representing the concatenated array.
482465
*/
483466
@BetaApi
484-
default ArrayConcat arrayConcat(Object... elements) {
485-
return new ArrayConcat(this, toExprList(elements));
467+
default ArrayConcat arrayConcat(List<Object> array) {
468+
return new ArrayConcat(this, toExprList(array.toArray()));
486469
}
487470

488471
/**
@@ -1140,39 +1123,39 @@ default EuclideanDistance euclideanDistance(Expr other) {
11401123
}
11411124

11421125
/**
1143-
* Calculates the dot product distance between two vectors.
1126+
* Calculates the dot product between two vectors.
11441127
*
11451128
* <p>Example:
11461129
*
11471130
* <pre>{@code
1148-
* // Calculate the dot product distance between a feature vector and a target vector
1149-
* Field.of("features").dotProductDistance(new double[] {0.5, 0.8, 0.2});
1131+
* // Calculate the dot product between a feature vector and a target vector
1132+
* Field.of("features").dotProduct(new double[] {0.5, 0.8, 0.2});
11501133
* }</pre>
11511134
*
1152-
* @param other The other vector (as an array of doubles) to compare against.
1153-
* @return A new {@code Expr} representing the dot product distance between the two vectors.
1135+
* @param other The other vector (represented as an Expr) to calculate dot product with.
1136+
* @return A new {@code Expr} representing the dot product between the two vectors.
11541137
*/
11551138
@BetaApi
1156-
default DotProductDistance dotProductDistance(double[] other) {
1157-
return new DotProductDistance(this, Constant.vector(other));
1139+
default DotProduct dotProduct(double[] other) {
1140+
return new DotProduct(this, Constant.vector(other));
11581141
}
11591142

11601143
/**
1161-
* Calculates the dot product distance between two vectors.
1144+
* Calculates the dot product between two vectors.
11621145
*
11631146
* <p>Example:
11641147
*
11651148
* <pre>{@code
1166-
* // Calculate the dot product distance between two document vectors: 'docVector1' and 'docVector2'
1167-
* Field.of("docVector1").dotProductDistance(Field.of("docVector2"));
1149+
* // Calculate the dot product between two document vectors: 'docVector1' and 'docVector2'
1150+
* Field.of("docVector1").dotProduct(Field.of("docVector2"));
11681151
* }</pre>
11691152
*
1170-
* @param other The other vector (represented as an Expr) to compare against.
1171-
* @return A new {@code Expr} representing the dot product distance between the two vectors.
1153+
* @param other The other vector (represented as an Expr) to calculate dot product with.
1154+
* @return A new {@code Expr} representing the dot product between the two vectors.
11721155
*/
11731156
@BetaApi
1174-
default DotProductDistance dotProductDistance(Expr other) {
1175-
return new DotProductDistance(this, other);
1157+
default DotProduct dotProduct(Expr other) {
1158+
return new DotProduct(this, other);
11761159
}
11771160

11781161
// Ordering

google-cloud-firestore/src/main/java/com/google/cloud/firestore/pipeline/expressions/Function.java

Lines changed: 38 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -1172,79 +1172,41 @@ public static If ifThenElse(FilterCondition condition, Expr thenExpr, Expr elseE
11721172
}
11731173

11741174
/**
1175-
* Creates an expression that concatenates an array expression with other arrays.
1176-
*
1177-
* <p>Example:
1178-
*
1179-
* <pre>{@code
1180-
* // Combine the 'items' array with two new item arrays
1181-
* Function.arrayConcat(Field.of("items"), Field.of("newItems"), Field.of("otherItems"));
1182-
* }</pre>
1183-
*
1184-
* @param expr The array expression to concatenate to.
1185-
* @param elements The array expressions to concatenate.
1186-
* @return A new {@code Expr} representing the concatenated array.
1187-
*/
1188-
@BetaApi
1189-
public static ArrayConcat arrayConcat(Expr expr, Expr... elements) {
1190-
return new ArrayConcat(expr, Arrays.asList(elements));
1191-
}
1192-
1193-
/**
1194-
* Creates an expression that concatenates an array expression with other arrays and/or values.
1175+
* Creates an expression that concatenates an array expression with another array.
11951176
*
11961177
* <p>Example:
11971178
*
11981179
* <pre>{@code
11991180
* // Combine the 'tags' array with a new array
1200-
* Function.arrayConcat(Field.of("tags"), Arrays.asList("newTag1", "newTag2"));
1181+
* Function.arrayConcat(Field.of("tags"), List.newArrayList("newTag1", "newTag2"));
12011182
* }</pre>
12021183
*
12031184
* @param expr The array expression to concatenate to.
1204-
* @param elements The array expressions or single values to concatenate.
1205-
* @return A new {@code Expr} representing the concatenated array.
1206-
*/
1207-
@BetaApi
1208-
public static ArrayConcat arrayConcat(Expr expr, Object... elements) {
1209-
return new ArrayConcat(expr, toExprList(elements));
1210-
}
1211-
1212-
/**
1213-
* Creates an expression that concatenates a field's array value with other arrays.
1214-
*
1215-
* <p>Example:
1216-
*
1217-
* <pre>{@code
1218-
* // Combine the 'items' array with two new item arrays
1219-
* Function.arrayConcat("items", Field.of("newItems"), Field.of("otherItems"));
1220-
* }</pre>
1221-
*
1222-
* @param field The field name containing array values.
1223-
* @param elements The array expressions to concatenate.
1185+
* @param array The array of constants or expressions to concat with.
12241186
* @return A new {@code Expr} representing the concatenated array.
12251187
*/
12261188
@BetaApi
1227-
public static ArrayConcat arrayConcat(String field, Expr... elements) {
1228-
return new ArrayConcat(Field.of(field), Arrays.asList(elements));
1189+
public static ArrayConcat arrayConcat(Expr expr, List<Object> array) {
1190+
return new ArrayConcat(expr, toExprList(array.toArray()));
12291191
}
12301192

12311193
/**
1232-
* Creates an expression that concatenates a field's array value with other arrays and/or values.
1194+
* Creates an expression that concatenates a field's array value with another array.
12331195
*
12341196
* <p>Example:
12351197
*
12361198
* <pre>{@code
12371199
* // Combine the 'tags' array with a new array
1238-
* Function.arrayConcat("tags", Arrays.asList("newTag1", "newTag2"));
1200+
* Function.arrayConcat("tags", List.newArrayList("newTag1", "newTag2"));
12391201
* }</pre>
12401202
*
12411203
* @param field The field name containing array values.
1242-
* @param elements The array expressions or single values to concatenate.
1204+
* @param array The array of constants or expressions to concat with.
12431205
* @return A new {@code Expr} representing the concatenated array.
12441206
*/
12451207
@BetaApi
1246-
public static ArrayConcat arrayConcat(String field, Object... elements) {
1247-
return new ArrayConcat(Field.of(field), toExprList(elements));
1208+
public static ArrayConcat arrayConcat(String field, List<Object> array) {
1209+
return new ArrayConcat(Field.of(field), toExprList(array.toArray()));
12481210
}
12491211

12501212
/**
@@ -2335,79 +2297,79 @@ public static CosineDistance cosineDistance(String field, double[] other) {
23352297
}
23362298

23372299
/**
2338-
* Calculates the dot product distance between two vector expressions.
2300+
* Calculates the dot product between two vector expressions.
23392301
*
23402302
* <p>Example:
23412303
*
23422304
* <pre>{@code
2343-
* // Calculate the dot product distance between two document vectors: 'docVector1' and 'docVector2'
2344-
* Function.dotProductDistance(Field.of("docVector1"), Field.of("docVector2"));
2305+
* // Calculate the dot product between two document vectors: 'docVector1' and 'docVector2'
2306+
* Function.dotProduct(Field.of("docVector1"), Field.of("docVector2"));
23452307
* }</pre>
23462308
*
2347-
* @param expr The first vector (represented as an Expr) to compare against.
2348-
* @param other The other vector (represented as an Expr) to compare against.
2349-
* @return A new {@code Expr} representing the dot product distance between the two vectors.
2309+
* @param expr The first vector (represented as an Expr) to calculate dot product with.
2310+
* @param other The other vector (represented as an Expr) to calculate dot product with.
2311+
* @return A new {@code Expr} representing the dot product between the two vectors.
23502312
*/
23512313
@BetaApi
2352-
public static DotProductDistance dotProductDistance(Expr expr, Expr other) {
2353-
return new DotProductDistance(expr, other);
2314+
public static DotProduct dotProduct(Expr expr, Expr other) {
2315+
return new DotProduct(expr, other);
23542316
}
23552317

23562318
/**
2357-
* Calculates the dot product distance between a vector expression and a double array.
2319+
* Calculates the dot product between a vector expression and a double array.
23582320
*
23592321
* <p>Example:
23602322
*
23612323
* <pre>{@code
2362-
* // Calculate the dot product distance between a feature vector and a target vector
2363-
* Function.dotProductDistance(Field.of("features"), new double[] {0.5, 0.8, 0.2});
2324+
* // Calculate the dot product between a feature vector and a target vector
2325+
* Function.dotProduct(Field.of("features"), new double[] {0.5, 0.8, 0.2});
23642326
* }</pre>
23652327
*
2366-
* @param expr The first vector (represented as an Expr) to compare against.
2367-
* @param other The other vector (as an array of doubles) to compare against.
2368-
* @return A new {@code Expr} representing the dot product distance between the two vectors.
2328+
* @param expr The first vector (represented as an Expr) to calculate dot product with.
2329+
* @param other The other vector (represented as an Expr) to calculate dot product with.
2330+
* @return A new {@code Expr} representing the dot product between the two vectors.
23692331
*/
23702332
@BetaApi
2371-
public static DotProductDistance dotProductDistance(Expr expr, double[] other) {
2372-
return new DotProductDistance(expr, Constant.vector(other));
2333+
public static DotProduct dotProduct(Expr expr, double[] other) {
2334+
return new DotProduct(expr, Constant.vector(other));
23732335
}
23742336

23752337
/**
2376-
* Calculates the dot product distance between a field's vector value and a vector expression.
2338+
* Calculates the dot product between a field's vector value and a vector expression.
23772339
*
23782340
* <p>Example:
23792341
*
23802342
* <pre>{@code
2381-
* // Calculate the dot product distance between two document vectors: 'docVector1' and 'docVector2'
2382-
* Function.dotProductDistance("docVector1", Field.of("docVector2"));
2343+
* // Calculate the dot product between two document vectors: 'docVector1' and 'docVector2'
2344+
* Function.dotProduct("docVector1", Field.of("docVector2"));
23832345
* }</pre>
23842346
*
23852347
* @param field The name of the field containing the first vector.
2386-
* @param other The other vector (represented as an Expr) to compare against.
2348+
* @param other The other vector (represented as an Expr) to calculate dot product with.
23872349
* @return A new {@code Expr} representing the dot product distance between the two vectors.
23882350
*/
23892351
@BetaApi
2390-
public static DotProductDistance dotProductDistance(String field, Expr other) {
2391-
return new DotProductDistance(Field.of(field), other);
2352+
public static DotProduct dotProduct(String field, Expr other) {
2353+
return new DotProduct(Field.of(field), other);
23922354
}
23932355

23942356
/**
2395-
* Calculates the dot product distance between a field's vector value and a double array.
2357+
* Calculates the dot product between a field's vector value and a double array.
23962358
*
23972359
* <p>Example:
23982360
*
23992361
* <pre>{@code
2400-
* // Calculate the dot product distance between a feature vector and a target vector
2401-
* Function.dotProductDistance("features", new double[] {0.5, 0.8, 0.2});
2362+
* // Calculate the dot product between a feature vector and a target vector
2363+
* Function.dotProduct("features", new double[] {0.5, 0.8, 0.2});
24022364
* }</pre>
24032365
*
24042366
* @param field The name of the field containing the first vector.
2405-
* @param other The other vector (as an array of doubles) to compare against.
2367+
* @param other The other vector (represented as an Expr) to calculate dot product with.
24062368
* @return A new {@code Expr} representing the dot product distance between the two vectors.
24072369
*/
24082370
@BetaApi
2409-
public static DotProductDistance dotProductDistance(String field, double[] other) {
2410-
return new DotProductDistance(Field.of(field), Constant.vector(other));
2371+
public static DotProduct dotProduct(String field, double[] other) {
2372+
return new DotProduct(Field.of(field), Constant.vector(other));
24112373
}
24122374

24132375
/**

google-cloud-firestore/src/test/java/com/google/cloud/firestore/it/ITPipelineTest.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
import static com.google.cloud.firestore.pipeline.expressions.Function.collectionId;
3030
import static com.google.cloud.firestore.pipeline.expressions.Function.cosineDistance;
3131
import static com.google.cloud.firestore.pipeline.expressions.Function.countAll;
32-
import static com.google.cloud.firestore.pipeline.expressions.Function.dotProductDistance;
3332
import static com.google.cloud.firestore.pipeline.expressions.Function.endsWith;
3433
import static com.google.cloud.firestore.pipeline.expressions.Function.eq;
3534
import static com.google.cloud.firestore.pipeline.expressions.Function.euclideanDistance;
@@ -479,7 +478,10 @@ public void testArrayConcat() throws Exception {
479478
List<PipelineResult> results =
480479
collection
481480
.pipeline()
482-
.select(Field.of("tags").arrayConcat("newTag1", "newTag2").as("modifiedTags"))
481+
.select(
482+
Field.of("tags")
483+
.arrayConcat(Lists.newArrayList("newTag1", "newTag2"))
484+
.as("modifiedTags"))
483485
.limit(1)
484486
.execute()
485487
.get();
@@ -832,7 +834,7 @@ public void testDistanceFunctions() throws Exception {
832834
.collection(collection.getPath())
833835
.select(
834836
cosineDistance(Constant.vector(sourceVector), targetVector).as("cosineDistance"),
835-
dotProductDistance(Constant.vector(sourceVector), targetVector)
837+
Function.dotProduct(Constant.vector(sourceVector), targetVector)
836838
.as("dotProductDistance"),
837839
euclideanDistance(Constant.vector(sourceVector), targetVector)
838840
.as("euclideanDistance"))

0 commit comments

Comments
 (0)