Repository navigation
feat(firestore): add over() frame override on WindowFunction #19148
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
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 |
|---|---|---|
|
|
@@ -16,6 +16,7 @@ | |
|
|
||
| import datetime | ||
| from abc import ABC, abstractmethod | ||
| from dataclasses import dataclass | ||
| from enum import Enum | ||
| from typing import ( | ||
| TYPE_CHECKING, | ||
|
|
@@ -31,8 +32,10 @@ | |
|
|
||
| from google.cloud.firestore_v1._helpers import GeoPoint, decode_value, encode_value | ||
| from google.cloud.firestore_v1.pipeline_types import ( | ||
| DocumentWindowFrame, | ||
| Ordering, | ||
| PipelineDataType, | ||
| RangeWindowFrame, | ||
| TimeGranularity, | ||
| TimePart, | ||
| TimeUnit, | ||
|
|
@@ -2685,6 +2688,38 @@ def _to_pb(self): | |
| class WindowFunction(FunctionExpression): | ||
| """A base class for window functions that operate over a window of documents.""" | ||
|
|
||
| def over( | ||
| self, | ||
| frame: DocumentWindowFrame | RangeWindowFrame, | ||
| ) -> "WindowFunction": | ||
| """Configures a window frame over which this function is evaluated. | ||
|
|
||
| Example: | ||
| >>> from google.cloud.firestore_v1.pipeline_expressions import Field | ||
| >>> from google.cloud.firestore_v1.pipeline_types import ( | ||
| ... DocumentWindowFrame, | ||
| ... WindowBound, | ||
| ... ) | ||
| >>> expr = ( | ||
| ... Field.of("rating") | ||
| ... .average() | ||
| ... .over(DocumentWindowFrame(1, WindowBound.CURRENT)) | ||
| ... .as_("moving_avg") | ||
| ... ) | ||
|
|
||
| Args: | ||
| frame: A `DocumentWindowFrame` or `RangeWindowFrame` specifying the | ||
| window frame override for this function. | ||
|
|
||
| Returns: | ||
| A `WindowFunction` with the specified window frame override. | ||
| """ | ||
| return WindowFunction( | ||
| "over", | ||
| [self, _WindowFrameExpression(frame)], | ||
| repr_function=lambda _: f"{self!r}.over({frame!r})", | ||
| ) | ||
|
|
||
|
|
||
| class AggregateFunction(WindowFunction): | ||
| """A base class for aggregation functions that operate across multiple inputs.""" | ||
|
|
@@ -2878,6 +2913,16 @@ def _to_pb(self) -> Value: | |
| return Value(pipeline_value=pipeline_pb) | ||
|
|
||
|
|
||
| @dataclass | ||
| class _WindowFrameExpression(Expression): | ||
|
Contributor
Author
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. Chosen Design: Wrap Alternatives Considered & Why Rejected:
|
||
| """Internal wrapper to represent a window frame as an expression.""" | ||
|
|
||
| frame: DocumentWindowFrame | RangeWindowFrame | ||
|
|
||
| def _to_pb(self) -> Value: | ||
| return Value(map_value={"fields": self.frame._to_dict()}) | ||
|
|
||
|
|
||
| class Array(FunctionExpression): | ||
| """ | ||
| Creates an expression that creates a Firestore array value from an input list. | ||
|
|
||
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.
Chosen Design: Define
over(frame)directly onWindowFunction(inherited byAggregateFunction, which subclassesWindowFunction), returning aWindowFunctionand letting the backend reject invalid pairings likeRank().over(frame)(verified inwindow.yaml).Alternatives Considered & Why Rejected:
over(frame)only onAggregateFunction: Excludes upcoming non-aggregate window functions that do accept frames (e.g.,first_value,last_value) or requires a third intermediate class solely for framing.Rank().over(frame): Duplicates backend validation and risks drifting as window function support evolves; aligns with Node.js and Java.