-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2709 from kconvey/kconvey-copy-job
Add a BigQuery adapter macro to enable usage of CopyJobs
- Loading branch information
Showing
12 changed files
with
233 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
plugins/bigquery/dbt/include/bigquery/macros/materializations/copy.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
{% materialization copy, adapter='bigquery' -%} | ||
|
||
{# Setup #} | ||
{{ run_hooks(pre_hooks) }} | ||
|
||
{# there should be exactly one ref or exactly one source #} | ||
{% set destination = this.incorporate(type='table') %} | ||
|
||
{% set dependency_type = none %} | ||
{% if (model.refs | length) == 1 and (model.sources | length) == 0 %} | ||
{% set dependency_type = 'ref' %} | ||
{% elif (model.refs | length) == 0 and (model.sources | length) == 1 %} | ||
{% set dependency_type = 'source' %} | ||
{% else %} | ||
{% set msg %} | ||
Expected exactly one ref or exactly one source, instead got {{ model.refs | length }} models and {{ model.sources | length }} sources. | ||
{% endset %} | ||
{% do exceptions.raise_compiler_error(msg) %} | ||
{% endif %} | ||
|
||
{% if dependency_type == 'ref' %} | ||
{% set src = ref(*model.refs[0]) %} | ||
{% else %} | ||
{% set src = source(*model.sources[0]) %} | ||
{% endif %} | ||
|
||
{%- set result_str = adapter.copy_table( | ||
src, | ||
destination, | ||
config.get('copy_materialization', 'table')) -%} | ||
|
||
{{ store_result('main', status=result_str) }} | ||
|
||
{# Clean up #} | ||
{{ run_hooks(post_hooks) }} | ||
{{ adapter.commit() }} | ||
|
||
{{ return({'relations': [destination]}) }} | ||
{%- endmaterialization %} |
2 changes: 2 additions & 0 deletions
2
test/integration/022_bigquery_test/copy-failing-models/copy_bad_materialization.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
{{ config(copy_materialization='view') }} | ||
{{ ref('original') }} |
1 change: 1 addition & 0 deletions
1
test/integration/022_bigquery_test/copy-failing-models/original.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
select 1 as id |
2 changes: 2 additions & 0 deletions
2
test/integration/022_bigquery_test/copy-models/copy_as_incremental.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
{{ config(copy_materialization='incremental') }} | ||
{{ ref('original') }} |
2 changes: 2 additions & 0 deletions
2
test/integration/022_bigquery_test/copy-models/copy_as_table.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
{{ config(copy_materialization='table') }} | ||
{{ ref('original') }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
select 1 as id |
36 changes: 36 additions & 0 deletions
36
test/integration/022_bigquery_test/test_bigquery_copy_failing_models.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
from test.integration.base import DBTIntegrationTest, use_profile | ||
import textwrap | ||
import yaml | ||
|
||
|
||
class TestBigqueryCopyTableFails(DBTIntegrationTest): | ||
|
||
@property | ||
def schema(self): | ||
return "bigquery_test_022" | ||
|
||
@property | ||
def models(self): | ||
return "copy-failing-models" | ||
|
||
@property | ||
def profile_config(self): | ||
return self.bigquery_profile() | ||
|
||
@property | ||
def project_config(self): | ||
return yaml.safe_load(textwrap.dedent('''\ | ||
config-version: 2 | ||
models: | ||
test: | ||
original: | ||
materialized: table | ||
copy_bad_materialization: | ||
materialized: copy | ||
''')) | ||
|
||
@use_profile('bigquery') | ||
def test__bigquery_copy_table_fails(self): | ||
results = self.run_dbt(expect_pass=False) | ||
self.assertEqual(len(results), 2) | ||
self.assertTrue(results[1].error) |
37 changes: 37 additions & 0 deletions
37
test/integration/022_bigquery_test/test_bigquery_copy_models.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
from test.integration.base import DBTIntegrationTest, use_profile | ||
import textwrap | ||
import yaml | ||
|
||
|
||
class TestBigqueryCopyTable(DBTIntegrationTest): | ||
|
||
@property | ||
def schema(self): | ||
return "bigquery_test_022" | ||
|
||
@property | ||
def models(self): | ||
return "copy-models" | ||
|
||
@property | ||
def profile_config(self): | ||
return self.bigquery_profile() | ||
|
||
@property | ||
def project_config(self): | ||
return yaml.safe_load(textwrap.dedent('''\ | ||
config-version: 2 | ||
models: | ||
test: | ||
original: | ||
materialized: table | ||
copy_as_table: | ||
materialized: copy | ||
copy_as_incremental: | ||
materialized: copy | ||
''')) | ||
|
||
@use_profile('bigquery') | ||
def test__bigquery_copy_table(self): | ||
results = self.run_dbt() | ||
self.assertEqual(len(results), 3) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters