-
Notifications
You must be signed in to change notification settings - Fork 114
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
### Description This PR adds support for [Delta Constraints](https://docs.databricks.com/delta/delta-constraints.html) with dbt table models and incremental models. It supports two types of constraints: model level [Check constraint](https://docs.databricks.com/delta/delta-constraints.html#check-constraint) and column level [not_null](https://docs.databricks.com/delta/delta-constraints.html#not-null-constraint) constraint. Users can specify model-level and column-level constraints under the `meta` field of the model config. For example: ```yaml # schema.yml models: - name: my_model meta: constraints: - name: id_greater_than_zero condition: id > 0 columns: - name: id - name: name meta: constraint: not_null ``` Constraints specified will be created if the `persist_constraints` config is enabled (default: false): ```sql -- my_model.sql {{ config( materailized='table', persist_constraints=True ) }} ... ``` Note, Delta constraints are only available in Databricks Runtime 7.4 and above. So DBR 7.3 LTS won't support this feature.
- Loading branch information
1 parent
2ed551d
commit 8db21bc
Showing
22 changed files
with
449 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
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
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 @@ | ||
insert into {schema}.seed values (0, 'Cathy', '2022-03-01'); |
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 @@ | ||
insert into {schema}.seed values (3, null, '2022-03-01'); |
7 changes: 7 additions & 0 deletions
7
tests/integration/persist_constraints/models/expected/expected_incremental_model.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,7 @@ | ||
{{ config(materialized='table') }} | ||
|
||
select * from values | ||
(1, 'Alice', '2022-01-01'), | ||
(2, 'Bob', '2022-02-01'), | ||
(3, 'Cathy', '2022-03-01') | ||
t(id, name, date); |
6 changes: 6 additions & 0 deletions
6
tests/integration/persist_constraints/models/expected/expected_model.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,6 @@ | ||
{{ config(materialized='table') }} | ||
|
||
select * from values | ||
(1, 'Alice', '2022-01-01'), | ||
(2, 'Bob', '2022-02-01') | ||
t(id, name, date); |
7 changes: 7 additions & 0 deletions
7
tests/integration/persist_constraints/models/expected/expected_model_with_invalid_name.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,7 @@ | ||
{{ config(materialized='table') }} | ||
|
||
select * from values | ||
(1, 'Alice', '2022-01-01'), | ||
(2, 'Bob', '2022-02-01'), | ||
(3, null, '2022-03-01') | ||
t(id, name, date); |
7 changes: 7 additions & 0 deletions
7
tests/integration/persist_constraints/models/incremental_model.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,7 @@ | ||
{{config(materialized='incremental')}} | ||
|
||
select * from {{ ref('seed') }} | ||
|
||
{% if is_incremental() %} | ||
where date > (select max(date) from {{ this }}) | ||
{% endif %} |
3 changes: 3 additions & 0 deletions
3
tests/integration/persist_constraints/models/invalid_check_constraint.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,3 @@ | ||
{{config(materialized='table')}} | ||
|
||
select * from {{ ref('seed') }} |
4 changes: 4 additions & 0 deletions
4
tests/integration/persist_constraints/models/invalid_column_constraint.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,4 @@ | ||
{{config(materialized='table')}} | ||
|
||
select * from {{ ref('seed') }} | ||
|
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,50 @@ | ||
version: 2 | ||
|
||
models: | ||
- name: table_model | ||
meta: | ||
constraints: | ||
- name: id_greater_than_zero | ||
condition: id > 0 | ||
columns: | ||
- name: id | ||
- name: name | ||
meta: | ||
constraint: not_null | ||
- name: date | ||
|
||
- name: incremental_model | ||
meta: | ||
constraints: | ||
- name: id_greater_than_zero | ||
condition: id > 0 | ||
columns: | ||
- name: id | ||
- name: name | ||
meta: | ||
constraint: not_null | ||
- name: date | ||
|
||
- name: invalid_check_constraint | ||
meta: | ||
constraints: | ||
- name: invalid_constraint | ||
condition: | ||
|
||
- name: invalid_column_constraint | ||
columns: | ||
- name: id | ||
meta: | ||
constraint: invalid | ||
|
||
- name: table_model_disable_constraints | ||
meta: | ||
constraints: | ||
- name: id_greater_than_zero | ||
condition: id > 0 | ||
columns: | ||
- name: id | ||
- name: name | ||
meta: | ||
constraint: not_null | ||
- name: date |
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,3 @@ | ||
{{config(materialized='table')}} | ||
|
||
select * from {{ ref('seed') }} |
3 changes: 3 additions & 0 deletions
3
tests/integration/persist_constraints/models/table_model_disable_constraints.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,3 @@ | ||
{{config(materialized='table', persist_constraints=False)}} | ||
|
||
select * from {{ ref('seed') }} |
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,3 @@ | ||
id,name,date | ||
1,Alice,2022-01-01 | ||
2,Bob,2022-02-01 |
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,9 @@ | ||
version: 2 | ||
|
||
seeds: | ||
- name: seed | ||
config: | ||
column_types: | ||
id: int | ||
name: string | ||
date: string |
15 changes: 15 additions & 0 deletions
15
tests/integration/persist_constraints/snapshots/snapshot.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,15 @@ | ||
{% snapshot my_snapshot %} | ||
|
||
{{ | ||
config( | ||
check_cols=["name", "date"], | ||
unique_key="id", | ||
strategy="check", | ||
target_schema=schema | ||
) | ||
|
||
}} | ||
|
||
select * from {{ ref('seed') }} | ||
|
||
{% endsnapshot %} |
12 changes: 12 additions & 0 deletions
12
tests/integration/persist_constraints/snapshots/snapshot.yml
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,12 @@ | ||
version: 2 | ||
|
||
snapshots: | ||
- name: my_snapshot | ||
meta: | ||
constraints: | ||
- name: id_greater_than_zero | ||
condition: id > 0 | ||
columns: | ||
- name: name | ||
meta: | ||
constraint: not_null |
Oops, something went wrong.