diff --git a/.changes/unreleased/Fixes-20230906-141819.yaml b/.changes/unreleased/Fixes-20230906-141819.yaml new file mode 100644 index 000000000..f40944670 --- /dev/null +++ b/.changes/unreleased/Fixes-20230906-141819.yaml @@ -0,0 +1,6 @@ +kind: Fixes +body: Fix bigquery copy materialization +time: 2023-09-06T14:18:19.445262+02:00 +custom: + Author: m-sche + Issue: "820" diff --git a/dbt/include/bigquery/macros/materializations/copy.sql b/dbt/include/bigquery/macros/materializations/copy.sql index 8285dc845..3ce2de184 100644 --- a/dbt/include/bigquery/macros/materializations/copy.sql +++ b/dbt/include/bigquery/macros/materializations/copy.sql @@ -9,7 +9,7 @@ {# cycle over ref() and source() to create source tables array #} {% set source_array = [] %} {% for ref_table in model.refs %} - {{ source_array.append(ref(*ref_table)) }} + {{ source_array.append(ref(ref_table.get('package'), ref_table.name, version=ref_table.get('version'))) }} {% endfor %} {% for src_table in model.sources %} diff --git a/tests/functional/adapter/test_copy_materialization.py b/tests/functional/adapter/test_copy_materialization.py new file mode 100644 index 000000000..77d5f4af6 --- /dev/null +++ b/tests/functional/adapter/test_copy_materialization.py @@ -0,0 +1,62 @@ +import pytest +from pathlib import Path +from dbt.tests.util import run_dbt, write_file, check_relations_equal + +_SEED_A = """ +load_date,id,first_name,last_name,email,gender,ip_address +2021-03-05,1,Jack,Hunter,jhunter0@pbs.org,Male,59.80.20.168 +2021-03-05,2,Kathryn,Walker,kwalker1@ezinearticles.com,Female,194.121.179.35 +2021-03-05,3,Gerald,Ryan,gryan2@com.com,Male,11.3.212.243 +""".lstrip() + +_SEED_B = """ +load_date,id,first_name,last_name,email,gender,ip_address +2021-03-05,4,Bonnie,Spencer,bspencer3@ameblo.jp,Female,216.32.196.175 +2021-03-05,5,Harold,Taylor,htaylor4@people.com.cn,Male,253.10.246.136 +""".lstrip() + +_EXPECTED_RESULT = """ +load_date,id,first_name,last_name,email,gender,ip_address +2021-03-05,1,Jack,Hunter,jhunter0@pbs.org,Male,59.80.20.168 +2021-03-05,2,Kathryn,Walker,kwalker1@ezinearticles.com,Female,194.121.179.35 +2021-03-05,3,Gerald,Ryan,gryan2@com.com,Male,11.3.212.243 +2021-03-05,4,Bonnie,Spencer,bspencer3@ameblo.jp,Female,216.32.196.175 +2021-03-05,5,Harold,Taylor,htaylor4@people.com.cn,Male,253.10.246.136 +""".lstrip() + +_COPY_MODEL = """ +{{ config( + materialized="copy", + copy_materialization="incremental", +) }} + +SELECT * FROM {{ ref("seed") }} +""" + + +class BaseCopyModelConfig: + @pytest.fixture(scope="class") + def models(self): + return {"copy_model.sql": _COPY_MODEL} + + @pytest.fixture(scope="class") + def seeds(self): + return { + "seed.csv": _SEED_A, + "expected_result.csv": _EXPECTED_RESULT, + } + + +class TestCopyMaterialization(BaseCopyModelConfig): + def test_incremental_copy(self, project): + run_dbt(["seed"]) + run_dbt(["run"]) + + # Replace original seed _SEED_A with _SEED_B + seed_file = project.project_root / Path("seeds") / Path("seed.csv") + write_file(_SEED_B, seed_file) + + run_dbt(["seed"]) + run_dbt(["run"]) + + check_relations_equal(project.adapter, ["copy_model", "expected_result"])