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

Add support for sql as a header to create or replace #1967

Merged
merged 10 commits into from
Dec 20, 2019
12 changes: 12 additions & 0 deletions core/dbt/include/global_project/macros/adapters/common.sql
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@
{%- endmacro %}

{% macro default__create_table_as(temporary, relation, sql) -%}
{%- set sql_header = config.get('sql_header', none) -%}

{{ sql_header if sql_header is not none }}

create {% if temporary: -%}temporary{%- endif %} table
{{ relation.include(database=(not temporary), schema=(not temporary)) }}
as (
Expand All @@ -81,6 +85,10 @@
{%- endmacro %}

{% macro default__create_view_as(relation, sql) -%}
{%- set sql_header = config.get('sql_header', none) -%}

{{ sql_header if sql_header is not none }}

create view {{ relation }} as (
{{ sql }}
);
Expand Down Expand Up @@ -269,3 +277,7 @@

{% do return(tmp_relation) %}
{% endmacro %}

{% macro set_sql_header(config) -%}
{{ config.set('sql_header', caller()) }}
{%- endmacro %}
2 changes: 1 addition & 1 deletion core/dbt/source_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class SourceConfig:
'unique_key',
'database',
'severity',

'sql_header',
'incremental_strategy',

# snapshots
Expand Down
7 changes: 7 additions & 0 deletions plugins/bigquery/dbt/include/bigquery/macros/adapters.sql
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@
{%- set raw_persist_docs = config.get('persist_docs', {}) -%}
{%- set raw_kms_key_name = config.get('kms_key_name', none) -%}
{%- set raw_labels = config.get('labels', []) -%}
{%- set sql_header = config.get('sql_header', none) -%}

{{ sql_header if sql_header is not none }}

create or replace table {{ relation }}
{{ partition_by(raw_partition_by) }}
{{ cluster_by(raw_cluster_by) }}
Expand All @@ -76,6 +80,9 @@
{% macro bigquery__create_view_as(relation, sql) -%}
{%- set raw_persist_docs = config.get('persist_docs', {}) -%}
{%- set raw_labels = config.get('labels', []) -%}
{%- set sql_header = config.get('sql_header', none) -%}

{{ sql_header if sql_header is not none }}

create or replace view {{ relation }}
{{ bigquery_table_options(persist_docs=raw_persist_docs, temporary=false, labels=raw_labels) }}
Expand Down
3 changes: 3 additions & 0 deletions plugins/postgres/dbt/include/postgres/macros/adapters.sql
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
{% macro postgres__create_table_as(temporary, relation, sql) -%}
{%- set unlogged = config.get('unlogged', default=false) -%}
{%- set sql_header = config.get('sql_header', none) -%}

{{ sql_header if sql_header is not none }}

create {% if temporary -%}
temporary
Expand Down
6 changes: 6 additions & 0 deletions plugins/redshift/dbt/include/redshift/macros/adapters.sql
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@
{%- set _sort = config.get(
'sort',
validator=validation.any[list, basestring]) -%}
{%- set sql_header = config.get('sql_header', none) -%}

{{ sql_header if sql_header is not none }}

create {% if temporary -%}temporary{%- endif %} table
{{ relation.include(database=(not temporary), schema=(not temporary)) }}
Expand All @@ -51,6 +54,9 @@
{% macro redshift__create_view_as(relation, sql) -%}

{% set bind_qualifier = '' if config.get('bind', default=True) else 'with no schema binding' %}
{%- set sql_header = config.get('sql_header', none) -%}

{{ sql_header if sql_header is not none }}

create view {{ relation }} as (
{{ sql }}
Expand Down
6 changes: 6 additions & 0 deletions plugins/snowflake/dbt/include/snowflake/macros/adapters.sql
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
{% else %}
{%- set cluster_by_string = none -%}
{%- endif -%}
{%- set sql_header = config.get('sql_header', none) -%}

{{ sql_header if sql_header is not none }}

create or replace {% if temporary -%}
temporary
Expand Down Expand Up @@ -38,6 +41,9 @@
{% macro snowflake__create_view_as(relation, sql) -%}
{%- set secure = config.get('secure', default=false) -%}
{%- set copy_grants = config.get('copy_grants', default=false) -%}
{%- set sql_header = config.get('sql_header', none) -%}

{{ sql_header if sql_header is not none }}
create or replace {% if secure -%}
secure
{%- endif %} view {{ relation }} {% if copy_grants -%} copy grants {%- endif %} as (
Expand Down
14 changes: 14 additions & 0 deletions test/integration/022_bigquery_test/models/sql_header_model.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{{ config(materialized="table") }}

{# This will fail if it is not extracted correctly #}
{% call set_sql_header(config) %}
CREATE TEMPORARY FUNCTION a_to_b(str STRING)
RETURNS STRING AS (
CASE
WHEN LOWER(str) = 'a' THEN 'b'
ELSE str
END
);
{% endcall %}

select a_to_b(dupe) as dupe from {{ ref('view_model') }}
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def test__bigquery_simple_run(self):
self.run_dbt(['seed', '--full-refresh'])
results = self.run_dbt()
# Bump expected number of results when adding new model
self.assertEqual(len(results), 7)
self.assertEqual(len(results), 8)
self.assert_nondupes_pass()


Expand All @@ -64,7 +64,7 @@ class TestUnderscoreBigQueryRun(TestBaseBigQueryRun):
def test_bigquery_run_twice(self):
self.run_dbt(['seed'])
results = self.run_dbt()
self.assertEqual(len(results), 7)
self.assertEqual(len(results), 8)
results = self.run_dbt()
self.assertEqual(len(results), 7)
self.assertEqual(len(results), 8)
self.assert_nondupes_pass()