Skip to content

Commit 0a893bc

Browse files
committed
Fix: Cron shouldn't be ignored when missing intervals are bounded by the end date (#2153)
1 parent c778fd4 commit 0a893bc

File tree

2 files changed

+67
-7
lines changed

2 files changed

+67
-7
lines changed

sqlmesh/core/snapshot/definition.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -785,17 +785,17 @@ def missing_intervals(
785785

786786
interval_unit = self.node.interval_unit
787787

788-
if end_bounded:
789-
upper_bound_ts = min(end_ts, to_timestamp(execution_time))
790-
end_ts = upper_bound_ts
791-
elif allow_partials:
792-
upper_bound_ts = to_timestamp(execution_time)
793-
end_ts = min(end_ts, upper_bound_ts)
794-
else:
788+
if not allow_partials:
795789
upper_bound_ts = to_timestamp(
796790
self.node.cron_floor(execution_time) if not ignore_cron else execution_time
797791
)
798792
end_ts = min(end_ts, to_timestamp(interval_unit.cron_floor(upper_bound_ts)))
793+
else:
794+
upper_bound_ts = to_timestamp(execution_time)
795+
end_ts = min(end_ts, upper_bound_ts)
796+
797+
if end_bounded:
798+
upper_bound_ts = end_ts
799799

800800
lookback = self.model.lookback if self.is_model else 0
801801

tests/core/test_snapshot.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,66 @@ def test_missing_intervals_partial(make_snapshot):
286286
(to_timestamp(start), to_timestamp("2023-01-02")),
287287
]
288288
assert snapshot.missing_intervals(start, start, execution_time=start, ignore_cron=True) == []
289+
assert snapshot.missing_intervals(start, start, execution_time=end_ts, end_bounded=True) == []
290+
291+
292+
def test_missing_intervals_end_bounded_with_lookback(make_snapshot):
293+
snapshot = make_snapshot(
294+
SqlModel(
295+
name="test_model",
296+
kind=IncrementalByTimeRangeKind(time_column=TimeColumn(column="ds"), lookback=1),
297+
owner="owner",
298+
cron="@daily",
299+
query=parse_one("SELECT 1, ds FROM name"),
300+
)
301+
)
302+
303+
start = "2023-01-01"
304+
end = "2023-01-02"
305+
306+
snapshot.intervals = [(to_timestamp(start), to_timestamp(end))]
307+
308+
execution_ts = to_timestamp("2023-01-03")
309+
assert snapshot.missing_intervals(start, start, execution_time=execution_ts) == [
310+
(to_timestamp(start), to_timestamp(end)),
311+
]
312+
assert (
313+
snapshot.missing_intervals(start, start, execution_time=execution_ts, end_bounded=True)
314+
== []
315+
)
316+
317+
318+
def test_missing_intervals_end_bounded_with_ignore_cron(make_snapshot):
319+
snapshot = make_snapshot(
320+
SqlModel(
321+
name="test_model",
322+
kind=IncrementalByTimeRangeKind(time_column=TimeColumn(column="ds")),
323+
owner="owner",
324+
cron="1 0 * * *",
325+
query=parse_one("SELECT 1, ds FROM name"),
326+
)
327+
)
328+
329+
start = "2023-01-01"
330+
end = "2023-01-03"
331+
332+
snapshot.intervals = [(to_timestamp(start), to_timestamp("2023-01-02"))]
333+
334+
execution_ts = to_timestamp(end)
335+
assert snapshot.missing_intervals(start, end, execution_time=execution_ts) == []
336+
assert snapshot.missing_intervals(
337+
start, end, execution_time=execution_ts, ignore_cron=True
338+
) == [
339+
(to_timestamp("2023-01-02"), to_timestamp(end)),
340+
]
341+
assert (
342+
snapshot.missing_intervals(start, end, execution_time=execution_ts, end_bounded=True) == []
343+
)
344+
assert snapshot.missing_intervals(
345+
start, end, execution_time=execution_ts, ignore_cron=True, end_bounded=True
346+
) == [
347+
(to_timestamp("2023-01-02"), to_timestamp(end)),
348+
]
289349

290350

291351
def test_incremental_time_self_reference(make_snapshot):

0 commit comments

Comments
 (0)