feat(ingest/snowflake): column-level lineage for semantic view metrics
Builds on #21764 (SDK support for metric column lineage). It supersedes the Snowflake connector part of #20001 and applies that PR's review feedback.
What changes
With semantic_views.column_lineage: true, Snowflake semantic view metrics (semantic model entities mode) now carry column-level lineage: metricUpstreams.fieldUpstreams edges to the logical dataset's columns (the facts and dimensions the metric expression reads). This is the same option that already gates the semantic view's column-level lineage, and its description already covers metrics. With the option off, metrics keep their dataset upstreams and derivedFrom edges only. The logical-to-physical column mapping stays on the logical dataset's upstreamLineage, as before.
- Metrics are built with the SDK. Each metric is a
datahub.sdk.Metric, as in the dbt, Cube, Unity and MicroStrategy connectors, instead of hand-built aspects. The resolution logic (column refs, upstreams,derivedFrom, report counters) is unchanged; only how the aspects are emitted changes. Details below. - One walk for both lists. Dataset and column upstreams are resolved together, so every column's parent is in
datasetUpstreamsby construction (whatMetricUpstreamsValidatorrequires).datasetUpstreamsis always written.fieldUpstreamsis written when column lineage is on and the metric reads columns, and left out otherwise; an upsert replaces the whole aspect, so re-ingestion clears stale column edges either way. - One fold helper.
fold_column_refis the single quote-aware identifier fold, used by the metric upstream walk,derivedFromandsnowflake_schema_gen._extract_columns_from_expression. The "verified against Snowflake" rationale now lives in its docstring. Each metric's expression is parsed and folded once, and a column is counted and reported once even when it is written both qualified and unqualified (xandORDERS.xin anORDERSmetric). A qualified star (COUNT(customers.*)) is not a column ref. - Metric refs are never column refs. A ref is checked against every table-bound metric, including those on logical tables dropped for a case collision. Metric refs stay
derivedFromonly. - Unqualified refs to a metric of the same table. Snowflake lets a name go unqualified when it is not ambiguous, so
total / cntin anORDERSmetric, where both areORDERSmetrics, now getsderivedFromedges to them. Before, only qualified refs did. - Blank expressions. A whitespace-only metric expression is neither emitted nor parsed, so it no longer raises a parse-failure warning.
- Separate report buckets (counted only with column lineage on):
num_semantic_view_metric_field_refs_without_logical_dataset: the ref's table (the metric's own table for an unqualified ref) has no Semantic Model Dataset. This is reported asinfo. The message names both causes: the table is missing from the view's table list, or it differs from another logical table only by case (that collision is also reported as a warning where the datasets are built).num_semantic_view_metric_field_refs_to_base_table_columns: the table has a dataset, but the name is neither a fact, a dimension nor a metric of it, so the expression reads a base-table column directly (for exampleCOUNT(c_custkey)). The dataset edge is kept and no column edge is written. This is reported asinfo, because Snowflake accepts such expressions.
- A comment in the code explains why no
upstreamMetricsare emitted. A metric's logical dataset is already its upstream, so the reverse edge would create a dataset–metric cycle.
Design decisions
Cross-table refs: keep the edge. A table-bound metric can aggregate a fact of a related table (for example an ORDERS metric over CUSTOMERS.DISCOUNT). Instead of dropping that column edge, the referenced table's logical dataset is added to the metric's datasetUpstreams, as the #20001 review suggested.
- The metric really reads that table, so the table-level lineage is more accurate too.
- The SDK does the same thing: it adds a field's parent dataset.
- It also removes the "outside dataset upstreams" warning that operators could not act on, so that bucket no longer exists.
- The visible change is that some table-bound metrics gain a second dataset upstream.
Metrics through the SDK; the semantic model and logical datasets not yet. Metric takes the name, description, created and modified times, expression, derivedFrom, upstream datasets and fields, synonyms (aiContext), tags, domain and platform instance as arguments.
- The browse path is passed in
extra_aspects. Its platform-instance and view entries are named by the instance and view name, not by URN, which the SDK'sbrowse_pathinput cannot express. - Structured properties (tags with
extract_tags_as_structured_properties) still go through the shared helper, which honoursstructured_properties_write_mode(PATCH or UPSERT). The SDK always writes a full aspect. - Output differences:
- Every metric now has a
dataPlatformInstanceaspect, also without aplatform_instance, as SDK-built metrics in other connectors do. The semantic model and its logical datasets now write it the same way: the platform always, the instance only whenplatform_instanceis set. Before, they wrote it only with a platform instance, and GMS derives no default forsemanticModel. fieldUpstreamsis left out instead of[]when a metric has no column edges.- The
created/lastModifiedactor isurn:li:corpuser:__ingestioninstead ofurn:li:corpuser:unknown, on bothmetricInfo(the SDK's default) and the semantic model'ssemanticModelInfo, so the two agree and match the Unity, dbt and Cube connectors. Snowflake reports no actor for semantic views. Metric lineage edges carry no stamps, before and after this change.
- Every metric now has a
SemanticModelandSemanticModelDatasetare not used yet: their output would differ in ways that are not just the SDK's style.SemanticModel.as_mcpsraises on a relationship whose alias or join column it cannot match, where the connector reports a warning and keeps going.SemanticModelDatasetwrites a differentschemaMetadata(emptyschemaName, schemaless platform schema, types fromresolve_sql_type), itsparent_containerreplaces the custom browse path that includes the semantic model, and field structured properties are always a full aspect stamped with the current time, ignoringstructured_properties_write_mode.
Tests
- New unit tests in
tests/unit/snowflake/test_snowflake_semantic_model.py:- own-table facts become field edges, with duplicate refs folded into one;
- a cross-table fact ref adds that table's dataset and keeps the column edge;
- a name that is a FACT on
ORDERSand a METRIC onRETURNS: onlyORDERS.REVENUEbecomes a field edge,RETURNS.revenueisderivedFrom-only, andRETURNS.amountadds 1 to the base-table-columns counter; - refs to a collided table: a metric ref is not counted as a column ref, a fact ref is counted as without-dataset, and no warning is reported;
- with
semantic_views.column_lineageoff: dataset edges (including a cross-table one) are kept, and no field edges, counters or info entries are produced; - an unqualified ref to a same-table metric becomes a
derivedFromedge; xandORDERS.xcount as one base-table column, andcustomers.*adds nothing;- a blank expression is neither emitted nor reported as a parse failure;
- the semantic model and its metrics share one actor;
- the semantic model, logical datasets and metrics all carry
dataPlatformInstance: the platform only without a platform instance, and the instance when one is configured.
- An existing quoted-table test now asserts the base-table-columns counter and no
fieldUpstreamsedges. - Every new test checks that each field's parent is a dataset upstream.
- Golden
snowflake_semantic_view_golden.json(column lineage on): compared with master, the changes are onefieldUpstreamsedge (orders.AMOUNT), adataPlatformInstanceaspect on each of the two metrics, the semantic model and its two logical datasets, and the__ingestionactor in themetricInfoandsemanticModelInfostamps. The golden check ignorescreated/lastModified, so the actor lines were updated to the real output by hand. Run metadata (lastObserved/runId) of existing entries was left unchanged. pytest tests/unit/snowflake tests/integration/snowflake tests/unit/sdk: 1623 passed (test_snowflake_basichas a known cache-count flake when run after the unit tests; it passes alone).
Checklist
- The PR conforms to DataHub's Contributing Guideline (particularly PR Title Format)
- Links to related issues (if applicable)
- Tests for the changes have been added/updated (if applicable)
- Docs related to the changes have been added/updated (if applicable). If a new feature has been added a Usage Guide has been added for the same.
- For any breaking change/potential downtime/deprecation/big changes an entry has been made in Breaking Changes (not needed: this adds lineage edges behind an existing option and no config or URN changes)
🤖 Generated with Claude Code