-
Notifications
You must be signed in to change notification settings - Fork 976
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
Updates to Model Contracts (v1.5 betas) #3100
Changes from 3 commits
a1855e5
066b96b
4b91304
52f65b4
1c2446a
b5c1a0e
1887599
79ad9d7
9047028
0b47c02
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,7 @@ description: New features and changes in dbt Core v1.5 | |
--- | ||
|
||
:::info | ||
v1.5 is currently available as a **beta prerelease.** Availability in dbt Cloud coming soon! | ||
v1.5 is currently available as a **beta prerelease** | ||
::: | ||
|
||
### Resources | ||
|
@@ -16,11 +16,11 @@ v1.5 is currently available as a **beta prerelease.** Availability in dbt Cloud | |
|
||
:::info | ||
|
||
Planned release date: April 26, 2023 | ||
Planned release date: April 27, 2023 | ||
|
||
::: | ||
|
||
dbt Core v1.5 is a feature release with two significant additions planned: | ||
dbt Core v1.5 is a feature release, with two significant additions planned: | ||
1. Models as APIs — the first phase of [multi-project deployments](https://github.com/dbt-labs/dbt-core/discussions/6725) | ||
2. An initial Python API for dbt-core supporting programmatic invocations at parity with the CLI. | ||
|
||
|
@@ -40,11 +40,14 @@ Changes planned for v1.5: | |
|
||
### For consumers of dbt artifacts (metadata) | ||
|
||
The manifest schema version will be updated to `v9`. Specific changes to be noted here. | ||
The manifest schema version will be updated to `v9`. Specific changes: | ||
- Addition of `groups` as a top-level key | ||
- Addition of `access` as a top-level node config for models | ||
- Addition of `group` and `contract` as node configs | ||
|
||
### For maintainers of adapter plugins | ||
|
||
Coming soon: GH discussion detailing interface changes and offering a forum for Q&A | ||
For more detailed information and to ask any questions, please visit [dbt-core/discussions/6624](https://github.com/dbt-labs/dbt-core/discussions/6624). | ||
|
||
## New and changed documentation | ||
|
||
|
@@ -53,9 +56,9 @@ More to come! | |
::: | ||
|
||
### Publishing models as APIs | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Some of the nomenclature here is liable to change, as we go through formal internal naming process. No updates just yet / for this PR; I expect to be ready with those updates before the RC (April 13). |
||
- [Model contracts](model-contracts) ([#2839](https://github.com/dbt-labs/docs.getdbt.com/issues/2839)) | ||
- [Model access](model-access) ([#2840](https://github.com/dbt-labs/docs.getdbt.com/issues/2840)) | ||
- [Model versions](model-versions) ([#2841](https://github.com/dbt-labs/docs.getdbt.com/issues/2841)) | ||
- [Model contracts](model-contracts) | ||
- [Model access](model-access) | ||
- [Model versions](model-versions) | ||
|
||
### dbt-core Python API | ||
- Auto-generated documentation ([#2674](https://github.com/dbt-labs/docs.getdbt.com/issues/2674)) for dbt-core CLI & Python API for programmatic invocations |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,78 +11,90 @@ id: "contract" | |
- [Defining `columns`](resource-properties/columns) | ||
- [Defining `constraints`](resource-properties/constraints) | ||
|
||
<!-- TODO: move some of this content elsewhere, and update to reflect new proposed syntax --> | ||
|
||
:::info Beta functionality | ||
This functionality is new in v1.5. These docs exist to provide a high-level overview of what's to come. The specific syntax is liable to change. | ||
|
||
In particular: | ||
- The current name of the `contract` config is `constraints_enabled`. | ||
- The "preflight" check only includes column `name` and is order-sensitive. The goal is to add `data_type` and make it insensitive to column order. | ||
This functionality is new in v1.5! The syntax is mostly locked, but some small details are still liable to change. | ||
::: | ||
|
||
# Definition | ||
|
||
When the `contract` configuration is enabled, dbt will ensure that your model's returned dataset exactly matches the attributes you have defined in yaml: | ||
When the `contract` configuration is enforced, dbt will ensure that your model's returned dataset exactly matches the attributes you have defined in yaml: | ||
- `name` and `data_type` for every column | ||
- additional [`constraints`](resource-properties/constraints), as supported for this materialization + data platform | ||
|
||
:::caution Under construction 🚧 | ||
More to come! | ||
::: | ||
|
||
You can manage data type constraints on your models using the `constraints_enabled` configuration. This configuration is available on all models and is disabled by default. When enabled, dbt will automatically add constraints to your models based on the data types of the columns in your model's schema. This is a great way to ensure your data is always in the correct format. For example, if you have a column in your model defined as a `date` data type, dbt will automatically add a data type constraint to that column to ensure the data in that column is always a valid date. If you want to add a `not null` condition to a column in a preventative manner rather than as a test, you can add the `not null` value to the column definition in your model's schema: `constraints: ['not null']`. | ||
The `data_type` defined in your yaml file should match a data type recognized by your data platform. dbt does not do any type aliasing itself; if your data platform recognizes both `int` and `integer` as corresponding to the same type, then they will return a match. | ||
matthewshaver marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
## When to use constraints vs. tests | ||
## Example | ||
|
||
Constraints serve as a **preventative** measure against bad data quality **before** the dbt model is (re)built. It is only limited by the respective database's functionality and the supported data types. Examples of constraints: `not null`, `unique`, `primary key`, `foreign key`, `check` | ||
<File name='models/dim_customers.yml'> | ||
|
||
Tests serve as a **detective** measure against bad data quality _after_ the dbt model is (re)built. | ||
```yml | ||
models: | ||
- name: dim_customers | ||
config: | ||
contract: | ||
enforced: true | ||
columns: | ||
- name: customer_id | ||
data_type: int | ||
constraints: | ||
- type: not_null | ||
- name: customer_name | ||
data_type: string | ||
``` | ||
|
||
Constraints are great when you define `constraints: ['not null']` for a column in your model's schema because it'll prevent `null` values from being inserted into that column at dbt model creation time and prevent other unintended values from being inserted into that column without dbt's intervention as it relies on the database to enforce the constraint. This can **replace** the `not_null` test. However, performance issues may arise depending on your database. | ||
</File> | ||
|
||
Tests should be used in addition to and instead of constraints when you want to test things like `accepted_values` and `relationships`. These are usually not enforced with built-in database functionality and are not possible with constraints. Also, custom tests will allow more flexibility and address nuanced data quality issues that may not be possible with constraints. | ||
<File name='models/dim_customers.yml'> | ||
|
||
## Current Limitations | ||
Let's say your model is defined as: | ||
```sql | ||
select | ||
'abc123' as customer_id, | ||
'My Best Customer' as customer_name | ||
``` | ||
|
||
- `contract` (a.k.a. `constraints_enabled`) must be configured in the yaml [`config`] property _only_. Setting this configuration via in-file config or in `dbt_project.yml` is not supported. | ||
- `contract` (a.k.a. `constraints_enabled`) is supported only for a SQL model materialized as `table`. | ||
- Prerequisite checks include the column `name,` but not yet their `data_type`. We intend to support `data_type` verification in an upcoming beta prerelease. | ||
- The order of columns in your `yml` file must match the order of columns returned by your model's SQL query. | ||
- While most data platforms support `not_null` checks, support for [additional `constraints`](resource-properties/constraints) varies by data platform. | ||
</File> | ||
|
||
When you `dbt run` your model, _before_ dbt has materialized it as a table in the database, you will see this error: | ||
```txt | ||
# example error message | ||
Compilation Error in model constraints_example (models/constraints_examples/constraints_example.sql) | ||
Please ensure the name, order, and number of columns in your `yml` file match the columns in your SQL file. | ||
Schema File Columns: ['id', 'date_day', 'color'] | ||
SQL File Columns: ['id', 'color', 'date_day'] | ||
Compilation Error in model dim_customers (models/dim_customers.sql) | ||
Contracts are enabled for this model. Please ensure the name, data_type, and number of columns in your `yml` file match the columns in your SQL file. | ||
Schema File Columns: customer_id INT, customer_name TEXT | ||
SQL File Columns: customer_id TEXT, customer_name TEXT | ||
``` | ||
|
||
## Example | ||
## Support | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a more-positive spin on "Limitations" :) but if you think that's a clearer way of expressing it, we can switch out one for the other (or for something else) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It looks good. Always more pleasing to the reader to put a positive spin on anything. "Don't apologize for keeping them waiting; thank them for their patience" type of experience. |
||
|
||
<File name='models/schema.yml'> | ||
At present, model contracts are supported for: | ||
- SQL models (not yet Python) | ||
- Models materialized as `table`, `view`, and `incremental` (with `on_schema_change: append_new_columns`) | ||
- On the most popular data platforms — but which [`constraints`](resource-properties/constraints) are supported/enforced varies by platform | ||
|
||
```yml | ||
models: | ||
- name: constraints_example | ||
config: | ||
constraints_enabled: true | ||
columns: | ||
- name: id | ||
data_type: integer | ||
description: hello | ||
constraints: ['not null', 'primary key'] | ||
constraints_check: (id > 0) | ||
tests: | ||
- unique | ||
- name: color | ||
constraints: | ||
- not null | ||
- primary key | ||
data_type: string | ||
- name: date_day | ||
data_type: date | ||
### Incremental models and `on_schema_change` | ||
|
||
Why require that incremental models also set [`on_schema_change`](incremental-models#what-if-the-columns-of-my-incremental-model-change), and why to `append_new_columns`? | ||
|
||
Imagine: | ||
- You add a new column to both the SQL and the yaml spec | ||
- You don't set `on_schema_change`, or you set `on_schema_change: 'ignore'` | ||
- dbt doesn't actually add that new column to the existing table — and the upsert/merge still succeeds, because it does that upsert/merge on the basis of the already-existing "destination" columns only (this is long-established behavior) | ||
- The result is a delta between the yaml-defined contract, and the actual table in the database - which means the contract is now incorrect! | ||
|
||
Why `append_new_columns`, rather than `sync_all_columns`? Because removing existing columns is a breaking change for contracted models! | ||
|
||
### Catching breaking changes | ||
matthewshaver marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
When you use the `state:modified` selection method in Slim CI, dbt will detect changes to model contracts, and raise an error if any of those changes could be breaking for downstream consumers. | ||
|
||
Breaking changes include: | ||
- Removing an existing column | ||
- Changing the `data_type` of an existing column | ||
- (Future) Removing or modifying one of the `constraints` on an existing column | ||
|
||
``` | ||
dbt.exceptions.ModelContractError: Contract Error in model dim_customers (models/dim_customers.sql) | ||
There is a breaking change in the model contract because column definitions have changed; you may need to create a new version. See: https://docs.getdbt.com/docs/collaborate/publish/model-versions | ||
``` | ||
|
||
</File> | ||
Adding new columns, or adding new constraints to existing columns, is not considered a breaking change. | ||
matthewshaver marked this conversation as resolved.
Show resolved
Hide resolved
|
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.
Stealing from #3052 - no reason to delay getting this info live