-
Notifications
You must be signed in to change notification settings - Fork 98
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
Support multiple time spines using new time spine configs #1348
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
kind: Features | ||
body: Support multiple time spines with different granularities. | ||
time: 2024-07-25T16:00:38.07984-07:00 | ||
custom: | ||
Author: courtneyholcomb | ||
Issue: "1348" |
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
2 changes: 1 addition & 1 deletion
2
metricflow-semantics/extra-hatch-configuration/requirements.txt
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 |
---|---|---|
@@ -1,5 +1,5 @@ | ||
# dbt Cloud depends on metricflow-semantics (dependency set in dbt-mantle), so DSI must always point to a production version here. | ||
dbt-semantic-interfaces>=0.6.9, <2.0.0 | ||
dbt-semantic-interfaces>=0.6.10, <2.0.0 | ||
graphviz>=0.18.2, <0.21 | ||
python-dateutil>=2.9.0, <2.10.0 | ||
rapidfuzz>=3.0, <4.0 |
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
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 |
---|---|---|
|
@@ -183,7 +183,7 @@ def __init__( | |
self._semantic_manifest_lookup = semantic_manifest_lookup | ||
self._metric_lookup = semantic_manifest_lookup.metric_lookup | ||
self._semantic_model_lookup = semantic_manifest_lookup.semantic_model_lookup | ||
self._time_spine_source = TimeSpineSource.create_from_manifest(semantic_manifest_lookup.semantic_manifest) | ||
self._time_spine_sources = TimeSpineSource.create_from_manifest(semantic_manifest_lookup.semantic_manifest) | ||
|
||
@property | ||
def column_association_resolver(self) -> ColumnAssociationResolver: # noqa: D102 | ||
|
@@ -222,24 +222,49 @@ def _next_unique_table_alias(self) -> str: | |
"""Return the next unique table alias to use in generating queries.""" | ||
return SequentialIdGenerator.create_next_id(StaticIdPrefix.SUB_QUERY).str_value | ||
|
||
def _choose_time_spine_source( | ||
self, agg_time_dimension_instances: Tuple[TimeDimensionInstance, ...] | ||
) -> TimeSpineSource: | ||
"""Determine which time spine source to use when building time spine dataset. | ||
|
||
Will choose the time spine with the largest granularity that can be used to get the smallest granularity requested | ||
in the agg time dimension instances. Example: | ||
- Time spines available: SECOND, MINUTE, DAY | ||
- Agg time dimension granularity needed for request: HOUR, DAY | ||
--> Selected time spine: MINUTE | ||
""" | ||
assert ( | ||
agg_time_dimension_instances | ||
), "Building time spine dataset requires agg_time_dimension_instances, but none were found." | ||
smallest_agg_time_grain = min(dim.spec.time_granularity for dim in agg_time_dimension_instances) | ||
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. Ok, this works but it'll start failing with custom granularities. Good thing for me to note here - we'll need to be able to go from the agg time dimension instance to the time spine index granularity and check up on that. |
||
compatible_time_spine_grains = [ | ||
grain for grain in self._time_spine_sources.keys() if grain.to_int() <= smallest_agg_time_grain.to_int() | ||
] | ||
if not compatible_time_spine_grains: | ||
raise RuntimeError( | ||
# TODO: update docs link when new docs are available | ||
f"This query requires a time spine with granularity {smallest_agg_time_grain.name} or smaller, which is not configured. " | ||
f"The smallest available time spine granularity is {min(self._time_spine_sources.keys()).name}, which is too large." | ||
"See documentation for how to configure a new time spine: https://docs.getdbt.com/docs/build/metricflow-time-spine" | ||
) | ||
return self._time_spine_sources[max(compatible_time_spine_grains)] | ||
|
||
def _make_time_spine_data_set( | ||
self, | ||
agg_time_dimension_instances: Tuple[TimeDimensionInstance, ...], | ||
time_spine_source: TimeSpineSource, | ||
time_range_constraint: Optional[TimeRangeConstraint] = None, | ||
) -> SqlDataSet: | ||
"""Make a time spine data set, which contains all date/time values like '2020-01-01', '2020-01-02'... | ||
"""Returns a dataset with a datetime column for each agg_time_dimension granularity requested. | ||
|
||
Returns a dataset with a column selected for each agg_time_dimension requested. | ||
Column alias will use 'metric_time' or the agg_time_dimension name depending on which the user requested. | ||
""" | ||
time_spine_instance_set = InstanceSet(time_dimension_instances=agg_time_dimension_instances) | ||
time_spine_table_alias = self._next_unique_table_alias() | ||
|
||
time_spine_source = self._choose_time_spine_source(agg_time_dimension_instances) | ||
column_expr = SqlColumnReferenceExpression.from_table_and_column_names( | ||
table_alias=time_spine_table_alias, column_name=time_spine_source.time_column_name | ||
) | ||
|
||
select_columns: Tuple[SqlSelectColumn, ...] = () | ||
apply_group_by = False | ||
for agg_time_dimension_instance in agg_time_dimension_instances: | ||
|
@@ -314,7 +339,6 @@ def visit_join_over_time_range_node(self, node: JoinOverTimeRangeNode) -> SqlDat | |
# Assemble time_spine dataset with requested agg time dimension instances selected. | ||
time_spine_data_set = self._make_time_spine_data_set( | ||
agg_time_dimension_instances=requested_agg_time_dimension_instances, | ||
time_spine_source=self._time_spine_source, | ||
time_range_constraint=node.time_range_constraint, | ||
) | ||
table_alias_to_instance_set[time_spine_data_set_alias] = time_spine_data_set.instance_set | ||
|
@@ -1234,7 +1258,6 @@ def visit_join_to_time_spine_node(self, node: JoinToTimeSpineNode) -> SqlDataSet | |
time_spine_alias = self._next_unique_table_alias() | ||
time_spine_dataset = self._make_time_spine_data_set( | ||
agg_time_dimension_instances=(agg_time_dimension_instance_for_join,), | ||
time_spine_source=self._time_spine_source, | ||
time_range_constraint=node.time_range_constraint, | ||
) | ||
|
||
|
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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
So I read below and I see why this is this way, but I think it's a bit weird - this is mainly an artifact of us expecting callers to wire in the time spine nodes as group by item source nodes.
I can't come up with anything obviously better, and it's probably outside the scope of this PR to do that refactor, but we should probably address this. I suspect it'll get confusing with things like custom calendar processing.