Implement this without any macros.
Things to note:
- Use of the
run_started_atvariable - We've added some meta fields to make debugging easier
Pros:
- Relatively easy to intuit what's going on
Cons:
- SQL is re-used — two models have the transformation SQL (e.g.
page_views_currentandpage_views_historical), and the SQL in the models that union together the two relations are very similar - Very brittle — have to remember to materialize each model appropriately
Use macros to reduce duplicated code:
- Use a macro,
<model_name>_sql.sqlfor the transformation SQL - Use macros,
lambda_filterandlambda_unionto template thewhereclauses and theunionmodel
Things to note:
- Removed the
__lambda_currentviews, since you don't strictly need to materialize those in your warehouse - Optional var,
lambda_split, that can be overridden for the cutoff time - Added logic for a unique key (though that may have performance impacts)
- The
lambda_filtermacro relies on the model having a matching column in both the source and target table:
where {{ column_name }} >= (select max({{ column_name }}) from {{ this }})
and {{ column_name }} < '{{ filter_time }}'Pros:
- Less duplicated code
- Less chance of silly mistakes
- Fewer objects materialized in the warehouse
Cons:
- Harder to reason about — the model code lives separately to the models
Use a custom materialization
Note: This doesn't actually work
I've included a mockup in models/thought_experiment_only. As the name suggests, this is only a thought experiment.
Pros:
- We can both keep model SQL within the model file and write that SQL only once
Cons:
- It obfuscates a lot of logic into the materialization layer
Challenges:
- How to pass
configvalues down to the historical model? Namelymaterialization(table or incremental),schema,alias - How to "call" one materialization from another? We don't want to copy+paste all the logic from every adapter's
incrementalmaterialization into a newlambda_viewmaterialization - Will dbt break in new and interesting ways if it creates multiple objects in a database for one model?

