Develop semantic views locally with DuckDB and dbt

The portable semantic starter lets you write and test one governed business definition on your own machine, then prepare the same definition for a Snowflake semantic view.

Status as of 2026-10-31

  • Local path: verified. The DuckDB build and queries ran on the supported platform.
  • Snowflake DDL and query plan: generated offline. Nothing connected to Snowflake.
  • Live Snowflake execution: never performed.
  • Cross-engine parity: unverified. Seamless migration is a goal, not a proven claim.

Recorded evidence is scoped narrowly. An accepted suite of 153 tests (networking disabled) and 8 independent fixture cases passed on Linux x86_64 with Python 3.12. They show the local path works and the offline plan is generated. They say nothing about how Snowflake behaves.

Why a semantic view

A Snowflake semantic view records business meaning once: which tables, how they join, the grain of each, and named dimensions and metrics. Every report then asks for "order revenue by region" instead of re-deriving it.

The hazard it removes is join fan-out. An order has many lines. Naively joining orders to lines and summing the order amount counts each order once per line. In this starter, order revenue and order count are defined at order grain, so adding lines never changes them. Line revenue is defined at line grain. A test inserts child lines and checks that parent metrics are unchanged.

What this is, and is not

  • dbt Core SQL models. Staging views and mart tables are linked with ref() (dbt SQL models) and covered by data tests on keys and line foreign keys. A custom semantic_view materialization creates the semantic view.
  • Not the dbt Semantic Layer or MetricFlow. Those are different products.
  • Native execution on DuckDB. The semantic_views community extension runs the SEMANTIC_VIEW(...) query construct. This is not a plain SQL-view imitation.
  • A starter, not a translator. It has one semantic view and three small adapter macros. It does not convert arbitrary projects between engines.

Upstream components are open source (DuckDB and the extension are MIT; dbt packages and connectors are Apache-2.0), and the two vendored dbt packages are Apache-2.0. The starter's own code has no chosen licence and no public repository location, so this page gives no clone URL and does not call it open source. Use it from a checkout you already have.

flowchart LR
    CSV["3 fixed CSVs"] --> Raw["Raw tables"] --> Stg["dbt staging views"] --> Marts["dbt mart tables"]
    Marts --> Def["One semantic definition"]
    Contract["Query contract"] --> Duck["DuckDB: executed locally"]
    Def --> Duck
    Def --> Snow["Snowflake: offline plan only"]
    Contract --> Snow

Shared definition, engine-specific edges

The business definition in models/semantic/sales.sql (logical tables, relationships, dimensions, metrics) is one shared source template used for both engines. The rendered text is not byte-identical: ref() resolves to adapter-specific physical relation names, and the average-order-value money expression comes from an adapter-specific macro. In the semantic-view DDL, the creation prefix the materialization adds also differs (DuckDB includes an AS; Snowflake does not). Schema placement differs as well. On DuckDB, raw tables and staging views are in the staging schema, and the marts and the semantic view are in analytics. On Snowflake, everything, including the semantic view, is built in the single schema you name.

Reports live in models/semantic/query_contract.json and render as the same native SEMANTIC_VIEW(view DIMENSIONS ... METRICS ... WHERE ...) text on both engines; only the view name qualification differs.

Pins and platform

Verified only on Linux x86_64 with Python 3.12. The Python lock file (requirements.lock) pins exactly: DuckDB 2.0.0, dbt-core 1.12.6, dbt-duckdb 1.11.1, dbt-snowflake 1.12.2 and snowflake-connector-python 4.8.0. The semantic_views extension, v0.17.0, is pinned separately: the bootstrap installer verifies the loaded version after installing it, and the local runner checks it again on every run. These are the versions verified together, not claims about newer releases. Other platforms are untested.

Run it locally

From the root of an existing checkout. Bootstrap is the only step that needs network; it creates a virtual environment, installs the lock, and installs the signed extension, failing unless the loaded version is v0.17.0.

scripts/bootstrap.sh            # or: PYTHON=/path/to/python3.12 scripts/bootstrap.sh
source .venv/bin/activate

Then build and query offline:

python scripts/portability.py local \
  --fixtures acceptance/fixtures --work-dir .work/local --output .work/local.json
python scripts/portability.py snowflake-plan --output .work/snowflake-plan
python -m pytest -q -p no:cacheprovider acceptance/test_contract.py tests

The third command runs the optional tests. The work directory is rebuilt from scratch on every run, and the source fixtures are only read.

Reading the evidence

.work/local.json records engine: duckdb, execution_status: succeeded and parity_status: unverified, plus hashes of the fixtures and the query contract, each query's results, the observed extension version, dbt run evidence, and the executed SQL with DuckDB's EXPLAIN. Money is an exact decimal string; NULL is JSON null. On the sample fixtures you should see order revenue 385.00 over 5 orders and an average order value of 77.00.

