|
29 | 29 | from google.cloud.firestore_v1.base_pipeline import _BasePipeline |
30 | 30 |
|
31 | 31 | 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 | +) |
33 | 39 | from google.cloud.firestore_v1.types.document import Pipeline as Pipeline_pb |
| 40 | +from google.cloud.firestore_v1.types.document import Value |
34 | 41 | from google.cloud.firestore_v1.types.query import StructuredQuery as Query_pb |
35 | 42 | from google.cloud.firestore_v1.vector import Vector |
36 | 43 |
|
|
48 | 55 | ) |
49 | 56 |
|
50 | 57 |
|
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 | | - |
187 | 58 | class Expression(ABC): |
188 | 59 | """Represents an expression that can be evaluated to a value within the |
189 | 60 | execution of a pipeline. |
@@ -2503,7 +2374,9 @@ def type(self) -> "Expression": |
2503 | 2374 | return FunctionExpression("type", [self]) |
2504 | 2375 |
|
2505 | 2376 | @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": |
2507 | 2380 | """Creates an expression that checks if the result is of the specified type. |
2508 | 2381 |
|
2509 | 2382 | Example: |
|
0 commit comments