diff --git a/integration_tests/models/generic_tests/recency_with_where_filter.sql b/integration_tests/models/generic_tests/recency_with_where_filter.sql new file mode 100644 index 00000000..0dcc2ff3 --- /dev/null +++ b/integration_tests/models/generic_tests/recency_with_where_filter.sql @@ -0,0 +1,12 @@ +-- Test model with data that's intentionally old +-- This simulates a scenario where we have data but it's all outside the recency window +-- this should fail with a where clause but currently it would pass +select + 1 as id, + {{ dbt.dateadd('day', -10, dbt.current_timestamp()) }} as created_at + +union all + +select + 2 as id, + {{ dbt.dateadd('day', -15, dbt.current_timestamp()) }} as created_at diff --git a/integration_tests/models/generic_tests/schema.yml b/integration_tests/models/generic_tests/schema.yml index b12e3c7f..db5449dc 100644 --- a/integration_tests/models/generic_tests/schema.yml +++ b/integration_tests/models/generic_tests/schema.yml @@ -261,3 +261,19 @@ models: compare_model: ref('data_test_equality_a') exclude_columns: - col_c + + - name: recency_with_where_filter + data_tests: + # This test should FAIL and return 0 rows (test failure) because when we filter to id = 3, + # no records match the where condition, so most_recent will be null. + # Before the fix: test would incorrectly PASS (no rows returned) + # After the fix: test should correctly FAIL (returns the null row) + - dbt_utils.recency: + datepart: day + field: created_at + ignore_time_component: true + interval: 5 + config: + where: "id = 3" # This filters to no records since only id 1,2 exist + error_if: "<1" + warn_if: "<0" diff --git a/macros/generic_tests/recency.sql b/macros/generic_tests/recency.sql index 7fe2cafd..8ffd3c91 100644 --- a/macros/generic_tests/recency.sql +++ b/macros/generic_tests/recency.sql @@ -37,5 +37,6 @@ select from recency where most_recent < {{ threshold }} + or most_recent is null {% endmacro %}