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

Commit c606ff9

Browse files
authored
feat: add isType expression (#2329)
1 parent b2f79ec commit c606ff9

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
@@ -3622,6 +3622,41 @@ public static Expression type(String fieldName) {
36223622
return type(field(fieldName));
36233623
}
36243624

3625+
/**
3626+
* Creates an expression that checks if the result of an expression is of the given type.
3627+
*
3628+
* <p>Supported values for {@code type} are: "null", "array", "boolean", "bytes", "timestamp",
3629+
* "geo_point", "number", "int32", "int64", "float64", "decimal128", "map", "reference", "string",
3630+
* "vector", "max_key", "min_key", "object_id", "regex", and "request_timestamp".
3631+
*
3632+
* @param expr The expression to check the type of.
3633+
* @param type The type to check for.
3634+
* @return A new {@link BooleanExpression} that evaluates to true if the expression's result is of
3635+
* the given type, false otherwise.
3636+
*/
3637+
@BetaApi
3638+
public static BooleanExpression isType(Expression expr, String type) {
3639+
return new BooleanFunctionExpression("is_type", ImmutableList.of(expr, constant(type)));
3640+
}
3641+
3642+
/**
3643+
* Creates an expression that checks if the value of a field is of the given type.
3644+
*
3645+
* <p>Supported values for {@code type} are: "null", "array", "boolean", "bytes", "timestamp",
3646+
* "geo_point", "number", "int32", "int64", "float64", "decimal128", "map", "reference", "string",
3647+
* "vector", "max_key", "min_key", "object_id", "regex", and "request_timestamp".
3648+
*
3649+
* @param fieldName The name of the field to check the type of.
3650+
* @param type The type to check for.
3651+
* @return A new {@link BooleanExpression} that evaluates to true if the expression's result is of
3652+
* the given type, false otherwise.
3653+
*/
3654+
@BetaApi
3655+
public static BooleanExpression isType(String fieldName, String type) {
3656+
return new BooleanFunctionExpression(
3657+
"is_type", ImmutableList.of(field(fieldName), constant(type)));
3658+
}
3659+
36253660
// Numeric Operations
36263661
/**
36273662
* Creates an expression that rounds {@code numericExpr} to nearest integer.
@@ -5668,4 +5703,20 @@ public final Expression collectionId() {
56685703
public final Expression type() {
56695704
return type(this);
56705705
}
5706+
5707+
/**
5708+
* Creates an expression that checks if the result of this expression is of the given type.
5709+
*
5710+
* <p>Supported values for {@code type} are: "null", "array", "boolean", "bytes", "timestamp",
5711+
* "geo_point", "number", "int32", "int64", "float64", "decimal128", "map", "reference", "string",
5712+
* "vector", "max_key", "min_key", "object_id", "regex", and "request_timestamp".
5713+
*
5714+
* @param type The type to check for.
5715+
* @return A new {@link BooleanExpression} that evaluates to true if the expression's result is of
5716+
* the given type, false otherwise.
5717+
*/
5718+
@BetaApi
5719+
public final BooleanExpression isType(String type) {
5720+
return isType(this, type);
5721+
}
56715722
}

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
@@ -3576,6 +3576,89 @@ public void testType() throws Exception {
35763576
"vector"));
35773577
}
35783578

3579+
@Test
3580+
public void testIsType() throws Exception {
3581+
List<PipelineResult> results =
3582+
firestore
3583+
.pipeline()
3584+
.collection(collection.getPath())
3585+
.replaceWith(
3586+
Expression.map(
3587+
map(
3588+
"int",
3589+
1,
3590+
"float",
3591+
1.1,
3592+
"str",
3593+
"a string",
3594+
"bool",
3595+
true,
3596+
"null",
3597+
null,
3598+
"geoPoint",
3599+
new GeoPoint(0.1, 0.2),
3600+
"timestamp",
3601+
Timestamp.ofTimeSecondsAndNanos(123456, 0),
3602+
"bytes",
3603+
com.google.cloud.firestore.Blob.fromBytes(new byte[] {1, 2, 3}),
3604+
"docRef",
3605+
collection.document("bar"),
3606+
"vector",
3607+
vector(new double[] {1.0, 2.0, 3.0}),
3608+
"map",
3609+
Expression.map(map("numberK", 1, "stringK", "a string")),
3610+
"array",
3611+
array(1, 2, true))))
3612+
.select(
3613+
Expression.isType("int", "int64").as("isInt64"),
3614+
Expression.isType("int", "number").as("isInt64IsNumber"),
3615+
Expression.isType("int", "decimal128").as("isInt64IsDecimal128"),
3616+
Expression.isType("float", "float64").as("isFloat64"),
3617+
Expression.isType("float", "number").as("isFloat64IsNumber"),
3618+
Expression.isType("float", "decimal128").as("isFloat64IsDecimal128"),
3619+
Expression.isType("str", "string").as("isStr"),
3620+
Expression.isType("str", "int64").as("isStrNum"),
3621+
Expression.isType("int", "string").as("isNumStr"),
3622+
Expression.isType("bool", "boolean").as("isBool"),
3623+
Expression.isType("null", "null").as("isNull"),
3624+
Expression.isType("geoPoint", "geo_point").as("isGeoPoint"),
3625+
Expression.isType("timestamp", "timestamp").as("isTimestamp"),
3626+
Expression.isType("bytes", "bytes").as("isBytes"),
3627+
Expression.isType("docRef", "reference").as("isDocRef"),
3628+
Expression.isType("vector", "vector").as("isVector"),
3629+
Expression.isType("map", "map").as("isMap"),
3630+
Expression.isType("array", "array").as("isArray"),
3631+
Expression.isType(constant(1), "int64").as("exprIsInt64"),
3632+
field("int").isType("int64").as("staticIsInt64"))
3633+
.limit(1)
3634+
.execute()
3635+
.get()
3636+
.getResults();
3637+
assertThat(data(results))
3638+
.containsExactly(
3639+
map(
3640+
"isInt64", true,
3641+
"isInt64IsNumber", true,
3642+
"isInt64IsDecimal128", false,
3643+
"isFloat64", true,
3644+
"isFloat64IsNumber", true,
3645+
"isFloat64IsDecimal128", false,
3646+
"isStr", true,
3647+
"isStrNum", false,
3648+
"isNumStr", false,
3649+
"isBool", true,
3650+
"isNull", true,
3651+
"isGeoPoint", true,
3652+
"isTimestamp", true,
3653+
"isBytes", true,
3654+
"isDocRef", true,
3655+
"isVector", true,
3656+
"isMap", true,
3657+
"isArray", true,
3658+
"exprIsInt64", true,
3659+
"staticIsInt64", true));
3660+
}
3661+
35793662
@Test
35803663
public void testExplainWithError() {
35813664
assumeFalse(

0 commit comments

Comments
 (0)