-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
use
create table as
syntax on BigQuery (#717)
- Loading branch information
Showing
9 changed files
with
81 additions
and
59 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 |
---|---|---|
@@ -1,8 +1,27 @@ | ||
{% macro partition_by(raw_partition_by) %} | ||
{%- if raw_partition_by is none -%} | ||
{{ return('') }} | ||
{% endif %} | ||
|
||
{% set partition_by_clause %} | ||
partition by {{ raw_partition_by }} | ||
{%- endset -%} | ||
|
||
{{ return(partition_by_clause) }} | ||
{%- endmacro -%} | ||
|
||
{% macro bigquery__create_table_as(temporary, identifier, sql) -%} | ||
{{ adapter.execute_model({"name": identifier, "injected_sql": sql, "schema": schema}, 'table') }} | ||
{%- set raw_partition_by = config.get('partition_by', none) -%} | ||
|
||
create or replace table `{{ schema }}`.`{{ identifier }}` | ||
{{ partition_by(raw_partition_by) }} | ||
as ( | ||
{{ sql }} | ||
); | ||
{% endmacro %} | ||
|
||
{% macro bigquery__create_view_as(identifier, sql) -%} | ||
{{ adapter.execute_model({"name": identifier, "injected_sql": sql, "schema": schema}, 'view') }} | ||
create or replace view `{{ schema }}`.`{{ identifier }}` as ( | ||
{{ sql }} | ||
); | ||
{% endmacro %} |
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
File renamed without changes.
34 changes: 34 additions & 0 deletions
34
dbt/include/global_project/macros/materializations/view/bigquery_view.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,34 @@ | ||
|
||
{% materialization view, adapter='bigquery' -%} | ||
|
||
{%- set identifier = model['name'] -%} | ||
{%- set non_destructive_mode = (flags.NON_DESTRUCTIVE == True) -%} | ||
{%- set existing = adapter.query_for_existing(schema) -%} | ||
{%- set existing_type = existing.get(identifier) -%} | ||
|
||
{%- if existing_type is not none -%} | ||
{%- if existing_type == 'table' and not flags.FULL_REFRESH -%} | ||
{# this is only intended for date partitioned tables, but we cant see that field in the context #} | ||
{% set error_message -%} | ||
Trying to create model '{{ identifier }}' as a view, but it already exists as a table. | ||
Either drop the '{{ schema }}.{{ identifier }}' table manually, or use --full-refresh | ||
{%- endset %} | ||
{{ exceptions.raise_compiler_error(error_message) }} | ||
{%- endif -%} | ||
|
||
{{ adapter.drop(schema, identifier, existing_type) }} | ||
{%- endif -%} | ||
|
||
-- build model | ||
{% if existing_type == 'view' and non_destructive_mode -%} | ||
{% call noop_statement('main', status="PASS", res=None) -%} | ||
-- Not running : non-destructive mode | ||
{{ sql }} | ||
{%- endcall %} | ||
{%- else -%} | ||
{% call statement('main') -%} | ||
{{ create_view_as(identifier, sql) }} | ||
{%- endcall %} | ||
{%- endif %} | ||
|
||
{%- endmaterialization %} |
File renamed without changes.
20 changes: 0 additions & 20 deletions
20
dbt/include/global_project/macros/materializations/wrapper.sql
This file was deleted.
Oops, something went wrong.