-
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.
Update support for custom granularity in other types (#365)
<!--- Include the number of the issue addressed by this PR above if applicable. PRs for code changes without an associated issue *will not be merged*. See CONTRIBUTING.md for more information. --> ### Description Support custom grain by switching the typing to be not on `TimeGranularity` - `Metric.time_granularity` - `TimeGranularity -> str` - `Metric.type_params.cumulative_type_params.grain_to_date` - `TimeGranularity -> str` - `MetricInput.offset_to_grain` - `TimeGranularity -> str` - `MetricTimeWindow.granularity` - `TimeGranularity -> str` affects the following - `Metric.type_params.conversion_type_params.window` - `Metric.type_params.cumulative_type_params.window` - `MetricInput.offset_window` #### Changes to `MetricTimeWindow` Because `MetricTimeWindow.granularity` is now a string, we allow it to be not as strict when ensuring it's a valid granularity during initial yaml parsing (since we don't have access to the custom granularities). Then during validations, we will properly validate that it's a valid grain, and during the transformation step we will remove trailing 's'. <!--- Describe the Pull Request here. Add any references and info to help reviewers understand your changes. Include any tradeoffs you considered. --> ### 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) Resolves SL-2826
- Loading branch information
1 parent
5083005
commit b7ce0aa
Showing
13 changed files
with
552 additions
and
124 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: Breaking Changes | ||
body: Adding support for custom grain in windows/grain_to_dates | ||
time: 2024-11-08T21:59:06.162076-05:00 | ||
custom: | ||
Author: WilliamDee | ||
Issue: None |
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
89 changes: 89 additions & 0 deletions
89
dbt_semantic_interfaces/transformations/remove_plural_from_window_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,89 @@ | ||
from typing import Sequence | ||
|
||
from typing_extensions import override | ||
|
||
from dbt_semantic_interfaces.enum_extension import assert_values_exhausted | ||
from dbt_semantic_interfaces.errors import ModelTransformError | ||
from dbt_semantic_interfaces.implementations.metric import PydanticMetricTimeWindow | ||
from dbt_semantic_interfaces.implementations.semantic_manifest import ( | ||
PydanticSemanticManifest, | ||
) | ||
from dbt_semantic_interfaces.protocols import ProtocolHint | ||
from dbt_semantic_interfaces.transformations.transform_rule import ( | ||
SemanticManifestTransformRule, | ||
) | ||
from dbt_semantic_interfaces.type_enums import MetricType | ||
|
||
|
||
class RemovePluralFromWindowGranularityRule(ProtocolHint[SemanticManifestTransformRule[PydanticSemanticManifest]]): | ||
"""Remove trailing s from granularity in MetricTimeWindow. | ||
During parsing, MetricTimeWindow.granularity can still contain he trailing 's' (ie., 3 days). | ||
This is because with the introduction of custom granularities, we don't have access to the valid | ||
custom grains during parsing. This transformation rule is introduced to remove the trailing 's' | ||
from `MetricTimeWindow.granularity` if necessary. | ||
""" | ||
|
||
@override | ||
def _implements_protocol(self) -> SemanticManifestTransformRule[PydanticSemanticManifest]: # noqa: D | ||
return self | ||
|
||
@staticmethod | ||
def _update_metric( | ||
semantic_manifest: PydanticSemanticManifest, metric_name: str, custom_granularity_names: Sequence[str] | ||
) -> None: | ||
"""Mutates all the MetricTimeWindow by reparsing to remove the trailing 's'.""" | ||
|
||
def reparse_window(window: PydanticMetricTimeWindow) -> PydanticMetricTimeWindow: | ||
"""Reparse the window to remove the trailing 's'.""" | ||
return PydanticMetricTimeWindow.parse( | ||
window=window.window_string, custom_granularity_names=custom_granularity_names | ||
) | ||
|
||
matched_metric = next( | ||
iter((metric for metric in semantic_manifest.metrics if metric.name == metric_name)), None | ||
) | ||
if matched_metric: | ||
if matched_metric.type is MetricType.CUMULATIVE: | ||
if ( | ||
matched_metric.type_params.cumulative_type_params | ||
and matched_metric.type_params.cumulative_type_params.window | ||
): | ||
matched_metric.type_params.cumulative_type_params.window = reparse_window( | ||
matched_metric.type_params.cumulative_type_params.window | ||
) | ||
elif matched_metric.type is MetricType.CONVERSION: | ||
if ( | ||
matched_metric.type_params.conversion_type_params | ||
and matched_metric.type_params.conversion_type_params.window | ||
): | ||
matched_metric.type_params.conversion_type_params.window = reparse_window( | ||
matched_metric.type_params.conversion_type_params.window | ||
) | ||
|
||
elif matched_metric.type is MetricType.DERIVED or matched_metric.type is MetricType.RATIO: | ||
for input_metric in matched_metric.input_metrics: | ||
if input_metric.offset_window: | ||
input_metric.offset_window = reparse_window(input_metric.offset_window) | ||
elif matched_metric.type is MetricType.SIMPLE: | ||
pass | ||
else: | ||
assert_values_exhausted(matched_metric.type) | ||
else: | ||
raise ModelTransformError(f"Metric '{metric_name}' is not configured as a metric in the model.") | ||
|
||
@staticmethod | ||
def transform_model(semantic_manifest: PydanticSemanticManifest) -> PydanticSemanticManifest: # noqa: D | ||
custom_granularity_names = [ | ||
granularity.name | ||
for time_spine in semantic_manifest.project_configuration.time_spines | ||
for granularity in time_spine.custom_granularities | ||
] | ||
|
||
for metric in semantic_manifest.metrics: | ||
RemovePluralFromWindowGranularityRule._update_metric( | ||
semantic_manifest=semantic_manifest, | ||
metric_name=metric.name, | ||
custom_granularity_names=custom_granularity_names, | ||
) | ||
return semantic_manifest |
Oops, something went wrong.