Skip to content

fix(recency): handle empty tables returning NULL#1065

Merged
b-per merged 1 commit intodbt-labs:mainfrom
dinaatmiro:fix/recency-test-empty-tables
Jan 9, 2026
Merged

fix(recency): handle empty tables returning NULL#1065
b-per merged 1 commit intodbt-labs:mainfrom
dinaatmiro:fix/recency-test-empty-tables

Conversation

@dinaatmiro
Copy link
Contributor

Problem

When a table is completely empty, the recency test incorrectly passes instead of failing.

Root Cause

When a table has no data, MAX(field) returns NULL. The current WHERE clause:

where most_recent < {{ threshold }}

With most_recent = NULL, the comparison NULL < threshold evaluates to NULL (not TRUE), so the WHERE clause filters out the row and returns zero rows — causing the test to pass.

Reproduction

  1. Create an empty table
  2. Add a recency test to it
  3. Run dbt test — the test passes even though there's no recent data

Solution

Add OR most_recent IS NULL to the WHERE clause:

where most_recent < {{ threshold }}
   or most_recent is null

This ensures the test fails when:

  • The table has data older than the threshold (most_recent < threshold)
  • The table is completely empty (most_recent IS NULL)

Testing

Verified locally with an empty Snowflake table that was previously passing the recency test incorrectly.

When a table is empty, MAX(field) returns NULL. The comparison
NULL < threshold evaluates to NULL (not TRUE), causing the WHERE
clause to filter out the row and incorrectly pass the test.

This fix adds 'OR most_recent IS NULL' to properly fail the test
when a table has no data within the recency window, including when
the table is completely empty.
@dinaatmiro dinaatmiro requested a review from a team as a code owner January 8, 2026 17:09
@b-per b-per added this pull request to the merge queue Jan 9, 2026
Merged via the queue into dbt-labs:main with commit e2add69 Jan 9, 2026
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants

Comments