Skip to content

fix(audits): make not_null_proportion threshold an inclusive minimum - #6251

Open
DG47 wants to merge 2 commits into
SQLMesh:mainfrom
DG47:fix/not-null-proportion-threshold
Open

DG47 wants to merge 2 commits into
SQLMesh:mainfrom
DG47:fix/not-null-proportion-threshold

Conversation

@DG47

@DG47 DG47 commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

Description

Fixes #6250

The built-in not_null_proportion audit had two problems:

  1. The comparison was strict. The query failed when count(column) <= count(*) * threshold. So a column whose non-NULL proportion equalled the threshold was reported as failing, and threshold := 1 could never pass, even for a column without a single NULL.
  2. The docs described the threshold backwards. docs/concepts/audits.md says the audit "ensures that the specified column's proportion of NULL values is no greater than a threshold", and that threshold := 0.8 allows up to 80% NULLs. The implementation has always treated threshold as the minimum proportion of non-NULL values, so the documented example checks the opposite of what it says (a column that is 50% NULL fails it).

Changes:

  • sqlmesh/core/audit/builtin.py: compare with < instead of <=, so threshold is the inclusive minimum proportion of non-NULL values.
  • docs/concepts/audits.md: describe threshold as the minimum proportion of non-NULL values and reword the zip example to match ("at least 80% not NULL, i.e. no more than 20% NULL").

I chose to fix the docs rather than flip the implementation. The audit's name and the usage comment in builtin.py (threshold=0.9) both point to the non-NULL reading, and flipping the direction would quietly change what every existing not_null_proportion audit checks.

A side effect of using <: an empty table, or a condition that matches no rows, now passes (0 < 0 is false). That matches not_null and the other per-row audits, which also pass on an empty table. Previously it failed for any threshold, including 0.

Test Plan

  • Added test_not_null_proportion_audit in tests/core/test_audit.py, which runs the rendered audit against DuckDB for six cases: no NULLs with threshold 1, one NULL with threshold 1, exactly at the threshold, below the threshold, an all-NULL column with threshold 0, and an empty table.
    • On main: 4 failed, 2 passed (the no-NULLs-at-threshold-1, exactly-at-threshold, threshold-0 and empty-table cases fail).
    • With this change: 6 passed.
  • pytest tests/core/test_audit.py tests/core/test_model.py tests/core/test_context.py -n 4: 519 passed.
  • pre-commit run --files on the changed files: ruff, ruff-format, mypy all pass.

Checklist

  • I have run make style and fixed any issues
  • I have added tests for my changes (if applicable)
  • All existing tests pass (make fast-test)
  • My commits are signed off (git commit -s) per the DCO

Summary by CodeRabbit

  • Bug Fixes
    • Corrected the not_null_proportion audit so a result passes when the proportion of non-NULL values exactly meets the configured threshold.
  • Documentation
    • Clarified that the threshold is the minimum proportion of non-NULL values. For example, a threshold of 0.8 allows up to 20% NULL values.
  • Tests
    • Added coverage for threshold boundaries, empty tables, and tables containing only NULL values.

DG47 added 2 commits November 21, 2026 07:14
The not_null_proportion audit failed whenever the share of non-NULL
values was less than or equal to the threshold, so a column that met
the threshold exactly was reported as failing and `threshold := 1`
could never pass, even for a column without a single NULL. Compare
with `<` so the threshold is the minimum accepted proportion of
non-NULL values.

The docs described the threshold the other way around, as the maximum
proportion of NULL values, so the documented example asserted the
opposite of what the audit checks. Describe the threshold as the
minimum proportion of non-NULL values, matching the implementation.

Signed-off-by: Dhruv Gupta <todhruvg@gmail.com>
@coderabbitai

coderabbitai Bot commented Nov 24, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

The not_null_proportion audit now passes when the non-NULL count equals the threshold-derived count. The documentation describes the threshold as a minimum non-NULL proportion, and parameterized DuckDB tests cover threshold boundaries and empty or all-NULL tables.

