From 72b332f1177ec97fbaa06de46e0d66de274b5892 Mon Sep 17 00:00:00 2001 From: George Sittas Date: Mon, 2 Dec 2024 17:02:33 +0200 Subject: [PATCH] Fix: use model dialect as fallback for audits --- sqlmesh/core/model/definition.py | 2 +- tests/core/test_audit.py | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/sqlmesh/core/model/definition.py b/sqlmesh/core/model/definition.py index 15ff880557..ca13ccbb3f 100644 --- a/sqlmesh/core/model/definition.py +++ b/sqlmesh/core/model/definition.py @@ -464,7 +464,7 @@ def render_audit_query( query_renderer = QueryRenderer( audit.query, - audit.dialect, + audit.dialect or self.dialect, audit.macro_definitions, path=audit._path or Path(), jinja_macro_registry=audit.jinja_macros, diff --git a/tests/core/test_audit.py b/tests/core/test_audit.py index 4b313cffe8..90ee1603de 100644 --- a/tests/core/test_audit.py +++ b/tests/core/test_audit.py @@ -12,6 +12,7 @@ ) from sqlmesh.core.dialect import parse from sqlmesh.core.model import ( + FullKind, IncrementalByTimeRangeKind, Model, SeedModel, @@ -880,3 +881,20 @@ def test_model_inline_audits(sushi_context: Context): assert len(model.audit_definitions) == 3 assert isinstance(model.audit_definitions["assert_valid_name"], ModelAudit) model.render_audit_query(model.audit_definitions["assert_positive_id"]).sql() == expected_query + + +def test_audit_query_normalization(): + model = create_sql_model( + "db.test_model", + parse_one("SELECT a, b, ds"), + kind=FullKind(), + dialect="snowflake", + ) + rendered_audit_query = model.render_audit_query( + builtin.not_null_audit, + columns=[exp.to_column("a")], + ) + assert ( + rendered_audit_query.sql("snowflake") + == """SELECT * FROM (SELECT * FROM "DB"."TEST_MODEL" AS "TEST_MODEL") AS "_Q_0" WHERE "A" IS NULL AND TRUE""" + )