Repository navigation
feat(ingest/dbt,cube): column-level lineage for metrics #21768
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
base: master
Are you sure you want to change the base?
Changes from 72 commits
968862a
c9cdcb9
a56cb31
676d966
a081564
bcfe6c5
a6d203c
d6cb3b6
9ac3bbc
78fc668
69f35af
d56b42c
7c5f6ae
6d9198a
c4a6af0
b3700f3
2595c6f
5c5cd3a
1e0a326
a10a0b4
378e706
655939b
d898cf5
95f190c
31233b5
d32a5db
49e0b79
3be325b
2ba33d1
12f27d2
0c9cba3
bbde868
2c9740b
ca68719
52b8115
4bf3cb5
b169101
41a4abb
98ebdcf
e5c6e7a
22fb161
bc76258
2e121c4
a66a5f6
f65845f
1c76de8
b8f2592
06d4b9d
973567d
45e5d25
cefca97
c82a85f
04c5a8f
ed62ce3
04f72ac
9bf9551
53db492
dc20ed0
7f525b6
229619b
8dbd7b5
f2ddaae
9d6fb3a
9436914
e30a1a1
92d913d
df34847
15c7f99
cdd82df
d15c60f
bb116db
8a00a1f
131f03d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -166,9 +166,10 @@ class _MetricComputation: | |
|
|
||
| @dataclass(frozen=True) | ||
| class _MeasureLocation: | ||
| """Where a measure lives, and how it aggregates.""" | ||
| """Where a measure lives, its column there, and how it aggregates.""" | ||
|
|
||
| dataset_urn: str | ||
| field_path: str | ||
| expression: Optional[str] | ||
|
|
||
|
|
||
|
|
@@ -867,6 +868,7 @@ def _index_measures(self, models: List[_PreparedModel]) -> _MeasureIndex: | |
| key = measure.name.casefold() | ||
| location = _MeasureLocation( | ||
| dataset_urn=dataset_urn, | ||
| field_path=self._column_path(measure.name), | ||
| expression=self._measure_expression(measure, prepared.alias), | ||
| ) | ||
| index.by_model.setdefault(prepared.node.dbt_name, {})[key] = location | ||
|
|
@@ -1038,6 +1040,11 @@ def _metric_from_measure( | |
| description=measure.description or None, | ||
| expression=expression, | ||
| upstream_datasets=[dataset_urn], | ||
| # The measure's own column, which the dbt path already emitted on | ||
| # this dataset: `measure` comes from `_AnnotatedFields.measures`. | ||
| upstream_fields=[ | ||
| SchemaFieldUrn(dataset_urn, self._column_path(measure.name)) | ||
| ], | ||
| # A simple metric derives from no metric: [] clears stale edges. | ||
| derived_from=[], | ||
| extra_aspects=[ | ||
|
|
@@ -1058,10 +1065,12 @@ def _metric_from_definition( | |
| ) | ||
|
|
||
| upstreams: List[str] = [] | ||
| measure_names = [ | ||
| upstream_fields: List[SchemaFieldUrn] = [] | ||
| declared_measure_names = [ | ||
| measure_input.name for measure_input in metric_definition.measures | ||
| ] | ||
| measure_names.extend(resolved.measure_names) | ||
| declared_folded = {name.casefold() for name in declared_measure_names} | ||
| measure_names = [*declared_measure_names, *resolved.measure_names] | ||
|
Contributor
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. P2: Derived metrics are given direct column lineage from their transitive Prompt for AI agents
Contributor
Author
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. Not changed: this is intentional. For a derived metric, a compiled dbt manifest lists in |
||
| # A ratio's sides usually name other metrics, so they become derivedFrom | ||
| # edges rather than measure references - but `_ratio_side` still renders | ||
| # each side as `agg(alias.measure)` whenever the name also resolves to a | ||
|
|
@@ -1075,10 +1084,30 @@ def _metric_from_definition( | |
| for side in (metric_definition.numerator, metric_definition.denominator) | ||
| if side is not None | ||
| ) | ||
| # A dbt 1.6-style ratio over measures lists the same measure both in | ||
| # input_measures and as a ratio side; visit each name once so a missing | ||
| # measure is counted once. | ||
| seen_folded: Set[str] = set() | ||
| for measure_name in measure_names: | ||
| folded = measure_name.casefold() | ||
| if folded in seen_folded: | ||
| continue | ||
| seen_folded.add(folded) | ||
| located = self._locate_measure(metric_definition, index, measure_name) | ||
| if located and located.dataset_urn not in upstreams: | ||
| if located is None: | ||
| # Only a name dbt declares as a measure was expected to resolve: | ||
| # a ratio side that matches no measure names another metric, | ||
| # and is already a derivedFrom edge. | ||
| if folded in declared_folded: | ||
| self.report.num_metric_upstream_fields_unresolved += 1 | ||
| continue | ||
| if located.dataset_urn not in upstreams: | ||
| upstreams.append(located.dataset_urn) | ||
| # Built from the field set the dataset annotation uses, so the | ||
| # column is one the dataset's schema carries. | ||
| field_urn = SchemaFieldUrn(located.dataset_urn, located.field_path) | ||
| if field_urn not in upstream_fields: | ||
| upstream_fields.append(field_urn) | ||
| for dbt_name in metric_definition.depends_on: | ||
| if not dbt_name.startswith(_SEMANTIC_MODEL_DEPENDS_ON_PREFIX): | ||
| continue | ||
|
|
@@ -1100,6 +1129,9 @@ def _metric_from_definition( | |
| description=metric_definition.description or None, | ||
| expression=self._metric_definition_expression(metric_definition, index), | ||
| upstream_datasets=upstreams, | ||
| # None when unresolved, so fieldUpstreams is left unset (the SDK | ||
| # default); the whole-aspect upsert clears stale edges either way. | ||
| upstream_fields=upstream_fields or None, | ||
| derived_from=resolved.metric_urns, | ||
| tags=[make_tag_urn(tag) for tag in metric_definition.tags] or None, | ||
| extra_aspects=[ | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.