Skip to content

Commit d42639b

Browse files
DG47mday-io
andauthored
fix(audits): apply condition to the rows checked by z_score and valid_http_method (#6156)
Signed-off-by: Dhruv Gupta <todhruvg@gmail.com> Co-authored-by: Michael Day <mdaytn@gmail.com>
1 parent 966e34d commit d42639b

2 files changed

Lines changed: 89 additions & 2 deletions

File tree

‎sqlmesh/core/audit/builtin.py‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -318,10 +318,14 @@ def create_non_blocking_copy(audit: Audit) -> Audit:
318318
# valid_http_method(column=column_name)
319319
valid_http_method_audit = ModelAudit(
320320
name="valid_http_method",
321+
defaults={"condition": exp.true()},
321322
query="""
322323
SELECT *
323324
FROM @this_model
324-
WHERE NOT @column IN ('GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'HEAD', 'OPTIONS', 'TRACE', 'CONNECT')
325+
WHERE @AND(
326+
NOT @column IN ('GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'HEAD', 'OPTIONS', 'TRACE', 'CONNECT'),
327+
@condition,
328+
)
325329
""",
326330
)
327331

@@ -432,7 +436,7 @@ def create_non_blocking_copy(audit: Audit) -> Audit:
432436
@column,
433437
(@column - mean_@column) / NULLIF(stddev_@column, 0) AS z_score
434438
FROM @this_model, stats
435-
WHERE ABS((@column - mean_@column) / NULLIF(stddev_@column, 0)) > @threshold
439+
WHERE @AND(ABS((@column - mean_@column) / NULLIF(stddev_@column, 0)) > @threshold, @condition)
436440
""",
437441
)
438442

‎tests/core/test_audit.py‎

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -697,6 +697,89 @@ def test_pattern_audits(model: Model):
697697
)
698698

699699

700+
def test_z_score_audit(model: Model):
701+
rendered_query = model.render_audit_query(
702+
builtin.z_score_audit,
703+
column=exp.to_column("a"),
704+
threshold=exp.convert(3),
705+
)
706+
assert (
707+
rendered_query.sql()
708+
== """WITH "stats" AS (SELECT AVG("a") AS "mean_a", STDDEV("a") AS "stddev_a" FROM (SELECT * FROM "db"."test_model" AS "test_model" WHERE "ds" BETWEEN '1970-01-01' AND '1970-01-01') AS "_0" WHERE TRUE) SELECT "a" AS "a", ("a" - "mean_a") / NULLIF("stddev_a", 0) AS "z_score" FROM (SELECT * FROM "db"."test_model" AS "test_model" WHERE "ds" BETWEEN '1970-01-01' AND '1970-01-01') AS "_1", "stats" AS "stats" WHERE ABS(("a" - "mean_a") / NULLIF("stddev_a", 0)) > 3 AND TRUE"""
709+
)
710+
711+
# The condition must filter the rows being checked, not just the rows used to compute the stats
712+
rendered_query = model.render_audit_query(
713+
builtin.z_score_audit,
714+
column=exp.to_column("a"),
715+
threshold=exp.convert(3),
716+
condition=parse_one("b > 0"),
717+
)
718+
assert (
719+
rendered_query.sql()
720+
== """WITH "stats" AS (SELECT AVG("a") AS "mean_a", STDDEV("a") AS "stddev_a" FROM (SELECT * FROM "db"."test_model" AS "test_model" WHERE "ds" BETWEEN '1970-01-01' AND '1970-01-01') AS "_0" WHERE "b" > 0) SELECT "a" AS "a", ("a" - "mean_a") / NULLIF("stddev_a", 0) AS "z_score" FROM (SELECT * FROM "db"."test_model" AS "test_model" WHERE "ds" BETWEEN '1970-01-01' AND '1970-01-01') AS "_1", "stats" AS "stats" WHERE ABS(("a" - "mean_a") / NULLIF("stddev_a", 0)) > 3 AND "b" > 0"""
721+
)
722+
723+
724+
def test_valid_http_method_audit(model: Model):
725+
rendered_query = model.render_audit_query(
726+
builtin.valid_http_method_audit,
727+
column=exp.to_column("a"),
728+
)
729+
assert (
730+
rendered_query.sql()
731+
== """SELECT * FROM (SELECT * FROM "db"."test_model" AS "test_model" WHERE "ds" BETWEEN '1970-01-01' AND '1970-01-01') AS "_0" WHERE NOT "a" IN ('GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'HEAD', 'OPTIONS', 'TRACE', 'CONNECT') AND TRUE"""
732+
)
733+
734+
rendered_query = model.render_audit_query(
735+
builtin.valid_http_method_audit,
736+
column=exp.to_column("a"),
737+
condition=parse_one("b > 0"),
738+
)
739+
assert (
740+
rendered_query.sql()
741+
== """SELECT * FROM (SELECT * FROM "db"."test_model" AS "test_model" WHERE "ds" BETWEEN '1970-01-01' AND '1970-01-01') AS "_0" WHERE NOT "a" IN ('GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'HEAD', 'OPTIONS', 'TRACE', 'CONNECT') AND "b" > 0"""
742+
)
743+
744+
745+
def test_builtin_audits_condition_filters_audited_rows():
746+
import duckdb
747+
748+
model = create_sql_model(
749+
"db.test_model",
750+
parse_one("SELECT region, v, method FROM db.src"),
751+
kind=FullKind(),
752+
dialect="duckdb",
753+
)
754+
connection = duckdb.connect()
755+
connection.execute(
756+
"CREATE SCHEMA db; CREATE TABLE db.test_model AS "
757+
"SELECT * FROM (VALUES ('us', 10, 'GET'), ('us', 10, 'POST'), ('us', 11, 'PUT'), "
758+
"('eu', 1000, 'get'), ('eu', 1000, 'post')) AS t(region, v, method)"
759+
)
760+
condition = parse_one("region = 'us'", dialect="duckdb")
761+
762+
z_score_query = model.render_audit_query(
763+
builtin.z_score_audit,
764+
column=exp.to_column("v"),
765+
threshold=exp.convert(1),
766+
condition=condition,
767+
)
768+
# Only the rows matching the condition are compared against the stats computed for them
769+
assert connection.execute(z_score_query.sql("duckdb")).fetchall() == [
770+
(11, pytest.approx(1.1547005))
771+
]
772+
773+
valid_http_method_query = model.render_audit_query(
774+
builtin.valid_http_method_audit,
775+
column=exp.to_column("method"),
776+
condition=condition,
777+
)
778+
assert connection.execute(valid_http_method_query.sql("duckdb")).fetchall() == []
779+
780+
connection.close()
781+
782+
700783
def test_standalone_audit(model: Model, assert_exp_eq):
701784
audit = StandaloneAudit(
702785
name="test_audit", query=parse_one(f"SELECT * FROM {model.name} WHERE col IS NULL")

0 commit comments

Comments
 (0)