-
Notifications
You must be signed in to change notification settings - Fork 96
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fe9a711
commit e8fc9dd
Showing
8 changed files
with
351 additions
and
24 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
117 changes: 117 additions & 0 deletions
117
metricflow/dataflow/nodes/offset_by_custom_granularity.py
This file contains 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 |
---|---|---|
@@ -0,0 +1,117 @@ | ||
from __future__ import annotations | ||
|
||
from abc import ABC | ||
from dataclasses import dataclass | ||
from typing import Optional, Sequence | ||
|
||
from dbt_semantic_interfaces.protocols import MetricTimeWindow | ||
from dbt_semantic_interfaces.type_enums import TimeGranularity | ||
from metricflow_semantics.dag.id_prefix import IdPrefix, StaticIdPrefix | ||
from metricflow_semantics.dag.mf_dag import DisplayedProperty | ||
from metricflow_semantics.specs.time_dimension_spec import TimeDimensionSpec | ||
from metricflow_semantics.sql.sql_join_type import SqlJoinType | ||
from metricflow_semantics.visitor import VisitorOutputT | ||
|
||
from metricflow.dataflow.dataflow_plan import DataflowPlanNode | ||
from metricflow.dataflow.dataflow_plan_visitor import DataflowPlanNodeVisitor | ||
|
||
|
||
@dataclass(frozen=True, eq=False) | ||
class OffsetByCustomGranularityNode(DataflowPlanNode, ABC): | ||
"""Offset. | ||
Attributes: | ||
offset_window: Time dimensions requested in the query. | ||
join_type: Join type to use when joining to time spine. | ||
join_on_time_dimension_spec: The time dimension to use in the join ON condition. | ||
offset_window: Time window to offset the parent dataset by when joining to time spine. | ||
offset_to_grain: Granularity period to offset the parent dataset to when joining to time spine. | ||
""" | ||
|
||
time_spine_node: DataflowPlanNode | ||
requested_agg_time_dimension_specs: Sequence[TimeDimensionSpec] | ||
join_on_time_dimension_spec: TimeDimensionSpec | ||
join_type: SqlJoinType | ||
offset_window: Optional[MetricTimeWindow] | ||
offset_to_grain: Optional[TimeGranularity] | ||
|
||
def __post_init__(self) -> None: # noqa: D105 | ||
super().__post_init__() | ||
assert len(self.parent_nodes) == 1 | ||
|
||
assert not ( | ||
self.offset_window and self.offset_to_grain | ||
), "Can't set both offset_window and offset_to_grain when joining to time spine. Choose one or the other." | ||
assert ( | ||
len(self.requested_agg_time_dimension_specs) > 0 | ||
), "Must have at least one value in requested_agg_time_dimension_specs for JoinToTimeSpineNode." | ||
|
||
@staticmethod | ||
def create( # noqa: D102 | ||
parent_node: DataflowPlanNode, | ||
time_spine_node: DataflowPlanNode, | ||
requested_agg_time_dimension_specs: Sequence[TimeDimensionSpec], | ||
join_on_time_dimension_spec: TimeDimensionSpec, | ||
join_type: SqlJoinType, | ||
offset_window: Optional[MetricTimeWindow] = None, | ||
offset_to_grain: Optional[TimeGranularity] = None, | ||
) -> JoinToTimeSpineNode: | ||
return JoinToTimeSpineNode( | ||
parent_nodes=(parent_node,), | ||
time_spine_node=time_spine_node, | ||
requested_agg_time_dimension_specs=tuple(requested_agg_time_dimension_specs), | ||
join_on_time_dimension_spec=join_on_time_dimension_spec, | ||
join_type=join_type, | ||
offset_window=offset_window, | ||
offset_to_grain=offset_to_grain, | ||
) | ||
|
||
@classmethod | ||
def id_prefix(cls) -> IdPrefix: # noqa: D102 | ||
return StaticIdPrefix.DATAFLOW_NODE_JOIN_TO_TIME_SPINE_ID_PREFIX | ||
|
||
def accept(self, visitor: DataflowPlanNodeVisitor[VisitorOutputT]) -> VisitorOutputT: # noqa: D102 | ||
return visitor.visit_join_to_time_spine_node(self) | ||
|
||
@property | ||
def description(self) -> str: # noqa: D102 | ||
return """Join to Time Spine Dataset""" | ||
|
||
@property | ||
def displayed_properties(self) -> Sequence[DisplayedProperty]: # noqa: D102 | ||
props = tuple(super().displayed_properties) + ( | ||
DisplayedProperty("requested_agg_time_dimension_specs", self.requested_agg_time_dimension_specs), | ||
DisplayedProperty("join_on_time_dimension_spec", self.join_on_time_dimension_spec), | ||
DisplayedProperty("join_type", self.join_type), | ||
) | ||
if self.offset_window: | ||
props += (DisplayedProperty("offset_window", self.offset_window),) | ||
if self.offset_to_grain: | ||
props += (DisplayedProperty("offset_to_grain", self.offset_to_grain),) | ||
return props | ||
|
||
@property | ||
def parent_node(self) -> DataflowPlanNode: # noqa: D102 | ||
return self.parent_nodes[0] | ||
|
||
def functionally_identical(self, other_node: DataflowPlanNode) -> bool: # noqa: D102 | ||
return ( | ||
isinstance(other_node, self.__class__) | ||
and other_node.offset_window == self.offset_window | ||
and other_node.offset_to_grain == self.offset_to_grain | ||
and other_node.requested_agg_time_dimension_specs == self.requested_agg_time_dimension_specs | ||
and other_node.join_on_time_dimension_spec == self.join_on_time_dimension_spec | ||
and other_node.join_type == self.join_type | ||
) | ||
|
||
def with_new_parents(self, new_parent_nodes: Sequence[DataflowPlanNode]) -> JoinToTimeSpineNode: # noqa: D102 | ||
assert len(new_parent_nodes) == 1 | ||
return JoinToTimeSpineNode.create( | ||
parent_node=new_parent_nodes[0], | ||
time_spine_node=self.time_spine_node, | ||
requested_agg_time_dimension_specs=self.requested_agg_time_dimension_specs, | ||
offset_window=self.offset_window, | ||
offset_to_grain=self.offset_to_grain, | ||
join_type=self.join_type, | ||
join_on_time_dimension_spec=self.join_on_time_dimension_spec, | ||
) |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Oops, something went wrong.