Repository navigation
feat(firestore): DML #14703
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
dlarocque
wants to merge
19
commits into
main
Choose a base branch
from
feat-java-pipeline-dml-remediations
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
feat(firestore): DML #14703
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
0b103de
feat(firestore): add DML stages (Insert, Upsert) and atomic execution…
wu-hui bef876e
refactor(firestore): add withDocumentIdExpression and rename document…
wu-hui 3bf07e3
refactor(firestore): add withAdditionalFields and rename transformedF…
wu-hui 194fec0
test(firestore): expand pipeline DML unit and integration test covera…
wu-hui f3c9c0b
refactor(firestore): align DML stages with GA API surface by removing…
wu-hui a7278e1
fix(firestore): evaluate expressions nested in pipeline literal values
dlarocque 9d19469
fix(firestore): validate pipeline literals documents and values clien…
dlarocque 09ebb29
feat(firestore): add List overloads for pipeline literals() and Updat…
dlarocque 123e7e2
feat(firestore): add Pipeline.insert() and Pipeline.delete(Delete); e…
dlarocque 9ee4d4a
refactor(firestore): remove unreleased alias methods from Insert and …
dlarocque 74d47c4
fix(firestore): reject atomic pipeline execution inside a transaction…
dlarocque 71d716a
docs(firestore): document pipeline DML stages, literals and atomic ex…
dlarocque f3360d3
fix(firestore): leave the empty pipeline literals() check to the backend
dlarocque 9b40f5c
docs(firestore): clean up pipeline DML comments
dlarocque 180ad3b
revert(firestore): don't reject atomic pipeline execution in a transa…
dlarocque 200aadb
refactor(firestore): reject FieldValue sentinels in literals through …
dlarocque a910279
docs(firestore): trim pipeline DML Javadoc
dlarocque b5b5713
refactor(firestore): drop unnecessary Delete/List overloads and liter…
dlarocque 578beed
Merge branch 'main' into feat-java-pipeline-dml-remediations
dlarocque File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
126 changes: 126 additions & 0 deletions
126
...ogle-cloud-firestore/src/main/java/com/google/cloud/firestore/pipeline/stages/Insert.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,126 @@ | ||
| /* | ||
| * Copyright 2026 Google LLC | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package com.google.cloud.firestore.pipeline.stages; | ||
|
|
||
| import com.google.cloud.firestore.PipelineUtils; | ||
| import com.google.cloud.firestore.pipeline.expressions.Expression; | ||
| import com.google.common.collect.ImmutableMap; | ||
| import com.google.firestore.v1.Value; | ||
| import java.util.ArrayList; | ||
| import java.util.HashMap; | ||
| import java.util.Map; | ||
| import javax.annotation.Nullable; | ||
|
|
||
| /** | ||
| * A pipeline stage that inserts each document produced by the previous stages as a new document. | ||
| * | ||
| * <p>The insert fails with {@code ALREADY_EXISTS} if a target document already exists. | ||
| * | ||
| * <p>The target path of each document is determined as follows: | ||
| * | ||
| * <ul> | ||
| * <li>With neither a collection nor a document ID expression, the input document's own path (its | ||
| * {@code __name__} field) is used, so inserting documents read from the database fails with | ||
| * {@code ALREADY_EXISTS}, and a document without a {@code __name__} field fails with {@code | ||
| * INVALID_ARGUMENT}. | ||
| * <li>With only a collection, the document is written to that collection under the input | ||
| * document's ID, or under a generated ID if the input document has no {@code __name__}. | ||
| * <li>With only a document ID expression, the document is written to the input document's parent | ||
| * collection under the ID that the expression evaluates to. | ||
| * <li>With both, the document is written to the collection under the ID that the expression | ||
| * evaluates to. | ||
| * </ul> | ||
| * | ||
| * <p>Example: | ||
| * | ||
| * <pre>{@code | ||
| * // Copy a document into another collection under a new ID. | ||
| * firestore.pipeline() | ||
| * .collection("books") | ||
| * .where(field("title").equal("Dune")) | ||
| * .insert(new Insert().withCollection("archive").withDocumentIdExpression(constant("dune"))) | ||
| * .execute() | ||
| * .get(); | ||
| * }</pre> | ||
| * | ||
| * @see com.google.cloud.firestore.Pipeline#insert(Insert) | ||
| */ | ||
| public final class Insert extends Stage { | ||
|
|
||
| @Nullable private final String collectionPath; | ||
| @Nullable private final Expression documentIdExpression; | ||
|
|
||
| private Insert( | ||
| @Nullable String collectionPath, | ||
| @Nullable Expression documentIdExpression, | ||
| InternalOptions options) { | ||
| super("insert", buildOptions(collectionPath, documentIdExpression, options)); | ||
| this.collectionPath = collectionPath; | ||
| this.documentIdExpression = documentIdExpression; | ||
| } | ||
|
|
||
| /** | ||
| * Creates an insert stage with no target collection and no document ID expression, so each | ||
| * document is inserted at the input document's own path. | ||
| */ | ||
| public Insert() { | ||
| this(null, null, InternalOptions.EMPTY); | ||
| } | ||
|
|
||
| /** | ||
| * Sets the collection that documents are inserted into. | ||
| * | ||
| * @param collectionPath The path of the target collection, for example {@code "books"} or {@code | ||
| * "users/alice/books"}. | ||
| * @return A new {@code Insert} stage with the target collection set. | ||
| */ | ||
| public Insert withCollection(String collectionPath) { | ||
| return new Insert(collectionPath, this.documentIdExpression, this.options); | ||
| } | ||
|
|
||
| /** | ||
| * Sets the expression that produces the ID of each inserted document. | ||
| * | ||
| * @param documentIdExpression An expression that is evaluated against each input document and | ||
| * must evaluate to a document ID (not a document path). Use {@code constant("id")} for a | ||
| * fixed ID. | ||
| * @return A new {@code Insert} stage with the document ID expression set. | ||
| */ | ||
| public Insert withDocumentIdExpression(Expression documentIdExpression) { | ||
| return new Insert(this.collectionPath, documentIdExpression, this.options); | ||
| } | ||
|
|
||
| private static InternalOptions buildOptions( | ||
| @Nullable String collectionPath, | ||
| @Nullable Expression documentIdExpression, | ||
| InternalOptions baseOptions) { | ||
| Map<String, Value> optsMap = new HashMap<>(baseOptions.options); | ||
| if (collectionPath != null) { | ||
| String path = collectionPath.startsWith("/") ? collectionPath : "/" + collectionPath; | ||
| optsMap.put("collection", Value.newBuilder().setReferenceValue(path).build()); | ||
| } | ||
| if (documentIdExpression != null) { | ||
| optsMap.put("document_id", PipelineUtils.encodeValue(documentIdExpression)); | ||
| } | ||
| return new InternalOptions(ImmutableMap.copyOf(optsMap)); | ||
| } | ||
|
|
||
| @Override | ||
| Iterable<Value> toStageArgs() { | ||
| return new ArrayList<>(); | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If
collectionPathordocumentIdExpressionis set tonull(for example, to clear them), the old values are copied frombaseOptions.optionsand retained in the final options map because they are never explicitly removed. This causes the object's state to become out of sync with the underlying proto options. We should explicitly remove these keys fromoptsMapif the corresponding parameters are null.