Changes: Audit threshold behavior

Layer / File(s) Summary
Threshold semantics and validation
docs/concepts/audits.md, sqlmesh/core/audit/builtin.py, tests/core/test_audit.py
The documentation defines threshold as the minimum non-NULL proportion. The audit fails only when the non-NULL count is below the threshold-derived count. Parameterized tests cover thresholds 1, 0.8, and 0, plus empty and all-NULL tables.

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix · Severity of issue fixed: Medium

Possibly related issues

Suggested reviewers: mday-io

Merge Risk: Minimal · up to 15566

The audit now treats the threshold as an inclusive minimum proportion of non-NULL values, matching the updated documentation. The change is small, tested, and low risk to merge.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title names the main change: the not_null_proportion threshold becomes an inclusive minimum. It matches the builtin.py comparison change (<= to <) and the docs fix in the PR. The title uses the co…
Description check Passed The description follows the template. It has a Description section that explains the two problems (strict comparison and backwards docs) and lists the file changes. It has a Test Plan section with the…
Linked Issues check Passed Issue #6250 requires that threshold be the minimum proportion of non-NULL values, with an exact match passing. It also requires the docs and the audit to agree. At the reviewed head, sqlmesh/core/audi…
Out of Scope Changes check Passed The whole-PR changes touch three files: builtin.py (one comparison operator in not_null_proportion_audit), docs/concepts/audits.md (threshold description and zip example wording), and tests/core/test_…
Full details: Title check

Explanation

The title names the main change: the not_null_proportion threshold becomes an inclusive minimum. It matches the builtin.py comparison change (<= to <) and the docs fix in the PR. The title uses the conventional fix(audits) prefix and is specific, so it reads clearly in history.

Full details: Description check

Explanation

The description follows the template. It has a Description section that explains the two problems (strict comparison and backwards docs) and lists the file changes. It has a Test Plan section with the six DuckDB cases, the main-branch and PR results, and the pytest and pre-commit runs. The checklist is present, with the make fast-test item left unchecked and explained as not completed. The required sections are all filled in with specific information.

Full details: Linked Issues check

Explanation

Issue #6250 requires that threshold be the minimum proportion of non-NULL values, with an exact match passing. It also requires the docs and the audit to agree. At the reviewed head, sqlmesh/core/audit/builtin.py uses WHERE s.cnt_not_null &lt; s.cnt_tot * @threshold. The strict &lt; makes an exact match pass, so threshold := 1 passes when no NULLs exist. The PR also changes docs/concepts/audits.md to describe threshold as the minimum non-NULL proportion, and the zip example now reads as at least 80% non-NULL. The PR adds test_not_null_proportion_audit, which covers the no-NULL threshold 1 case, the exact-threshold case, and the below-threshold case on DuckDB. The issue's requirements are coding requirements, and the code change and test address them. The checklist item for make fast-test is not a requirement of this issue. The empty-table pass is a side effect of the same comparison. The PR description documents it, and it does not conflict with the issue.

Full details: Out of Scope Changes check

Explanation

The whole-PR changes touch three files: builtin.py (one comparison operator in not_null_proportion_audit), docs/concepts/audits.md (threshold description and zip example wording), and tests/core/test_audit.py (a new parameterized test for not_null_proportion_audit). Each change relates directly to issue #6250. No unrelated audits or code paths were changed in the reviewed summary. The empty-table and no-matching-rows pass behavior follows directly from the strict comparison and is disclosed in the PR description.

Finishing Touches

Generate unit tests (beta)

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 58e99585-e4cd-4ff5-9fbc-c4664d284404
Commits

Reviewing files that changed from the base of the PR and between ba3953b and 15566cc.

Files selected for processing: 3

  • docs/concepts/audits.md
  • sqlmesh/core/audit/builtin.py
  • tests/core/test_audit.py

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 7 remain after this review.

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.

not_null_proportion audit: threshold is described backwards in the docs, and threshold := 1 can never pass

1 participant