Skip to content

Commit

Permalink
Fix: Builtin pattern matching audits operators (TobikoData#3011)
Browse files Browse the repository at this point in the history
  • Loading branch information
themisvaltinos authored Aug 15, 2024
1 parent 371ccda commit 508b8bc
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 4 deletions.
9 changes: 5 additions & 4 deletions sqlmesh/core/audit/builtin.py
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@
@patterns,
c -> NOT REGEXP_LIKE(@column, c)
),
(l, r) -> l OR r
(l, r) -> l AND r
),
@condition,
)
Expand Down Expand Up @@ -375,14 +375,15 @@
query="""
SELECT *
FROM @this_model
WHERE @condition AND (
WHERE @AND(
@REDUCE(
@EACH(
@patterns,
c -> NOT @column LIKE c
),
(l, r) -> l OR r
)
(l, r) -> l AND r
),
@condition,
)
""",
)
Expand Down
42 changes: 42 additions & 0 deletions tests/core/test_audit.py
Original file line number Diff line number Diff line change
Expand Up @@ -624,6 +624,48 @@ def test_chi_square_audit(model: Model):
)


def test_pattern_audits(model: Model):
rendered_query = builtin.match_regex_pattern_list_audit.render_query(
model,
column=exp.to_column("a"),
patterns=["^\d.*", ".*!$"],
)
assert (
rendered_query.sql()
== """SELECT * FROM (SELECT * FROM "db"."test_model" AS "test_model" WHERE "ds" BETWEEN \'1970-01-01\' AND \'1970-01-01\') AS "_q_0" WHERE (NOT REGEXP_LIKE("a", \'^\\d.*\') AND NOT REGEXP_LIKE("a", \'.*!$\')) AND TRUE"""
)

rendered_query = builtin.not_match_regex_pattern_list_audit.render_query(
model,
column=exp.to_column("a"),
patterns=["^\d.*", ".*!$"],
)
assert (
rendered_query.sql()
== """SELECT * FROM (SELECT * FROM "db"."test_model" AS "test_model" WHERE "ds" BETWEEN \'1970-01-01\' AND \'1970-01-01\') AS "_q_0" WHERE (REGEXP_LIKE("a", \'^\\d.*\') OR REGEXP_LIKE("a", \'.*!$\')) AND TRUE"""
)

rendered_query = builtin.match_like_pattern_list.render_query(
model,
column=exp.to_column("a"),
patterns=["jim%", "pam%"],
)
assert (
rendered_query.sql()
== """SELECT * FROM (SELECT * FROM "db"."test_model" AS "test_model" WHERE "ds" BETWEEN \'1970-01-01\' AND \'1970-01-01\') AS "_q_0" WHERE (NOT "a" LIKE \'jim%\' AND NOT "a" LIKE \'pam%\') AND TRUE"""
)

rendered_query = builtin.not_match_like_pattern_list_audit.render_query(
model,
column=exp.to_column("a"),
patterns=["jim%", "pam%"],
)
assert (
rendered_query.sql()
== """SELECT * FROM (SELECT * FROM "db"."test_model" AS "test_model" WHERE "ds" BETWEEN \'1970-01-01\' AND \'1970-01-01\') AS "_q_0" WHERE ("a" LIKE \'jim%\' OR "a" LIKE \'pam%\') AND TRUE"""
)


def test_standalone_audit(model: Model, assert_exp_eq):
audit = StandaloneAudit(
name="test_audit", query=parse_one(f"SELECT * FROM {model.name} WHERE col IS NULL")
Expand Down

0 comments on commit 508b8bc

Please sign in to comment.