dbt
Models, ref and source, materializations, incremental strategies, tests, and model contracts.
8 questions
JuniorTheoryVery commonHow do generic and singular tests differ in dbt, and when does a failing test stop the build?
How do generic and singular tests differ in dbt, and when does a failing test stop the build?
A generic test is a parameterised macro attached in YAML (unique, not_null, accepted_values, relationships); a singular test is a .sql SELECT returning failing rows. Zero rows passes; severity: error stops the build, warn only reports.
Common mistakes
- ✗Thinking a test passes when its query returns rows rather than zero rows
- ✗Believing dbt's
uniquerelies on a warehouse primary-key constraint - ✗Expecting
severity: warnto fix or drop bad rows instead of only reporting
Follow-up questions
- →How would you write a custom generic test that checks an amount is positive?
- →How do the
warn_ifanderror_ifthresholds change a test's outcome?
JuniorTheoryVery commonWhat do the dbt materializations view, table, incremental and ephemeral each build in the warehouse?
What do the dbt materializations view, table, incremental and ephemeral each build in the warehouse?
A view stores no data and recomputes per query; a table is rebuilt in full each run; an incremental model is built whole once, then later runs add only new or changed rows; ephemeral creates no object and is inlined as a CTE into models that ref it.
Common mistakes
- ✗Believing a view holds data, so querying it costs as little as a table
- ✗Thinking ephemeral creates a temporary table in the warehouse
- ✗Assuming an incremental model can never be rebuilt from scratch
Follow-up questions
- →When does a view model become too slow for a dashboard that reads it often?
- →Why can't you query an ephemeral model directly in the warehouse?
JuniorTheoryVery commonIn the SQL transformation tool dbt, what is a model, and what do ref() and source() add over a hard-coded table name?
In the SQL transformation tool dbt, what is a model, and what do ref() and source() add over a hard-coded table name?
A model is a SELECT that dbt materializes as a view or table. ref() resolves to the model's relation in the current environment and adds a DAG edge for build order and lineage; source() names a raw YAML-declared table and enables freshness checks.
Common mistakes
- ✗Hard-coding
schema.table, so a dev run reads production and dbt loses the build-order edge - ✗Thinking
ref()only feeds documentation rather than deciding build order - ✗Pointing
ref()at raw tables instead of declaring them as sources
Follow-up questions
- →How does
ref()resolve to a different schema in a dev target than in prod? - →What does
dbt source freshnesscheck, and where are its thresholds set?
MiddleCodeVery commonWrite a dbt incremental model for an events table that processes only new rows
Write a dbt incremental model for an events table that processes only new rows
Wrap the filter in {% if is_incremental() %} — where loaded_at > (select max(loaded_at) from {{ this }}). The first run and --full-refresh skip it and build everything — a costly rerun needed after a logic change; unique_key merges a re-sent event.
Common mistakes
- ✗Filtering on
{{ this }}withoutis_incremental(), which breaks the first build - ✗Leaving out
unique_key, so re-delivered events append as duplicates - ✗Changing model logic without
--full-refresh, leaving old rows computed the old way
Follow-up questions
- →Why does the filter compare
loaded_atrather than the event timeevent_ts? - →What does
on_schema_changedo when you add a column to an incremental model?
MiddleDebuggingCommonAn incremental dbt model loses a few thousand rows some mornings — find the filter bug
An incremental dbt model loses a few thousand rows some mornings — find the filter bug
The filter reads only rows newer than the max event_ts loaded, so a late row with an older event_ts is skipped forever. Re-read a 48-hour lookback and let unique_key upsert the re-read rows; catch losses with a singular test comparing daily counts to the source.
Common mistakes
- ✗Watermarking on event time when late rows carry old event timestamps
- ✗Widening the lookback without
unique_key, which duplicates re-read rows - ✗Relying on freshness checks, which cannot see rows that never loaded
Follow-up questions
- →How does the batch-by-time strategy
microbatch(dbt 1.9+) handle late rows withlookback? - →Why can a load timestamp make a safer watermark than the event time?
MiddleDesignCommonYou own three dbt incremental models on different warehouses. (1) fct_clicks on dbt-bigquery: an append-only clickstream of about 2 billion rows a day; events never change, the table is partitioned by event day, and the scheduler retries a failed run. (2) fct_orders on dbt-snowflake: an order's status keeps changing for up to 30 days after it is created; the key is order_id. (3) agg_revenue_daily on dbt-postgres: one row per date, recomputed for the last 3 days on every run. A teammate proposes incremental_strategy='merge' for all three and wants to leave unique_key out of fct_clicks because clicks never repeat. Choose a strategy for each model and justify it, say what merge without unique_key actually does, and say what goes wrong when the column passed as unique_key repeats within one batch.
You own three dbt incremental models on different warehouses. (1) fct_clicks on dbt-bigquery: an append-only clickstream of about 2 billion rows a day; events never change, the table is partitioned by event day, and the scheduler retries a failed run. (2) fct_orders on dbt-snowflake: an order's status keeps changing for up to 30 days after it is created; the key is order_id. (3) agg_revenue_daily on dbt-postgres: one row per date, recomputed for the last 3 days on every run. A teammate proposes incremental_strategy='merge' for all three and wants to leave unique_key out of fct_clicks because clicks never repeat. Choose a strategy for each model and justify it, say what merge without unique_key actually does, and say what goes wrong when the column passed as unique_key repeats within one batch.
Clicks — insert_overwrite of day partitions (BigQuery); orders — merge on order_id (Snowflake); revenue — delete+insert by date (Postgres). Keyless merge is append, so retries duplicate; a key repeated in a batch fails merge on Snowflake and BigQuery if in the target, else duplicates.
Common mistakes
- ✗Assuming
mergewithoutunique_keydeduplicates instead of appending - ✗Treating every strategy as available everywhere, though support differs per adapter
- ✗Ignoring that a key repeated within a batch makes
mergefail or insert duplicates
Follow-up questions
- →When would the batch-by-time strategy
microbatch(dbt 1.9+) beatinsert_overwriteforfct_clicks? - →How would you make
fct_orderspick up status changes older than the incremental filter?
SeniorDesignOccasionalYour team moves the dbt model fct_orders — about 3 billion rows — from Snowflake to ClickHouse with the dbt-clickhouse adapter (1.10). On Snowflake it was materialized='incremental' with incremental_strategy='merge' and unique_key='order_id'. An order's status can change for 30 days after it is created, each hourly run brings about 200,000 changed orders, and the target is a plain MergeTree table partitioned by toYYYYMM(created_at). The first ClickHouse run fails on the strategy setting. Choose an incremental strategy for this model, explain what each dbt-clickhouse option costs here — the default (legacy) strategy, delete+insert, insert_overwrite and append — and say what you give up with your choice.
Your team moves the dbt model fct_orders — about 3 billion rows — from Snowflake to ClickHouse with the dbt-clickhouse adapter (1.10). On Snowflake it was materialized='incremental' with incremental_strategy='merge' and unique_key='order_id'. An order's status can change for 30 days after it is created, each hourly run brings about 200,000 changed orders, and the target is a plain MergeTree table partitioned by toYYYYMM(created_at). The first ClickHouse run fails on the strategy setting. Choose an incremental strategy for this model, explain what each dbt-clickhouse option costs here — the default (legacy) strategy, delete+insert, insert_overwrite and append — and say what you give up with your choice.
dbt-clickhouse has no merge. Legacy (the default) rebuilds via a temp table and a swap — safe on failure, but copies all 3B rows each run. delete+insert lightweight-deletes the changed order_ids and re-inserts them — fast, not atomic, needs allow_nondeterministic_mutations; pick it. insert_overwrite (experimental) replaces whole partitions; append fits only immutable rows.
Common mistakes
- ✗Assuming
mergeworks on every adapter the way it does on Snowflake - ✗Expecting a plain
MergeTreeto collapse appended versions by key - ✗Missing that
delete+inserton ClickHouse is not atomic and can leave the table half-updated
Follow-up questions
- →When would the deduplicating engine
ReplacingMergeTreeplusappendbeatdelete+inserthere? - →What does a reader see if a
delete+insertrun dies between the delete and the insert?
SeniorTheoryOccasionalWhat does an enforced dbt model contract check, when does it run, and which models cannot have one?
What does an enforced dbt model contract check, when does it run, and which models cannot have one?
A build-time preflight: before building, dbt checks the model's column names and data types against its YAML — shape, not content; tests check content afterwards. Ephemeral, Python, materialized-view and custom models can't have one; incremental only with on_schema_change set to append_new_columns or fail.
Common mistakes
- ✗Treating a contract as a content check that replaces data tests
- ✗Thinking every materialization, incremental included, takes a contract without extra settings
- ✗Believing a contract checks only column names and ignores data types
Follow-up questions
- →Which platforms actually enforce a contract's
not_nullandprimary_keyconstraints at build time? - →How does a model version help you ship a breaking change to a contracted model?