diff --git a/dbt/templates.py b/dbt/templates.py index 4b29458c1db..329a6197049 100644 --- a/dbt/templates.py +++ b/dbt/templates.py @@ -9,32 +9,32 @@ class BaseCreateTemplate(object): # but you cannot explicitly set them in the CREATE TABLE ... LIKE statement. # via http://docs.aws.amazon.com/redshift/latest/dg/r_CREATE_TABLE_NEW.html incremental_template = """ -create temporary table "{identifier}__dbt_incremental_tmp" {dist_qualifier} {sort_qualifier} as ( +create temporary table "{identifier}__dbt_incremental_empty_tmp" {dist_qualifier} {sort_qualifier} as ( select * from ( {query} ) as tmp limit 0 ); -create table if not exists "{schema}"."{identifier}" (like "{identifier}__dbt_incremental_tmp"); +create table if not exists "{schema}"."{identifier}" (like "{identifier}__dbt_incremental_empty_tmp"); -{incremental_delete_statement} - -insert into "{schema}"."{identifier}" ( +create temporary table "{identifier}__dbt_incremental_tmp" as ( with dbt_incr_sbq as ( {query} ) select * from dbt_incr_sbq where ({sql_where}) or ({sql_where}) is null ); + +{incremental_delete_statement} + +insert into "{schema}"."{identifier}" ( + select * from "{identifier}__dbt_incremental_tmp" +); """ incremental_delete_template = """ delete from "{schema}"."{identifier}" where ({unique_key}) in ( - with dbt_delete_sbq as ( - {query} - ) - select ({unique_key}) from dbt_delete_sbq - where ({sql_where}) or ({sql_where}) is null + select ({unique_key}) from "{identifier}__dbt_incremental_tmp" ); """ @@ -105,17 +105,16 @@ class DryCreateTemplate(object): incremental_template = """ -create temporary table "{identifier}__dbt_incremental_tmp" {dist_qualifier} {sort_qualifier} as ( +create temporary table "{identifier}__dbt_incremental_empty_tmp" {dist_qualifier} {sort_qualifier} as ( select * from ( {query} ) as tmp limit 0 ); -create table if not exists "{schema}"."{identifier}" (like "{identifier}__dbt_incremental_tmp"); -{incremental_delete_statement} +create table if not exists "{schema}"."{identifier}" (like "{identifier}__dbt_incremental_empty_tmp"); -insert into "{schema}"."{identifier}" ( +create temporary table "{identifier}__dbt_incremental_tmp" as ( with dbt_incr_sbq as ( {query} ) @@ -123,15 +122,18 @@ class DryCreateTemplate(object): where ({sql_where}) or ({sql_where}) is null limit 0 ); + + +{incremental_delete_statement} + +insert into "{schema}"."{identifier}" ( + select * from "{identifier}__dbt_incremental_tmp" +); """ incremental_delete_template = """ delete from "{schema}"."{identifier}" where ({unique_key}) in ( - with dbt_delete_sbq as ( - {query} - ) - select ({unique_key}) from dbt_delete_sbq - where ({sql_where}) or ({sql_where}) is null + select ({unique_key}) from "{identifier}__dbt_incremental_tmp" ); """