Skip to content

Commit c13c40f

Browse files
authored
Fix: Make date range inclusive for audits ran in sqlmesh plan (#4179)
1 parent 1cc9da4 commit c13c40f

3 files changed

Lines changed: 76 additions & 5 deletions

File tree

sqlmesh/core/model/definition.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -491,10 +491,11 @@ def render_audit_query(
491491
pass
492492

493493
if self.time_column:
494-
where = self.time_column.column.between(
495-
self.convert_to_time_column(start or c.EPOCH, columns_to_types),
496-
self.convert_to_time_column(end or c.EPOCH, columns_to_types),
497-
)
494+
low, high = [
495+
self.convert_to_time_column(dt, columns_to_types)
496+
for dt in make_inclusive(start or c.EPOCH, end or c.EPOCH, self.dialect)
497+
]
498+
where = self.time_column.column.between(low, high)
498499
else:
499500
where = None
500501

sqlmesh/core/snapshot/evaluator.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -555,7 +555,7 @@ def audit(
555555

556556
if wap_id is not None:
557557
logger.info(
558-
"Publishing evalaution results for snapshot %s, WAP ID '%s'",
558+
"Publishing evaluation results for snapshot %s, WAP ID '%s'",
559559
snapshot.snapshot_id,
560560
wap_id,
561561
)

tests/core/test_context.py

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1923,3 +1923,73 @@ def create_log_view(evaluator, view_name):
19231923

19241924
# Validate the schema is retrieved using resolve_template for the environment-specific schema
19251925
assert log_schema["my_schema"][0] == "db__dev"
1926+
1927+
1928+
def test_plan_audit_intervals(tmp_path: pathlib.Path, capsys, caplog):
1929+
ctx = Context(
1930+
paths=tmp_path, config=Config(model_defaults=ModelDefaultsConfig(dialect="duckdb"))
1931+
)
1932+
1933+
ctx.upsert_model(
1934+
load_sql_based_model(
1935+
parse(
1936+
"""
1937+
MODEL (
1938+
name sqlmesh_audit.date_example,
1939+
kind INCREMENTAL_BY_TIME_RANGE(
1940+
time_column(date_id, '%Y-%m-%d')
1941+
),
1942+
cron '@daily',
1943+
partitioned_by (date_id),
1944+
audits [unique_combination_of_columns(columns=(date_id))]
1945+
);
1946+
1947+
WITH sample_table AS (
1948+
SELECT
1949+
DATE('2025-02-01') as date_id,
1950+
)
1951+
SELECT date_id FROM sample_table WHERE date_id BETWEEN @start_ds AND @end_ds
1952+
"""
1953+
)
1954+
)
1955+
)
1956+
1957+
ctx.upsert_model(
1958+
load_sql_based_model(
1959+
parse(
1960+
"""
1961+
MODEL (
1962+
name sqlmesh_audit.timestamp_example,
1963+
kind INCREMENTAL_BY_TIME_RANGE(
1964+
time_column(timestamp_id, '%Y-%m-%d %H:%M:%S')
1965+
),
1966+
cron '@daily',
1967+
partitioned_by (timestamp_id),
1968+
audits [unique_combination_of_columns(columns=(timestamp_id))]
1969+
);
1970+
1971+
WITH sample_table AS (
1972+
SELECT
1973+
TIMESTAMP('2025-02-01') as timestamp_id,
1974+
)
1975+
SELECT timestamp_id FROM sample_table WHERE timestamp_id BETWEEN @start_ts AND @end_ts
1976+
"""
1977+
)
1978+
)
1979+
)
1980+
1981+
ctx.plan(
1982+
environment="dev", auto_apply=True, no_prompts=True, start="2025-02-01", end="2025-02-01"
1983+
)
1984+
1985+
# Case 1: The timestamp audit should be in the inclusive range ['2025-02-01 00:00:00', '2025-02-01 23:59:59.999999']
1986+
assert (
1987+
"""SELECT COUNT(*) FROM (SELECT ("timestamp_id") AS "timestamp_id" FROM (SELECT * FROM "sqlmesh__sqlmesh_audit"."sqlmesh_audit__timestamp_example__2797548448" AS "sqlmesh_audit__timestamp_example__2797548448" WHERE "timestamp_id" BETWEEN CAST('2025-02-01 00:00:00' AS TIMESTAMP) AND CAST('2025-02-01 23:59:59.999999' AS TIMESTAMP)) AS "_q_0" WHERE TRUE GROUP BY ("timestamp_id") HAVING COUNT(*) > 1) AS "audit\""""
1988+
in caplog.text
1989+
)
1990+
1991+
# Case 2: The date audit should be in the inclusive range ['2025-02-01', '2025-02-01']
1992+
assert (
1993+
"""SELECT COUNT(*) FROM (SELECT ("date_id") AS "date_id" FROM (SELECT * FROM "sqlmesh__sqlmesh_audit"."sqlmesh_audit__date_example__4100277424" AS "sqlmesh_audit__date_example__4100277424" WHERE "date_id" BETWEEN CAST('2025-02-01' AS DATE) AND CAST('2025-02-01' AS DATE)) AS "_q_0" WHERE TRUE GROUP BY ("date_id") HAVING COUNT(*) > 1) AS "audit\""""
1994+
in caplog.text
1995+
)

0 commit comments

Comments
 (0)