Skip to content

Commit

Permalink
ADAP-1051 - Temp Table Drop Fix (#1076)
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
5 people authored Jul 22, 2024
1 parent b8bc9c0 commit b9018f7
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 4 deletions.
6 changes: 6 additions & 0 deletions .changes/unreleased/Fixes-20240120-180818.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
kind: Fixes
body: Drop intermediate objects created in BigQuery for incremental models
time: 2024-01-20T18:08:18.817915-06:00
custom:
Author: vinit2107
Issue: "1036"
8 changes: 4 additions & 4 deletions dbt/include/bigquery/macros/materializations/incremental.sql
Original file line number Diff line number Diff line change
Expand Up @@ -151,10 +151,6 @@
{{ build_sql }}
{% endcall %}

{%- if language == 'python' and tmp_relation -%}
{{ adapter.drop_relation(tmp_relation) }}
{%- endif -%}

{% endif %}

{{ run_hooks(post_hooks) }}
Expand All @@ -166,6 +162,10 @@

{% do persist_docs(target_relation, model) %}

{%- if tmp_relation_exists -%}
{{ adapter.drop_relation(tmp_relation) }}
{%- endif -%}

{{ return({'relations': [target_relation]}) }}

{%- endmaterialization %}
60 changes: 60 additions & 0 deletions tests/functional/test_drop_temp_relation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import pytest
from google.api_core.exceptions import NotFound
from dbt.adapters.bigquery.relation import BigQueryRelation
from dbt.tests.util import run_dbt, get_connection, relation_from_name


_INCREMENTAL_MODEL = """
{{
config(
materialized="incremental",
on_schema_change="sync_all_columns"
)
}}
select 20 as id, cast('2020-01-01 01:00:00' as datetime) as date_hour union all
select 40 as id, cast('2020-01-01 02:00:00' as datetime) as date_hour
"""

_INCREMENTAL_MODEL_YAML = """version: 2
models:
- name: test_drop_relation
columns:
- name: id
type: int64
- name: date_hour
type: datetime
"""


class BaseIncrementalModelConfig:
@pytest.fixture(scope="class")
def models(self):
return {
"test_drop_relation.sql": _INCREMENTAL_MODEL,
"schema.yml": _INCREMENTAL_MODEL_YAML,
}


class TestIncrementalModel(BaseIncrementalModelConfig):
def test_incremental_model_succeeds(self, project):
"""
Steps:
1. Create the model
2. Merge into the model using __dbt_tmp table
3. Assert raises NotFound exception
"""
results = run_dbt(["run"])
assert len(results) == 1
results = run_dbt(["run"])
assert len(results) == 1
relation: BigQueryRelation = relation_from_name(
project.adapter, "test_drop_relation__dbt_tmp"
)
adapter = project.adapter
with pytest.raises(NotFound):
with get_connection(project.adapter) as conn:
conn.handle.get_table(
adapter.connections.get_bq_table(
relation.database, relation.schema, relation.table
)
)

0 comments on commit b9018f7

Please sign in to comment.