Skip to content

Commit e1095dc

Browse files
yakovlevvsdamian3031
authored andcommitted
Add __dbt_tmp suffix to specified location for temporary tables
1 parent 5e1cdaa commit e1095dc

File tree

3 files changed

+76
-3
lines changed

3 files changed

+76
-3
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
kind: Fixes
2+
body: Add __dbt_tmp suffix to specified location for temporary tables
3+
time: 2025-02-20T12:18:52.200467772+03:00
4+
custom:
5+
Author: yakovlevvs
6+
Issue: "467"
7+
PR: "468"

dbt/include/trino/macros/adapters.sql

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@
8282
{{ return(sql) }}
8383
{% endmacro %}
8484

85-
{% macro properties() %}
85+
{% macro properties(temporary=False) %}
8686
{%- set _properties = config.get('properties') -%}
8787
{%- set table_format = config.get('table_format') -%}
8888
{%- set file_format = config.get('file_format') -%}
@@ -117,6 +117,14 @@
117117
{%- endif -%}
118118
{%- endif -%}
119119

120+
{%- if temporary -%}
121+
{%- if _properties -%}
122+
{%- if _properties.location -%}
123+
{%- do _properties.update({'location': _properties.location[:-1] ~ "__dbt_tmp'"}) -%}
124+
{%- endif -%}
125+
{%- endif -%}
126+
{%- endif -%}
127+
120128
{%- if _properties is not none -%}
121129
WITH (
122130
{%- for key, value in _properties.items() -%}
@@ -154,7 +162,7 @@
154162
{{ get_assert_columns_equivalent(sql) }}
155163
{%- set sql = get_select_subquery(sql) %}
156164
{{ comment(model.get('description')) }}
157-
{{ properties() }}
165+
{{ properties(temporary) }}
158166
;
159167
160168
insert into {{ relation }}
@@ -167,7 +175,7 @@
167175
168176
create{{ or_replace }} table {{ relation }}
169177
{{ comment(model.get('description')) }}
170-
{{ properties() }}
178+
{{ properties(temporary) }}
171179
as (
172180
{{ sql }}
173181
);

tests/functional/adapter/materialization/test_incremental_delete_insert.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
models__unary_unique_key_list_sql,
1414
seeds__seed_csv,
1515
)
16+
from dbt.tests.util import run_dbt_and_capture
1617

1718
seeds__duplicate_insert_sql = """
1819
-- Insert statement which when applied to seed.csv triggers the inplace
@@ -93,6 +94,36 @@
9394
9495
"""
9596

97+
models__location_specified = """
98+
{{
99+
config(
100+
materialized='incremental',
101+
incremental_strategy='delete+insert',
102+
unique_key=['state', 'county', 'city'],
103+
properties= {
104+
"location": "'s3a://datalake/model'"
105+
}
106+
)
107+
}}
108+
109+
select
110+
'CT' as state,
111+
'Hartford' as county,
112+
'Hartford' as city,
113+
cast('2022-02-14' as date) as last_visit_date
114+
union all
115+
select 'MA','Suffolk','Boston',DATE '2020-02-12'
116+
union all
117+
select 'NJ','Mercer','Trenton',DATE '2022-01-01'
118+
union all
119+
select 'NY','Kings','Brooklyn',DATE '2021-04-02'
120+
union all
121+
select 'NY','New York','Manhattan',DATE '2021-04-01'
122+
union all
123+
select 'PA','Philadelphia','Philadelphia',DATE '2021-05-21'
124+
125+
"""
126+
96127

97128
class TrinoIncrementalUniqueKey(BaseIncrementalUniqueKey):
98129
@pytest.fixture(scope="class")
@@ -146,3 +177,30 @@ def project_config_update(self):
146177
"models": {"+on_table_exists": "drop", "+incremental_strategy": "delete+insert"},
147178
"seeds": {"incremental": {"seed": {"+column_types": {"some_date": "date"}}}},
148179
}
180+
181+
182+
@pytest.mark.iceberg
183+
@pytest.mark.skip_profile("starburst_galaxy")
184+
class TestIcebergIncrementalDeleteInsertWithLocation:
185+
@pytest.fixture(scope="class")
186+
def models(self):
187+
return {
188+
"model.sql": models__location_specified,
189+
}
190+
191+
def test_temporary_table_location(self, project):
192+
# Create model with properties
193+
results, logs = run_dbt_and_capture(["--debug", "run"], expect_pass=True)
194+
assert len(results) == 1
195+
assert f'create table "{project.database}"."{project.test_schema}"."model"' in logs
196+
assert "location = 's3a://datalake/model'" in logs
197+
198+
# Temporary table is created on the second run
199+
# So, now we check if the second run is successful and location
200+
# is patched correctly
201+
results, logs = run_dbt_and_capture(["--debug", "run"], expect_pass=True)
202+
assert len(results) == 1
203+
assert (
204+
f'create table "{project.database}"."{project.test_schema}"."model__dbt_tmp"' in logs
205+
)
206+
assert "location = 's3a://datalake/model__dbt_tmp'" in logs

0 commit comments

Comments
 (0)