|
53 | 53 | ) %} |
54 | 54 | {% endmacro %} |
55 | 55 |
|
56 | | -{# In Postgres / Redshift we do not want to replace the table, because that will cause views without |
57 | | - late binding to be deleted. So instead we atomically replace the data in a transaction #} |
| 56 | +{# Postgres - atomically replace data without dropping the table (preserves views). |
| 57 | + Each statement is executed separately for post_hook compatibility. #} |
58 | 58 | {% macro postgres__replace_table_data(relation, rows) %} |
59 | 59 | {% set intermediate_relation = elementary.create_intermediate_relation( |
60 | 60 | relation, rows, temporary=True |
61 | 61 | ) %} |
62 | 62 |
|
63 | | - {% set query %} |
64 | | - begin transaction; |
65 | | - delete from {{ relation }}; -- truncate supported in Redshift transactions, but causes an immediate commit |
66 | | - insert into {{ relation }} select * from {{ intermediate_relation }}; |
67 | | - commit; |
68 | | - {% endset %} |
69 | | - {% do elementary.run_query(query) %} |
| 63 | + {% do elementary.run_query("begin") %} |
| 64 | + {% do elementary.run_query("delete from " ~ relation) %} |
| 65 | + {% do elementary.run_query( |
| 66 | + "insert into " ~ relation ~ " select * from " ~ intermediate_relation |
| 67 | + ) %} |
| 68 | + {% do elementary.run_query("commit") %} |
| 69 | + |
| 70 | + {% do adapter.drop_relation(intermediate_relation) %} |
| 71 | +{% endmacro %} |
| 72 | + |
| 73 | +{# Redshift - replace data without dropping the table (preserves late-binding views). |
| 74 | + Separate statements without explicit transaction for post_hook compatibility |
| 75 | + (Redshift cannot run multiple statements in a single prepared statement). |
| 76 | + NOTE: Non-atomic - if insert fails after delete, data is lost until the next run. |
| 77 | + Acceptable here because these are internal artifact tables that are regenerated. #} |
| 78 | +{% macro redshift__replace_table_data(relation, rows) %} |
| 79 | + {% set intermediate_relation = elementary.create_intermediate_relation( |
| 80 | + relation, rows, temporary=True |
| 81 | + ) %} |
| 82 | + |
| 83 | + {% do elementary.run_query("delete from " ~ relation) %} |
| 84 | + {% do elementary.run_query( |
| 85 | + "insert into " ~ relation ~ " select * from " ~ intermediate_relation |
| 86 | + ) %} |
70 | 87 |
|
71 | 88 | {% do adapter.drop_relation(intermediate_relation) %} |
72 | 89 | {% endmacro %} |
|
81 | 98 | ) %} |
82 | 99 | {% endmacro %} |
83 | 100 |
|
| 101 | +{# Trino - drop and recreate (Trino does not support CREATE OR REPLACE TABLE) #} |
84 | 102 | {% macro trino__replace_table_data(relation, rows) %} |
85 | 103 | {% set intermediate_relation = elementary.create_intermediate_relation( |
86 | 104 | relation, rows, temporary=True |
87 | 105 | ) %} |
88 | | - {% do elementary.run_query( |
89 | | - adapter.dispatch("create_table_as")( |
90 | | - False, |
91 | | - relation, |
92 | | - "select * from {}".format(intermediate_relation), |
93 | | - replace=true, |
94 | | - ) |
| 106 | + {% do elementary.edr_create_table_as( |
| 107 | + False, |
| 108 | + relation, |
| 109 | + "select * from {}".format(intermediate_relation), |
| 110 | + drop_first=true, |
95 | 111 | ) %} |
96 | 112 | {% do adapter.drop_relation(intermediate_relation) %} |
97 | 113 | {% endmacro %} |
|
106 | 122 | chunk_size=elementary.get_config_var("dbt_artifacts_chunk_size"), |
107 | 123 | ) %} |
108 | 124 | {% endmacro %} |
| 125 | + |
| 126 | +{# ClickHouse - cluster-aware truncate and insert (non-atomic). |
| 127 | + Uses explicit TRUNCATE with on_cluster_clause for distributed/replicated tables, |
| 128 | + matching the pattern in delete_and_insert.sql and clean_elementary_test_tables.sql. #} |
| 129 | +{% macro clickhouse__replace_table_data(relation, rows) %} |
| 130 | + {% do elementary.run_query( |
| 131 | + "truncate table " ~ relation ~ " " ~ on_cluster_clause(relation) |
| 132 | + ) %} |
| 133 | + {% do elementary.insert_rows( |
| 134 | + relation, |
| 135 | + rows, |
| 136 | + should_commit=false, |
| 137 | + chunk_size=elementary.get_config_var("dbt_artifacts_chunk_size"), |
| 138 | + ) %} |
| 139 | +{% endmacro %} |
| 140 | + |
| 141 | +{# Vertica - truncate and insert (non-atomic) #} |
| 142 | +{% macro vertica__replace_table_data(relation, rows) %} |
| 143 | + {% do dbt.truncate_relation(relation) %} |
| 144 | + {% do elementary.insert_rows( |
| 145 | + relation, |
| 146 | + rows, |
| 147 | + should_commit=false, |
| 148 | + chunk_size=elementary.get_config_var("dbt_artifacts_chunk_size"), |
| 149 | + ) %} |
| 150 | +{% endmacro %} |
| 151 | + |
| 152 | +{# Fabric / SQL Server - truncate and insert (non-atomic). |
| 153 | + sqlserver dispatches through fabric via the chain: sqlserver__ -> fabric__ -> default__, |
| 154 | + so this covers both adapters. #} |
| 155 | +{% macro fabric__replace_table_data(relation, rows) %} |
| 156 | + {% do dbt.truncate_relation(relation) %} |
| 157 | + {% do elementary.insert_rows( |
| 158 | + relation, |
| 159 | + rows, |
| 160 | + should_commit=false, |
| 161 | + chunk_size=elementary.get_config_var("dbt_artifacts_chunk_size"), |
| 162 | + ) %} |
| 163 | +{% endmacro %} |
0 commit comments