dbt-expectations helps you check if your data is correct and clean. It makes sure your data follows rules you set.
dbt-expectations for data quality
tests:
- dbt_expectations.expect_column_values_to_not_be_null:
column: your_column_name
- dbt_expectations.expect_column_values_to_be_between:
column: your_column_name
min_value: 0
max_value: 100
- dbt_expectations.expect_column_values_to_be_unique:
column: your_column_nameEach test is written under the tests section in your dbt model or schema file.
You specify the test name and the column it applies to, plus any extra parameters like min or max values.
user_id column has no missing values.tests:
- dbt_expectations.expect_column_values_to_not_be_null:
column: user_idage column values are between 0 and 120.tests:
- dbt_expectations.expect_column_values_to_be_between:
column: age
min_value: 0
max_value: 120email column are unique.tests:
- dbt_expectations.expect_column_values_to_be_unique:
column: emailThis example shows a dbt schema file defining tests for the customers model. It checks that customer_id is never null and unique, and age is between 0 and 120.
version: 2 models: - name: customers columns: - name: customer_id tests: - dbt_expectations.expect_column_values_to_not_be_null - dbt_expectations.expect_column_values_to_be_unique - name: age tests: - dbt_expectations.expect_column_values_to_be_between: min_value: 0 max_value: 120
dbt-expectations is built on top of Great Expectations, so it uses similar test ideas but fits into dbt workflows.
You can add many types of tests like checking patterns, value sets, or statistical properties.
Running these tests regularly helps catch data problems early.
dbt-expectations lets you write simple tests to check your data quality.
You add tests in your dbt schema files under the tests section.
Common tests check for nulls, uniqueness, value ranges, and patterns.
Practice
dbt-expectations in data projects?Solution
Step 1: Understand dbt-expectations role
dbt-expectations is a package that helps add tests to check data quality.Step 2: Identify main function
Its main function is to write simple tests like checking for nulls or uniqueness in data models.Final Answer:
To write tests that check data quality in your models -> Option CQuick Check:
dbt-expectations = data quality tests [OK]
- Confusing dbt-expectations with visualization tools
- Thinking it loads or transforms data
- Assuming it speeds up SQL writing
Solution
Step 1: Recall dbt-expectations test syntax
In schema.yml, tests are listed by their function name without the package prefix.Step 2: Identify correct test name
The correct test for checking no nulls isexpect_column_values_to_not_be_null.Final Answer:
tests: - expect_column_values_to_not_be_null -> Option DQuick Check:
Test names exclude package prefix [OK]
- Including package name in test list
- Using wrong test name like 'expect_column_values_to_be_null'
- Using hyphens instead of underscores
tests:
- expect_column_values_to_be_between:
min_value: 10
max_value: 100
What does this test check for the column?
Solution
Step 1: Understand the test name
The testexpect_column_values_to_be_betweenchecks if values fall within a range.Step 2: Interpret parameters
Parametersmin_value: 10andmax_value: 100mean values must be between 10 and 100, including these limits.Final Answer:
Values are between 10 and 100 inclusive -> Option AQuick Check:
Between test checks range limits [OK]
- Thinking values must be exactly min or max
- Confusing with null or uniqueness tests
- Assuming exclusive range instead of inclusive
tests:
- expect_column_values_to_be_unique_or_nulls
But dbt throws an error when running tests. What is the likely cause?
Solution
Step 1: Check test name spelling
The correct test name isexpect_column_values_to_be_unique_or_nullwithout the trailing 's'.Step 2: Understand test usage
This test does not require parameters and is valid in schema.yml files.Final Answer:
The test name is incorrect; it should be expect_column_values_to_be_unique_or_null -> Option BQuick Check:
Exact test names matter for dbt-expectations [OK]
- Adding extra characters to test names
- Assuming parameters are needed for all tests
- Thinking some tests can't be in schema.yml
email in your users model has no nulls and all values match a simple email pattern using dbt-expectations. Which test setup in schema.yml is correct?Solution
Step 1: Identify null check test
Useexpect_column_values_to_not_be_nullto ensure no null emails.Step 2: Identify regex pattern test
Useexpect_column_values_to_match_regexwith a regex matching email format.Step 3: Evaluate options
tests: - expect_column_values_to_not_be_null - expect_column_values_to_match_regex: regex: '^[\w.-]+@[\w.-]+\.\w+$' correctly combines both tests with a valid regex pattern.Final Answer:
tests: - expect_column_values_to_not_be_null - expect_column_values_to_match_regex: regex: '^[\w.-]+@[\w.-]+\.\w+$' -> Option AQuick Check:
Null check + regex pattern = correct test setup [OK]
- Using expect_column_values_to_be_null instead of not null
- Using a fixed string instead of regex pattern
- Omitting the regex test for pattern check
Ready to prove it?
Pass the test and this pattern moves unknown
