Skip to content
This repository was archived by the owner on Sep 2, 2025. It is now read-only.

Commit b9018f7

Browse files
vinit2107dbeatty10mikealfaremartynydbtVersusFacit
authored
ADAP-1051 - Temp Table Drop Fix (#1076)
* Fix for ADAP-1051 * adding changie files * adding changie files * Revert "adding changie files" This reverts commit f51ca0c. * Add test cases * Revert adapters.sql * Update body for changie --------- Co-authored-by: Doug Beatty <[email protected]> Co-authored-by: Mike Alfare <[email protected]> Co-authored-by: Teresa Martyny <[email protected]> Co-authored-by: Mila Page <[email protected]>
1 parent b8bc9c0 commit b9018f7

File tree

3 files changed

+70
-4
lines changed

3 files changed

+70
-4
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
kind: Fixes
2+
body: Drop intermediate objects created in BigQuery for incremental models
3+
time: 2024-01-20T18:08:18.817915-06:00
4+
custom:
5+
Author: vinit2107
6+
Issue: "1036"

dbt/include/bigquery/macros/materializations/incremental.sql

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -151,10 +151,6 @@
151151
{{ build_sql }}
152152
{% endcall %}
153153

154-
{%- if language == 'python' and tmp_relation -%}
155-
{{ adapter.drop_relation(tmp_relation) }}
156-
{%- endif -%}
157-
158154
{% endif %}
159155

160156
{{ run_hooks(post_hooks) }}
@@ -166,6 +162,10 @@
166162

167163
{% do persist_docs(target_relation, model) %}
168164

165+
{%- if tmp_relation_exists -%}
166+
{{ adapter.drop_relation(tmp_relation) }}
167+
{%- endif -%}
168+
169169
{{ return({'relations': [target_relation]}) }}
170170

171171
{%- endmaterialization %}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import pytest
2+
from google.api_core.exceptions import NotFound
3+
from dbt.adapters.bigquery.relation import BigQueryRelation
4+
from dbt.tests.util import run_dbt, get_connection, relation_from_name
5+
6+
7+
_INCREMENTAL_MODEL = """
8+
{{
9+
config(
10+
materialized="incremental",
11+
on_schema_change="sync_all_columns"
12+
)
13+
}}
14+
select 20 as id, cast('2020-01-01 01:00:00' as datetime) as date_hour union all
15+
select 40 as id, cast('2020-01-01 02:00:00' as datetime) as date_hour
16+
"""
17+
18+
_INCREMENTAL_MODEL_YAML = """version: 2
19+
models:
20+
- name: test_drop_relation
21+
columns:
22+
- name: id
23+
type: int64
24+
- name: date_hour
25+
type: datetime
26+
"""
27+
28+
29+
class BaseIncrementalModelConfig:
30+
@pytest.fixture(scope="class")
31+
def models(self):
32+
return {
33+
"test_drop_relation.sql": _INCREMENTAL_MODEL,
34+
"schema.yml": _INCREMENTAL_MODEL_YAML,
35+
}
36+
37+
38+
class TestIncrementalModel(BaseIncrementalModelConfig):
39+
def test_incremental_model_succeeds(self, project):
40+
"""
41+
Steps:
42+
1. Create the model
43+
2. Merge into the model using __dbt_tmp table
44+
3. Assert raises NotFound exception
45+
"""
46+
results = run_dbt(["run"])
47+
assert len(results) == 1
48+
results = run_dbt(["run"])
49+
assert len(results) == 1
50+
relation: BigQueryRelation = relation_from_name(
51+
project.adapter, "test_drop_relation__dbt_tmp"
52+
)
53+
adapter = project.adapter
54+
with pytest.raises(NotFound):
55+
with get_connection(project.adapter) as conn:
56+
conn.handle.get_table(
57+
adapter.connections.get_bq_table(
58+
relation.database, relation.schema, relation.table
59+
)
60+
)

0 commit comments

Comments
 (0)