Skip to content

Commit d4c9ca8

Browse files
fix: check for old tmp table before deleting it (#550)
Co-authored-by: Spencer horton <[email protected]>
1 parent d126c21 commit d4c9ca8

File tree

2 files changed

+19
-18
lines changed

2 files changed

+19
-18
lines changed

dbt/include/athena/macros/materializations/models/incremental/incremental.sql

+10-11
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
{% materialization incremental, adapter='athena' -%}
2-
32
{% set raw_strategy = config.get('incremental_strategy') or 'insert_overwrite' %}
43
{% set table_type = config.get('table_type', default='hive') | lower %}
54
{% set strategy = validate_get_incremental_strategy(raw_strategy, table_type) %}
@@ -11,7 +10,10 @@
1110
{% set force_batch = config.get('force_batch', False) | as_bool -%}
1211
{% set target_relation = this.incorporate(type='table') %}
1312
{% set existing_relation = load_relation(this) %}
14-
{% set tmp_relation = make_temp_relation(this) %}
13+
{% set old_tmp_relation = adapter.get_relation(identifier=target_relation.identifier ~ '__dbt_tmp',
14+
schema=schema,
15+
database=database) %}
16+
{% set tmp_relation = make_temp_relation(target_relation, '__dbt_tmp') %}
1517

1618
-- If no partitions are used with insert_overwrite, we fall back to append mode.
1719
{% if partitioned_by is none and strategy == 'insert_overwrite' %}
@@ -32,9 +34,8 @@
3234
{% set query_result = safe_create_table_as(False, target_relation, sql, force_batch) -%}
3335
{% set build_sql = "select '" ~ query_result ~ "'" -%}
3436
{% elif partitioned_by is not none and strategy == 'insert_overwrite' %}
35-
{% set tmp_relation = make_temp_relation(target_relation) %}
36-
{% if tmp_relation is not none %}
37-
{% do drop_relation(tmp_relation) %}
37+
{% if old_tmp_relation is not none %}
38+
{% do drop_relation(old_tmp_relation) %}
3839
{% endif %}
3940
{% set query_result = safe_create_table_as(True, tmp_relation, sql, force_batch) -%}
4041
{% do delete_overlapping_partitions(target_relation, tmp_relation, partitioned_by) %}
@@ -44,9 +45,8 @@
4445
%}
4546
{% do to_drop.append(tmp_relation) %}
4647
{% elif strategy == 'append' %}
47-
{% set tmp_relation = make_temp_relation(target_relation) %}
48-
{% if tmp_relation is not none %}
49-
{% do drop_relation(tmp_relation) %}
48+
{% if old_tmp_relation is not none %}
49+
{% do drop_relation(old_tmp_relation) %}
5050
{% endif %}
5151
{% set query_result = safe_create_table_as(True, tmp_relation, sql, force_batch) -%}
5252
{% set build_sql = incremental_insert(
@@ -74,9 +74,8 @@
7474
{% do exceptions.raise_compiler_error(inc_predicates_not_list) %}
7575
{% endif %}
7676
{% endif %}
77-
{% set tmp_relation = make_temp_relation(target_relation) %}
78-
{% if tmp_relation is not none %}
79-
{% do drop_relation(tmp_relation) %}
77+
{% if old_tmp_relation is not none %}
78+
{% do drop_relation(old_tmp_relation) %}
8079
{% endif %}
8180
{% set query_result = safe_create_table_as(True, tmp_relation, sql, force_batch) -%}
8281
{% set build_sql = iceberg_merge(

dbt/include/athena/macros/materializations/models/table/table.sql

+9-7
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77

88
{%- set table_type = config.get('table_type', default='hive') | lower -%}
99
{%- set old_relation = adapter.get_relation(database=database, schema=schema, identifier=identifier) -%}
10+
{%- set old_tmp_relation = adapter.get_relation(identifier=identifier ~ '__ha',
11+
schema=schema,
12+
database=database) -%}
1013
{%- set is_ha = config.get('ha', default=false) -%}
1114
{%- set s3_data_dir = config.get('s3_data_dir', default=target.s3_data_dir) -%}
1215
{%- set s3_data_naming = config.get('s3_data_naming', default='table_unique') -%}
@@ -44,9 +47,9 @@
4447

4548
-- for ha tables that are not in full refresh mode and when the relation exists we use the swap behavior
4649
{%- if is_ha and not is_full_refresh_mode and old_relation is not none -%}
47-
-- drop the tmp_relation
48-
{%- if tmp_relation is not none -%}
49-
{%- do adapter.delete_from_glue_catalog(tmp_relation) -%}
50+
-- drop the old_tmp_relation if it exists
51+
{%- if old_tmp_relation is not none -%}
52+
{%- do adapter.delete_from_glue_catalog(old_tmp_relation) -%}
5053
{%- endif -%}
5154

5255
-- create tmp table
@@ -69,7 +72,6 @@
6972
{%- endif -%}
7073

7174
{{ set_table_classification(target_relation) }}
72-
7375
{%- else -%}
7476

7577
{%- if old_relation is none -%}
@@ -80,9 +82,9 @@
8082
{%- do drop_relation(old_relation) -%}
8183
{%- do rename_relation(tmp_relation, target_relation) -%}
8284
{%- else -%}
83-
84-
{%- if tmp_relation is not none -%}
85-
{%- do drop_relation(tmp_relation) -%}
85+
-- delete old tmp iceberg table if it exists
86+
{%- if old_tmp_relation is not none -%}
87+
{%- do drop_relation(old_tmp_relation) -%}
8688
{%- endif -%}
8789

8890
{%- set old_relation_bkp = make_temp_relation(old_relation, '__bkp') -%}

0 commit comments

Comments
 (0)