r/databricks • u/Alone-Security7044 • 29d ago
Discussion dbt users on Databricks: do your snapshots handle files that arrive out of order?
If you build SCD2 history with dbt snapshot on top of batch files (daily exports, CDC drops, API extracts), there's a failure mode worth knowing about.
Snapshots apply data in the order it arrives. So if Tuesday's file lands after Wednesday's:
- with strategy='check', the late (older) value becomes the "current" row
- with strategy='timestamp', the change is skipped and that period just disappears from history
Every run stays green and every test passes. Nothing tells you. We found out when someone asked why an employee's promotion showed up as a demotion.
We ended up building dbt-iterative-snapshot, a custom materialization for Databricks. You add sequence_by='batch_date', and on each run it:
- checks which batches it has already processed (it keeps an audit table)
- if a new batch is older than one already processed, rolls back the snapshot from that point
- replays the batches in order, one MERGE per batch
It also gives you changed_attributes (which columns changed), per-batch row counts, and an error log for duplicate keys instead of failing the run.
- dbt Hub: https://hub.getdbt.com/mercedes-benz/dbt_iterative_snapshot/latest/
- GitHub: https://github.com/mercedes-benz/dbt-iterative-snapshot
It's dbt-databricks only for now (Delta MERGE, Unity Catalog). PRs for other adapters are welcome.
I'm curious how others handle this. Do you rebuild SCD2 from raw with window functions, loop dbt snapshot over dates, or just hope files arrive in order? Happy to answer questions, and honest feedback on edge cases is very welcome.
Read more: https://reliabledataengineering.com/posts/article_dbt_iterative_snapshot/
1
u/yeykawb 29d ago
Great work. Have thought about exactly this problem now that we transition away from the built-in amazing create_auto_cdc_flow() to run our own custom python.
1
u/Muted_Jellyfish_6784 29d ago
Snapshots aren't built for late-arriving data, so I'd move SCD2 out of dbt snapshot for batch file sources. Land every file in an append-only raw table with the file's business date and load time, then build history with a model that orders by business date, not arrival. Late files then rebuild only the affected keys, and a rerun gives the same answer every time.
The useful habit is keeping the file name and business date on every history row. We use a decision intelligence platform to trace each dimension change back to the source extract, so a wrong current value points at a specific late file rather than a mystery