Skip to content

Commit 735b26f

Browse files
committed
fix(audits): fail at_least_one when no row has a non-NULL value
The at_least_one audit grouped by the constant 1, so an empty table, or a condition that filters out every row, produced no groups at all and the audit passed even though no row has a non-NULL value in the column. Aggregate over the whole table instead and keep the HAVING check, which returns a single row whenever the non-NULL count is zero. Signed-off-by: Dhruv Gupta <todhruvg@gmail.com>
1 parent f0ed98e commit 735b26f

2 files changed

Lines changed: 37 additions & 3 deletions

File tree

‎sqlmesh/core/audit/builtin.py‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,14 +137,16 @@ def create_non_blocking_copy(audit: Audit) -> Audit:
137137
)
138138

139139
# at_least_one(column=column_name)
140+
# The aggregate is computed over the whole table rather than per group: grouping by the constant
141+
# produces no groups at all when the table is empty or @condition filters out every row, which
142+
# would make the audit pass even though no row has a non-NULL value.
140143
at_least_one_audit = ModelAudit(
141144
name="at_least_one",
142145
defaults={"condition": exp.true()},
143146
query="""
144-
SELECT 1
147+
SELECT COUNT(@column) AS cnt_not_null
145148
FROM @this_model
146149
WHERE @condition
147-
GROUP BY 1
148150
HAVING COUNT(@column) = 0
149151
""",
150152
)

‎tests/core/test_audit.py‎

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import json
2+
import typing as t
3+
24
import pytest
35
from sqlglot import exp, parse_one
46

@@ -712,8 +714,38 @@ def test_at_least_one_audit(model: Model):
712714
)
713715
assert (
714716
rendered_query.sql()
715-
== 'SELECT 1 AS "1" FROM (SELECT * FROM "db"."test_model" AS "test_model" WHERE "ds" BETWEEN \'1970-01-01\' AND \'1970-01-01\') AS "_0" WHERE TRUE GROUP BY 1 HAVING COUNT("a") = 0'
717+
== 'SELECT COUNT("a") AS "cnt_not_null" FROM (SELECT * FROM "db"."test_model" AS "test_model" WHERE "ds" BETWEEN \'1970-01-01\' AND \'1970-01-01\') AS "_0" WHERE TRUE HAVING COUNT("a") = 0'
718+
)
719+
720+
721+
def test_at_least_one_audit_fails_when_no_row_has_a_value():
722+
import duckdb
723+
724+
model = load_sql_based_model(
725+
parse("MODEL (name db.t, kind FULL); SELECT 1 AS a, TRUE AS flag", default_dialect="duckdb")
716726
)
727+
conn = duckdb.connect()
728+
conn.execute("CREATE SCHEMA db")
729+
conn.execute("CREATE TABLE db.t (a INT, flag BOOLEAN)")
730+
731+
def audit_rows(**kwargs: exp.Expr) -> t.List[t.Tuple[int]]:
732+
query = model.render_audit_query(
733+
builtin.at_least_one_audit, column=exp.to_column("a"), **kwargs
734+
)
735+
return conn.execute(query.sql("duckdb")).fetchall()
736+
737+
# An empty table has no row with a non-NULL value, so the audit must fail
738+
assert audit_rows() == [(0,)]
739+
740+
conn.execute("INSERT INTO db.t VALUES (NULL, TRUE), (NULL, FALSE)")
741+
assert audit_rows() == [(0,)]
742+
743+
conn.execute("INSERT INTO db.t VALUES (1, FALSE)")
744+
assert audit_rows() == []
745+
746+
# The only non-NULL value is excluded by the condition, so the audit must fail
747+
assert audit_rows(condition=exp.column("flag")) == [(0,)]
748+
assert audit_rows(condition=exp.not_(exp.column("flag"))) == []
717749

718750

719751
def test_mutually_exclusive_ranges_audit(model: Model):

0 commit comments

Comments
 (0)