Skip to content

Commit 8e7bc22

Browse files
authored
feat(firestore): Add array expressions (#2350)
1 parent aa853b5 commit 8e7bc22

2 files changed

Lines changed: 468 additions & 0 deletions

File tree

  • java-firestore/google-cloud-firestore/src

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

Lines changed: 275 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2707,6 +2707,196 @@ public static Expression arrayReverse(String arrayFieldName) {
27072707
return arrayReverse(field(arrayFieldName));
27082708
}
27092709

2710+
/**
2711+
* Filters an array expression based on a predicate.
2712+
*
2713+
* @param array The expression representing the array to filter.
2714+
* @param alias The alias for the current element in the filter expression.
2715+
* @param filter The predicate boolean expression used to filter the elements.
2716+
* @return A new {@link Expression} representing the filtered array.
2717+
*/
2718+
@BetaApi
2719+
public static Expression arrayFilter(Expression array, String alias, BooleanExpression filter) {
2720+
return new FunctionExpression("array_filter", ImmutableList.of(array, constant(alias), filter));
2721+
}
2722+
2723+
/**
2724+
* Filters an array field based on a predicate.
2725+
*
2726+
* @param arrayFieldName The field name of the array to filter.
2727+
* @param alias The alias for the current element in the filter expression.
2728+
* @param filter The predicate boolean expression used to filter the elements.
2729+
* @return A new {@link Expression} representing the filtered array.
2730+
*/
2731+
@BetaApi
2732+
public static Expression arrayFilter(
2733+
String arrayFieldName, String alias, BooleanExpression filter) {
2734+
return arrayFilter(field(arrayFieldName), alias, filter);
2735+
}
2736+
2737+
/**
2738+
* Creates an expression that applies a provided transformation to each element in an array.
2739+
*
2740+
* @param array The expression representing the array to transform.
2741+
* @param elementAlias The alias for the current element in the transform expression.
2742+
* @param transform The expression used to transform the elements.
2743+
* @return A new {@link Expression} representing the transformed array.
2744+
*/
2745+
@BetaApi
2746+
public static Expression arrayTransform(
2747+
Expression array, String elementAlias, Expression transform) {
2748+
return new FunctionExpression(
2749+
"array_transform", ImmutableList.of(array, constant(elementAlias), transform));
2750+
}
2751+
2752+
/**
2753+
* Creates an expression that applies a provided transformation to each element in an array.
2754+
*
2755+
* @param arrayFieldName The field name of the array to transform.
2756+
* @param elementAlias The alias for the current element in the transform expression.
2757+
* @param transform The expression used to transform the elements.
2758+
* @return A new {@link Expression} representing the transformed array.
2759+
*/
2760+
@BetaApi
2761+
public static Expression arrayTransform(
2762+
String arrayFieldName, String elementAlias, Expression transform) {
2763+
return arrayTransform(field(arrayFieldName), elementAlias, transform);
2764+
}
2765+
2766+
/**
2767+
* Creates an expression that applies a provided transformation to each element in an array,
2768+
* providing the element's index to the transformation expression.
2769+
*
2770+
* @param array The expression representing the array to transform.
2771+
* @param elementAlias The alias for the current element in the transform expression.
2772+
* @param indexAlias The alias for the current index.
2773+
* @param transform The expression used to transform the elements.
2774+
* @return A new {@link Expression} representing the transformed array.
2775+
*/
2776+
@BetaApi
2777+
public static Expression arrayTransformWithIndex(
2778+
Expression array, String elementAlias, String indexAlias, Expression transform) {
2779+
return new FunctionExpression(
2780+
"array_transform",
2781+
ImmutableList.of(array, constant(elementAlias), constant(indexAlias), transform));
2782+
}
2783+
2784+
/**
2785+
* Creates an expression that applies a provided transformation to each element in an array,
2786+
* providing the element's index to the transformation expression.
2787+
*
2788+
* @param arrayFieldName The field name of the array to transform.
2789+
* @param elementAlias The alias for the current element in the transform expression.
2790+
* @param indexAlias The alias for the current index.
2791+
* @param transform The expression used to transform the elements.
2792+
* @return A new {@link Expression} representing the transformed array.
2793+
*/
2794+
@BetaApi
2795+
public static Expression arrayTransformWithIndex(
2796+
String arrayFieldName, String elementAlias, String indexAlias, Expression transform) {
2797+
return arrayTransformWithIndex(field(arrayFieldName), elementAlias, indexAlias, transform);
2798+
}
2799+
2800+
/**
2801+
* Creates an expression that returns a slice of an array.
2802+
*
2803+
* @param array The expression representing the array to slice.
2804+
* @param offset The starting index.
2805+
* @param length The number of elements to return.
2806+
* @return A new {@link Expression} representing the array slice.
2807+
*/
2808+
@BetaApi
2809+
public static Expression arraySlice(Expression array, Expression offset, Expression length) {
2810+
return new FunctionExpression("array_slice", ImmutableList.of(array, offset, length));
2811+
}
2812+
2813+
/**
2814+
* Creates an expression that returns a slice of an array.
2815+
*
2816+
* @param array The expression representing the array to slice.
2817+
* @param offset The starting index.
2818+
* @param length The number of elements to return.
2819+
* @return A new {@link Expression} representing the array slice.
2820+
*/
2821+
@BetaApi
2822+
public static Expression arraySlice(Expression array, int offset, int length) {
2823+
return arraySlice(array, constant(offset), constant(length));
2824+
}
2825+
2826+
/**
2827+
* Creates an expression that returns a slice of an array.
2828+
*
2829+
* @param arrayFieldName The field name of the array to slice.
2830+
* @param offset The starting index.
2831+
* @param length The number of elements to return.
2832+
* @return A new {@link Expression} representing the array slice.
2833+
*/
2834+
@BetaApi
2835+
public static Expression arraySlice(String arrayFieldName, int offset, int length) {
2836+
return arraySlice(field(arrayFieldName), constant(offset), constant(length));
2837+
}
2838+
2839+
/**
2840+
* Creates an expression that returns a slice of an array.
2841+
*
2842+
* @param arrayFieldName The field name of the array to slice.
2843+
* @param offset The starting index.
2844+
* @param length The number of elements to return.
2845+
* @return A new {@link Expression} representing the array slice.
2846+
*/
2847+
@BetaApi
2848+
public static Expression arraySlice(String arrayFieldName, Expression offset, Expression length) {
2849+
return arraySlice(field(arrayFieldName), offset, length);
2850+
}
2851+
2852+
/**
2853+
* Creates an expression that returns a slice of an array to its end.
2854+
*
2855+
* @param array The expression representing the array to slice.
2856+
* @param offset The expression representing the starting index.
2857+
* @return A new {@link Expression} representing the array slice.
2858+
*/
2859+
@BetaApi
2860+
public static Expression arraySliceToEnd(Expression array, Expression offset) {
2861+
return new FunctionExpression("array_slice", ImmutableList.of(array, offset));
2862+
}
2863+
2864+
/**
2865+
* Creates an expression that returns a slice of an array to its end.
2866+
*
2867+
* @param array The expression representing the array to slice.
2868+
* @param offset The starting index.
2869+
* @return A new {@link Expression} representing the array slice.
2870+
*/
2871+
@BetaApi
2872+
public static Expression arraySliceToEnd(Expression array, int offset) {
2873+
return arraySliceToEnd(array, constant(offset));
2874+
}
2875+
2876+
/**
2877+
* Creates an expression that returns a slice of an array to its end.
2878+
*
2879+
* @param arrayFieldName The field name of the array to slice.
2880+
* @param offset The starting index.
2881+
* @return A new {@link Expression} representing the array slice.
2882+
*/
2883+
@BetaApi
2884+
public static Expression arraySliceToEnd(String arrayFieldName, int offset) {
2885+
return arraySliceToEnd(field(arrayFieldName), constant(offset));
2886+
}
2887+
2888+
/**
2889+
* Creates an expression that returns a slice of an array to its end.
2890+
*
2891+
* @param arrayFieldName The field name of the array to slice.
2892+
* @param offset The expression representing the starting index.
2893+
* @return A new {@link Expression} representing the array slice.
2894+
*/
2895+
@BetaApi
2896+
public static Expression arraySliceToEnd(String arrayFieldName, Expression offset) {
2897+
return arraySliceToEnd(field(arrayFieldName), offset);
2898+
}
2899+
27102900
/**
27112901
* Creates an expression that checks if an array contains a specified element.
27122902
*
@@ -6583,6 +6773,91 @@ public final Expression arrayReverse() {
65836773
return arrayReverse(this);
65846774
}
65856775

6776+
/**
6777+
* Filters this array based on a predicate.
6778+
*
6779+
* @param alias The alias for the current element in the filter expression.
6780+
* @param filter The predicate boolean expression used to filter the elements.
6781+
* @return A new {@link Expression} representing the filtered array.
6782+
*/
6783+
@BetaApi
6784+
public final Expression arrayFilter(String alias, BooleanExpression filter) {
6785+
return arrayFilter(this, alias, filter);
6786+
}
6787+
6788+
/**
6789+
* Creates an expression that applies a provided transformation to each element in an array.
6790+
*
6791+
* @param elementAlias The alias for the current element in the transform expression.
6792+
* @param transform The expression used to transform the elements.
6793+
* @return A new {@link Expression} representing the transformed array.
6794+
*/
6795+
@BetaApi
6796+
public final Expression arrayTransform(String elementAlias, Expression transform) {
6797+
return arrayTransform(this, elementAlias, transform);
6798+
}
6799+
6800+
/**
6801+
* Creates an expression that applies a provided transformation to each element in an array,
6802+
* providing the element's index to the transformation expression.
6803+
*
6804+
* @param elementAlias The alias for the current element in the transform expression.
6805+
* @param indexAlias The alias for the current index.
6806+
* @param transform The expression used to transform the elements.
6807+
* @return A new {@link Expression} representing the transformed array.
6808+
*/
6809+
@BetaApi
6810+
public final Expression arrayTransformWithIndex(
6811+
String elementAlias, String indexAlias, Expression transform) {
6812+
return arrayTransformWithIndex(this, elementAlias, indexAlias, transform);
6813+
}
6814+
6815+
/**
6816+
* Returns a slice of this array.
6817+
*
6818+
* @param offset The starting index.
6819+
* @param length The number of elements to return.
6820+
* @return A new {@link Expression} representing the array slice.
6821+
*/
6822+
@BetaApi
6823+
public final Expression arraySlice(int offset, int length) {
6824+
return arraySlice(this, offset, length);
6825+
}
6826+
6827+
/**
6828+
* Returns a slice of this array.
6829+
*
6830+
* @param offset The starting index expressed as an Expression.
6831+
* @param length The number of elements to return expressed as an Expression.
6832+
* @return A new {@link Expression} representing the array slice.
6833+
*/
6834+
@BetaApi
6835+
public final Expression arraySlice(Expression offset, Expression length) {
6836+
return arraySlice(this, offset, length);
6837+
}
6838+
6839+
/**
6840+
* Returns a slice of this array to its end.
6841+
*
6842+
* @param offset The starting index.
6843+
* @return A new {@link Expression} representing the array slice.
6844+
*/
6845+
@BetaApi
6846+
public final Expression arraySliceToEnd(int offset) {
6847+
return arraySliceToEnd(this, offset);
6848+
}
6849+
6850+
/**
6851+
* Returns a slice of this array to its end.
6852+
*
6853+
* @param offset The starting index expressed as an Expression.
6854+
* @return A new {@link Expression} representing the array slice.
6855+
*/
6856+
@BetaApi
6857+
public final Expression arraySliceToEnd(Expression offset) {
6858+
return arraySliceToEnd(this, offset);
6859+
}
6860+
65866861
/**
65876862
* Creates an expression that checks if array contains a specific {@code element}.
65886863
*

0 commit comments

Comments
 (0)