Lesson 08 dbt Intro
Theme:

Homework 08 — dbt Extensions

Lesson 08 · Data Engineering with Python · 3 exercises

All three tasks extend the dbt project committed to data-engineering/sample-pipeline/dbt/. Complete the lab first so that dbt build passes cleanly before you start.

Task 1 — Add an intermediate model with a relationships test

Create models/intermediate/int_actor_event_summary.sql that joins stg_actors and stg_events on actor and returns one row per actor with:

Then create models/intermediate/schema.yml declaring the model and adding a relationships test on actor: every value must exist in stg_actors.actor. Update dbt_project.yml to set +materialized: view for the new intermediate/ layer.

Self-check:

Task 2 — Promote mart_daily_event_counts to incremental

Change the materialization on mart_daily_event_counts.sql from table (inherited from the +materialized: table config at the marts layer) to incremental. Use ['event_date', 'type'] as the unique_key. The incremental filter should select only new rows:

{% if is_incremental() %}
WHERE event_date > (
  SELECT COALESCE(MAX(event_date), '1970-01-01'::date)
  FROM {{ this }}
)
{% endif %}

Run dbt build --full-refresh once to build from scratch, then run dbt build again without --full-refresh. The second run should be near-instant (no new rows arrive between runs).

Self-check:

Task 3 — Add a singular freshness test

Add a singular test tests/recent_data_present.sql that fails the build if the most-recent event_date in mart_daily_event_counts is more than 7 days behind today. Use DuckDB’s CURRENT_DATE. The test is one SELECT that returns rows only when the freshness invariant is violated.

Run dbt build after adding the test.

Self-check:

View solutions →