Skip to content

Commit 36d49e3

Browse files
committed
moved options and enums into pipeline_types
1 parent 3ab3312 commit 36d49e3

4 files changed

Lines changed: 348 additions & 299 deletions

File tree

packages/google-cloud-firestore/google/cloud/firestore_v1/base_pipeline.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919

2020
from google.cloud.firestore_v1 import pipeline_stages as stages
21+
from google.cloud.firestore_v1 import pipeline_types as types
2122
from google.cloud.firestore_v1.base_vector_query import DistanceMeasure
2223
from google.cloud.firestore_v1.pipeline_expressions import (
2324
AggregateFunction,
@@ -275,7 +276,7 @@ def find_nearest(
275276
field: str | Expression,
276277
vector: Sequence[float] | "Vector",
277278
distance_measure: "DistanceMeasure",
278-
options: stages.FindNearestOptions | None = None,
279+
options: types.FindNearestOptions | None = None,
279280
) -> "_BasePipeline":
280281
"""
281282
Performs vector distance (similarity) search with given parameters on the
@@ -396,7 +397,7 @@ def sort(self, *orders: stages.Ordering) -> "_BasePipeline":
396397
return self._append(stages.Sort(*orders))
397398

398399
def search(
399-
self, query_or_options: str | BooleanExpression | stages.SearchOptions
400+
self, query_or_options: str | BooleanExpression | types.SearchOptions
400401
) -> "_BasePipeline":
401402
"""
402403
Adds a search stage to the pipeline.
@@ -426,7 +427,7 @@ def search(
426427
"""
427428
return self._append(stages.Search(query_or_options))
428429

429-
def sample(self, limit_or_options: int | stages.SampleOptions) -> "_BasePipeline":
430+
def sample(self, limit_or_options: int | types.SampleOptions) -> "_BasePipeline":
430431
"""
431432
Performs a pseudo-random sampling of the documents from the previous stage.
432433
@@ -490,7 +491,7 @@ def unnest(
490491
self,
491492
field: str | Selectable,
492493
alias: str | Field | None = None,
493-
options: stages.UnnestOptions | None = None,
494+
options: types.UnnestOptions | None = None,
494495
) -> "_BasePipeline":
495496
"""
496497
Produces a document for each element in an array field from the previous stage document.

packages/google-cloud-firestore/google/cloud/firestore_v1/pipeline_expressions.py

Lines changed: 11 additions & 138 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,15 @@
2929
from google.cloud.firestore_v1.base_pipeline import _BasePipeline
3030

3131
from google.cloud.firestore_v1._helpers import GeoPoint, decode_value, encode_value
32-
from google.cloud.firestore_v1.types.document import Value
32+
from google.cloud.firestore_v1.pipeline_types import (
33+
Ordering,
34+
PipelineDataType,
35+
TimeGranularity,
36+
TimePart,
37+
TimeUnit,
38+
)
3339
from google.cloud.firestore_v1.types.document import Pipeline as Pipeline_pb
40+
from google.cloud.firestore_v1.types.document import Value
3441
from google.cloud.firestore_v1.types.query import StructuredQuery as Query_pb
3542
from google.cloud.firestore_v1.vector import Vector
3643

@@ -48,142 +55,6 @@
4855
)
4956

5057

51-
class TimeUnit(str, Enum):
52-
"""Enumeration of the different time units supported by the Firestore backend."""
53-
54-
MICROSECOND = "microsecond"
55-
MILLISECOND = "millisecond"
56-
SECOND = "second"
57-
MINUTE = "minute"
58-
HOUR = "hour"
59-
DAY = "day"
60-
61-
62-
class TimeGranularity(str, Enum):
63-
"""Enumeration of the different time granularities supported by the Firestore backend."""
64-
65-
# Inherit from TimeUnit
66-
MICROSECOND = TimeUnit.MICROSECOND.value
67-
MILLISECOND = TimeUnit.MILLISECOND.value
68-
SECOND = TimeUnit.SECOND.value
69-
MINUTE = TimeUnit.MINUTE.value
70-
HOUR = TimeUnit.HOUR.value
71-
DAY = TimeUnit.DAY.value
72-
73-
# Additional granularities
74-
WEEK = "week"
75-
WEEK_MONDAY = "week(monday)"
76-
WEEK_TUESDAY = "week(tuesday)"
77-
WEEK_WEDNESDAY = "week(wednesday)"
78-
WEEK_THURSDAY = "week(thursday)"
79-
WEEK_FRIDAY = "week(friday)"
80-
WEEK_SATURDAY = "week(saturday)"
81-
WEEK_SUNDAY = "week(sunday)"
82-
ISOWEEK = "isoweek"
83-
MONTH = "month"
84-
QUARTER = "quarter"
85-
YEAR = "year"
86-
ISOYEAR = "isoyear"
87-
88-
89-
class TimePart(str, Enum):
90-
"""Enumeration of the different time parts supported by the Firestore backend."""
91-
92-
# Inherit from TimeUnit
93-
MICROSECOND = TimeUnit.MICROSECOND.value
94-
MILLISECOND = TimeUnit.MILLISECOND.value
95-
SECOND = TimeUnit.SECOND.value
96-
MINUTE = TimeUnit.MINUTE.value
97-
HOUR = TimeUnit.HOUR.value
98-
DAY = TimeUnit.DAY.value
99-
100-
# Inherit from TimeGranularity
101-
WEEK = TimeGranularity.WEEK.value
102-
WEEK_MONDAY = TimeGranularity.WEEK_MONDAY.value
103-
WEEK_TUESDAY = TimeGranularity.WEEK_TUESDAY.value
104-
WEEK_WEDNESDAY = TimeGranularity.WEEK_WEDNESDAY.value
105-
WEEK_THURSDAY = TimeGranularity.WEEK_THURSDAY.value
106-
WEEK_FRIDAY = TimeGranularity.WEEK_FRIDAY.value
107-
WEEK_SATURDAY = TimeGranularity.WEEK_SATURDAY.value
108-
WEEK_SUNDAY = TimeGranularity.WEEK_SUNDAY.value
109-
ISOWEEK = TimeGranularity.ISOWEEK.value
110-
MONTH = TimeGranularity.MONTH.value
111-
QUARTER = TimeGranularity.QUARTER.value
112-
YEAR = TimeGranularity.YEAR.value
113-
ISOYEAR = TimeGranularity.ISOYEAR.value
114-
115-
# Additional parts
116-
DAY_OF_WEEK = "dayofweek"
117-
DAY_OF_YEAR = "dayofyear"
118-
119-
120-
class Ordering:
121-
"""Represents the direction for sorting results in a pipeline."""
122-
123-
class Direction(Enum):
124-
ASCENDING = "ascending"
125-
DESCENDING = "descending"
126-
127-
def __init__(self, expr, order_dir: Direction | str = Direction.ASCENDING):
128-
"""
129-
Initializes an Ordering instance
130-
131-
Args:
132-
expr (Expression | str): The expression or field path string to sort by.
133-
If a string is provided, it's treated as a field path.
134-
order_dir (Direction | str): The direction to sort in.
135-
Defaults to ascending
136-
"""
137-
self.expr = expr if isinstance(expr, Expression) else Field.of(expr)
138-
self.order_dir = (
139-
Ordering.Direction[order_dir.upper()]
140-
if isinstance(order_dir, str)
141-
else order_dir
142-
)
143-
144-
def __repr__(self):
145-
if self.order_dir is Ordering.Direction.ASCENDING:
146-
order_str = ".ascending()"
147-
else:
148-
order_str = ".descending()"
149-
return f"{self.expr!r}{order_str}"
150-
151-
def _to_pb(self) -> Value:
152-
return Value(
153-
map_value={
154-
"fields": {
155-
"direction": Value(string_value=self.order_dir.value),
156-
"expression": self.expr._to_pb(),
157-
}
158-
}
159-
)
160-
161-
162-
class PipelineDataType(str, Enum):
163-
"""Enumeration of the different types generated by the Firestore backend."""
164-
165-
NULL = "null"
166-
ARRAY = "array"
167-
BOOLEAN = "boolean"
168-
BYTES = "bytes"
169-
TIMESTAMP = "timestamp"
170-
GEO_POINT = "geo_point"
171-
NUMBER = "number"
172-
INT32 = "int32"
173-
INT64 = "int64"
174-
FLOAT64 = "float64"
175-
DECIMAL128 = "decimal128"
176-
MAP = "map"
177-
REFERENCE = "reference"
178-
STRING = "string"
179-
VECTOR = "vector"
180-
MAX_KEY = "max_key"
181-
MIN_KEY = "min_key"
182-
OBJECT_ID = "object_id"
183-
REGEX = "regex"
184-
REQUEST_TIMESTAMP = "request_timestamp"
185-
186-
18758
class Expression(ABC):
18859
"""Represents an expression that can be evaluated to a value within the
18960
execution of a pipeline.
@@ -2503,7 +2374,9 @@ def type(self) -> "Expression":
25032374
return FunctionExpression("type", [self])
25042375

25052376
@expose_as_static
2506-
def is_type(self, type_val: PipelineDataType | str | Expression) -> "BooleanExpression":
2377+
def is_type(
2378+
self, type_val: PipelineDataType | str | Expression
2379+
) -> "BooleanExpression":
25072380
"""Creates an expression that checks if the result is of the specified type.
25082381
25092382
Example:

0 commit comments

Comments
 (0)