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

Commit c02ef30

Browse files
committed
save work
1 parent 1d92887 commit c02ef30

6 files changed

Lines changed: 738 additions & 0 deletions

File tree

google-cloud-firestore/src/main/java/com/google/cloud/firestore/Pipeline.java

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
import com.google.cloud.firestore.pipeline.stages.AddFields;
4242
import com.google.cloud.firestore.pipeline.stages.Aggregate;
4343
import com.google.cloud.firestore.pipeline.stages.AggregateOptions;
44+
import com.google.cloud.firestore.pipeline.stages.DefineStage;
4445
import com.google.cloud.firestore.pipeline.stages.Distinct;
4546
import com.google.cloud.firestore.pipeline.stages.FindNearest;
4647
import com.google.cloud.firestore.pipeline.stages.FindNearestOptions;
@@ -55,6 +56,7 @@
5556
import com.google.cloud.firestore.pipeline.stages.Sort;
5657
import com.google.cloud.firestore.pipeline.stages.Stage;
5758
import com.google.cloud.firestore.pipeline.stages.StageUtils;
59+
import com.google.cloud.firestore.pipeline.stages.SubcollectionSource;
5860
import com.google.cloud.firestore.pipeline.stages.Union;
5961
import com.google.cloud.firestore.pipeline.stages.Unnest;
6062
import com.google.cloud.firestore.pipeline.stages.UnnestOptions;
@@ -261,6 +263,51 @@ public Pipeline addFields(Selectable field, Selectable... additionalFields) {
261263
.toArray(new Selectable[0]))));
262264
}
263265

266+
/**
267+
* Initializes a pipeline scoped to a subcollection.
268+
*
269+
* @param path The path of the subcollection.
270+
* @return A new {@code Pipeline} instance scoped to the subcollection.
271+
*/
272+
@BetaApi
273+
public static Pipeline subcollection(String path) {
274+
return new Pipeline(null, new SubcollectionSource(path));
275+
}
276+
277+
/**
278+
* Adds new fields or redefines existing fields in the output documents by
279+
* evaluating given expressions.
280+
*
281+
* @param expressions The expressions to define or redefine fields using
282+
* {@link AliasedExpression}.
283+
* @return A new Pipeline object with this stage appended to the stage list.
284+
*/
285+
@BetaApi
286+
public Pipeline define(Selectable... expressions) {
287+
return append(
288+
new DefineStage(PipelineUtils.selectablesToMap(expressions)));
289+
}
290+
291+
/**
292+
* Converts the pipeline into an array expression.
293+
*
294+
* @return A new {@link Expression} representing the pipeline as an array.
295+
*/
296+
@BetaApi
297+
public Expression toArrayExpression() {
298+
return Expression.rawExpression("array", Expression.pipeline(this));
299+
}
300+
301+
/**
302+
* Converts the pipeline into a scalar expression.
303+
*
304+
* @return A new {@link Expression} representing the pipeline as a scalar.
305+
*/
306+
@BetaApi
307+
public Expression toScalarExpression() {
308+
return Expression.rawExpression("scalar", Expression.pipeline(this));
309+
}
310+
264311
/**
265312
* Remove fields from outputs of previous stages.
266313
*

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

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import com.google.cloud.firestore.FieldPath;
2525
import com.google.cloud.firestore.FieldValue;
2626
import com.google.cloud.firestore.GeoPoint;
27+
import com.google.cloud.firestore.Pipeline;
2728
import com.google.cloud.firestore.VectorValue;
2829
import com.google.common.collect.ImmutableList;
2930
import com.google.firestore.v1.Value;
@@ -227,6 +228,7 @@ public static Expression currentTimestamp() {
227228
return new FunctionExpression("current_timestamp", ImmutableList.of());
228229
}
229230

231+
230232
/**
231233
* Creates an expression that returns a default value if an expression evaluates to an absent
232234
* value.
@@ -4796,4 +4798,86 @@ public final Expression collectionId() {
47964798
public final Expression type() {
47974799
return type(this);
47984800
}
4801+
4802+
/**
4803+
* Creates an expression that represents the current document being processed.
4804+
*
4805+
* @return An {@link Expression} representing the current document.
4806+
*/
4807+
@BetaApi
4808+
public static Expression currentDocument() {
4809+
return new FunctionExpression("current_document", ImmutableList.of());
4810+
}
4811+
4812+
/**
4813+
* Creates an expression that retrieves the value of a variable bound via
4814+
* pipeline definitions.
4815+
*
4816+
* @param name The name of the variable to retrieve.
4817+
* @return An {@link Expression} representing the variable's value.
4818+
*/
4819+
@BetaApi
4820+
public static Expression variable(String name) {
4821+
return new Variable(name);
4822+
}
4823+
4824+
/**
4825+
* Accesses a field/property of the expression (useful when the expression
4826+
* evaluates to a Map or Document).
4827+
*
4828+
* @param expression The expression evaluating to a map/document.
4829+
* @param key The key of the field to access.
4830+
* @return An {@link Expression} representing the value of the field.
4831+
*/
4832+
@BetaApi
4833+
public static Expression field(Expression expression, String key) {
4834+
return new FunctionExpression("field", ImmutableList.of(expression, constant(key)));
4835+
}
4836+
4837+
/**
4838+
* Creates an expression that evaluates to the provided pipeline.
4839+
*
4840+
* @param pipeline The pipeline to use as an expression.
4841+
* @return A new {@link Expression} representing the pipeline value.
4842+
*/
4843+
@InternalApi
4844+
public static Expression pipeline(Pipeline pipeline) {
4845+
return new PipelineValueExpression(pipeline);
4846+
}
4847+
4848+
/**
4849+
* Internal expression representing a variable reference.
4850+
*
4851+
* <p>
4852+
* This evaluates to the value of a variable defined in a pipeline context.
4853+
*/
4854+
static class Variable extends Expression {
4855+
private final String name;
4856+
4857+
Variable(String name) {
4858+
this.name = name;
4859+
}
4860+
4861+
@Override
4862+
public Value toProto() {
4863+
return Value.newBuilder().setVariableReferenceValue(name).build();
4864+
}
4865+
}
4866+
4867+
/**
4868+
* Internal expression representing a pipeline value.
4869+
*/
4870+
static class PipelineValueExpression extends Expression {
4871+
private final Pipeline pipeline;
4872+
4873+
PipelineValueExpression(Pipeline pipeline) {
4874+
this.pipeline = pipeline;
4875+
}
4876+
4877+
@Override
4878+
public Value toProto() {
4879+
return pipeline.toProtoValue();
4880+
}
4881+
}
4882+
47994883
}

0 commit comments

Comments
 (0)