-
Notifications
You must be signed in to change notification settings - Fork 154
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
dbt Constraints / model contracts #426
Merged
Merged
Changes from 24 commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
cbc9672
Create bigquery adapter macro for constraints
Victoriapm ae99388
fix order
Victoriapm 5eb87b6
update checks validation and constraints list
Victoriapm bc10cd2
add test
Victoriapm c570acd
add changie
Victoriapm b731d42
update issue number
Victoriapm b548eab
fix trailing space
Victoriapm 4ff89d1
fix test name
Victoriapm aaea6a8
update BQ data types
Victoriapm a074d7d
Update test_bigquery_constraints.py
Victoriapm 60dda35
Update test_bigquery_constraints.py
Victoriapm 99abbbd
fix quotes
Victoriapm 96939bf
Update test_bigquery_constraints.py
Victoriapm e12a4c0
fix constratints config
Victoriapm 374f135
fix config
Victoriapm e727cc8
Update test_bigquery_constraints.py
Victoriapm 1ee39de
update constraints config
Victoriapm 6c46db4
Switch to new functional tests
jtcohen6 16c00ff
Fix failing tests
jtcohen6 7ad6851
Small cleanup
jtcohen6 4f419c5
Reset to dbt-core main
jtcohen6 c7ebc1e
Merge branch 'main' into dbt-constraints-bigquery
MichelleArk f5ae9c9
Merge branch 'main' into dbt-constraints-bigquery
jtcohen6 484038c
Resolve PR comment
jtcohen6 2ac91c9
PR feedback
jtcohen6 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
18 changes: 18 additions & 0 deletions
18
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,18 @@ | ||
{% macro bigquery__get_columns_spec_ddl() %} | ||
{# loop through user_provided_columns to create DDL with data types and constraints #} | ||
{% if config.get('constraints_enabled', False) %} | ||
{%- 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 %} | ||
{% 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"] |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Move this if statement to
adapters.sql
similar to this: https://github.com/dbt-labs/dbt-core/pull/6271/files#diff-7246f19f4d12042d172ac61b3d0c4564ea27dd8825870b35425ede1ea41625dbR28There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like the if statement been added to adapters.sql, and can now be deleted from this macro.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch!