15
15
from metricflow_semantics .naming .linkable_spec_name import StructuredLinkableSpecName
16
16
from metricflow_semantics .specs .dimension_spec import DimensionSpec
17
17
from metricflow_semantics .specs .instance_spec import InstanceSpecVisitor
18
+ from metricflow_semantics .sql .sql_exprs import SqlWindowFunction
18
19
from metricflow_semantics .time .granularity import ExpandedTimeGranularity
19
20
from metricflow_semantics .visitor import VisitorOutputT
20
21
@@ -91,6 +92,8 @@ class TimeDimensionSpec(DimensionSpec): # noqa: D101
91
92
# Used for semi-additive joins. Some more thought is needed, but this may be useful in InstanceSpec.
92
93
aggregation_state : Optional [AggregationState ] = None
93
94
95
+ window_function : Optional [SqlWindowFunction ] = None
96
+
94
97
@property
95
98
def without_first_entity_link (self ) -> TimeDimensionSpec : # noqa: D102
96
99
assert len (self .entity_links ) > 0 , f"Spec does not have any entity links: { self } "
@@ -99,6 +102,8 @@ def without_first_entity_link(self) -> TimeDimensionSpec: # noqa: D102
99
102
entity_links = self .entity_links [1 :],
100
103
time_granularity = self .time_granularity ,
101
104
date_part = self .date_part ,
105
+ aggregation_state = self .aggregation_state ,
106
+ window_function = self .window_function ,
102
107
)
103
108
104
109
@property
@@ -108,6 +113,8 @@ def without_entity_links(self) -> TimeDimensionSpec: # noqa: D102
108
113
time_granularity = self .time_granularity ,
109
114
date_part = self .date_part ,
110
115
entity_links = (),
116
+ aggregation_state = self .aggregation_state ,
117
+ window_function = self .window_function ,
111
118
)
112
119
113
120
@property
@@ -153,6 +160,7 @@ def with_grain(self, time_granularity: ExpandedTimeGranularity) -> TimeDimension
153
160
time_granularity = time_granularity ,
154
161
date_part = self .date_part ,
155
162
aggregation_state = self .aggregation_state ,
163
+ window_function = self .window_function ,
156
164
)
157
165
158
166
def with_base_grain (self ) -> TimeDimensionSpec : # noqa: D102
@@ -162,6 +170,7 @@ def with_base_grain(self) -> TimeDimensionSpec: # noqa: D102
162
170
time_granularity = ExpandedTimeGranularity .from_time_granularity (self .time_granularity .base_granularity ),
163
171
date_part = self .date_part ,
164
172
aggregation_state = self .aggregation_state ,
173
+ window_function = self .window_function ,
165
174
)
166
175
167
176
def with_grain_and_date_part ( # noqa: D102
@@ -173,6 +182,7 @@ def with_grain_and_date_part( # noqa: D102
173
182
time_granularity = time_granularity ,
174
183
date_part = date_part ,
175
184
aggregation_state = self .aggregation_state ,
185
+ window_function = self .window_function ,
176
186
)
177
187
178
188
def with_aggregation_state (self , aggregation_state : AggregationState ) -> TimeDimensionSpec : # noqa: D102
@@ -182,6 +192,17 @@ def with_aggregation_state(self, aggregation_state: AggregationState) -> TimeDim
182
192
time_granularity = self .time_granularity ,
183
193
date_part = self .date_part ,
184
194
aggregation_state = aggregation_state ,
195
+ window_function = self .window_function ,
196
+ )
197
+
198
+ def with_window_function (self , window_function : SqlWindowFunction ) -> TimeDimensionSpec : # noqa: D102
199
+ return TimeDimensionSpec (
200
+ element_name = self .element_name ,
201
+ entity_links = self .entity_links ,
202
+ time_granularity = self .time_granularity ,
203
+ date_part = self .date_part ,
204
+ aggregation_state = self .aggregation_state ,
205
+ window_function = window_function ,
185
206
)
186
207
187
208
def comparison_key (self , exclude_fields : Sequence [TimeDimensionSpecField ] = ()) -> TimeDimensionSpecComparisonKey :
@@ -243,6 +264,7 @@ def with_entity_prefix(self, entity_prefix: EntityReference) -> TimeDimensionSpe
243
264
time_granularity = self .time_granularity ,
244
265
date_part = self .date_part ,
245
266
aggregation_state = self .aggregation_state ,
267
+ window_function = self .window_function ,
246
268
)
247
269
248
270
@staticmethod
0 commit comments