Fix: prevent division-by-zero in not_null_proportion and align empty-table behavior#1083
Open
shivasingh4945-tech wants to merge 5 commits into
Open
Fix: prevent division-by-zero in not_null_proportion and align empty-table behavior#1083shivasingh4945-tech wants to merge 5 commits into
shivasingh4945-tech wants to merge 5 commits into
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
This PR fixes a division-by-zero issue in the
not_null_proportiongeneric test.Problem
The current implementation computes:
This can cause division-by-zero errors when the denominator evaluates to 0. This situation can occur in real-world scenarios such as:
Depending on query shape, this can either return NULL or raise a runtime error, leading to inconsistent and non-portable behavior across warehouses.
Solution
Replaced the aggregation logic with:
This ensures:
NULLIFNULL, which aligns with dbt test semanticsThis approach is widely used in SQL to safely handle division in aggregate queries.
Why no COALESCE?
Empty datasets now return NULL. In dbt, tests fail only when rows are returned. Since comparisons with NULL do not evaluate to TRUE, empty datasets do not produce failing rows and therefore pass, consistent with existing dbt-utils conventions.
Reproducible Example
-- ❌ Current behavior (can error depending on query shape)
SELECT
SUM(CASE WHEN value IS NULL THEN 0 ELSE 1 END)
/ COUNT(*) FILTER (WHERE false) AS not_null_proportion
FROM (VALUES (1), (2)) t(value);
-- Result:
-- ERROR: division by zero
-- ✅ Fixed behavior (safe)
SELECT
(COUNT(value) * 1.0)
/ NULLIF(COUNT(*) FILTER (WHERE false), 0) AS not_null_proportion
FROM (VALUES (1), (2)) t(value);
-- Result:
-- NULL (no error)
Additional Improvements
SUM(CASE...)withCOUNT(column_name)for clarity and performancecolumn_nameat_least <= at_mostBehavior
Testing
Validated with:
Notes
This change focuses on correctness and stability without altering expected behavior for valid datasets. No breaking changes are expected for valid inputs.