Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ADAP-1051 - Temp Table Drop Fix #1076

Merged
merged 25 commits into from
Jul 22, 2024
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
d0a94a2
Fix for ADAP-1051
vinit2107 Jan 20, 2024
5adbcb2
Merge pull request #1 from vinit2107/ADAP-1051
vinit2107 Jan 20, 2024
f51ca0c
adding changie files
vinit2107 Jan 21, 2024
318a864
adding changie files
vinit2107 Jan 21, 2024
6a3b185
Merge branch 'ADAP-1051' of https://github.com/vinit2107/dbt-bigquery…
vinit2107 Jan 21, 2024
5d73ef2
Revert "adding changie files"
vinit2107 Jan 21, 2024
8251dcf
Merge pull request #2 from vinit2107/ADAP-1051
vinit2107 Jan 21, 2024
c76839a
Add test cases
vinit2107 Jan 21, 2024
7034b33
Merge pull request #3 from vinit2107/ADAP-1051
vinit2107 Jan 21, 2024
6539fc9
Merge branch 'main' into main
vinit2107 Jan 23, 2024
c472376
Merge branch 'main' into main
dbeatty10 Jan 24, 2024
2b7ba5e
Merge pull request #4 from vinit2107/main
vinit2107 Jan 27, 2024
d755045
Revert adapters.sql
vinit2107 Jan 27, 2024
763a91a
Update body for changie
vinit2107 Jan 27, 2024
563e2d6
Merge pull request #5 from vinit2107/ADAP-1051
vinit2107 Jan 27, 2024
dbd401c
Merge branch 'main' into main
vinit2107 Jan 27, 2024
5b1271a
Merge branch 'main' into main
dbeatty10 Feb 5, 2024
7752212
Merge branch 'main' into main
mikealfare Feb 8, 2024
6b1b270
Merge branch 'main' into main
vinit2107 Feb 9, 2024
55154c1
Merge branch 'main' into main
vinit2107 Feb 14, 2024
02b153b
Merge branch 'main' into main
vinit2107 Mar 4, 2024
b6a82e0
Merge branch 'main' into main
martynydbt Mar 15, 2024
32fd304
Merge branch 'main' into main
vinit2107 Apr 12, 2024
0980d27
Merge branch 'main' into main
vinit2107 Jun 28, 2024
341acf4
Merge branch 'main' into main
VersusFacit Jul 21, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .changes/unreleased/Fixes-20240120-180818.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
kind: Fixes
body: Temp relations not being dropped in BigQuery for incremental models + ability
to provide target dataset for temp relations
dbeatty10 marked this conversation as resolved.
Show resolved Hide resolved
time: 2024-01-20T18:08:18.817915-06:00
custom:
Author: vinit2107
Issue: "1036"
8 changes: 8 additions & 0 deletions dbt/include/bigquery/macros/adapters/adapters.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{% macro bigquery__make_temp_relation(base_relation, suffix='__dbt_tmp') %}
{% if var('tmp_relation_dataset', none) is none %}
{{ return(default__make_temp_relation(base_relation, suffix)) }}
{% else %}
{{ return(base_relation.incorporate(path={"schema": var('tmp_relation_dataset'),
"identifier": base_relation.identifier ~ suffix})) }}
{% endif %}
{% endmacro %}
dbeatty10 marked this conversation as resolved.
Show resolved Hide resolved
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
)
)