diff --git a/google-cloud-firestore/src/main/java/com/google/cloud/firestore/pipeline/expressions/Expression.java b/google-cloud-firestore/src/main/java/com/google/cloud/firestore/pipeline/expressions/Expression.java index 14c310743e..695051c208 100644 --- a/google-cloud-firestore/src/main/java/com/google/cloud/firestore/pipeline/expressions/Expression.java +++ b/google-cloud-firestore/src/main/java/com/google/cloud/firestore/pipeline/expressions/Expression.java @@ -2217,6 +2217,459 @@ public static Expression arrayLength(String arrayFieldName) { return arrayLength(field(arrayFieldName)); } + /** + * Creates an expression that returns the first element of an array. + * + * @param array The expression representing the array. + * @return A new {@link Expression} representing the first element of the array. + */ + @BetaApi + public static Expression arrayFirst(Expression array) { + return new FunctionExpression("array_first", ImmutableList.of(array)); + } + + /** + * Creates an expression that returns the first element of an array. + * + * @param arrayFieldName The field name of the array. + * @return A new {@link Expression} representing the first element of the array. + */ + @BetaApi + public static Expression arrayFirst(String arrayFieldName) { + return arrayFirst(field(arrayFieldName)); + } + + /** + * Creates an expression that returns the first n elements of an array. + * + * @param array The expression representing the array. + * @param n The Expression evaluates to the number of elements to return. + * @return A new {@link Expression} representing the first n elements of the array. + */ + @BetaApi + public static Expression arrayFirstN(Expression array, Expression n) { + return new FunctionExpression("array_first_n", ImmutableList.of(array, n)); + } + + /** + * Creates an expression that returns the first n elements of an array. + * + * @param array The expression representing the array. + * @param n The number of elements to return. + * @return A new {@link Expression} representing the first n elements of the array. + */ + @BetaApi + public static Expression arrayFirstN(Expression array, int n) { + return arrayFirstN(array, constant(n)); + } + + /** + * Creates an expression that returns the first n elements of an array. + * + * @param arrayFieldName The field name of the array. + * @param n The number of elements to return. + * @return A new {@link Expression} representing the first n elements of the array. + */ + @BetaApi + public static Expression arrayFirstN(String arrayFieldName, int n) { + return arrayFirstN(field(arrayFieldName), constant(n)); + } + + /** + * Creates an expression that returns the first n elements of an array. + * + * @param arrayFieldName The field name of the array. + * @param n The Expression evaluates to the number of elements to return. + * @return A new {@link Expression} representing the first n elements of the array. + */ + @BetaApi + public static Expression arrayFirstN(String arrayFieldName, Expression n) { + return arrayFirstN(field(arrayFieldName), n); + } + + /** + * Creates an expression that returns the last element of an array. + * + * @param array The expression representing the array. + * @return A new {@link Expression} representing the last element of the array. + */ + @BetaApi + public static Expression arrayLast(Expression array) { + return new FunctionExpression("array_last", ImmutableList.of(array)); + } + + /** + * Creates an expression that returns the last element of an array. + * + * @param arrayFieldName The field name of the array. + * @return A new {@link Expression} representing the last element of the array. + */ + @BetaApi + public static Expression arrayLast(String arrayFieldName) { + return arrayLast(field(arrayFieldName)); + } + + /** + * Creates an expression that returns the last n elements of an array. + * + * @param array The expression representing the array. + * @param n The Expression evaluates to the number of elements to return. + * @return A new {@link Expression} representing the last n elements of the array. + */ + @BetaApi + public static Expression arrayLastN(Expression array, Expression n) { + return new FunctionExpression("array_last_n", ImmutableList.of(array, n)); + } + + /** + * Creates an expression that returns the last n elements of an array. + * + * @param array The expression representing the array. + * @param n The number of elements to return. + * @return A new {@link Expression} representing the last n elements of the array. + */ + @BetaApi + public static Expression arrayLastN(Expression array, int n) { + return arrayLastN(array, constant(n)); + } + + /** + * Creates an expression that returns the last n elements of an array. + * + * @param arrayFieldName The field name of the array. + * @param n The number of elements to return. + * @return A new {@link Expression} representing the last n elements of the array. + */ + @BetaApi + public static Expression arrayLastN(String arrayFieldName, int n) { + return arrayLastN(field(arrayFieldName), n); + } + + /** + * Creates an expression that returns the last n elements of an array. + * + * @param arrayFieldName The field name of the array. + * @param n The Expression evaluates to the number of elements to return. + * @return A new {@link Expression} representing the last n elements of the array. + */ + @BetaApi + public static Expression arrayLastN(String arrayFieldName, Expression n) { + return arrayLastN(field(arrayFieldName), n); + } + + /** + * Creates an expression that returns the minimum value of an array. + * + * @param array The expression representing the array. + * @return A new {@link Expression} representing the minimum value of the array. + */ + @BetaApi + public static Expression arrayMinimum(Expression array) { + return new FunctionExpression("minimum", ImmutableList.of(array)); + } + + /** + * Creates an expression that returns the minimum value of an array. + * + * @param arrayFieldName The field name of the array. + * @return A new {@link Expression} representing the minimum value of the array. + */ + @BetaApi + public static Expression arrayMinimum(String arrayFieldName) { + return arrayMinimum(field(arrayFieldName)); + } + + /** + * Creates an expression that returns the n minimum values of an array. + * + *
Note: Returns the n smallest non-null elements in the array, in ascending order. This does + * not use a stable sort, meaning the order of equivalent elements is undefined. + * + * @param array The expression representing the array. + * @param n The Expression evaluates to the number of elements to return. + * @return A new {@link Expression} representing the n minimum values of the array. + */ + @BetaApi + public static Expression arrayMinimumN(Expression array, Expression n) { + return new FunctionExpression("minimum_n", ImmutableList.of(array, n)); + } + + /** + * Creates an expression that returns the n minimum values of an array. + * + *
Note: Returns the n smallest non-null elements in the array, in ascending order. This does + * not use a stable sort, meaning the order of equivalent elements is undefined. + * + * @param array The expression representing the array. + * @param n The number of elements to return. + * @return A new {@link Expression} representing the n minimum values of the array. + */ + @BetaApi + public static Expression arrayMinimumN(Expression array, int n) { + return arrayMinimumN(array, constant(n)); + } + + /** + * Creates an expression that returns the n minimum values of an array. + * + *
Note: Returns the n smallest non-null elements in the array, in ascending order. This does + * not use a stable sort, meaning the order of equivalent elements is undefined. + * + * @param arrayFieldName The field name of the array. + * @param n The number of elements to return. + * @return A new {@link Expression} representing the n minimum values of the array. + */ + @BetaApi + public static Expression arrayMinimumN(String arrayFieldName, int n) { + return arrayMinimumN(field(arrayFieldName), n); + } + + /** + * Creates an expression that returns the n minimum values of an array. + * + *
Note: Returns the n smallest non-null elements in the array, in ascending order. This does + * not use a stable sort, meaning the order of equivalent elements is undefined. + * + * @param arrayFieldName The field name of the array. + * @param n The Expression evaluates to the number of elements to return. + * @return A new {@link Expression} representing the n minimum values of the array. + */ + @BetaApi + public static Expression arrayMinimumN(String arrayFieldName, Expression n) { + return arrayMinimumN(field(arrayFieldName), n); + } + + /** + * Creates an expression that returns the maximum value of an array. + * + * @param array The expression representing the array. + * @return A new {@link Expression} representing the maximum value of the array. + */ + @BetaApi + public static Expression arrayMaximum(Expression array) { + return new FunctionExpression("maximum", ImmutableList.of(array)); + } + + /** + * Creates an expression that returns the maximum value of an array. + * + * @param arrayFieldName The field name of the array. + * @return A new {@link Expression} representing the maximum value of the array. + */ + @BetaApi + public static Expression arrayMaximum(String arrayFieldName) { + return arrayMaximum(field(arrayFieldName)); + } + + /** + * Creates an expression that returns the n maximum values of an array. + * + *
Note: Returns the n largest non-null elements in the array, in descending order. This does + * not use a stable sort, meaning the order of equivalent elements is undefined. + * + * @param array The expression representing the array. + * @param n The Expression evaluates to the number of elements to return. + * @return A new {@link Expression} representing the n maximum values of the array. + */ + @BetaApi + public static Expression arrayMaximumN(Expression array, Expression n) { + return new FunctionExpression("maximum_n", ImmutableList.of(array, n)); + } + + /** + * Creates an expression that returns the n maximum values of an array. + * + *
Note: Returns the n largest non-null elements in the array, in descending order. This does + * not use a stable sort, meaning the order of equivalent elements is undefined. + * + * @param array The expression representing the array. + * @param n The number of elements to return. + * @return A new {@link Expression} representing the n maximum values of the array. + */ + @BetaApi + public static Expression arrayMaximumN(Expression array, int n) { + return arrayMaximumN(array, constant(n)); + } + + /** + * Creates an expression that returns the n maximum values of an array. + * + *
Note: Returns the n largest non-null elements in the array, in descending order. This does + * not use a stable sort, meaning the order of equivalent elements is undefined. + * + * @param arrayFieldName The field name of the array. + * @param n The number of elements to return. + * @return A new {@link Expression} representing the n maximum values of the array. + */ + @BetaApi + public static Expression arrayMaximumN(String arrayFieldName, int n) { + return arrayMaximumN(field(arrayFieldName), n); + } + + /** + * Creates an expression that returns the n maximum values of an array. + * + *
Note: Returns the n largest non-null elements in the array, in descending order. This does + * not use a stable sort, meaning the order of equivalent elements is undefined. + * + * @param arrayFieldName The field name of the array. + * @param n The Expression evaluates to the number of elements to return. + * @return A new {@link Expression} representing the n maximum values of the array. + */ + @BetaApi + public static Expression arrayMaximumN(String arrayFieldName, Expression n) { + return arrayMaximumN(field(arrayFieldName), n); + } + + /** + * Creates an expression that returns the index of the first occurrence of a value in an array. + * + * @param array The expression representing the array. + * @param value The value to search for. + * @return A new {@link Expression} representing the index. + */ + @BetaApi + public static Expression arrayIndexOf(Expression array, Expression value) { + return new FunctionExpression( + "array_index_of", + ImmutableList.of(array, toExprOrConstant(value), toExprOrConstant("first"))); + } + + /** + * Creates an expression that returns the index of the first occurrence of a value in an array. + * + * @param array The expression representing the array. + * @param value The value to search for. + * @return A new {@link Expression} representing the index. + */ + @BetaApi + public static Expression arrayIndexOf(Expression array, Object value) { + return arrayIndexOf(array, toExprOrConstant(value)); + } + + /** + * Creates an expression that returns the index of the first occurrence of a value in an array. + * + * @param arrayFieldName The field name of the array. + * @param value The value to search for. + * @return A new {@link Expression} representing the index. + */ + @BetaApi + public static Expression arrayIndexOf(String arrayFieldName, Object value) { + return arrayIndexOf(field(arrayFieldName), value); + } + + /** + * Creates an expression that returns the index of the first occurrence of a value in an array. + * + * @param arrayFieldName The field name of the array. + * @param value The value to search for. + * @return A new {@link Expression} representing the index. + */ + @BetaApi + public static Expression arrayIndexOf(String arrayFieldName, Expression value) { + return arrayIndexOf(field(arrayFieldName), value); + } + + /** + * Creates an expression that returns the index of the last occurrence of a value in an array. + * + * @param array The expression representing the array. + * @param value The value to search for. + * @return A new {@link Expression} representing the last index. + */ + @BetaApi + public static Expression arrayLastIndexOf(Expression array, Expression value) { + return new FunctionExpression( + "array_index_of", + ImmutableList.of(array, toExprOrConstant(value), toExprOrConstant("last"))); + } + + /** + * Creates an expression that returns the index of the last occurrence of a value in an array. + * + * @param array The expression representing the array. + * @param value The value to search for. + * @return A new {@link Expression} representing the last index. + */ + @BetaApi + public static Expression arrayLastIndexOf(Expression array, Object value) { + return arrayLastIndexOf(array, toExprOrConstant(value)); + } + + /** + * Creates an expression that returns the index of the last occurrence of a value in an array. + * + * @param arrayFieldName The field name of the array. + * @param value The value to search for. + * @return A new {@link Expression} representing the last index. + */ + @BetaApi + public static Expression arrayLastIndexOf(String arrayFieldName, Object value) { + return arrayLastIndexOf(field(arrayFieldName), value); + } + + /** + * Creates an expression that returns the index of the last occurrence of a value in an array. + * + * @param arrayFieldName The field name of the array. + * @param value The value to search for. + * @return A new {@link Expression} representing the last index. + */ + @BetaApi + public static Expression arrayLastIndexOf(String arrayFieldName, Expression value) { + return arrayLastIndexOf(field(arrayFieldName), value); + } + + /** + * Creates an expression that returns all indices of a value in an array. + * + * @param array The expression representing the array. + * @param value The value to search for. + * @return A new {@link Expression} representing the indices. + */ + @BetaApi + public static Expression arrayIndexOfAll(Expression array, Expression value) { + return new FunctionExpression( + "array_index_of_all", ImmutableList.of(array, toExprOrConstant(value))); + } + + /** + * Creates an expression that returns all indices of a value in an array. + * + * @param array The expression representing the array. + * @param value The value to search for. + * @return A new {@link Expression} representing the indices. + */ + @BetaApi + public static Expression arrayIndexOfAll(Expression array, Object value) { + return arrayIndexOfAll(array, toExprOrConstant(value)); + } + + /** + * Creates an expression that returns all indices of a value in an array. + * + * @param arrayFieldName The field name of the array. + * @param value The value to search for. + * @return A new {@link Expression} representing the indices. + */ + @BetaApi + public static Expression arrayIndexOfAll(String arrayFieldName, Object value) { + return arrayIndexOfAll(field(arrayFieldName), toExprOrConstant(value)); + } + + /** + * Creates an expression that returns all indices of a value in an array. + * + * @param arrayFieldName The field name of the array. + * @param value The value to search for. + * @return A new {@link Expression} representing the indices. + */ + @BetaApi + public static Expression arrayIndexOfAll(String arrayFieldName, Expression value) { + return arrayIndexOfAll(field(arrayFieldName), value); + } + /** * Creates an expression that returns an element from an array at a specified index. * @@ -4571,6 +5024,212 @@ public final Expression arrayConcat(Expression... otherArrays) { return arrayConcat(this, otherArrays); } + /** + * Returns the first element of an array. + * + * @return A new {@link Expression} representing the first element of the array. + */ + @BetaApi + public final Expression arrayFirst() { + return arrayFirst(this); + } + + /** + * Returns the first n elements of an array. + * + * @param n The number of elements to return. + * @return A new {@link Expression} representing the first n elements of the array. + */ + @BetaApi + public final Expression arrayFirstN(int n) { + return arrayFirstN(this, n); + } + + /** + * Returns the first n elements of an array. + * + * @param n The Expression evaluates to the number of elements to return. + * @return A new {@link Expression} representing the first n elements of the array. + */ + @BetaApi + public final Expression arrayFirstN(Expression n) { + return arrayFirstN(this, n); + } + + /** + * Returns the last element of an array. + * + * @return A new {@link Expression} representing the last element of the array. + */ + @BetaApi + public final Expression arrayLast() { + return arrayLast(this); + } + + /** + * Returns the last n elements of an array. + * + * @param n The number of elements to return. + * @return A new {@link Expression} representing the last n elements of the array. + */ + @BetaApi + public final Expression arrayLastN(int n) { + return arrayLastN(this, n); + } + + /** + * Returns the last n elements of an array. + * + * @param n The Expression evaluates to the number of elements to return. + * @return A new {@link Expression} representing the last n elements of the array. + */ + @BetaApi + public final Expression arrayLastN(Expression n) { + return arrayLastN(this, n); + } + + /** + * Returns the minimum value of an array. + * + * @return A new {@link Expression} representing the minimum value of the array. + */ + @BetaApi + public final Expression arrayMinimum() { + return arrayMinimum(this); + } + + /** + * Returns the n minimum values of an array. + * + *
Note: Returns the n smallest non-null elements in the array, in ascending order. This does + * not use a stable sort, meaning the order of equivalent elements is undefined. + * + * @param n The number of elements to return. + * @return A new {@link Expression} representing the n minimum values of the array. + */ + @BetaApi + public final Expression arrayMinimumN(int n) { + return arrayMinimumN(this, n); + } + + /** + * Returns the n minimum values of an array. + * + *
Note: Returns the n smallest non-null elements in the array, in ascending order. This does + * not use a stable sort, meaning the order of equivalent elements is undefined. + * + * @param n The Expression evaluates to the number of elements to return. + * @return A new {@link Expression} representing the n minimum values of the array. + */ + @BetaApi + public final Expression arrayMinimumN(Expression n) { + return arrayMinimumN(this, n); + } + + /** + * Returns the maximum value of an array. + * + * @return A new {@link Expression} representing the maximum value of the array. + */ + @BetaApi + public final Expression arrayMaximum() { + return arrayMaximum(this); + } + + /** + * Returns the n maximum values of an array. + * + *
Note: Returns the n largest non-null elements in the array, in descending order. This does + * not use a stable sort, meaning the order of equivalent elements is undefined. + * + * @param n The number of elements to return. + * @return A new {@link Expression} representing the n maximum values of the array. + */ + @BetaApi + public final Expression arrayMaximumN(int n) { + return arrayMaximumN(this, n); + } + + /** + * Returns the n maximum values of an array. + * + *
Note: Returns the n largest non-null elements in the array, in descending order. This does
+ * not use a stable sort, meaning the order of equivalent elements is undefined.
+ *
+ * @param n The Expression evaluates to the number of elements to return.
+ * @return A new {@link Expression} representing the n maximum values of the array.
+ */
+ @BetaApi
+ public final Expression arrayMaximumN(Expression n) {
+ return arrayMaximumN(this, n);
+ }
+
+ /**
+ * Returns the index of the first occurrence of a value in an array.
+ *
+ * @param value The value to search for.
+ * @return A new {@link Expression} representing the index.
+ */
+ @BetaApi
+ public final Expression arrayIndexOf(Object value) {
+ return arrayIndexOf(this, value);
+ }
+
+ /**
+ * Returns the index of the first occurrence of a value in an array.
+ *
+ * @param value The value to search for.
+ * @return A new {@link Expression} representing the index.
+ */
+ @BetaApi
+ public final Expression arrayIndexOf(Expression value) {
+ return arrayIndexOf(this, value);
+ }
+
+ /**
+ * Returns the index of the last occurrence of a value in an array.
+ *
+ * @param value The value to search for.
+ * @return A new {@link Expression} representing the last index.
+ */
+ @BetaApi
+ public final Expression arrayLastIndexOf(Object value) {
+ return arrayLastIndexOf(this, value);
+ }
+
+ /**
+ * Returns the index of the last occurrence of a value in an array.
+ *
+ * @param value The value to search for.
+ * @return A new {@link Expression} representing the last index.
+ */
+ @BetaApi
+ public final Expression arrayLastIndexOf(Expression value) {
+ return arrayLastIndexOf(this, value);
+ }
+
+ /**
+ * Returns all indices of a value in an array.
+ *
+ * @param value The value to search for.
+ * @return A new {@link Expression} representing the indices.
+ */
+ @BetaApi
+ public final Expression arrayIndexOfAll(Object value) {
+ return arrayIndexOfAll(this, value);
+ }
+
+ /**
+ * Returns all indices of a value in an array.
+ *
+ * @param value The value to search for.
+ * @return A new {@link Expression} representing the indices.
+ */
+ @BetaApi
+ public final Expression arrayIndexOfAll(Expression value) {
+ return arrayIndexOfAll(this, value);
+ }
+
/**
* Reverses the order of elements in the array.
*
diff --git a/google-cloud-firestore/src/test/java/com/google/cloud/firestore/it/ITPipelineTest.java b/google-cloud-firestore/src/test/java/com/google/cloud/firestore/it/ITPipelineTest.java
index 8dc00b03ff..e14fa8a948 100644
--- a/google-cloud-firestore/src/test/java/com/google/cloud/firestore/it/ITPipelineTest.java
+++ b/google-cloud-firestore/src/test/java/com/google/cloud/firestore/it/ITPipelineTest.java
@@ -34,7 +34,18 @@
import static com.google.cloud.firestore.pipeline.expressions.Expression.arrayContains;
import static com.google.cloud.firestore.pipeline.expressions.Expression.arrayContainsAll;
import static com.google.cloud.firestore.pipeline.expressions.Expression.arrayContainsAny;
+import static com.google.cloud.firestore.pipeline.expressions.Expression.arrayFirst;
+import static com.google.cloud.firestore.pipeline.expressions.Expression.arrayFirstN;
import static com.google.cloud.firestore.pipeline.expressions.Expression.arrayGet;
+import static com.google.cloud.firestore.pipeline.expressions.Expression.arrayIndexOf;
+import static com.google.cloud.firestore.pipeline.expressions.Expression.arrayIndexOfAll;
+import static com.google.cloud.firestore.pipeline.expressions.Expression.arrayLast;
+import static com.google.cloud.firestore.pipeline.expressions.Expression.arrayLastIndexOf;
+import static com.google.cloud.firestore.pipeline.expressions.Expression.arrayLastN;
+import static com.google.cloud.firestore.pipeline.expressions.Expression.arrayMaximum;
+import static com.google.cloud.firestore.pipeline.expressions.Expression.arrayMaximumN;
+import static com.google.cloud.firestore.pipeline.expressions.Expression.arrayMinimum;
+import static com.google.cloud.firestore.pipeline.expressions.Expression.arrayMinimumN;
import static com.google.cloud.firestore.pipeline.expressions.Expression.arrayReverse;
import static com.google.cloud.firestore.pipeline.expressions.Expression.ceil;
import static com.google.cloud.firestore.pipeline.expressions.Expression.concat;
@@ -80,6 +91,7 @@
import static com.google.cloud.firestore.pipeline.expressions.Expression.vectorLength;
import static com.google.cloud.firestore.pipeline.expressions.Expression.xor;
import static com.google.common.truth.Truth.assertThat;
+import static java.util.Collections.emptyList;
import static org.junit.Assert.assertThrows;
import static org.junit.Assume.assumeFalse;
@@ -920,6 +932,603 @@ public void testArrayLength() throws Exception {
assertThat(data(results)).hasSize(10);
}
+ @Test
+ public void testArrayFirst() throws Exception {
+ List