forked from dbt-labs/dbt-core
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement pre/post hooks and incremental tables on BigQuery. dbt-labs…
- Loading branch information
Showing
7 changed files
with
128 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -68,3 +68,6 @@ target/ | |
|
||
# Sublime Text | ||
*.sublime-* | ||
|
||
#PyCharm/IntelliJ | ||
.idea/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
101 changes: 101 additions & 0 deletions
101
dbt/include/global_project/macros/materializations/incremental/bigquery_incremental.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
{% macro dbt_bigquery__incremental_delete(target_relation, tmp_relation) -%} | ||
|
||
{%- set unique_key = config.require('unique_key') -%} | ||
{%- set identifier = model['name'] -%} | ||
|
||
delete | ||
from {{ target_relation }} | ||
where ({{ unique_key }}) in ( | ||
select ({{ unique_key }}) | ||
from {{ tmp_relation.include(schema=True) }} | ||
); | ||
|
||
{%- endmacro %} | ||
|
||
{% materialization incremental, default -%} | ||
{%- set sql_where = config.require('sql_where') -%} | ||
{%- set unique_key = config.get('unique_key') -%} | ||
|
||
{%- set identifier = model['name'] -%} | ||
{%- set tmp_identifier = model['name'] + '__dbt_incremental_tmp' -%} | ||
|
||
{%- set existing_relations = adapter.list_relations(schema=schema) -%} | ||
{%- set old_relation = adapter.get_relation(relations_list=existing_relations, | ||
schema=schema, identifier=identifier) -%} | ||
{%- set target_relation = api.Relation.create(identifier=identifier, schema=schema, type='table') -%} | ||
{%- set tmp_relation = api.Relation.create(identifier=tmp_identifier, | ||
schema=schema, type='table') -%} | ||
|
||
{%- set non_destructive_mode = (flags.NON_DESTRUCTIVE == True) -%} | ||
{%- set full_refresh_mode = (flags.FULL_REFRESH == True) -%} | ||
|
||
{%- set exists_as_table = (old_relation is not none and old_relation.is_table) -%} | ||
{%- set exists_not_as_table = (old_relation is not none and not old_relation.is_table) -%} | ||
|
||
{%- set should_truncate = (non_destructive_mode and full_refresh_mode and exists_as_table) -%} | ||
{%- set should_drop = (not should_truncate and (full_refresh_mode or exists_not_as_table)) -%} | ||
{%- set force_create = (flags.FULL_REFRESH and not flags.NON_DESTRUCTIVE) -%} | ||
|
||
-- setup | ||
{% if old_relation is none -%} | ||
-- noop | ||
{%- elif should_truncate -%} | ||
{{ adapter.truncate_relation(old_relation) }} | ||
{%- elif should_drop -%} | ||
{{ adapter.drop_relation(old_relation) }} | ||
{%- set old_relation = none -%} | ||
{%- endif %} | ||
|
||
{{ run_bigquery_hooks(pre_hooks) }} | ||
|
||
-- build model | ||
{% if force_create or old_relation is none -%} | ||
{%- call statement('main') -%} | ||
{{ create_table_as(False, target_relation, sql) }} | ||
{%- endcall -%} | ||
{%- else -%} | ||
{%- call statement() -%} | ||
|
||
{% set tmp_table_sql -%} | ||
with dbt_incr_sbq as ( | ||
{{ sql }} | ||
) | ||
select * from dbt_incr_sbq | ||
where ({{ sql_where }}) | ||
or ({{ sql_where }}) is null | ||
{%- endset %} | ||
|
||
{{ dbt.create_table_as(True, tmp_relation, tmp_table_sql) }} | ||
|
||
{%- endcall -%} | ||
|
||
{{ adapter.expand_target_column_types(temp_table=tmp_identifier, | ||
to_schema=schema, | ||
to_table=identifier) }} | ||
|
||
|
||
{% if unique_key is not none -%} | ||
{%- call statement() -%} | ||
{{ dbt_bigquery__incremental_delete(target_relation, tmp_relation) }} | ||
{%- endcall -%} | ||
|
||
{%- endif %} | ||
{%- call statement('main') -%} | ||
{% set dest_columns = adapter.get_columns_in_table(schema, identifier) %} | ||
{% set dest_cols_csv = dest_columns | map(attribute='quoted') | join(', ') %} | ||
|
||
insert into {{ target_relation }} ({{ dest_cols_csv }}) | ||
( | ||
select {{ dest_cols_csv }} | ||
from {{ tmp_relation.include(schema=True) }} | ||
); | ||
{% endcall %} | ||
|
||
-- clean up temp table in bigquery | ||
{{ drop_relation_if_exists(tmp_relation) }} | ||
|
||
{%- endif %} | ||
|
||
{{ run_bigquery_hooks(post_hooks) }} | ||
|
||
{%- endmaterialization %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters