|
12 | 12 |
|
13 | 13 | Low-level specifics: |
14 | 14 | If an invalid option is specified, then we will raise an |
15 | | - excpetion with corresponding message. |
| 15 | + exception with a corresponding message. |
16 | 16 |
|
17 | 17 | Languages other than SQL (like Python) will use a temporary table. |
18 | | - With the default strategy of merge, the user may choose between a temporary |
19 | | - table and view (defaulting to view). |
20 | | -
|
21 | | - The append strategy can use a view because it will run a single INSERT statement. |
22 | | -
|
23 | | - When unique_key is none, the delete+insert and microbatch strategies can use a view beacuse a |
24 | | - single INSERT statement is run with no DELETES as part of the statement. |
25 | | - Otherwise, play it safe by using a temporary table. |
26 | | -
|
27 | | - Catalog-linked databases (Iceberg tables) does not support using temporary relations. |
| 18 | + With the default strategy of merge, the user may choose between a |
| 19 | + temporary table and view (defaulting to view). |
| 20 | +
|
| 21 | + The append strategy can use a view because it will run a single INSERT |
| 22 | + statement. |
| 23 | +
|
| 24 | + When unique_key is none, the delete+insert and microbatch strategies |
| 25 | + can use a view because a single INSERT statement is run with no DELETES |
| 26 | + as part of the statement. Otherwise, play it safe by using a table. |
| 27 | +
|
| 28 | + Catalog-linked databases (Iceberg tables) do not support temporary |
| 29 | + relations or transient tables — only Iceberg tables are allowed. A |
| 30 | + permanent table is used as the tmp relation for CLD models. |
| 31 | +
|
| 32 | + 'transient' is also available as a user-facing tmp_relation_type for |
| 33 | + non-Iceberg models. Unlike session-scoped temporary tables, transient |
| 34 | + tables are visible to Snowflake's lineage tracking. Note that transient |
| 35 | + tables share the regular schema namespace; use the |
| 36 | + snowflake__resolve_incremental_tmp_relation dispatch macro to redirect |
| 37 | + tmp relations to a dedicated schema to avoid name collisions when |
| 38 | + multiple runs share the same target schema. |
28 | 39 | #} */ |
29 | 40 |
|
30 | | - {#-- Always use table for catalog-linked databases (Iceberg) --#} |
31 | | - {% if snowflake__is_catalog_linked_database(relation=config.model) %} |
32 | | - {{ return("table") }} |
33 | | - {% endif %} |
34 | | - |
35 | 41 | {% if language == "python" and tmp_relation_type is not none %} |
36 | 42 | {% do exceptions.raise_compiler_error( |
37 | 43 | "Python models currently only support 'table' for tmp_relation_type but " |
38 | 44 | ~ tmp_relation_type ~ " was specified." |
39 | 45 | ) %} |
40 | 46 | {% endif %} |
41 | 47 |
|
42 | | - {% if strategy in ["delete+insert", "microbatch"] and tmp_relation_type is not none and tmp_relation_type != "table" and unique_key is not none %} |
| 48 | + {#-- Python always uses a temporary table, regardless of other conditions --#} |
| 49 | + {% if language != "sql" %} |
| 50 | + {{ return("table") }} |
| 51 | + {% endif %} |
| 52 | + |
| 53 | + {#-- CLD schemas only support Iceberg tables; use table (not transient) --#} |
| 54 | + {% if snowflake__is_catalog_linked_database(relation=config.model) %} |
| 55 | + {{ return("table") }} |
| 56 | + {% endif %} |
| 57 | + |
| 58 | + {% if strategy in ["delete+insert", "microbatch"] and tmp_relation_type is not none and tmp_relation_type not in ("table", "transient") and unique_key is not none %} |
43 | 59 | {% do exceptions.raise_compiler_error( |
44 | 60 | "In order to maintain consistent results when `unique_key` is not none, |
45 | | - the `" ~ strategy ~ "` strategy only supports `table` for `tmp_relation_type` but " |
| 61 | + the `" ~ strategy ~ "` strategy only supports `table` or `transient` for `tmp_relation_type` but " |
46 | 62 | ~ tmp_relation_type ~ " was specified." |
47 | 63 | ) |
48 | 64 | %} |
49 | 65 | {% endif %} |
50 | 66 |
|
51 | | - {% if language != "sql" %} |
52 | | - {{ return("table") }} |
53 | | - {% elif tmp_relation_type == "table" %} |
| 67 | + {% if tmp_relation_type == "table" %} |
54 | 68 | {{ return("table") }} |
55 | 69 | {% elif tmp_relation_type == "view" %} |
56 | 70 | {{ return("view") }} |
| 71 | + {% elif tmp_relation_type == "transient" %} |
| 72 | + {{ return("transient") }} |
57 | 73 | {% elif strategy in ("default", "merge", "append", "insert_overwrite") %} |
58 | 74 | {{ return("view") }} |
59 | 75 | {% elif strategy in ["delete+insert", "microbatch"] and unique_key is none %} |
|
63 | 79 | {% endif %} |
64 | 80 | {% endmacro %} |
65 | 81 |
|
| 82 | + |
| 83 | +{% macro resolve_incremental_tmp_relation(tmp_relation) %} |
| 84 | + {{ return(adapter.dispatch('resolve_incremental_tmp_relation', 'dbt')(tmp_relation)) }} |
| 85 | +{% endmacro %} |
| 86 | + |
| 87 | + |
| 88 | +{% macro snowflake__resolve_incremental_tmp_relation(tmp_relation) %} |
| 89 | + {#-- |
| 90 | + Override this macro in your project to control where the incremental |
| 91 | + tmp relation is created. Useful for redirecting to a dedicated scratch |
| 92 | + schema to avoid name collisions when multiple runs share the same |
| 93 | + target schema. |
| 94 | + |
| 95 | + Example: |
| 96 | + {% macro snowflake__resolve_incremental_tmp_relation(tmp_relation) %} |
| 97 | + {{ return(tmp_relation.incorporate(schema='scratch')) }} |
| 98 | + {% endmacro %} |
| 99 | + --#} |
| 100 | + {{ return(tmp_relation) }} |
| 101 | +{% endmacro %} |
| 102 | + |
66 | 103 | {% materialization incremental, adapter='snowflake', supported_languages=['sql', 'python'] -%} |
67 | 104 |
|
68 | 105 | {% set original_query_tag = set_query_tag() %} |
|
94 | 131 | {% if is_catalog_linked_db %} |
95 | 132 | {% set tmp_relation = make_temp_relation(this).incorporate(type=tmp_relation_type, catalog=catalog_relation.catalog_name, is_table=true) %} |
96 | 133 | {% else %} |
97 | | - {% set tmp_relation = make_temp_relation(this).incorporate(type=tmp_relation_type) %} |
| 134 | + {#-- Transient tables are dropped with DROP TABLE, so the relation type must be 'table' --#} |
| 135 | + {% set tmp_relation_object_type = 'table' if tmp_relation_type == 'transient' else tmp_relation_type %} |
| 136 | + {% set tmp_relation = make_temp_relation(this).incorporate(type=tmp_relation_object_type) %} |
98 | 137 | {% endif %} |
| 138 | + {% set tmp_relation = resolve_incremental_tmp_relation(tmp_relation) %} |
99 | 139 |
|
100 | 140 | {% set grant_config = config.get('grants') %} |
101 | 141 |
|
|
131 | 171 | %} |
132 | 172 |
|
133 | 173 | {% else %} |
134 | | - {#-- Create the temp relation, either as a view or as a temp table --#} |
| 174 | + {#-- Create the temp relation as a view, temp table, or transient table --#} |
135 | 175 | {% if is_catalog_linked_db %} |
136 | 176 | {%- call statement('create_tmp_relation', language=language) -%} |
137 | 177 | {{ create_table_as(False, tmp_relation, compiled_code, language) }} |
|
140 | 180 | {%- call statement('create_tmp_relation') -%} |
141 | 181 | {{ snowflake__create_view_as_with_temp_flag(tmp_relation, compiled_code, True) }} |
142 | 182 | {%- endcall -%} |
| 183 | + {% elif tmp_relation_type == 'transient' %} |
| 184 | + {%- call statement('create_tmp_relation', language=language) -%} |
| 185 | + {{ snowflake__create_table_transient_sql(tmp_relation, compiled_code) }} |
| 186 | + {%- endcall -%} |
143 | 187 | {% else %} |
144 | 188 | {%- call statement('create_tmp_relation', language=language) -%} |
145 | 189 | {{ create_table_as(True, tmp_relation, compiled_code, language) }} |
|
0 commit comments