Skip to content

Commit

Permalink
Backwards-compatible handling of temporary and transient configs …
Browse files Browse the repository at this point in the history
…for dbt python models
  • Loading branch information
dbeatty10 committed Oct 11, 2023
1 parent 2011c4f commit 7074d92
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 9 deletions.
15 changes: 13 additions & 2 deletions dbt/include/snowflake/macros/materializations/table.sql
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,18 @@

{% endmaterialization %}

{% macro py_write_table(compiled_code, target_relation, temporary=False, table_type='transient') %}
{% macro py_write_table(compiled_code, target_relation, temporary=False, table_type=none) %}
{#- The following logic is only for backwards-compatiblity with deprecated `temporary` parameter -#}
{% if table_type is not none %}
{#- Just use the table_type as-is -#}
{% elif temporary -%}
{#- Case 1 when the deprecated `temporary` parameter is used without the replacement `table_type` parameter -#}
{%- set table_type = "temporary" -%}
{% else %}
{#- Case 2 when the deprecated `temporary` parameter is used without the replacement `table_type` parameter -#}
{#- Snowflake treats "" as meaning "permanent" -#}
{%- set table_type = "" -%}
{%- endif %}
{{ compiled_code }}
def materialize(session, df, target_relation):
# make sure pandas exists
Expand All @@ -52,7 +63,7 @@ def materialize(session, df, target_relation):
# session.write_pandas does not have overwrite function
df = session.createDataFrame(df)
{% set target_relation_name = resolve_model_name(target_relation) %}
df.write.mode("overwrite").save_as_table('{{ target_relation_name }}', create_temp_table={{temporary}}, table_type='{{table_type}}')
df.write.mode("overwrite").save_as_table('{{ target_relation_name }}', table_type='{{table_type}}')

def main(session):
dbt = dbtObj(session.table)
Expand Down
18 changes: 11 additions & 7 deletions dbt/include/snowflake/macros/relations/table/create.sql
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
{% macro snowflake__create_table_as(temporary, relation, compiled_code, language='sql') -%}
{%- set transient = config.get('transient', default=true) -%}

{% if temporary -%}
{%- set table_type = "temporary"
{%- elif transient -%}
{%- set table_type = "transient"
{%- else -%}
{%- set table_type = ""
{%- endif %}

{%- if language == 'sql' -%}
{%- set cluster_by_keys = config.get('cluster_by', default=none) -%}
{%- set enable_automatic_clustering = config.get('automatic_clustering', default=false) -%}
Expand All @@ -17,11 +26,7 @@

{{ sql_header if sql_header is not none }}

create or replace {% if temporary -%}
temporary
{%- elif transient -%}
transient
{%- endif %} table {{ relation }}
create or replace {{ table_type }} table {{ relation }}
{%- set contract_config = config.get('contract') -%}
{%- if contract_config.enforced -%}
{{ get_assert_columns_equivalent(sql) }}
Expand All @@ -46,8 +51,7 @@
{%- endif -%}

{%- elif language == 'python' -%}
{%- set table_type = 'transient' if transient else '' -%}
{{ py_write_table(compiled_code=compiled_code, target_relation=relation, temporary=temporary, table_type=table_type) }}
{{ py_write_table(compiled_code=compiled_code, target_relation=relation, table_type=table_type) }}
{%- else -%}
{% do exceptions.raise_compiler_error("snowflake__create_table_as macro didn't get supported language, it got %s" % language) %}
{%- endif -%}
Expand Down

0 comments on commit 7074d92

Please sign in to comment.