dbt incremental load merge operation without IS_INCREMENTAL() macro

2.3k Views Asked by At

I have set my model to be incremental by

{{
  config(
    materialized='incremental',
    unique_key='my_key',
    incremental_strategy='merge'
  )
}}

Do I absolutely have to have IS_INCREMENTAL() macro at the end of the statement?

If not, then without IS_INCREMENTAL(),

1- Is dbt running a full refresh in the pipeline?

2- Is dbt still merging the output on my_key in Snowflake? Or is dbt going to completely overwrite the entire Snowflake table with the full refresh output?

1

There are 1 best solutions below

0
On

is_incremental() is just a convenience macro. It evaluates to True if the model is being run in "incremental mode" (i.e., the table already exists and the --full-refresh flag has not been passed).

If a model is running for the first time or --full-refresh is set, dbt will drop the target table and create a new table with the results of the select statement in your model.

Otherwise, in incremental mode, an incremental model will attempt to upsert any records returned by the select statement.

Typically, an incremental model's source continues to grow over time (maybe it's a table of 100M records growing by 2M records/week). In this case, you would use something like:

select *
from source
{% if is_incremental() %}
where updated_at > (select max(updated_at) from {{ this }})
{% endif %}

so that you're only selecting the new records from your source table.

However, if your source table already purges old records, you may not need the special where clause. Just note that this isn't the pattern dbt was designed for, and if someone ever runs --full-refresh on your prod target you'll lose all of the old data.

If you do have a source that purges (deletes/truncates) old records, I would consider creating a snapshot of that source, instead of loading the values into an incremental model directly. Snapshots are designed for this kind of use case, and will generalize better across multiple dev environments, etc.

Incremental docs

Snapshot docs