Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 1.8k
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
Merged
Uh oh!
There was an error while loading. Please reload this page.
Merged
Changes from all commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
2a8213c
feat: literals pipeline stage
Linchin 28acee9
add dict as supported type and update docstring
Linchin a2e11ed
correct docstring and type annotation
Linchin a97cb13
type annotation
Linchin cc1b737
add more unit tests
Linchin 3861223
correct docstring
Linchin 7bad13c
add cases to system test
Linchin 36e45bf
fix system test
Linchin 816498e
move literals to pipeline_source.py
Linchin 09f59f6
correct system test
Linchin 11c22ee
type annotation and add unit tests
Linchin 941101f
docstring
Linchin ccb159a
type annotation
Linchin 6aae503
add more system test
Linchin fa30d5f
update to accept Expression
Linchin db98e44
lint
Linchin 4a85ee6
lint
Linchin 5627c75
remove languange about join stage
Linchin 0dbfc77
use dict instead of Mapping for type hint
Linchin 837e52c
improve wording for args docstring
Linchin fc2880d
remove unused code and test
Linchin a515903
correct unit test cases
Linchin eee827e
remove unused typing
Linchin c4c8398
mypy
Linchin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
65 changes: 64 additions & 1 deletion
65 packages/google-cloud-firestore/google/cloud/firestore_v1/pipeline_source.py
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
23 changes: 22 additions & 1 deletion
23 packages/google-cloud-firestore/google/cloud/firestore_v1/pipeline_stages.py
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
61 changes: 60 additions & 1 deletion
61 packages/google-cloud-firestore/tests/system/pipeline_e2e/general.yaml
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 |
|---|---|---|
| @@ -684,4 +684,63 @@ tests: | ||
| - args: | ||
| - fieldReferenceValue: awards | ||
| - stringValue: full_replace | ||
| name: replace_with | ||
| name: replace_with | ||
| - description: literals | ||
| pipeline: | ||
| - Literals: | ||
| - title: "The Hitchhiker's Guide to the Galaxy" | ||
| author: "Douglas Adams" | ||
daniel-sanche marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| - genre: "Science Fiction" | ||
| year: 1979 | ||
| assert_results: | ||
| - title: "The Hitchhiker's Guide to the Galaxy" | ||
| author: "Douglas Adams" | ||
| - genre: "Science Fiction" | ||
| year: 1979 | ||
| assert_proto: | ||
| pipeline: | ||
| stages: | ||
| - args: | ||
| - mapValue: | ||
| fields: | ||
| author: | ||
| stringValue: "Douglas Adams" | ||
| title: | ||
| stringValue: "The Hitchhiker's Guide to the Galaxy" | ||
| - mapValue: | ||
| fields: | ||
| genre: | ||
| stringValue: "Science Fiction" | ||
| year: | ||
| integerValue: '1979' | ||
| name: literals | ||
| - description: literals_with_expression_input | ||
| pipeline: | ||
| - Literals: | ||
| - res: | ||
| FunctionExpression.string_concat: | ||
| - Constant: "A" | ||
| - Constant: "B" | ||
| - Select: | ||
| - res | ||
| assert_results: | ||
| - res: "AB" | ||
| assert_proto: | ||
| pipeline: | ||
| stages: | ||
| - args: | ||
| - mapValue: | ||
| fields: | ||
| res: | ||
| functionValue: | ||
| args: | ||
| - stringValue: "A" | ||
| - stringValue: "B" | ||
| name: string_concat | ||
| name: literals | ||
| - args: | ||
| - mapValue: | ||
| fields: | ||
| res: | ||
| fieldReferenceValue: res | ||
| name: select | ||
11 changes: 11 additions & 0 deletions
11 packages/google-cloud-firestore/tests/unit/v1/test_pipeline_source.py
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
106 changes: 106 additions & 0 deletions
106 packages/google-cloud-firestore/tests/unit/v1/test_pipeline_stages.py
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 |
|---|---|---|
| @@ -517,6 +517,112 @@ def test_to_pb(self): | ||
| assert len(result.options) == 0 | ||
| class TestLiterals: | ||
| def _make_one(self, *args, **kwargs): | ||
| return stages.Literals(*args, **kwargs) | ||
| def test_ctor(self): | ||
| val1 = {"a": Field.of("a")} | ||
| val2 = {"b": 2} | ||
| instance = self._make_one(val1, val2) | ||
| assert instance.documents == (val1, val2) | ||
| assert instance.name == "literals" | ||
daniel-sanche marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| def test_ctor_extended_types(self): | ||
| import datetime | ||
| from google.cloud.firestore_v1._helpers import GeoPoint | ||
| from google.cloud.firestore_v1.vector import Vector | ||
| doc = { | ||
| "a": 1, | ||
| "b": "string", | ||
| "c": 3.14, | ||
| "d": True, | ||
| "e": None, | ||
| "f": b"bytes", | ||
| "g": datetime.datetime(2025, 1, 1, tzinfo=datetime.timezone.utc), | ||
| "h": GeoPoint(1.0, 2.0), | ||
| "i": Vector([1.0, 2.0]), | ||
| } | ||
| instance = self._make_one(doc) | ||
| assert instance.documents == (doc,) | ||
| assert instance.name == "literals" | ||
| def test_ctor_w_expressions(self): | ||
| from google.cloud.firestore_v1.pipeline_expressions import FunctionExpression | ||
| expr = FunctionExpression("string_concat", [Constant("A"), Constant("B")]) | ||
| doc = {"res": expr} | ||
| instance = self._make_one(doc) | ||
| assert instance.documents == (doc,) | ||
| assert instance.name == "literals" | ||
| def test_repr(self): | ||
| from google.cloud.firestore_v1.pipeline_expressions import Field | ||
| val1 = {"a": Field.of("a")} | ||
| instance = self._make_one(val1, {"b": 2}) | ||
| repr_str = repr(instance) | ||
| assert repr_str == "Literals(documents=({'a': Field.of('a')}, {'b': 2}))" | ||
| def test_to_pb_constant_types(self): | ||
| import datetime | ||
| from google.cloud.firestore_v1._helpers import GeoPoint | ||
| from google.cloud.firestore_v1.vector import Vector | ||
| doc = { | ||
| "a": 1, | ||
| "b": "string", | ||
| "c": 3.14, | ||
| "d": True, | ||
| "e": None, | ||
| "f": b"bytes", | ||
| "g": datetime.datetime(2025, 1, 1, tzinfo=datetime.timezone.utc), | ||
| "h": GeoPoint(1.0, 2.0), | ||
| "i": Vector([1.0, 2.0]), | ||
| } | ||
| instance = self._make_one(doc) | ||
| result = instance._to_pb() | ||
| assert result.name == "literals" | ||
| assert len(result.args) == 1 | ||
| fields = result.args[0].map_value.fields | ||
| assert fields["a"].integer_value == 1 | ||
| assert fields["b"].string_value == "string" | ||
| assert fields["c"].double_value == 3.14 | ||
| assert fields["d"].boolean_value is True | ||
| assert fields["e"].null_value == 0 | ||
| assert fields["f"].bytes_value == b"bytes" | ||
| assert fields["g"].timestamp_value == datetime.datetime( | ||
| 2025, 1, 1, tzinfo=datetime.timezone.utc | ||
| ) | ||
| assert fields["h"].geo_point_value.latitude == 1.0 | ||
| assert fields["h"].geo_point_value.longitude == 2.0 | ||
| assert ( | ||
| fields["i"].map_value.fields["value"].array_value.values[0].double_value | ||
| == 1.0 | ||
| ) | ||
| assert ( | ||
| fields["i"].map_value.fields["value"].array_value.values[1].double_value | ||
| == 2.0 | ||
| ) | ||
| def test_to_pb_w_expression(self): | ||
| from google.cloud.firestore_v1.pipeline_expressions import FunctionExpression | ||
| expr = FunctionExpression("string_concat", [Constant("A"), Constant("B")]) | ||
| doc = {"res": expr} | ||
| instance = self._make_one(doc) | ||
| result = instance._to_pb() | ||
| assert result.name == "literals" | ||
| assert len(result.args) == 1 | ||
| fields = result.args[0].map_value.fields | ||
| assert fields["res"].function_value.name == "string_concat" | ||
| assert fields["res"].function_value.args[0].string_value == "A" | ||
| assert fields["res"].function_value.args[1].string_value == "B" | ||
| class TestOffset: | ||
| def _make_one(self, *args, **kwargs): | ||
| return stages.Offset(*args, **kwargs) | ||
Oops, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
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.
Uh oh!
There was an error while loading. Please reload this page.
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.
I'd prefer if we didn't use Any here, since this is a publicly exposed attribute
We could resolve it using a TypeVar:
T = TypeVar("T", bound=Union[Expression, CONSTANT_TYPE])But honestly, I'd recommend converting the constants at init time instead, which would both resolve this issue, raise conversion errors in a better place, and simplify the code:
Uh oh!
There was an error while loading. Please reload this page.
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.
I'm not sure if it's a good idea because encoding the values here could break the
__repr__()method at the parent class, and we would expose the encoded values in the repr string. For example, instead of"Literals(documents=('hello',))", we would be getting"Literals(documents=(Value(string_value='hello'),))".What do you think?
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.
Sorry, I should have tested the code before posting. Value isn't an expression, so that wouldn't have passed mypy. This should though:
This would result in the repr of
Literals({"hello": "world"}))beingLiterals(documents=[{"hello": Constant.of('world')}]), which is fine, because represents the same object. And that's how the other stages are set up