An incremental dbt model loses a few thousand rows some mornings — find the filter bug
The fct_events model on dbt-snowflake runs every hour. Mobile clients upload events up to 48 hours late. Some mornings the model holds a few thousand fewer rows for the previous day than the source app.events; nothing errors and every test is green.
Constraints: keep the model incremental (a full rebuild each hour is too expensive), and rows that are read twice must not be duplicated.
{{ config(materialized='incremental', unique_key='event_id') }}
select event_id, user_id, event_type, event_ts
from {{ source('app', 'events') }}
{% if is_incremental() %}
where event_ts > (select max(event_ts) from {{ this }})
{% endif %}Find and fix the bug, then say how you would detect this kind of loss automatically.
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.
- 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
- 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?
The watermark is the maximum event_ts already in the model. An event uploaded a day late carries yesterday's event_ts, while the maximum has moved on: event_ts > max(event_ts) is false, and the row never reaches the model on any later run. Nothing errors because the query is valid — it just answers the wrong question.
The fix is to re-read a window that covers the maximum lateness and rely on unique_key: on dbt-snowflake the default strategy is merge, so rows already loaded from the window are updated, not duplicated.
{{ config(materialized='incremental', unique_key='event_id') }}
select event_id, user_id, event_type, event_ts
from {{ source('app', 'events') }}
{% if is_incremental() %}
-- the lookback window covers clients that upload up to 48 hours late
where event_ts > (select dateadd(hour, -48, max(event_ts)) from {{ this }})
{% endif %}The cost is rescanning 48 hours of source every hour. An alternative is a watermark on the load time (_loaded_at) if the source stores it, so event lateness stops mattering. On dbt ≥ 1.9 the microbatch strategy with event_time and lookback gives the same result.
Detection is a singular test that returns the days whose counts differ (zero rows means it passed):
-- tests/assert_fct_events_complete.sql
select s.day, s.n as source_rows, coalesce(m.n, 0) as model_rows
from (select date_trunc('day', event_ts) as day, count(*) as n
from {{ source('app', 'events') }} group by 1) s
left join (select date_trunc('day', event_ts) as day, count(*) as n
from {{ ref('fct_events') }} group by 1) m using (day)
where s.day < current_date and s.n <> coalesce(m.n, 0)A source freshness check would not help here: fresh rows keep arriving, and only the late ones go missing.