This repository has been archived by the owner on Dec 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #36 from azhard/bigquery-support
Added BigQuery support
- Loading branch information
Showing
15 changed files
with
244 additions
and
23 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
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 |
---|---|---|
|
@@ -2,3 +2,6 @@ | |
target/ | ||
dbt_modules/ | ||
logs/ | ||
|
||
# pycharm | ||
.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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
{% macro bigquery__log_audit_event(event_name, schema, relation, user, target_name, is_full_refresh) %} | ||
|
||
insert into {{ logging.get_audit_relation() }} ( | ||
event_name, | ||
event_timestamp, | ||
event_schema, | ||
event_model, | ||
event_target, | ||
event_is_full_refresh, | ||
invocation_id | ||
) | ||
|
||
values ( | ||
'{{ event_name }}', | ||
{{ dbt_utils.current_timestamp_in_utc() }}, | ||
{% if schema != None %}'{{ schema }}'{% else %}null{% endif %}, | ||
{% if relation != None %}'{{ relation }}'{% else %}null{% endif %}, | ||
{% if target_name != None %}'{{ target_name }}'{% else %}null{% endif %}, | ||
{% if is_full_refresh %}TRUE{% else %}FALSE{% endif %}, | ||
'{{ invocation_id }}' | ||
); | ||
|
||
{% endmacro %} | ||
|
||
|
||
{% macro bigquery__create_audit_log_table() -%} | ||
|
||
{% set required_columns = [ | ||
["event_name", dbt_utils.type_string()], | ||
["event_timestamp", dbt_utils.type_timestamp()], | ||
["event_schema", dbt_utils.type_string()], | ||
["event_model", dbt_utils.type_string()], | ||
["event_target", dbt_utils.type_string()], | ||
["event_is_full_refresh", "BOOLEAN"], | ||
["invocation_id", dbt_utils.type_string()], | ||
] -%} | ||
|
||
{% set audit_table = logging.get_audit_relation() -%} | ||
|
||
{% set audit_table_exists = adapter.get_relation(audit_table.database, audit_table.schema, audit_table.name) -%} | ||
|
||
|
||
{% if audit_table_exists -%} | ||
|
||
{%- set columns_to_create = [] -%} | ||
|
||
{# map to lower to cater for snowflake returning column names as upper case #} | ||
{%- set existing_columns = adapter.get_columns_in_relation(audit_table)|map(attribute='column')|map('lower')|list -%} | ||
|
||
{%- for required_column in required_columns -%} | ||
{%- if required_column[0] not in existing_columns -%} | ||
{%- do columns_to_create.append(required_column) -%} | ||
|
||
{%- endif -%} | ||
{%- endfor -%} | ||
|
||
|
||
{%- for column in columns_to_create -%} | ||
alter table {{ audit_table }} | ||
add column {{ column[0] }} {{ column[1] }} | ||
default null; | ||
{% endfor -%} | ||
|
||
{%- else -%} | ||
create table if not exists {{ audit_table }} | ||
( | ||
{% for column in required_columns %} | ||
{{ column[0] }} {{ column[1] }}{% if not loop.last %},{% endif %} | ||
{% endfor %} | ||
) | ||
{%- endif -%} | ||
|
||
{%- endmacro %} |
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,33 @@ | ||
with events as ( | ||
|
||
select * from {{ref('stg_dbt_audit_log')}} | ||
|
||
), | ||
|
||
aggregated as ( | ||
|
||
select | ||
|
||
invocation_id, | ||
event_target as target, | ||
event_is_full_refresh as is_full_refresh, | ||
|
||
min(case | ||
when event_name = 'run started' then event_timestamp | ||
end) as deployment_started_at, | ||
|
||
min(case | ||
when event_name = 'run completed' then event_timestamp | ||
end) as deployment_completed_at, | ||
|
||
count(distinct case | ||
when event_name like '%model%' then event_model | ||
end) as models_deployed | ||
|
||
from events | ||
|
||
{{ dbt_utils.group_by(n=3) }} | ||
|
||
) | ||
|
||
select * from aggregated |
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,38 @@ | ||
with events as ( | ||
|
||
select * from {{ ref('stg_dbt_audit_log') }} | ||
|
||
), | ||
|
||
aggregated as ( | ||
|
||
select | ||
|
||
{{ dbt_utils.surrogate_key([ | ||
'event_model', | ||
'invocation_id' | ||
]) }} as model_deployment_id, | ||
|
||
invocation_id, | ||
event_model as model, | ||
event_schema as schema, | ||
event_target as target, | ||
event_is_full_refresh as is_full_refresh, | ||
|
||
min(case | ||
when event_name = 'model deployment started' then event_timestamp | ||
end) as deployment_started_at, | ||
|
||
min(case | ||
when event_name = 'model deployment completed' then event_timestamp | ||
end) as deployment_completed_at | ||
|
||
from events | ||
|
||
where event_name like '%model%' | ||
|
||
{{ dbt_utils.group_by(n=6) }} | ||
|
||
) | ||
|
||
select * from aggregated |
File renamed without changes.
File renamed without changes.
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 |
---|---|---|
@@ -1,3 +1,3 @@ | ||
packages: | ||
- package: fishtown-analytics/dbt_utils | ||
version: [">=0.4.0", "<0.7.0"] | ||
version: [">=0.6.0", "<0.7.0"] |