This repository was archived by the owner on Mar 2, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 86
feat: literals pipeline stage #1170
Closed
Closed
Changes from all commits
Commits
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
Some comments aren't visible on the classic Files Changed page.
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
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -273,6 +273,71 @@ def find_nearest( | |||||||||||||||||||||||||
| stages.FindNearest(field, vector, distance_measure, options) | ||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| def literals(self, *documents: str | Selectable) -> "_BasePipeline": | ||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||
| Returns documents from a fixed set of predefined document objects. | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| This stage is commonly used for testing other stages in isolation, though it can | ||||||||||||||||||||||||||
| also be used as inputs to join conditions. | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| Example: | ||||||||||||||||||||||||||
| >>> from google.cloud.firestore_v1.pipeline_expressions import Constant | ||||||||||||||||||||||||||
| >>> documents = [ | ||||||||||||||||||||||||||
| ... {"name": "joe", "age": 10}, | ||||||||||||||||||||||||||
| ... {"name": "bob", "age": 30}, | ||||||||||||||||||||||||||
| ... {"name": "alice", "age": 40} | ||||||||||||||||||||||||||
| ... ] | ||||||||||||||||||||||||||
| >>> pipeline = client.pipeline() | ||||||||||||||||||||||||||
| ... .literals(Constant.of(documents)) | ||||||||||||||||||||||||||
| ... .where(field("age").lessThan(35)) | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| Output documents: | ||||||||||||||||||||||||||
| ```json | ||||||||||||||||||||||||||
| [ | ||||||||||||||||||||||||||
| {"name": "joe", "age": 10}, | ||||||||||||||||||||||||||
| {"name": "bob", "age": 30} | ||||||||||||||||||||||||||
| ] | ||||||||||||||||||||||||||
| ``` | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| Behavior: | ||||||||||||||||||||||||||
| The `literals(...)` stage can only be used as the first stage in a pipeline (or | ||||||||||||||||||||||||||
| sub-pipeline). The order of documents returned from the `literals` matches the | ||||||||||||||||||||||||||
| order in which they are defined. | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| While literal values are the most common, it is also possible to pass in | ||||||||||||||||||||||||||
| expressions, which will be evaluated and returned, making it possible to test | ||||||||||||||||||||||||||
| out different query / expression behavior without first needing to create some | ||||||||||||||||||||||||||
| test data. | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| For example, the following shows how to quickly test out the `length(...)` | ||||||||||||||||||||||||||
| function on some constant test sets: | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| Example: | ||||||||||||||||||||||||||
| >>> from google.cloud.firestore_v1.pipeline_expressions import Constant | ||||||||||||||||||||||||||
| >>> documents = [ | ||||||||||||||||||||||||||
| ... {"x": Constant.of("foo-bar-baz").char_length()}, | ||||||||||||||||||||||||||
| ... {"x": Constant.of("bar").char_length()} | ||||||||||||||||||||||||||
| ... ] | ||||||||||||||||||||||||||
| >>> pipeline = client.pipeline().literals(Constant.of(documents)) | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| Output documents: | ||||||||||||||||||||||||||
| ```json | ||||||||||||||||||||||||||
| [ | ||||||||||||||||||||||||||
| {"x": 11}, | ||||||||||||||||||||||||||
| {"x": 3} | ||||||||||||||||||||||||||
| ] | ||||||||||||||||||||||||||
| ``` | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| Args: | ||||||||||||||||||||||||||
| documents: A `str` or `Selectable` expression. If a `str`, it's | ||||||||||||||||||||||||||
| treated as a field path to an array of documents. | ||||||||||||||||||||||||||
| If a `Selectable`, it's usually a `Constant` | ||||||||||||||||||||||||||
| containing an array of documents (as dictionaries). | ||||||||||||||||||||||||||
|
Comment on lines
+331
to
+335
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The docstring for the
I suggest updating the docstring for clarity and correctness.
Suggested change
|
||||||||||||||||||||||||||
| Returns: | ||||||||||||||||||||||||||
| A new Pipeline object with this stage appended to the stage list. | ||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||
| return self._append(stages.Literals(*documents)) | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| def replace_with( | ||||||||||||||||||||||||||
| self, | ||||||||||||||||||||||||||
| field: Selectable, | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
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 | ||||
|---|---|---|---|---|---|---|
|
|
@@ -342,6 +342,23 @@ def _pb_args(self): | |||||
| return [Value(integer_value=self.limit)] | ||||||
|
|
||||||
|
|
||||||
| class Literals(Stage): | ||||||
| """Returns documents from a fixed set of predefined document objects.""" | ||||||
|
|
||||||
| def __init__(self, *documents: str | Selectable): | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The type hint for
Suggested change
|
||||||
| super().__init__("literals") | ||||||
| self.documents = documents | ||||||
|
|
||||||
| def _pb_args(self): | ||||||
| args = [] | ||||||
| for doc in self.documents: | ||||||
| if hasattr(doc, "_to_pb"): | ||||||
| args.append(doc._to_pb()) | ||||||
| else: | ||||||
| args.append(encode_value(doc)) | ||||||
| return args | ||||||
|
|
||||||
|
|
||||||
| class Offset(Stage): | ||||||
| """Skips a specified number of documents.""" | ||||||
|
|
||||||
|
|
||||||
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
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.
The type hint for
*documentsis a bit too restrictive. It doesn't account for passingdictobjects directly to represent documents, which is a common and valid use case. Please consider broadening it to includedict.