Repository: TobikoData/sqlmesh
Pull request #2713 (2024-05-31 21:30:52)

Fix: render audit condition when used in a @WHERE(@condition) clause
The `@WHERE(@condition)` clauses weren't being rendered when eg. `condition` was `item_id IS NULL`, because the macro evaluator evaluated the condition as `exp.to_column('item_id') is None` which is false.


Patch:
diff --git a/sqlmesh/core/audit/builtin.py b/sqlmesh/core/audit/builtin.py
index 8a8a9012..dbaf59f1 100644
--- a/sqlmesh/core/audit/builtin.py
+++ b/sqlmesh/core/audit/builtin.py
@@ -7,7 +7,7 @@ from sqlmesh.core.audit.definition import ModelAudit
 # not_null(columns=(column_1, column_2))
 not_null_audit = ModelAudit(
     name="not_null",
-    defaults={"condition": exp.null()},
+    defaults={"condition": exp.true()},
     query="""
 SELECT *
 FROM @this_model
@@ -27,7 +27,7 @@ WHERE @AND(
 # unique_values(columns=(column_1, column_2))
 unique_values_audit = ModelAudit(
     name="unique_values",
-    defaults={"condition": exp.null()},
+    defaults={"condition": exp.true()},
     query="""
 SELECT *
 FROM (
@@ -37,7 +37,7 @@ FROM (
       c -> row_number() OVER (PARTITION BY c ORDER BY c) AS rank_@c
     )
   FROM @this_model
-  @WHERE(@condition) @condition
+  WHERE @condition
 )
 WHERE @REDUCE(
   @EACH(
@@ -52,7 +52,7 @@ WHERE @REDUCE(
 # accepted_values(column=column_name, is_in=(1, 2, 3))
 accepted_values_audit = ModelAudit(
     name="accepted_values",
-    defaults={"condition": exp.null()},
+    defaults={"condition": exp.true()},
     query="""
 SELECT *
 FROM @this_model
@@ -63,13 +63,13 @@ WHERE @AND(@column NOT IN @is_in, @condition)
 # number_of_rows(threshold=100)
 number_of_rows_audit = ModelAudit(
     name="number_of_rows",
-    defaults={"condition": exp.null()},
+    defaults={"condition": exp.true()},
     query="""
 SELECT COUNT(*)
 FROM (
   SELECT 1
   FROM @this_model
-  @WHERE(@condition) @condition
+  WHERE @condition
   LIMIT @threshold + 1
 )
 HAVING COUNT(*) <= @threshold
@@ -83,7 +83,7 @@ HAVING COUNT(*) <= @threshold
 # ))
 forall_audit = ModelAudit(
     name="forall",
-    defaults={"condition": exp.null()},
+    defaults={"condition": exp.true()},
     query="""
 SELECT *
 FROM @this_model
@@ -110,7 +110,7 @@ accepted_range_audit = ModelAudit(
         "min_v": exp.null(),
         "max_v": exp.null(),
         "inclusive": exp.true(),
-        "condition": exp.null(),
+        "condition": exp.true(),
     },
     query="""
 SELECT *
@@ -131,11 +131,11 @@ WHERE
 # at_least_one(column=column_name)
 at_least_one_audit = ModelAudit(
     name="at_least_one",
-    defaults={"condition": exp.null()},
+    defaults={"condition": exp.true()},
     query="""
 SELECT 1
 FROM @this_model
-@WHERE(@condition) @condition
+WHERE @condition
 GROUP BY 1
 HAVING COUNT(@column) = 0
     """,
@@ -144,13 +144,13 @@ HAVING COUNT(@column) = 0
 # not_constant(column=column_name)
 not_constant_audit = ModelAudit(
     name="not_constant",
-    defaults={"condition": exp.null()},
+    defaults={"condition": exp.true()},
     query="""
 SELECT 1
 FROM (
   SELECT COUNT(DISTINCT @column) AS t_cardinality
   FROM @this_model
-  @WHERE(@condition) @condition
+  WHERE @condition
 ) AS r
 WHERE r.t_cardinality <= 1
     """,
@@ -159,7 +159,7 @@ WHERE r.t_cardinality <= 1
 # not_empty_string(column=column_name)
 not_empty_string_audit = ModelAudit(
     name="not_empty_string",
-    defaults={"condition": exp.null()},
+    defaults={"condition": exp.true()},
     query="""
 SELECT *
 FROM @this_model
@@ -170,7 +170,7 @@ WHERE @AND(@column = '', @condition)
 # not_null_proportion(column=column_name, threshold=0.9)
 not_null_proportion_audit = ModelAudit(
     name="not_null_proportion",
-    defaults={"condition": exp.null()},
+    defaults={"condition": exp.true()},
     query="""
 SELECT *
 FROM (
@@ -179,7 +179,7 @@ FROM (
     count(@column) as cnt_not_null,
     count(*) - count(@column) as cnt_null
   FROM @this_model
-  @WHERE(@condition) @condition
+  WHERE @condition
 ) AS s
 WHERE s.cnt_not_null <= s.cnt_tot * @threshold
     """,
@@ -188,7 +188,7 @@ WHERE s.cnt_not_null <= s.cnt_tot * @threshold
 # not_accepted_values(column=column_name, is_in=(1, 2, 3))
 not_accepted_values_audit = ModelAudit(
     name="not_accepted_values",
-    defaults={"condition": exp.null()},
+    defaults={"condition": exp.true()},
     query="""
 SELECT *
 FROM @this_model
@@ -200,7 +200,7 @@ WHERE @AND(@column IN @is_in, @condition)
 # TODO: support grouping
 sequential_values_audit = ModelAudit(
     name="sequential_values",
-    defaults={"interval": exp.Literal.number(1), "condition": exp.null()},
+    defaults={"interval": exp.Literal.number(1), "condition": exp.true()},
     query="""
 WITH windowed AS (
   SELECT
@@ -209,7 +209,7 @@ WITH windowed AS (
       ORDER BY @column
     ) AS prv
   FROM @this_model
-  @WHERE(@condition) @condition
+  WHERE @condition
 ), validation_errors AS (
     SELECT *
     FROM windowed
@@ -224,11 +224,11 @@ FROM validation_errors
 # unique_combination_of_columns(columns=(column_1, column_2))
 unique_combination_of_columns_audit = ModelAudit(
     name="unique_combination_of_columns",
-    defaults={"condition": exp.null()},
+    defaults={"condition": exp.true()},
     query="""
 SELECT @EACH(@columns, c -> c)
 FROM @this_model
-@WHERE(@condition) @condition
+WHERE @condition
 GROUP BY @EACH(@columns, c -> c)
 HAVING COUNT(*) > 1
     """,
@@ -238,7 +238,7 @@ HAVING COUNT(*) > 1
 # TODO: make inclusivity configurable
 mutually_exclusive_ranges_audit = ModelAudit(
     name="mutually_exclusive_ranges",
-    defaults={"partition_clause": exp.false(), "condition": exp.null()},
+    defaults={"partition_clause": exp.false(), "condition": exp.true()},
     query="""
 WITH window_functions AS (
   SELECT
@@ -253,7 +253,7 @@ WITH window_functions AS (
       ORDER BY @lower_bound_column desc, @upper_bound_column desc
     ) = 1 AS is_last_record
   FROM @this_model
-  @WHERE(@condition) @condition
+  WHERE @condition
 ), calc AS (
   SELECT
     *,
@@ -284,7 +284,7 @@ FROM validation_errors
 # valid_uuid(column=column_name)
 valid_uuid_audit = ModelAudit(
     name="valid_uuid",
-    defaults={"condition": exp.null()},
+    defaults={"condition": exp.true()},
     query="""
 SELECT *
 FROM @this_model
@@ -299,7 +299,7 @@ WHERE @AND(
 # valid_url(column=column_name)
 valid_url_audit = ModelAudit(
     name="valid_url",
-    defaults={"condition": exp.null()},
+    defaults={"condition": exp.true()},
     query=r"""
 SELECT *
 FROM @this_model
@@ -320,7 +320,7 @@ WHERE NOT @column IN ('GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'HEAD', 'OPTIONS'
 # valid_email(column=column_name)
 valid_email_audit = ModelAudit(
     name="valid_email",
-    defaults={"condition": exp.null()},
+    defaults={"condition": exp.true()},
     query=r"""
 SELECT *
 FROM @this_model
@@ -331,7 +331,7 @@ WHERE @AND(NOT REGEXP_LIKE(@column, '^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9
 # match_regex_pattern_list(column=column_name, patterns=('^pattern_1', 'pattern_2$'))
 match_regex_pattern_list_audit = ModelAudit(
     name="match_regex_pattern_list",
-    defaults={"condition": exp.null()},
+    defaults={"condition": exp.true()},
     query="""
 SELECT *
 FROM @this_model
@@ -351,7 +351,7 @@ WHERE @AND(
 # not_match_regex_pattern_list(column=column_name, patterns=('^pattern_1', 'pattern_2$'))
 not_match_regex_pattern_list_audit = ModelAudit(
     name="not_match_regex_pattern_list",
-    defaults={"condition": exp.null()},
+    defaults={"condition": exp.true()},
     query="""
 SELECT *
 FROM @this_model
@@ -390,7 +390,7 @@ WHERE @condition AND (
 # not_match_like_pattern_list(column=column_name, patterns=('%pattern_1%', 'pattern_2%'))
 not_match_like_pattern_list_audit = ModelAudit(
     name="not_match_like_pattern_list",
-    defaults={"condition": exp.null()},
+    defaults={"condition": exp.true()},
     query="""
 SELECT *
 FROM @this_model
@@ -410,14 +410,14 @@ WHERE @AND(
 # z_score_audit(column=column_name, threshold=3)
 z_score_audit = ModelAudit(
     name="z_score",
-    defaults={"condition": exp.null()},
+    defaults={"condition": exp.true()},
     query="""
 WITH stats AS (
   SELECT
     AVG(@column) AS mean_@column,
     STDDEV(@column) AS stddev_@column
   FROM @this_model
-  @WHERE(@condition) @condition
+  WHERE @condition
 )
 SELECT
   @column,
@@ -434,7 +434,7 @@ string_length_between_audit = ModelAudit(
         "min_v": exp.null(),
         "max_v": exp.null(),
         "inclusive": exp.true(),
-        "condition": exp.null(),
+        "condition": exp.true(),
     },
     query="""
 SELECT *
@@ -455,7 +455,7 @@ WHERE
 # string_length_equal_audit(column=column_name, v=22)
 string_length_equal_audit = ModelAudit(
     name="string_length_equal",
-    defaults={"condition": exp.null()},
+    defaults={"condition": exp.true()},
     query="""
 SELECT *
 FROM @this_model
@@ -470,14 +470,14 @@ stddev_in_range_audit = ModelAudit(
         "min_v": exp.null(),
         "max_v": exp.null(),
         "inclusive": exp.true(),
-        "condition": exp.null(),
+        "condition": exp.true(),
     },
     query="""
 SELECT *
 FROM (
   SELECT STDDEV(@column) AS stddev_@column
   FROM @this_model
-  @WHERE(@condition) @condition
+  WHERE @condition
 )
 WHERE
   @OR(
@@ -496,14 +496,14 @@ mean_in_range_audit = ModelAudit(
         "min_v": exp.null(),
         "max_v": exp.null(),
         "inclusive": exp.true(),
-        "condition": exp.null(),
+        "condition": exp.true(),
     },
     query="""
 SELECT *
 FROM (
   SELECT AVG(@column) AS mean_@column
   FROM @this_model
-  @WHERE(@condition) @condition
+  WHERE @condition
 )
 WHERE
   @OR(
@@ -518,7 +518,7 @@ WHERE
 # kl_divergence(column=age, target_column=normalized_age, threshold=0.1)
 kl_divergence_audit = ModelAudit(
     name="kl_divergence",
-    defaults={"condition": exp.null()},
+    defaults={"condition": exp.true()},
     query="""
 WITH
   table_a AS (
@@ -526,7 +526,7 @@ WITH
       @source_column,
       COUNT(*) AS num_rows
     FROM @this_model
-    @WHERE(@condition) @condition
+    WHERE @condition
     GROUP BY @source_column
   ),
   table_b AS (
@@ -534,7 +534,7 @@ WITH
       @target_column,
       COUNT(*) AS num_rows
     FROM @this_model
-    @WHERE(@condition) @condition
+    WHERE @condition
     GROUP BY @target_column
   ),
   table_a_with_p AS (
@@ -617,7 +617,7 @@ chi_square_audit = ModelAudit(
         "@def(r, (SELECT COUNT(DISTINCT x_b) FROM contingency_table))",
         "@def(E, (tot_a * tot_b / g_t))",
     ],
-    defaults={"dependent": exp.true(), "condition": exp.null()},
+    defaults={"dependent": exp.true(), "condition": exp.true()},
     query="""
 WITH
   samples AS (


Test patch:
diff --git a/tests/core/test_audit.py b/tests/core/test_audit.py
index a43d0fde..725a89a4 100644
--- a/tests/core/test_audit.py
+++ b/tests/core/test_audit.py
@@ -399,7 +399,7 @@ def test_not_null_audit(model: Model):
     )
     assert (
         rendered_query_a.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" IS NULL"""
+        == """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" IS NULL AND TRUE"""
     )
 
     rendered_query_a_and_b = builtin.not_null_audit.render_query(
@@ -408,7 +408,7 @@ def test_not_null_audit(model: Model):
     )
     assert (
         rendered_query_a_and_b.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" IS NULL OR "b" IS NULL"""
+        == """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" IS NULL OR "b" IS NULL) AND TRUE"""
     )
 
 
@@ -419,7 +419,7 @@ def test_not_null_audit_default_catalog(model_default_catalog: Model):
     )
     assert (
         rendered_query_a.sql()
-        == """SELECT * FROM (SELECT * FROM "test_catalog"."db"."test_model" AS "test_model" WHERE "ds" BETWEEN '1970-01-01' AND '1970-01-01') AS "_q_0" WHERE "a" IS NULL"""
+        == """SELECT * FROM (SELECT * FROM "test_catalog"."db"."test_model" AS "test_model" WHERE "ds" BETWEEN '1970-01-01' AND '1970-01-01') AS "_q_0" WHERE "a" IS NULL AND TRUE"""
     )
 
     rendered_query_a_and_b = builtin.not_null_audit.render_query(
@@ -428,15 +428,17 @@ def test_not_null_audit_default_catalog(model_default_catalog: Model):
     )
     assert (
         rendered_query_a_and_b.sql()
-        == """SELECT * FROM (SELECT * FROM "test_catalog"."db"."test_model" AS "test_model" WHERE "ds" BETWEEN '1970-01-01' AND '1970-01-01') AS "_q_0" WHERE "a" IS NULL OR "b" IS NULL"""
+        == """SELECT * FROM (SELECT * FROM "test_catalog"."db"."test_model" AS "test_model" WHERE "ds" BETWEEN '1970-01-01' AND '1970-01-01') AS "_q_0" WHERE ("a" IS NULL OR "b" IS NULL) AND TRUE"""
     )
 
 
 def test_unique_values_audit(model: Model):
-    rendered_query_a = builtin.unique_values_audit.render_query(model, columns=[exp.to_column("a")])
+    rendered_query_a = builtin.unique_values_audit.render_query(
+        model, columns=[exp.to_column("a")], condition=parse_one("b IS NULL")
+    )
     assert (
         rendered_query_a.sql()
-        == 'SELECT * FROM (SELECT ROW_NUMBER() OVER (PARTITION BY "a" ORDER BY "a") AS "rank_a" FROM (SELECT * FROM "db"."test_model" AS "test_model" WHERE "ds" BETWEEN \'1970-01-01\' AND \'1970-01-01\') AS "_q_0") AS "_q_1" WHERE "rank_a" > 1'
+        == 'SELECT * FROM (SELECT ROW_NUMBER() OVER (PARTITION BY "a" ORDER BY "a") AS "rank_a" FROM (SELECT * FROM "db"."test_model" AS "test_model" WHERE "ds" BETWEEN \'1970-01-01\' AND \'1970-01-01\') AS "_q_0" WHERE "b" IS NULL) AS "_q_1" WHERE "rank_a" > 1'
     )
 
     rendered_query_a_and_b = builtin.unique_values_audit.render_query(
@@ -444,7 +446,7 @@ def test_unique_values_audit(model: Model):
     )
     assert (
         rendered_query_a_and_b.sql()
-        == 'SELECT * FROM (SELECT ROW_NUMBER() OVER (PARTITION BY "a" ORDER BY "a") AS "rank_a", ROW_NUMBER() OVER (PARTITION BY "b" ORDER BY "b") AS "rank_b" FROM (SELECT * FROM "db"."test_model" AS "test_model" WHERE "ds" BETWEEN \'1970-01-01\' AND \'1970-01-01\') AS "_q_0") AS "_q_1" WHERE "rank_a" > 1 OR "rank_b" > 1'
+        == 'SELECT * FROM (SELECT ROW_NUMBER() OVER (PARTITION BY "a" ORDER BY "a") AS "rank_a", ROW_NUMBER() OVER (PARTITION BY "b" ORDER BY "b") AS "rank_b" FROM (SELECT * FROM "db"."test_model" AS "test_model" WHERE "ds" BETWEEN \'1970-01-01\' AND \'1970-01-01\') AS "_q_0" WHERE TRUE) AS "_q_1" WHERE "rank_a" > 1 OR "rank_b" > 1'
     )
 
 
@@ -456,7 +458,7 @@ def test_accepted_values_audit(model: Model):
     )
     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" IN ('value_a', 'value_b')"""
+        == """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" IN ('value_a', 'value_b') AND TRUE"""
     )
 
 
@@ -467,7 +469,7 @@ def test_number_of_rows_audit(model: Model):
     )
     assert (
         rendered_query.sql()
-        == """SELECT COUNT(*) FROM (SELECT 1 FROM (SELECT * FROM "db"."test_model" AS "test_model" WHERE "ds" BETWEEN '1970-01-01' AND '1970-01-01') AS "_q_0" LIMIT 0 + 1) AS "_q_1" HAVING COUNT(*) <= 0"""
+        == """SELECT COUNT(*) FROM (SELECT 1 FROM (SELECT * FROM "db"."test_model" AS "test_model" WHERE "ds" BETWEEN '1970-01-01' AND '1970-01-01') AS "_q_0" WHERE TRUE LIMIT 0 + 1) AS "_q_1" HAVING COUNT(*) <= 0"""
     )
 
 
@@ -478,7 +480,7 @@ def test_forall_audit(model: Model):
     )
     assert (
         rendered_query_a.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" >= "b")"""
+        == """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" >= "b") AND TRUE"""
     )
 
     rendered_query_a = builtin.forall_audit.render_query(
@@ -487,7 +489,7 @@ def test_forall_audit(model: Model):
     )
     assert (
         rendered_query_a.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" >= "b") OR NOT ("c" + "d" - "e" < 1.0)"""
+        == """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" >= "b") OR NOT ("c" + "d" - "e" < 1.0)) AND TRUE"""
     )
 
     rendered_query_a = builtin.forall_audit.render_query(
@@ -507,21 +509,21 @@ def test_accepted_range_audit(model: Model):
     )
     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" < 0'
+        == '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" < 0 AND TRUE'
     )
     rendered_query = builtin.accepted_range_audit.render_query(
         model, column=exp.to_column("a"), max_v=100, inclusive=exp.false()
     )
     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" >= 100'
+        == '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" >= 100 AND TRUE'
     )
     rendered_query = builtin.accepted_range_audit.render_query(
         model, column=exp.to_column("a"), min_v=100, max_v=100
     )
     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" < 100 OR "a" > 100'
+        == '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" < 100 OR "a" > 100) AND TRUE'
     )
 
 
@@ -532,7 +534,7 @@ def test_at_least_one_audit(model: Model):
     )
     assert (
         rendered_query.sql()
-        == 'SELECT 1 AS "1" FROM (SELECT * FROM "db"."test_model" AS "test_model" WHERE "ds" BETWEEN \'1970-01-01\' AND \'1970-01-01\') AS "_q_0" GROUP BY 1 HAVING COUNT("a") = 0'
+        == 'SELECT 1 AS "1" FROM (SELECT * FROM "db"."test_model" AS "test_model" WHERE "ds" BETWEEN \'1970-01-01\' AND \'1970-01-01\') AS "_q_0" WHERE TRUE GROUP BY 1 HAVING COUNT("a") = 0'
     )
 
 
@@ -544,7 +546,7 @@ def test_mutually_exclusive_ranges_audit(model: Model):
     )
     assert (
         rendered_query.sql()
-        == '''WITH "window_functions" AS (SELECT "a" AS "lower_bound", "a" AS "upper_bound", LEAD("a") OVER (ORDER BY "a", "a") AS "next_lower_bound", ROW_NUMBER() OVER (ORDER BY "a" DESC, "a" DESC) = 1 AS "is_last_record" FROM (SELECT * FROM "db"."test_model" AS "test_model" WHERE "ds" BETWEEN '1970-01-01' AND '1970-01-01') AS "_q_0"), "calc" AS (SELECT *, COALESCE("lower_bound" <= "upper_bound", FALSE) AS "lower_bound_lte_upper_bound", COALESCE("upper_bound" <= "next_lower_bound", "is_last_record", FALSE) AS "upper_bound_lte_next_lower_bound" FROM "window_functions" AS "window_functions"), "validation_errors" AS (SELECT * FROM "calc" AS "calc" WHERE NOT ("lower_bound_lte_upper_bound" AND "upper_bound_lte_next_lower_bound")) SELECT * FROM "validation_errors" AS "validation_errors"'''
+        == '''WITH "window_functions" AS (SELECT "a" AS "lower_bound", "a" AS "upper_bound", LEAD("a") OVER (ORDER BY "a", "a") AS "next_lower_bound", ROW_NUMBER() OVER (ORDER BY "a" DESC, "a" DESC) = 1 AS "is_last_record" FROM (SELECT * FROM "db"."test_model" AS "test_model" WHERE "ds" BETWEEN '1970-01-01' AND '1970-01-01') AS "_q_0" WHERE TRUE), "calc" AS (SELECT *, COALESCE("lower_bound" <= "upper_bound", FALSE) AS "lower_bound_lte_upper_bound", COALESCE("upper_bound" <= "next_lower_bound", "is_last_record", FALSE) AS "upper_bound_lte_next_lower_bound" FROM "window_functions" AS "window_functions"), "validation_errors" AS (SELECT * FROM "calc" AS "calc" WHERE NOT ("lower_bound_lte_upper_bound" AND "upper_bound_lte_next_lower_bound")) SELECT * FROM "validation_errors" AS "validation_errors"'''
     )
 
 
@@ -555,7 +557,7 @@ def test_sequential_values_audit(model: Model):
     )
     assert (
         rendered_query.sql()
-        == '''WITH "windowed" AS (SELECT "a", LAG("a") OVER (ORDER BY "a") AS "prv" FROM (SELECT * FROM "db"."test_model" AS "test_model" WHERE "ds" BETWEEN '1970-01-01' AND '1970-01-01') AS "_q_0"), "validation_errors" AS (SELECT * FROM "windowed" AS "windowed" WHERE NOT ("a" = "prv" + 1)) SELECT * FROM "validation_errors" AS "validation_errors"'''
+        == '''WITH "windowed" AS (SELECT "a", LAG("a") OVER (ORDER BY "a") AS "prv" FROM (SELECT * FROM "db"."test_model" AS "test_model" WHERE "ds" BETWEEN '1970-01-01' AND '1970-01-01') AS "_q_0" WHERE TRUE), "validation_errors" AS (SELECT * FROM "windowed" AS "windowed" WHERE NOT ("a" = "prv" + 1)) SELECT * FROM "validation_errors" AS "validation_errors"'''
     )
 
 
@@ -568,7 +570,7 @@ def test_chi_square_audit(model: Model):
     )
     assert (
         rendered_query.sql()
-        == """WITH "samples" AS (SELECT "a" AS "x_a", "b" AS "x_b" 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" IS NULL AND NOT "b" IS NULL), "contingency_table" AS (SELECT "x_a", "x_b", COUNT(*) AS "observed", (SELECT COUNT(*) FROM "samples" AS "t" WHERE "r"."x_a" = "t"."x_a") AS "tot_a", (SELECT COUNT(*) FROM "samples" AS "t" WHERE "r"."x_b" = "t"."x_b") AS "tot_b", (SELECT COUNT(*) FROM "samples" AS "samples") AS "g_t" /* g_t is the grand total */ FROM "samples" AS "r" GROUP BY "x_a", "x_b") SELECT ((SELECT COUNT(DISTINCT "x_a") FROM "contingency_table" AS "contingency_table") - 1) * ((SELECT COUNT(DISTINCT "x_b") FROM "contingency_table" AS "contingency_table") - 1) AS "degrees_of_freedom", SUM(("observed" - ("tot_a" * "tot_b" / "g_t")) * ("observed" - ("tot_a" * "tot_b" / "g_t")) / ("tot_a" * "tot_b" / "g_t")) AS "chi_square" FROM "contingency_table" AS "contingency_table" HAVING NOT "chi_square" > 9.48773"""
+        == """WITH "samples" AS (SELECT "a" AS "x_a", "b" AS "x_b" 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" IS NULL AND NOT "b" IS NULL) AND TRUE), "contingency_table" AS (SELECT "x_a", "x_b", COUNT(*) AS "observed", (SELECT COUNT(*) FROM "samples" AS "t" WHERE "r"."x_a" = "t"."x_a") AS "tot_a", (SELECT COUNT(*) FROM "samples" AS "t" WHERE "r"."x_b" = "t"."x_b") AS "tot_b", (SELECT COUNT(*) FROM "samples" AS "samples") AS "g_t" /* g_t is the grand total */ FROM "samples" AS "r" GROUP BY "x_a", "x_b") SELECT ((SELECT COUNT(DISTINCT "x_a") FROM "contingency_table" AS "contingency_table") - 1) * ((SELECT COUNT(DISTINCT "x_b") FROM "contingency_table" AS "contingency_table") - 1) AS "degrees_of_freedom", SUM(("observed" - ("tot_a" * "tot_b" / "g_t")) * ("observed" - ("tot_a" * "tot_b" / "g_t")) / ("tot_a" * "tot_b" / "g_t")) AS "chi_square" FROM "contingency_table" AS "contingency_table" HAVING NOT "chi_square" > 9.48773"""
     )
 
 
@@ -685,7 +687,7 @@ def test_string_length_between_audit(model: Model):
     )
     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 LENGTH("x") < 1 OR LENGTH("x") > 5"""
+        == """SELECT * FROM (SELECT * FROM "db"."test_model" AS "test_model" WHERE "ds" BETWEEN '1970-01-01' AND '1970-01-01') AS "_q_0" WHERE (LENGTH("x") < 1 OR LENGTH("x") > 5) AND TRUE"""
     )
 
 
diff --git a/tests/core/test_snapshot_evaluator.py b/tests/core/test_snapshot_evaluator.py
index 5a6a9bda..abe4fa88 100644
--- a/tests/core/test_snapshot_evaluator.py
+++ b/tests/core/test_snapshot_evaluator.py
@@ -1853,7 +1853,7 @@ def test_audit_wap(adapter_mock, make_snapshot):
     not_null_query = call_args[0][0][0]
     assert (
         not_null_query.sql(dialect="spark")
-        == "SELECT COUNT(*) FROM (SELECT * FROM (SELECT * FROM `spark_catalog`.`test_schema`.`test_table`.`branch_wap_test_wap_id` AS `branch_wap_test_wap_id`) AS `_q_0` WHERE `a` IS NULL) AS audit"
+        == "SELECT COUNT(*) FROM (SELECT * FROM (SELECT * FROM `spark_catalog`.`test_schema`.`test_table`.`branch_wap_test_wap_id` AS `branch_wap_test_wap_id`) AS `_q_0` WHERE `a` IS NULL AND TRUE) AS audit"
     )
 
     custom_audit_query = call_args[1][0][0]