-
Notifications
You must be signed in to change notification settings - Fork 154
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
dbt Constraints / model contracts (#426)
* Create bigquery adapter macro for constraints * fix order * update checks validation and constraints list * add test * add changie * update issue number * fix trailing space * fix test name * update BQ data types * Update test_bigquery_constraints.py * Update test_bigquery_constraints.py * fix quotes Co-authored-by: Sung Won Chung <[email protected]> * Update test_bigquery_constraints.py * fix constratints config * fix config * Update test_bigquery_constraints.py * update constraints config * Switch to new functional tests * Fix failing tests * Small cleanup * Reset to dbt-core main * Resolve PR comment * PR feedback --------- Co-authored-by: Sung Won Chung <[email protected]> Co-authored-by: Jeremy Cohen <[email protected]> Co-authored-by: Michelle Ark <[email protected]>
- Loading branch information
1 parent
3817c07
commit f26fadd
Showing
5 changed files
with
87 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
kind: Features | ||
body: 'dbt-constraints support for BigQuery as per dbt-core issue #1358' | ||
time: 2022-12-20T19:37:31.982821+01:00 | ||
custom: | ||
Author: victoriapm | ||
Issue: "444" | ||
PR: "426" |
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
16 changes: 16 additions & 0 deletions
16
dbt/include/bigquery/macros/utils/get_columns_spec_ddl.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,16 @@ | ||
{% macro bigquery__get_columns_spec_ddl() %} | ||
{# loop through user_provided_columns to create DDL with data types and constraints #} | ||
{%- set ns = namespace(at_least_one_check=False) -%} | ||
{%- set user_provided_columns = model['columns'] -%} | ||
( | ||
{% for i in user_provided_columns -%} | ||
{%- set col = user_provided_columns[i] -%} | ||
{% set constraints = col['constraints'] -%} | ||
{%- set ns.at_least_one_check = ns.at_least_one_check or col['constraints_check'] %} | ||
{{ col['name'] }} {{ col['data_type'] }} {% for x in constraints %} {{ x or "" }} {% endfor %} {{ "," if not loop.last }} | ||
{%- endfor %} | ||
) | ||
{%- if ns.at_least_one_check -%} | ||
{{exceptions.warn("We noticed you have `constraints_check` configs, these are NOT compatible with BigQuery and will be ignored")}} | ||
{%- endif %} | ||
{% endmacro %} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import pytest | ||
from dbt.tests.util import relation_from_name | ||
from dbt.tests.adapter.constraints.test_constraints import ( | ||
BaseConstraintsColumnsEqual, | ||
BaseConstraintsRuntimeEnforcement | ||
) | ||
from dbt.tests.adapter.constraints.fixtures import ( | ||
my_model_sql, | ||
my_model_wrong_order_sql, | ||
my_model_wrong_name_sql, | ||
model_schema_yml, | ||
) | ||
|
||
_expected_sql_bigquery = """ | ||
create or replace table {0} ( | ||
id integer not null , | ||
color string , | ||
date_day date | ||
) | ||
OPTIONS() | ||
as ( | ||
select | ||
1 as id, | ||
'blue' as color, | ||
cast('2019-01-01' as date) as date_day | ||
); | ||
""" | ||
|
||
# Different on BigQuery: | ||
# - does not support a data type named 'text' (TODO handle this via type translation/aliasing!) | ||
# - raises an explicit error, if you try to set a primary key constraint, because it's not enforced | ||
constraints_yml = model_schema_yml.replace("text", "string").replace("primary key", "") | ||
|
||
class TestBigQueryConstraintsColumnsEqual(BaseConstraintsColumnsEqual): | ||
@pytest.fixture(scope="class") | ||
def models(self): | ||
return { | ||
"my_model_wrong_order.sql": my_model_wrong_order_sql, | ||
"my_model_wrong_name.sql": my_model_wrong_name_sql, | ||
"constraints_schema.yml": constraints_yml, | ||
} | ||
|
||
|
||
class TestBigQueryConstraintsRuntimeEnforcement(BaseConstraintsRuntimeEnforcement): | ||
@pytest.fixture(scope="class") | ||
def models(self): | ||
return { | ||
"my_model.sql": my_model_sql, | ||
"constraints_schema.yml": constraints_yml, | ||
} | ||
|
||
@pytest.fixture(scope="class") | ||
def expected_sql(self, project): | ||
relation = relation_from_name(project.adapter, "my_model") | ||
return _expected_sql_bigquery.format(relation) | ||
|
||
@pytest.fixture(scope="class") | ||
def expected_error_messages(self): | ||
return ["Required field id cannot be null"] |