Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rework incremental models to use a temp table #156

Merged
merged 1 commit into from
Sep 23, 2016
Merged
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
40 changes: 21 additions & 19 deletions dbt/templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
);
"""

Expand Down Expand Up @@ -105,33 +105,35 @@ 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}
)
select * from dbt_incr_sbq
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"
);
"""

Expand Down