Skip to content

Commit 4c7d922

Browse files
Add Metric.time_granularity to metric spec (#10378)
1 parent b032915 commit 4c7d922

File tree

8 files changed

+64
-1
lines changed

8 files changed

+64
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
kind: Features
2+
body: Add time_granularity to metric spec.
3+
time: 2024-06-27T16:29:53.500917-07:00
4+
custom:
5+
Author: courtneyholcomb
6+
Issue: "10376"

core/dbt/artifacts/resources/v1/metric.py

+1
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ class Metric(GraphResource):
121121
type_params: MetricTypeParams
122122
filter: Optional[WhereFilterIntersection] = None
123123
metadata: Optional[SourceFileMetadata] = None
124+
time_granularity: Optional[TimeGranularity] = None
124125
resource_type: Literal[NodeType.Metric]
125126
meta: Dict[str, Any] = field(default_factory=dict, metadata=MergeBehavior.Update.meta())
126127
tags: List[str] = field(default_factory=list)

core/dbt/contracts/graph/unparsed.py

+1
Original file line numberDiff line numberDiff line change
@@ -563,6 +563,7 @@ class UnparsedMetric(dbtClassMixin):
563563
description: str = ""
564564
# Note: `Union` must be the outermost part of the type annotation for serialization to work properly.
565565
filter: Union[str, List[str], None] = None
566+
time_granularity: Optional[str] = None
566567
# metadata: Optional[Unparsedetadata] = None # TODO
567568
meta: Dict[str, Any] = field(default_factory=dict)
568569
tags: List[str] = field(default_factory=list)

core/dbt/parser/schema_yaml_readers.py

+3
Original file line numberDiff line numberDiff line change
@@ -435,6 +435,9 @@ def parse_metric(self, unparsed: UnparsedMetric, generated: bool = False) -> Non
435435
label=unparsed.label,
436436
type=MetricType(unparsed.type),
437437
type_params=self._get_metric_type_params(unparsed),
438+
time_granularity=(
439+
TimeGranularity(unparsed.time_granularity) if unparsed.time_granularity else None
440+
),
438441
filter=parse_where_filter(unparsed.filter),
439442
meta=unparsed.meta,
440443
tags=unparsed.tags,

core/setup.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@
6969
# Accept patches but avoid automatically updating past a set minor version range.
7070
"dbt-extractor>=0.5.0,<=0.6",
7171
"minimal-snowplow-tracker>=0.0.2,<0.1",
72-
"dbt-semantic-interfaces>=0.6.1,<0.7",
72+
"dbt-semantic-interfaces>=0.6.8,<0.7",
7373
# Minor versions for these are expected to be backwards-compatible
7474
"dbt-common>=1.3.0,<2.0",
7575
"dbt-adapters>=1.1.1,<2.0",

schemas/dbt/manifest/v12.json

+46
Original file line numberDiff line numberDiff line change
@@ -9044,6 +9044,29 @@
90449044
],
90459045
"default": null
90469046
},
9047+
"time_granularity": {
9048+
"anyOf": [
9049+
{
9050+
"enum": [
9051+
"nanosecond",
9052+
"microsecond",
9053+
"millisecond",
9054+
"second",
9055+
"minute",
9056+
"hour",
9057+
"day",
9058+
"week",
9059+
"month",
9060+
"quarter",
9061+
"year"
9062+
]
9063+
},
9064+
{
9065+
"type": "null"
9066+
}
9067+
],
9068+
"default": null
9069+
},
90479070
"meta": {
90489071
"type": "object",
90499072
"propertyNames": {
@@ -18020,6 +18043,29 @@
1802018043
],
1802118044
"default": null
1802218045
},
18046+
"time_granularity": {
18047+
"anyOf": [
18048+
{
18049+
"enum": [
18050+
"nanosecond",
18051+
"microsecond",
18052+
"millisecond",
18053+
"second",
18054+
"minute",
18055+
"hour",
18056+
"day",
18057+
"week",
18058+
"month",
18059+
"quarter",
18060+
"year"
18061+
]
18062+
},
18063+
{
18064+
"type": "null"
18065+
}
18066+
],
18067+
"default": null
18068+
},
1802318069
"meta": {
1802418070
"type": "object",
1802518071
"propertyNames": {

tests/functional/metrics/fixtures.py

+1
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@
113113
type: simple
114114
type_params:
115115
measure: people
116+
time_granularity: month
116117
config:
117118
meta:
118119
my_meta_config: 'config'

tests/functional/metrics/test_metrics.py

+5
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,11 @@ def test_simple_metric(
8383
)
8484
== 2
8585
)
86+
assert (
87+
manifest.metrics["metric.test.number_of_people"].time_granularity
88+
== TimeGranularity.MONTH
89+
)
90+
assert manifest.metrics["metric.test.collective_tenure"].time_granularity is None
8691

8792

8893
class TestInvalidRefMetrics:

0 commit comments

Comments
 (0)