Skip to content

Commit cfde8eb

Browse files
authored
fix(audits): make not_null_proportion threshold inclusive and correct its docs (#6251)
Signed-off-by: Dhruv Gupta <todhruvg@gmail.com>
1 parent 8b7e765 commit cfde8eb

3 files changed

Lines changed: 41 additions & 3 deletions

File tree

‎docs/concepts/audits.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -303,9 +303,9 @@ MODEL (
303303
```
304304

305305
#### not_null_proportion, not_null_proportion_non_blocking
306-
Ensures that the specified column's proportion of `NULL` values is no greater than a threshold.
306+
Ensures that the proportion of non-`NULL` values in the specified column is at least `threshold`, a number between 0 and 1.
307307

308-
This example asserts that the `zip` column has no more than 80% `NULL` values:
308+
This example asserts that at least 80% of the `zip` column's values are not `NULL` (that is, no more than 20% are `NULL`):
309309

310310
```sql linenums="1"
311311
MODEL (

‎sqlmesh/core/audit/builtin.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ def create_non_blocking_copy(audit: Audit) -> Audit:
191191
FROM @this_model
192192
WHERE @condition
193193
) AS s
194-
WHERE s.cnt_not_null <= s.cnt_tot * @threshold
194+
WHERE s.cnt_not_null < s.cnt_tot * @threshold
195195
""",
196196
)
197197

‎tests/core/test_audit.py‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1064,6 +1064,44 @@ def test_not_constant_audit(model: Model):
10641064
)
10651065

10661066

1067+
@pytest.mark.parametrize(
1068+
"non_null_rows, null_rows, threshold, passes",
1069+
[
1070+
(10, 0, 1, True),
1071+
(9, 1, 1, False),
1072+
(8, 2, 0.8, True),
1073+
(7, 3, 0.8, False),
1074+
(0, 10, 0, True),
1075+
(0, 0, 0.8, True),
1076+
],
1077+
)
1078+
def test_not_null_proportion_audit(
1079+
non_null_rows: int, null_rows: int, threshold: float, passes: bool
1080+
):
1081+
import duckdb
1082+
1083+
model = create_sql_model(
1084+
"db.test_model", parse_one("SELECT x FROM db.src"), kind=FullKind(), dialect="duckdb"
1085+
)
1086+
rendered_query = model.render_audit_query(
1087+
builtin.not_null_proportion_audit,
1088+
column=exp.column("x"),
1089+
threshold=exp.convert(threshold),
1090+
)
1091+
1092+
connection = duckdb.connect()
1093+
connection.execute("CREATE SCHEMA db; CREATE TABLE db.test_model (x INT)")
1094+
if non_null_rows or null_rows:
1095+
connection.execute(
1096+
"INSERT INTO db.test_model "
1097+
f"SELECT CASE WHEN i < {non_null_rows} THEN i END FROM range({non_null_rows + null_rows}) AS t(i)"
1098+
)
1099+
1100+
# threshold is the minimum proportion of non-NULL values; the audit returns a row when it fails
1101+
assert (connection.execute(rendered_query.sql("duckdb")).fetchall() == []) == passes
1102+
connection.close()
1103+
1104+
10671105
def test_valid_uuid_audit(model: Model):
10681106
import duckdb
10691107

0 commit comments

Comments
 (0)