-
Notifications
You must be signed in to change notification settings - Fork 1.6k
feat(firestore): literals pipeline stage #16028
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -275,6 +275,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
+334
to
+337
Contributor
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
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, | ||||||||||||||||||
|
|
||||||||||||||||||
| 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): | ||
|
Contributor
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. |
||
| 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.""" | ||
|
|
||
|
|
||
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 incomplete. It should includedictas documents are often passed as dictionaries, which is not covered bystr | Selectable.