-
Notifications
You must be signed in to change notification settings - Fork 241
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 #574
Merged
Merged
Changes from all commits
Commits
Show all changes
43 commits
Select commit
Hold shift + click to select a range
186df86
Add support for constraints in Spark
b-per 9aeb7cb
Add tests for constraints
b-per 59ef1bd
Update requirements for CI to pass
b-per c22d0f4
Update dispatched macro with argument
b-per e3f77c1
Use spark decorator for tests
b-per e1a9a54
Update test to remove unsupported constraints
b-per bf314dd
Allow multiple queries to be sent
b-per bc47cb6
Merge branch 'main' of github.com:dbt-labs/dbt-spark into bper/add-su…
b-per 8d392c2
Revert change on splitting satements in `execute`
b-per e633f52
Add `call statement` for table with constraints
b-per 4c961da
Add checks when the split by `;` is empty
b-per 89a740d
Fix typo in JInja variable name
b-per 7b92d71
Rename `constraints` to `constraints_check`
b-per 7c141af
Merge branch 'main' of github.com:dbt-labs/dbt-spark into bper/add-su…
b-per 4df795d
Support constraints with `alter` statements
b-per d1d3940
Changie entry
b-per d88cee7
Fix missing `endif`
b-per f9bec28
Remove get_columns_spec_ddl as we use alter
b-per e99a27f
Remove unused dispatch macro
b-per 0595d22
Update dispatched macro
b-per eccdbf3
Update tests to work with `alter` approach
b-per f2ac49d
Make tests valid for databricks only for delta
b-per 586ad97
Try other way to call tests
b-per b9b5d34
Add schema info
b-per c1d3d2c
Remove wrong argument to test
b-per 2de8296
Merge branch 'main' into bper/add-support-for-constraints
sungchun12 794570f
Use new testing framework
b-per 1e48bfc
Merge branch 'bper/add-support-for-constraints' of github.com:dbt-lab…
b-per 8650a20
Add check on column names and order
b-per 83427e7
Check only when constraints enabled
b-per 42791be
Remove config nesting
b-per 3460091
constraint_check is not a list
b-per 262fe69
Fix CICD
b-per c9b20e4
Typo
b-per 32e8769
Only allow not null
b-per 5eea874
Update expected SQL to the Spark one
b-per e7d2280
Make file_format delta
b-per ceb5b02
Try this
jtcohen6 deb5677
Check for earlier part of error message
jtcohen6 269e15e
Check for any rather than all error messages
jtcohen6 f8fd7dd
Reset to dbt-core main
jtcohen6 1a291f0
Merge branch 'main' into bper/add-support-for-constraints
MichelleArk 9762b70
Merge branch 'main' into bper/add-support-for-constraints
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,8 @@ | ||
kind: Features | ||
body: 'Support for data types constraints in Spark following the dbt Core feature | ||
#6271' | ||
time: 2023-01-30T12:58:55.972992+01:00 | ||
custom: | ||
Author: b-per | ||
Issue: "558" | ||
PR: "574" |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import pytest | ||
from dbt.tests.util import relation_from_name | ||
from dbt.tests.adapter.constraints.test_constraints import ( | ||
BaseConstraintsColumnsEqual, | ||
BaseConstraintsRuntimeEnforcement | ||
) | ||
|
||
# constraints are enforced via 'alter' statements that run after table creation | ||
_expected_sql_spark = """ | ||
create or replace table {0} | ||
using delta | ||
as | ||
|
||
select | ||
1 as id, | ||
'blue' as color, | ||
cast('2019-01-01' as date) as date_day | ||
""" | ||
|
||
@pytest.mark.skip_profile('spark_session', 'apache_spark') | ||
class TestSparkConstraintsColumnsEqual(BaseConstraintsColumnsEqual): | ||
pass | ||
|
||
@pytest.mark.skip_profile('spark_session', 'apache_spark') | ||
class TestSparkConstraintsRuntimeEnforcement(BaseConstraintsRuntimeEnforcement): | ||
@pytest.fixture(scope="class") | ||
def project_config_update(self): | ||
return { | ||
"models": { | ||
"+file_format": "delta", | ||
} | ||
} | ||
|
||
@pytest.fixture(scope="class") | ||
def expected_sql(self, project): | ||
relation = relation_from_name(project.adapter, "my_model") | ||
return _expected_sql_spark.format(relation) | ||
|
||
# On Spark/Databricks, constraints are applied *after* the table is replaced. | ||
# We don't have any way to "rollback" the table to its previous happy state. | ||
# So the 'color' column will be updated to 'red', instead of 'blue'. | ||
@pytest.fixture(scope="class") | ||
def expected_color(self): | ||
return "red" | ||
|
||
@pytest.fixture(scope="class") | ||
def expected_error_messages(self): | ||
return [ | ||
"violate the new CHECK constraint", | ||
"DELTA_NEW_CHECK_CONSTRAINT_VIOLATION", | ||
"violate the new NOT NULL constraint", | ||
] | ||
|
||
def assert_expected_error_messages(self, error_message, expected_error_messages): | ||
# This needs to be ANY instead of ALL | ||
# The CHECK constraint is added before the NOT NULL constraint | ||
# and different connection types display/truncate the error message in different ways... | ||
assert any(msg in error_message for msg in expected_error_messages) |
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.
For later: Constraint hash is a sensible default, since we need a unique identifier. We may also want to let users define their own custom
name
for the constraint.