Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions macros/materializations/timestamp_incremental.sql
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@
{{ sql }}
) as dbt_incr_sbq

{#- Always include rows when the target_relation is empty, gets around null-is-false logic #}
where not exists (select 1 from {{ target_relation }})
{#- Generate a check for each timestamp column to ensure we only update changed rows -#}
{#- Note: it doesnt have to be a timestamp, any comparable type will work too, as long as newer rows have a bigger value -#}
{%- for col in dest_columns if col.name.endswith(timestamp_suffix) %}
{% if loop.first %}where {% else %} or {% endif -%}
{{ col.quoted }} > (select max({{ col.quoted }}) from {{ target_relation }})
or {{ col.quoted }} > (select max({{ col.quoted }}) from {{ target_relation }})
{%- endfor %}
{%- endmacro %}

Expand Down Expand Up @@ -70,4 +71,4 @@

{{ return({'relations': [target_relation]}) }}

{%- endmaterialization %}
{%- endmaterialization %}