Managing semantic metric views#
Cosmos can render adapter-native semantic metric views (Databricks Unity Catalog metric views and
Snowflake semantic views) as their own semantic_metric_view resource type, instead of a regular model.
This is opt-in. Enable it with the enable_semantic_metric_view_reclassification: setting
(AIRFLOW__COSMOS__ENABLE_SEMANTIC_METRIC_VIEW_RECLASSIFICATION=True).
Prerequisites#
Databricks: dbt-databricks >= 1.12.5
Snowflake: the Snowflake-Labs/dbt_semantic_view package
How it works#
dbt has no distinct resource type for these objects. They are model nodes with
config.materialized: metric_view (Databricks) or semantic_view (Snowflake). Once enabled, Cosmos detects
that materialization at parse time and reclassifies the node as semantic_metric_view, which changes:
Task identity: a
_semantic_metric_runtask name suffix and aDbtSemanticMetricView*operator instead ofDbtRun*. The underlying dbt command is stilldbt run.TestBehavior.BUILD: collapses into
DbtBuild, same as any other buildable resource.Selectors:
resource_type:modelno longer matches it. Useresource_type:semantic_metric_viewinstead. This selector only works when Cosmos itself performs node selection (e.g.LoadMode.DBT_MANIFEST). UnderLoadMode.DBT_LS,select/excludeare passed directly todbt ls, which has nosemantic_metric_viewresource type, so useconfig.materialized:metric_vieworconfig.materialized:semantic_viewinstead. The same applies toExecutionMode.WATCHER, whose producer passesselect/excludetodbt build. Cosmos raises an error if they containresource_type:semantic_metric_view.
Testing#
Cosmos renders test tasks for semantic metric views like for any other model. Generic tests work on Databricks metric
views. On Snowflake, tests that aggregate over a column, such as accepted_values, fail on semantic views. Use
singular tests written with SEMANTIC_VIEW(...) instead.
Example#
{{ config(materialized='metric_view') }}
select * from {{ ref('customers') }}
from cosmos import DbtTaskGroup, LoadMode, RenderConfig
# LoadMode.DBT_MANIFEST (or another mode where Cosmos applies selection itself)
semantic_metric_views_only = DbtTaskGroup(
render_config=RenderConfig(
load_method=LoadMode.DBT_MANIFEST, select=["resource_type:semantic_metric_view"]
),
)
# LoadMode.DBT_LS: select/exclude are passed straight to `dbt ls`, which has no `semantic_metric_view`
# resource type, so filter on the underlying materialization instead
semantic_metric_views_only_dbt_ls = DbtTaskGroup(
render_config=RenderConfig(
load_method=LoadMode.DBT_LS, select=["config.materialized:metric_view"]
),
)
See Selecting & Excluding for the full selector reference.