Failure handling is per command and per output path. local and snowflake-live delete any earlier report at their --output before starting; if they fail, they exit nonzero and write a report with execution_status: failed. snowflake-plan removes its own plan files and leaves none if it fails. compare overwrites its own result file and exits nonzero unless the result is matched. A failure in one command does not remove another command's earlier output: a failing bootstrap or pytest run leaves an existing .work/local.json in place, and arguments that fail command-line parsing are rejected before any of this happens.

Edit loop

  1. Change a model. Edit SQL under models/ and rerun local. ref() resolves dependencies, so a new mart or column needs no other wiring.
  2. Add a metric or dimension. Edit models/semantic/sales.sql.
  3. Report on it. Add a query to models/semantic/query_contract.json with its columns and types (decimal:N, integer, string, date). The contract currently pins exactly five query ids, so also extend REQUIRED_QUERIES in scripts/starter/contract.py. Both edits are needed, and the new query changes the query-set hash, so older reports will no longer compare.
  4. Rerun local, then the tests. Treat a failing test as a prompt to review, not to silence.

Input is exactly three UTF-8 CSVs with fixed headers: customers.csv, orders.csv and order_lines.csv. Invalid input is refused rather than repaired.

Two behaviours worth knowing. Plain division on decimals gives a floating-point result in DuckDB, so average_order_value uses integer arithmetic on cents and rounds half-up to two decimals after the aggregate division. Orders with an empty or unknown customer id are kept and grouped under a NULL region; there is deliberately no customer foreign-key test.

The DuckDB naming limit

The DuckDB catalog and the dbt schema are both named analytics, so physical tables are analytics.analytics.<mart>, while the semantic query uses analytics.sales. DuckDB 2.0 cannot create a plain view that reads those marts, even fully qualified and even with a different destination schema. If you add a downstream model that reads a mart, materialize it as a table. The starter avoids the problem through where its sources live, not just where its views live. The raw source tables are loaded into analytics.staging, and the staging views read those sources. Only mart tables and the semantic view read from analytics.analytics. The failure is determined by the schema a view reads from, so moving a view alone does not fix it.

Prepare for Snowflake

The offline plan compiles the project for Snowflake with placeholder credentials and never connects. Choose the target names with environment variables:

SP_SNOWFLAKE_DATABASE=MY_DEV_DB SP_SNOWFLAKE_SCHEMA=SEMANTIC_STARTER \
  python scripts/portability.py snowflake-plan --output .work/snowflake-plan

It writes deploy.sql (raw table DDL, staging views, mart tables, and create or replace semantic view), queries.sql, and plan.json marked not_executed. The plan is preparation, not execution evidence, and compare rejects it.

Optional live run and comparison

This step is opt-in and has not been run by the starter's authors. It needs an existing development schema you name and credentials supplied through the environment: SNOWFLAKE_ACCOUNT, SNOWFLAKE_USER, and one of SNOWFLAKE_PASSWORD, SNOWFLAKE_PRIVATE_KEY_PATH (an unencrypted key file) or SNOWFLAKE_AUTHENTICATOR=externalbrowser. Set these yourself; never paste secrets into commands or reports.

Before you run it: the command creates or replaces RAW_CUSTOMERS, RAW_ORDERS and RAW_ORDER_LINES, the STG_* views, the marts FCT_ORDERS, FCT_ORDER_LINES and DIM_CUSTOMERS, and the SALES semantic view in the schema you name. It does not create the schema. Use a development schema where replacing those objects is acceptable.

python scripts/portability.py snowflake-live \
  --fixtures acceptance/fixtures --work-dir .work/snowflake --output .work/snowflake.json \
  --database MY_DEV_DB --schema SEMANTIC_STARTER --warehouse MY_WH --role MY_DEV_ROLE \
  --confirm-dev-target MY_DEV_DB.SEMANTIC_STARTER
python scripts/portability.py compare .work/local.json .work/snowflake.json --output .work/comparison.json

Missing configuration fails before any connection. --confirm-dev-target must repeat DATABASE.SCHEMA exactly. The role needs usage on the warehouse and database and permission to create tables, views and semantic views in the schema.

compare requires matching fixture and query hashes, identical query ids, columns and declared types, and equal row multisets with exact typed values. A matched result means only that the two supplied reports agree. It does not attest freshness, provenance, or who produced the reports, and it is not a proof that the engines are equivalent in general. Parity stays unverified until a real live report exists and compare matches.

Limits

Not emulated locally: Snowflake grants and role-based access control, Cortex Analyst, and semantic-view materializations or refresh behaviour. Out of scope: incremental models, snapshots, multiple semantic views, a generic translator, other platforms and other input formats.

Evidence basis: a sanitized public summary dated October 31, 2026, of the locally delivered starter and its recorded verification. No Snowflake execution is part of that evidence.

Audience, source and original lineage

Audience: Lee, owner/operator, developer · Reading modes: operator, engineer

Original lineage (not approval of later edits): registry-listed (no candidate packet)

Registry source: content/public/portable-semantic-views-how-to.md