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

Commit 746ded3

Browse files
committed
Add support for the parent expression
1 parent f8adbaa commit 746ded3

2 files changed

Lines changed: 69 additions & 0 deletions

File tree

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

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4364,6 +4364,39 @@ public static Expression collectionId(String pathFieldName) {
43644364
return collectionId(field(pathFieldName));
43654365
}
43664366

4367+
/**
4368+
* Creates an expression that returns the parent document of a document reference.
4369+
*
4370+
* @param documentPath An expression that evaluates to a document path.
4371+
* @return A new {@link Expression} representing the parent operation.
4372+
*/
4373+
@BetaApi
4374+
public static Expression parent(Expression documentPath) {
4375+
return new FunctionExpression("parent", ImmutableList.of(documentPath));
4376+
}
4377+
4378+
/**
4379+
* Creates an expression that returns the parent document of a document reference.
4380+
*
4381+
* @param documentPath The string representation of the document path.
4382+
* @return A new {@link Expression} representing the parent operation.
4383+
*/
4384+
@BetaApi
4385+
public static Expression parent(String documentPath) {
4386+
return parent(constant(documentPath));
4387+
}
4388+
4389+
/**
4390+
* Creates an expression that returns the parent document of a document reference.
4391+
*
4392+
* @param docRef The {@link DocumentReference}.
4393+
* @return A new {@link Expression} representing the parent operation.
4394+
*/
4395+
@BetaApi
4396+
public static Expression parent(DocumentReference docRef) {
4397+
return parent(constant(docRef));
4398+
}
4399+
43674400
// Type Checking Functions
43684401
/**
43694402
* Creates an expression that checks if a field exists.
@@ -7016,6 +7049,16 @@ public final Expression collectionId() {
70167049
return collectionId(this);
70177050
}
70187051

7052+
/**
7053+
* Creates an expression that returns the parent document of a document reference.
7054+
*
7055+
* @return A new {@link Expression} representing the parent operation.
7056+
*/
7057+
@BetaApi
7058+
public final Expression parent() {
7059+
return parent(this);
7060+
}
7061+
70197062
/**
70207063
* Creates an expression that returns a string indicating the type of the value this expression
70217064
* evaluates to.

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@
7272
import static com.google.cloud.firestore.pipeline.expressions.Expression.notEqual;
7373
import static com.google.cloud.firestore.pipeline.expressions.Expression.nullValue;
7474
import static com.google.cloud.firestore.pipeline.expressions.Expression.or;
75+
import static com.google.cloud.firestore.pipeline.expressions.Expression.parent;
7576
import static com.google.cloud.firestore.pipeline.expressions.Expression.pow;
7677
import static com.google.cloud.firestore.pipeline.expressions.Expression.rand;
7778
import static com.google.cloud.firestore.pipeline.expressions.Expression.regexMatch;
@@ -109,6 +110,7 @@
109110
import com.google.cloud.Timestamp;
110111
import com.google.cloud.firestore.Blob;
111112
import com.google.cloud.firestore.CollectionReference;
113+
import com.google.cloud.firestore.DocumentReference;
112114
import com.google.cloud.firestore.Firestore;
113115
import com.google.cloud.firestore.FirestoreOptions;
114116
import com.google.cloud.firestore.GeoPoint;
@@ -4292,4 +4294,28 @@ public void disallowDuplicateAliasesAcrossStages() {
42924294
});
42934295
assertThat(exception).hasMessageThat().contains("Duplicate alias or field name");
42944296
}
4297+
4298+
@Test
4299+
public void testSupportsParent() throws Exception {
4300+
DocumentReference docRef = collection.document("book4").collection("reviews").document("review1");
4301+
4302+
Pipeline pipeline =
4303+
firestore
4304+
.pipeline()
4305+
.collection(collection.getPath())
4306+
.limit(1)
4307+
.select(
4308+
parent(docRef).as("parentRefStatic"),
4309+
constant(docRef).parent().as("parentRefInstance"))
4310+
.select(
4311+
field("parentRefStatic").documentId().as("parentIdStatic"),
4312+
field("parentRefInstance").documentId().as("parentIdInstance"));
4313+
4314+
List<PipelineResult> results = pipeline.execute().get().getResults();
4315+
assertThat(results).hasSize(1);
4316+
Map<String, Object> data = results.get(0).getData();
4317+
4318+
assertThat(data.get("parentIdStatic")).isEqualTo("book4");
4319+
assertThat(data.get("parentIdInstance")).isEqualTo("book4");
4320+
}
42954321
}

0 commit comments

Comments
 (0)