Model contracts and access controls in dbt - Time & Space Complexity
When using model contracts and access controls in dbt, it is important to understand how the checks and permissions affect the time it takes to run your models.
We want to know how adding contracts and controls changes the work dbt does as data grows.
Analyze the time complexity of the following dbt model with a contract and access control.
-- model.sql
{{ config(
contracts=["not_null", "unique"],
access_control={"role": "data_analyst"}
) }}
select * from source_table
This model selects all data from a source table, applies contracts to check data quality, and restricts access by role.
Look at what repeats when dbt runs this model.
- Primary operation: Scanning all rows in
source_tableto apply contracts. - How many times: Once per model run, but the scan covers every row in the table.
The time to check contracts grows as the number of rows in source_table grows.
| Input Size (n rows) | Approx. Operations |
|---|---|
| 10 | 20 checks |
| 100 | 200 checks |
| 1000 | 2000 checks |
Pattern observation: The work increases directly with the number of rows and the number of contracts, so doubling rows doubles the checks.
Time Complexity: O(n)
This means the time to run the model and check contracts grows linearly with the number of rows in the source table.
[X] Wrong: "Adding access controls will make the model run slower by a lot."
[OK] Correct: Access controls usually only check permissions once per run and do not scan data rows, so they add very little time compared to contracts.
Understanding how data checks and permissions affect run time helps you design efficient data models and explain your choices clearly in real projects.
"What if the model had multiple contracts checking different columns? How would the time complexity change?"
Practice
model contract in dbt?Solution
Step 1: Understand what a model contract does
A model contract sets rules that data must follow to be considered valid in dbt models.Step 2: Compare options to the definition
Only To define rules that ensure data quality in models describes defining rules for data quality, which matches the purpose of model contracts.Final Answer:
To define rules that ensure data quality in models -> Option BQuick Check:
Model contract = data quality rules [OK]
- Confusing access control with model contracts
- Thinking contracts schedule runs
- Mixing contracts with visualization tools
schema.yml file?Solution
Step 1: Identify the syntax for model contracts in schema.yml
Model contracts are defined under thecontractkey with column tests insideschema.yml.Step 2: Match options to this syntax
models: - name: my_model contract: columns: - name: id tests: - not_null correctly showscontractwith columns and tests. Others show unrelated keys likeaccess_control,schedule, ordashboard.Final Answer:
models: - name: my_model contract: columns: - name: id tests: - not_null -> Option CQuick Check:
Contract syntax = models: - name: my_model contract: columns: - name: id tests: - not_null [OK]
- Confusing access control syntax with contract syntax
- Using schedule or dashboard keys incorrectly
- Missing indentation in YAML
schema.yml:
models:
- name: sales
contract:
columns:
- name: order_id
tests:
- not_null
- unique
- name: amount
tests:
- not_null
What happens if the order_id column contains duplicate values when the model runs?Solution
Step 1: Understand contract tests behavior
Contract tests likeuniqueenforce data rules strictly. If violated, the model run fails.Step 2: Apply to duplicate
Sinceorder_idvaluesorder_idmust be unique, duplicates cause the test to fail, stopping the model run.Final Answer:
The model run fails because the contract test for uniqueness fails -> Option DQuick Check:
Contract test fail = model run fails [OK]
- Thinking model logs warning but continues
- Assuming duplicates are ignored
- Believing model run is skipped automatically
dbt_project.yml:
access_control:
- role: analyst
models:
- sales
- customers
- role: viewer
models:
- sales
But analysts report they cannot see the customers model. What is the likely error?Solution
Step 1: Check where access controls are defined in dbt
dbt does not support anaccess_controlblock indbt_project.yml. Access controls are managed outside or via external tools.Step 2: Analyze other options
Role spelling or model existence might cause issues, but the main problem is unsupported syntax in the config file.Final Answer:
Theaccess_controlblock is not supported indbt_project.yml-> Option AQuick Check:
Access control block unsupported = Theaccess_controlblock is not supported indbt_project.yml[OK]
- Trying to set access control inside dbt_project.yml
- Assuming role names cause access issues
- Not verifying model existence
finance model, and that the model must always have a non-null transaction_id. How can you combine model contracts and access controls to achieve this?Solution
Step 1: Use model contracts for data quality
Define a contract test inschema.ymlto ensuretransaction_idis not null, enforcing data quality.Step 2: Manage access control outside dbt
dbt does not handle user permissions directly; access control is managed by your data platform or warehouse permissions for the 'data_engineer' role.Final Answer:
Define a model contract inschema.ymlfortransaction_idnot null, and manage access control via your data platform's permissions for 'data_engineer' role -> Option AQuick Check:
Contracts for data rules + external access control = Define a model contract inschema.ymlfortransaction_idnot null, and manage access control via your data platform's permissions for 'data_engineer' role [OK]
- Trying to set access control inside dbt schema.yml
- Using macros to check user roles inside SQL
- Assuming dbt manages user permissions directly
Ready to prove it?
Pass the test and this pattern moves unknown
