dbt-expectations for data quality - Time & Space Complexity
When using dbt-expectations to check data quality, it is important to understand how the time to run these checks changes as data grows.
We want to know how the cost of running these tests scales with the size of the data.
Analyze the time complexity of the following dbt-expectations test.
- name: test_not_null
config:
severity: error
test: not_null
column_name: user_id
model: users
This test checks that the column user_id in the users table has no missing values.
Look at what the test does repeatedly.
- Primary operation: Scanning each row in the
user_idcolumn to check for nulls. - How many times: Once for every row in the
userstable.
As the number of rows grows, the test must check more values.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 checks for null values |
| 100 | 100 checks for null values |
| 1000 | 1000 checks for null values |
Pattern observation: The number of checks grows directly with the number of rows.
Time Complexity: O(n)
This means the time to run the test grows in a straight line as the data size increases.
[X] Wrong: "The test runs instantly no matter how big the data is."
[OK] Correct: The test must look at every row to be sure there are no nulls, so it takes longer with more data.
Understanding how data quality checks scale helps you write efficient tests and explain their impact clearly in real projects.
"What if we added a test that checks uniqueness of a column instead of nulls? How would the time complexity change?"
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
