From 86f2ae8c7d3f7b1520c3f6a0fa1fea33e8531915 Mon Sep 17 00:00:00 2001 From: Naman Gururani Date: Fri, 4 Dec 2026 07:26:06 +0530 Subject: [PATCH] fix(scheduler): don't process unfinished intervals for crons with an hour range For a cron like `0 9-17 * * *`, `cron_floor` could return a time in the future outside of the cron's hours, because it estimated the previous tick. The run then marked an interval that hadn't ended as complete, and the rows that arrived later were never loaded. Signed-off-by: Naman Gururani --- sqlmesh/core/node.py | 2 +- tests/core/test_snapshot.py | 20 ++++++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/sqlmesh/core/node.py b/sqlmesh/core/node.py index 73e4a31940..d572b4036c 100644 --- a/sqlmesh/core/node.py +++ b/sqlmesh/core/node.py @@ -512,7 +512,7 @@ def cron_floor(self, value: TimeLike, estimate: bool = False) -> datetime: Returns: The timestamp floor. """ - return self.croniter(self.cron_next(value, estimate=estimate)).get_prev(estimate=True) + return self.croniter(self.cron_next(value, estimate=estimate)).get_prev(estimate=estimate) def text_diff(self, other: Node, rendered: bool = False) -> str: """Produce a text diff against another node. diff --git a/tests/core/test_snapshot.py b/tests/core/test_snapshot.py index d92f7dcc18..57f2b4965e 100644 --- a/tests/core/test_snapshot.py +++ b/tests/core/test_snapshot.py @@ -515,6 +515,26 @@ def test_missing_intervals_end_bounded_with_ignore_cron(make_snapshot): ] +@time_machine.travel("2023-01-02 20:30:00 UTC", tick=False) +def test_missing_intervals_cron_with_hour_range(make_snapshot): + snapshot = make_snapshot( + SqlModel( + name="test_model", + kind=IncrementalByTimeRangeKind(time_column=TimeColumn(column="ds")), + cron="0 9-17 * * *", + query=parse_one("SELECT 1, ds FROM name"), + ) + ) + + # The last run was due at 17:00 and the next one is due at 09:00 the following day + now = "2023-01-02 20:30:00" + assert snapshot.node.cron_floor(now) == to_datetime("2023-01-02 17:00:00") + assert snapshot.missing_intervals("2023-01-02 15:00:00", now) == [ + (to_timestamp("2023-01-02 15:00:00"), to_timestamp("2023-01-02 16:00:00")), + (to_timestamp("2023-01-02 16:00:00"), to_timestamp("2023-01-02 17:00:00")), + ] + + def test_missing_intervals_past_end_date_with_lookback(make_snapshot): snapshot: Snapshot = make_snapshot( # type: ignore SqlModel(