From d22e8171cf3dddf535b7f114f40fff62a049354b Mon Sep 17 00:00:00 2001 From: Ben Edwards Date: Wed, 18 Dec 2019 10:09:16 +1100 Subject: [PATCH] Handle incrementals on relations with no rows Fixes #19 --- macros/materializations/timestamp_incremental.sql | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/macros/materializations/timestamp_incremental.sql b/macros/materializations/timestamp_incremental.sql index 294fcc6..f335954 100644 --- a/macros/materializations/timestamp_incremental.sql +++ b/macros/materializations/timestamp_incremental.sql @@ -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 %} @@ -70,4 +71,4 @@ {{ return({'relations': [target_relation]}) }} -{%- endmaterialization %} \ No newline at end of file +{%- endmaterialization %}