Skip to content
This repository was archived by the owner on May 8, 2026. It is now read-only.

Commit c558914

Browse files
authored
Merge branch 'main' into yvonne/timestamp-expressions
2 parents eb76879 + c606ff9 commit c558914

2 files changed

Lines changed: 134 additions & 0 deletions

File tree

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

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3977,6 +3977,41 @@ public static Expression type(String fieldName) {
39773977
return type(field(fieldName));
39783978
}
39793979

3980+
/**
3981+
* Creates an expression that checks if the result of an expression is of the given type.
3982+
*
3983+
* <p>Supported values for {@code type} are: "null", "array", "boolean", "bytes", "timestamp",
3984+
* "geo_point", "number", "int32", "int64", "float64", "decimal128", "map", "reference", "string",
3985+
* "vector", "max_key", "min_key", "object_id", "regex", and "request_timestamp".
3986+
*
3987+
* @param expr The expression to check the type of.
3988+
* @param type The type to check for.
3989+
* @return A new {@link BooleanExpression} that evaluates to true if the expression's result is of
3990+
* the given type, false otherwise.
3991+
*/
3992+
@BetaApi
3993+
public static BooleanExpression isType(Expression expr, String type) {
3994+
return new BooleanFunctionExpression("is_type", ImmutableList.of(expr, constant(type)));
3995+
}
3996+
3997+
/**
3998+
* Creates an expression that checks if the value of a field is of the given type.
3999+
*
4000+
* <p>Supported values for {@code type} are: "null", "array", "boolean", "bytes", "timestamp",
4001+
* "geo_point", "number", "int32", "int64", "float64", "decimal128", "map", "reference", "string",
4002+
* "vector", "max_key", "min_key", "object_id", "regex", and "request_timestamp".
4003+
*
4004+
* @param fieldName The name of the field to check the type of.
4005+
* @param type The type to check for.
4006+
* @return A new {@link BooleanExpression} that evaluates to true if the expression's result is of
4007+
* the given type, false otherwise.
4008+
*/
4009+
@BetaApi
4010+
public static BooleanExpression isType(String fieldName, String type) {
4011+
return new BooleanFunctionExpression(
4012+
"is_type", ImmutableList.of(field(fieldName), constant(type)));
4013+
}
4014+
39804015
// Numeric Operations
39814016
/**
39824017
* Creates an expression that rounds {@code numericExpr} to nearest integer.
@@ -6232,4 +6267,20 @@ public final Expression collectionId() {
62326267
public final Expression type() {
62336268
return type(this);
62346269
}
6270+
6271+
/**
6272+
* Creates an expression that checks if the result of this expression is of the given type.
6273+
*
6274+
* <p>Supported values for {@code type} are: "null", "array", "boolean", "bytes", "timestamp",
6275+
* "geo_point", "number", "int32", "int64", "float64", "decimal128", "map", "reference", "string",
6276+
* "vector", "max_key", "min_key", "object_id", "regex", and "request_timestamp".
6277+
*
6278+
* @param type The type to check for.
6279+
* @return A new {@link BooleanExpression} that evaluates to true if the expression's result is of
6280+
* the given type, false otherwise.
6281+
*/
6282+
@BetaApi
6283+
public final BooleanExpression isType(String type) {
6284+
return isType(this, type);
6285+
}
62356286
}

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

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3715,6 +3715,89 @@ public void testType() throws Exception {
37153715
"vector"));
37163716
}
37173717

3718+
@Test
3719+
public void testIsType() throws Exception {
3720+
List<PipelineResult> results =
3721+
firestore
3722+
.pipeline()
3723+
.collection(collection.getPath())
3724+
.replaceWith(
3725+
Expression.map(
3726+
map(
3727+
"int",
3728+
1,
3729+
"float",
3730+
1.1,
3731+
"str",
3732+
"a string",
3733+
"bool",
3734+
true,
3735+
"null",
3736+
null,
3737+
"geoPoint",
3738+
new GeoPoint(0.1, 0.2),
3739+
"timestamp",
3740+
Timestamp.ofTimeSecondsAndNanos(123456, 0),
3741+
"bytes",
3742+
com.google.cloud.firestore.Blob.fromBytes(new byte[] {1, 2, 3}),
3743+
"docRef",
3744+
collection.document("bar"),
3745+
"vector",
3746+
vector(new double[] {1.0, 2.0, 3.0}),
3747+
"map",
3748+
Expression.map(map("numberK", 1, "stringK", "a string")),
3749+
"array",
3750+
array(1, 2, true))))
3751+
.select(
3752+
Expression.isType("int", "int64").as("isInt64"),
3753+
Expression.isType("int", "number").as("isInt64IsNumber"),
3754+
Expression.isType("int", "decimal128").as("isInt64IsDecimal128"),
3755+
Expression.isType("float", "float64").as("isFloat64"),
3756+
Expression.isType("float", "number").as("isFloat64IsNumber"),
3757+
Expression.isType("float", "decimal128").as("isFloat64IsDecimal128"),
3758+
Expression.isType("str", "string").as("isStr"),
3759+
Expression.isType("str", "int64").as("isStrNum"),
3760+
Expression.isType("int", "string").as("isNumStr"),
3761+
Expression.isType("bool", "boolean").as("isBool"),
3762+
Expression.isType("null", "null").as("isNull"),
3763+
Expression.isType("geoPoint", "geo_point").as("isGeoPoint"),
3764+
Expression.isType("timestamp", "timestamp").as("isTimestamp"),
3765+
Expression.isType("bytes", "bytes").as("isBytes"),
3766+
Expression.isType("docRef", "reference").as("isDocRef"),
3767+
Expression.isType("vector", "vector").as("isVector"),
3768+
Expression.isType("map", "map").as("isMap"),
3769+
Expression.isType("array", "array").as("isArray"),
3770+
Expression.isType(constant(1), "int64").as("exprIsInt64"),
3771+
field("int").isType("int64").as("staticIsInt64"))
3772+
.limit(1)
3773+
.execute()
3774+
.get()
3775+
.getResults();
3776+
assertThat(data(results))
3777+
.containsExactly(
3778+
map(
3779+
"isInt64", true,
3780+
"isInt64IsNumber", true,
3781+
"isInt64IsDecimal128", false,
3782+
"isFloat64", true,
3783+
"isFloat64IsNumber", true,
3784+
"isFloat64IsDecimal128", false,
3785+
"isStr", true,
3786+
"isStrNum", false,
3787+
"isNumStr", false,
3788+
"isBool", true,
3789+
"isNull", true,
3790+
"isGeoPoint", true,
3791+
"isTimestamp", true,
3792+
"isBytes", true,
3793+
"isDocRef", true,
3794+
"isVector", true,
3795+
"isMap", true,
3796+
"isArray", true,
3797+
"exprIsInt64", true,
3798+
"staticIsInt64", true));
3799+
}
3800+
37183801
@Test
37193802
public void testExplainWithError() {
37203803
assumeFalse(

0 commit comments

Comments
 (0)