feat: Add subquery support in pipeline by cherylEnkidu · Pull Request #2323 · googleapis/java-firestore (original) (raw)
bhshkh added a commit to googleapis/google-cloud-go that referenced this pull request
Overview This PR introduces support for pipeline subqueries, variable definitions, and joins in the Go Firestore SDK, achieving strict 1:1 feature parity with the Java SDK's pipeline APIs googleapis/java-firestore#2323.
What are Subqueries? In Firestore pipelines, subqueries allow you to embed an entire pipeline execution as a value within a single stage of an outer pipeline. This is incredibly powerful for performing complex "join-like" operations across different collections.
For example, while querying a restaurants collection, you can use a subquery to fetch, filter, and aggregate all documents from a nested reviews subcollection, and embed that aggregated result (e.g., average_rating) directly into the restaurant document being returned. Subqueries can be evaluated into either an array of results (ToArrayExpression()) or a single scalar value (ToScalarExpression()).
Key Features & API Additions:
Subqueries (Joins):
Implemented the Subcollection(path string) package-level function to instantiate relative-scope pipelines.
Added ToScalarExpression() and ToArrayExpression() methods on Pipeline to explicitly convert subqueries into expressions, allowing them to be seamlessly embedded inside stages like AddFields and Where.
- Variable Definition & References:
Introduced the Define pipeline stage and the AliasedExpressions variadic helper to ergonomically bind values to variables.
Added Variable("name") and CurrentDocument() top-level functions to reference bound values in subsequent pipeline stages.
- Field Access & Overloading:
Implemented GetField (accepting any to support both string and Expression arguments, mirroring Java's method overloading).
Type Safety & Defensive Constraints:
Strict Aliasing: Expression.As() now explicitly returns *AliasedExpression (similar to Java: https://github.com/googleapis/java-firestore/blob/0c8188520dfbada0d3fef0719e4f95fc231306be/google-cloud-firestore/src/main/java/com/google/cloud/firestore/pipeline/expressions/Expression.java#L6608-L6621) rather than a generic Selectable interface, and the Define stage strictly requires []*AliasedExpression.
Pipeline Scope Validation: Added validation to ensure relative-scope pipelines (e.g., those created via Subcollection) cannot be executed directly or passed into a Union() stage. Attempts to do so now return descriptive errors identical to the Java SDK's IllegalStateException and IllegalArgumentException.