Skip to content

Commit 63cddfa

Browse files
committed
Fix: Make dates inclusive for audit's BETWEEN clause
1 parent 6386a93 commit 63cddfa

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
@@ -1922,3 +1922,73 @@ def create_log_view(evaluator, view_name):
19221922

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

0 commit comments

Comments
 (0)