Skip to content

Commit 2ab17f8

Browse files
authored
fix: forall with only one criteria (#701)
* fix: forall with only one criteria Apply the `NOT` predicate before `REDUCE` to handle cases where only one criteria is specified. * tidy: don't add extra parens
1 parent 5ad301f commit 2ab17f8

3 files changed

Lines changed: 16 additions & 3 deletions

File tree

examples/sushi/models/waiter_as_customer_by_day.sql

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ MODEL (
77
owner jen,
88
cron '@daily',
99
audits (
10-
not_null(columns=[waiter_id])
10+
not_null(columns = [waiter_id]),
11+
forall(criteria = [LENGTH(waiter_name) > 0])
1112
)
1213
);
1314

sqlmesh/core/audit/builtin.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,11 @@
6868
SELECT *
6969
FROM @this_model
7070
WHERE @REDUCE(
71-
@criteria,
72-
(l, r) -> NOT (l) OR NOT (r)
71+
@EACH(
72+
@criteria,
73+
c -> NOT (c)
74+
),
75+
(l, r) -> l OR r
7376
)
7477
""",
7578
)

tests/core/test_audit.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,15 @@ def test_number_of_rows_audit(model: Model):
220220

221221

222222
def test_forall_audit(model: Model):
223+
rendered_query_a = builtin.forall_audit.render_query(
224+
model,
225+
criteria=[parse_one("a >= b")],
226+
)
227+
assert (
228+
rendered_query_a.sql()
229+
== "SELECT * FROM (SELECT * FROM db.test_model AS test_model WHERE test_model.ds <= '1970-01-01' AND test_model.ds >= '1970-01-01') AS _q_0 WHERE NOT (_q_0.a >= _q_0.b)"
230+
)
231+
223232
rendered_query_a = builtin.forall_audit.render_query(
224233
model,
225234
criteria=[parse_one("a >= b"), parse_one("c + d - e < 1.0")],

0 commit comments

Comments
 (0)