A dbt package that provides a custom materialization for creating Snowflake semantic views directly from dbt semantic model configurations.
This dbt package bridges the gap between dbt's Semantic Layer and Snowflake's native semantic views, allowing you to:
- Transform dbt semantic model definitions into Snowflake semantic views using native dbt workflows
- Maintain consistent business logic across your semantic layer
- Leverage dbt's dependency management, testing, and documentation features
- Use familiar dbt materialization patterns for semantic views
Add this package to your dbt project's packages.yml:
packages:
- git: "https://github.com/sfc-gh-ahuck/dbt_semantic_view_converter.git"
revision: mainThen run:
dbt depsImportant: After installing, make sure to run dbt deps and then dbt parse to ensure all macros are properly loaded.
Create semantic models in your schema.yml:
semantic_models:
- name: orders
description: "Order fact table"
model: ref('dim_orders')
entities:
- name: order_id
type: primary
- name: customer_id
type: foreign
dimensions:
- name: order_date
type: time
type_params:
time_granularity: day
- name: order_status
type: categorical
measures:
- name: order_total
agg: sum
- name: order_count
expr: 1
agg: sumCreate a model file using the semantic_view materialization:
-- models/semantic_views/orders_semantic_view.sql
{{ config(
materialized='semantic_view',
schema='semantic_layer'
) }}
SELECT 1 as placeholderdbt run --models orders_semantic_viewThis creates a Snowflake semantic view based on your semantic model definition!
Configure the package in your dbt_project.yml:
vars:
dbt_semantic_view_converter:
semantic_views_database: "{{ target.database }}"
semantic_views_schema: "semantic_layer"
copy_grants: true{{ config(
materialized='semantic_view',
schema='my_semantic_layer',
database='analytics',
tags=['semantic', 'daily']
) }}The package generates Snowflake CREATE SEMANTIC VIEW statements like:
CREATE OR REPLACE SEMANTIC VIEW analytics.semantic_layer.orders
COMMENT = 'Order fact table'
TABLES (
orders AS dim_orders
PRIMARY KEY (order_id)
)
RELATIONSHIPS (
to_customer_id AS
semantic_model (customer_id) REFERENCES customer
)
FACTS (
orders.order_total AS order_total
)
DIMENSIONS (
orders.order_date AS DATE_TRUNC('DAY', order_date),
orders.order_status AS order_status
)
METRICS (
orders.total_order_total AS SUM(order_total),
orders.total_count AS COUNT(*)
)
COPY GRANTS;Test the package with the included example models:
# Run example semantic views
dbt run --models semantic_views
# Check generated objects in Snowflake
SHOW SEMANTIC VIEWS;Define multiple semantic models and create corresponding view files:
# schema.yml
semantic_models:
- name: orders_semantic_view
# ... configuration
- name: customers_semantic_view
# ... configuration-- models/semantic_views/orders_semantic_view.sql
{{ config(materialized='semantic_view') }}
SELECT 1 as placeholder
-- models/semantic_views/customers_semantic_view.sql
{{ config(materialized='semantic_view') }}
SELECT 1 as placeholder{{ config(
materialized='semantic_view',
schema='custom_semantic_layer',
database='custom_analytics_db'
) }}If you encounter this error when importing the package:
- Ensure dbt deps is run: Make sure you've run
dbt depsafter adding the package - Parse the project: Run
dbt parseto load all macros - Check package installation: Verify the package appears in
dbt_packages/directory - Verify profile: Ensure your dbt profile is properly configured
dbt's semantic layer doesn't support avg aggregation. Use these instead:
sum,count,count_distinct,max,min,median,sum_boolean
This error occurs when:
- The semantic model name doesn't match your model file name
- The semantic model isn't defined in
schema.yml - The
schema.ymlfile isn't in the same directory as your model
If you see "The semantic layer requires a time spine model", add this to your project:
-- models/time_spine.sql
{{ config(materialized='table') }}
{{ dbt_utils.date_spine(
datepart="day",
start_date="cast('2020-01-01' as date)",
end_date="cast('2025-01-01' as date)"
) }}For detailed usage instructions, see docs/usage_guide.md.
Transform your dbt semantic models into Snowflake semantic views with native dbt workflows! π