From 7074d92cebc2f5d8b435b5bf2f88e9d260f6cdfe Mon Sep 17 00:00:00 2001 From: Doug Beatty Date: Wed, 11 Oct 2023 12:21:43 -0600 Subject: [PATCH] Backwards-compatible handling of `temporary` and `transient` configs for dbt python models --- .../macros/materializations/table.sql | 15 +++++++++++++-- .../macros/relations/table/create.sql | 18 +++++++++++------- 2 files changed, 24 insertions(+), 9 deletions(-) diff --git a/dbt/include/snowflake/macros/materializations/table.sql b/dbt/include/snowflake/macros/materializations/table.sql index e08c9d5da..ef201c705 100644 --- a/dbt/include/snowflake/macros/materializations/table.sql +++ b/dbt/include/snowflake/macros/materializations/table.sql @@ -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 @@ -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) diff --git a/dbt/include/snowflake/macros/relations/table/create.sql b/dbt/include/snowflake/macros/relations/table/create.sql index 7b94a8e0e..846dccf22 100644 --- a/dbt/include/snowflake/macros/relations/table/create.sql +++ b/dbt/include/snowflake/macros/relations/table/create.sql @@ -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) -%} @@ -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) }} @@ -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 -%}