Skip to content

Commit c773123

Browse files
breken-aiclaude
andcommitted
fix(snapshot): restate exactly N monthly intervals with auto restatement
`get_next_auto_restatement_interval` computed the start as `end - N * interval_unit.milliseconds`, and a MONTH interval unit is 28 days long. For monthly models the start drifted about 2.4 days per month, so from 13 intervals on the cron floor landed a month too late and the oldest month was never restated (13 restated 12, 24 restated 23). Step back N intervals with the interval unit's cron instead. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Signed-off-by: breken-ai <312387581+breken-ai@users.noreply.github.com>
1 parent 263723f commit c773123

2 files changed

Lines changed: 49 additions & 4 deletions

File tree

‎sqlmesh/core/snapshot/definition.py‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1280,10 +1280,11 @@ def get_next_auto_restatement_interval(self, execution_time: TimeLike) -> t.Opti
12801280
auto_restatement_end_ts = to_timestamp(
12811281
self.node.interval_unit.cron_floor(execution_time_ts)
12821282
)
1283-
auto_restatement_start_ts = (
1284-
auto_restatement_end_ts
1285-
- num_intervals_to_restate * self.node.interval_unit.milliseconds
1286-
)
1283+
# Step back one interval at a time, since intervals like months don't have a fixed length
1284+
croniter = self.node.interval_unit.croniter(auto_restatement_end_ts)
1285+
auto_restatement_start_ts = auto_restatement_end_ts
1286+
for _ in range(num_intervals_to_restate):
1287+
auto_restatement_start_ts = to_timestamp(croniter.get_prev(estimate=True))
12871288
return (auto_restatement_start_ts, auto_restatement_end_ts)
12881289

12891290
def update_next_auto_restatement_ts(self, execution_time: TimeLike) -> t.Optional[int]:

‎tests/core/test_snapshot.py‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3371,6 +3371,50 @@ def test_get_next_auto_restatement_interval(
33713371
)
33723372

33733373

3374+
@pytest.mark.parametrize(
3375+
"auto_restatement_intervals,expected_auto_restatement_start",
3376+
[
3377+
(1, "2024-06-01"),
3378+
(12, "2023-07-01"),
3379+
(13, "2023-06-01"),
3380+
(24, "2022-07-01"),
3381+
],
3382+
)
3383+
def test_apply_auto_restatements_monthly(
3384+
make_snapshot,
3385+
auto_restatement_intervals: int,
3386+
expected_auto_restatement_start: str,
3387+
):
3388+
snapshot = make_snapshot(
3389+
SqlModel(
3390+
name="test_model",
3391+
kind=IncrementalByTimeRangeKind(
3392+
time_column=TimeColumn(column="ds"),
3393+
auto_restatement_cron="0 10 * * *",
3394+
auto_restatement_intervals=auto_restatement_intervals,
3395+
),
3396+
cron="@monthly",
3397+
start="2020-01-01",
3398+
query=parse_one("SELECT 1, ds FROM name"),
3399+
)
3400+
)
3401+
snapshot.categorize_as(SnapshotChangeCategory.BREAKING)
3402+
snapshot.add_interval("2020-01-01", "2024-06-30")
3403+
snapshot.next_auto_restatement_ts = to_timestamp("2024-07-02 10:00:00")
3404+
3405+
restated_intervals, _ = apply_auto_restatements(
3406+
{snapshot.snapshot_id: snapshot}, to_timestamp("2024-07-02 10:01:00")
3407+
)
3408+
3409+
# Months have different lengths, so the last N monthly intervals must be restated exactly
3410+
expected_interval = (
3411+
to_timestamp(expected_auto_restatement_start),
3412+
to_timestamp("2024-07-01"),
3413+
)
3414+
assert restated_intervals[0].pending_restatement_intervals == [expected_interval]
3415+
assert snapshot.intervals == [(to_timestamp("2020-01-01"), expected_interval[0])]
3416+
3417+
33743418
def test_apply_auto_restatements(make_snapshot):
33753419
# Hourly upstream model with auto restatement intervals set to 24
33763420
model_a = SqlModel(

0 commit comments

Comments
 (0)