-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
default_granularity
to metric spec (#291)
Resolves #290 ### Description Now that we are enabling sub-daily granularities, the current behavior of `metric_time` will likely be unexpected to users. If they change any of their time dimensions to use a grain smaller than DAY, `metric_time` will default to that grain. To avoid this confusion, add a parameter that allows users to choose which grain they want their metric to default to. This will default to DAY if not set to avoid the unexpected behavior change. (If DAY is smaller than the defined granularity, will default to the smallest available granularity.) ### Checklist - [x] I have read [the contributing guide](https://github.com/dbt-labs/dbt-semantic-interfaces/blob/main/CONTRIBUTING.md) and understand what's expected of me - [x] I have signed the [CLA](https://docs.getdbt.com/docs/contributor-license-agreements) - [x] This PR includes tests, or tests are not required/relevant for this PR - [x] I have run `changie new` to [create a changelog entry](https://github.com/dbt-labs/dbt-semantic-interfaces/blob/main/CONTRIBUTING.md#adding-a-changelog-entry)
- Loading branch information
1 parent
d41075e
commit 1eb584b
Showing
14 changed files
with
371 additions
and
11 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
kind: Features | ||
body: Add default_granularity to metric spec. | ||
time: 2024-06-14T14:05:15.355931-07:00 | ||
custom: | ||
Author: courtneyholcomb | ||
Issue: "290" |
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
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 |
---|---|---|
@@ -0,0 +1,48 @@ | ||
from typing import Set | ||
|
||
from typing_extensions import override | ||
|
||
from dbt_semantic_interfaces.implementations.semantic_manifest import ( | ||
PydanticSemanticManifest, | ||
) | ||
from dbt_semantic_interfaces.protocols import ProtocolHint | ||
from dbt_semantic_interfaces.references import ( | ||
DimensionReference, | ||
TimeDimensionReference, | ||
) | ||
from dbt_semantic_interfaces.transformations.transform_rule import ( | ||
SemanticManifestTransformRule, | ||
) | ||
from dbt_semantic_interfaces.type_enums.time_granularity import TimeGranularity | ||
|
||
|
||
class SetDefaultGrainRule(ProtocolHint[SemanticManifestTransformRule[PydanticSemanticManifest]]): | ||
"""If default_granularity is not set for a metric, set it to DAY if available, else the smallest available grain.""" | ||
|
||
@override | ||
def _implements_protocol(self) -> SemanticManifestTransformRule[PydanticSemanticManifest]: # noqa: D | ||
return self | ||
|
||
@staticmethod | ||
def transform_model(semantic_manifest: PydanticSemanticManifest) -> PydanticSemanticManifest: | ||
"""For each metric, set default_granularity to DAY or smallest granularity supported by all agg_time_dims.""" | ||
for metric in semantic_manifest.metrics: | ||
if metric.default_granularity: | ||
continue | ||
|
||
default_granularity = TimeGranularity.DAY | ||
seen_agg_time_dimensions: Set[TimeDimensionReference] = set() | ||
for semantic_model in semantic_manifest.semantic_models: | ||
for measure_ref in set(metric.measure_references).intersection(semantic_model.measure_references): | ||
agg_time_dimension_ref = semantic_model.checked_agg_time_dimension_for_measure(measure_ref) | ||
if agg_time_dimension_ref in seen_agg_time_dimensions: | ||
continue | ||
seen_agg_time_dimensions.add(agg_time_dimension_ref) | ||
dimension = semantic_model.get_dimension(DimensionReference(agg_time_dimension_ref.element_name)) | ||
if ( | ||
dimension.type_params | ||
and dimension.type_params.time_granularity.to_int() > default_granularity.to_int() | ||
): | ||
default_granularity = dimension.type_params.time_granularity | ||
|
||
return semantic_manifest |
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.