Skip to content

Commit 920ad1c

Browse files
committed
Fix: Make sure downstream models for which restatements are disabled are not auto-restated (#3597)
1 parent 34b9a1a commit 920ad1c

File tree

2 files changed

+73
-0
lines changed

2 files changed

+73
-0
lines changed

sqlmesh/core/snapshot/definition.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1081,6 +1081,8 @@ def update_next_auto_restatement_ts(self, execution_time: TimeLike) -> t.Optiona
10811081

10821082
def apply_pending_restatement_intervals(self) -> None:
10831083
"""Applies the pending restatement intervals to the snapshot's intervals."""
1084+
if not self.is_model or self.model.disable_restatement:
1085+
return
10841086
for pending_restatement_interval in self.pending_restatement_intervals:
10851087
logger.info(
10861088
"Applying the auto restated interval (%s, %s) to snapshot %s",
@@ -1956,6 +1958,8 @@ def apply_auto_restatements(
19561958
if s_id not in snapshots:
19571959
continue
19581960
snapshot = snapshots[s_id]
1961+
if not snapshot.is_model or snapshot.model.disable_restatement:
1962+
continue
19591963

19601964
next_auto_restated_interval = snapshot.get_next_auto_restatement_interval(execution_time)
19611965
auto_restated_intervals = [

tests/core/test_snapshot.py

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2590,3 +2590,72 @@ def test_apply_auto_restatements(make_snapshot):
25902590
assert snapshot_f.intervals == [
25912591
(to_timestamp("2020-01-01"), to_timestamp("2020-01-06")),
25922592
]
2593+
2594+
2595+
def test_apply_auto_restatements_disable_restatement_downstream(make_snapshot):
2596+
# Hourly upstream model with auto restatement intervals set to 24
2597+
model_a = SqlModel(
2598+
name="test_model_a",
2599+
kind=IncrementalByTimeRangeKind(
2600+
time_column=TimeColumn(column="ds"),
2601+
auto_restatement_cron="0 10 * * *",
2602+
auto_restatement_intervals=24,
2603+
),
2604+
cron="@hourly",
2605+
query=parse_one("SELECT 1, ds FROM name"),
2606+
)
2607+
snapshot_a = make_snapshot(model_a, version="1")
2608+
snapshot_a.add_interval("2020-01-01", "2020-01-06 09:00:00")
2609+
snapshot_a.next_auto_restatement_ts = to_timestamp("2020-01-06 10:00:00")
2610+
2611+
# Daily downstream model with disable restatement
2612+
model_b = SqlModel(
2613+
name="test_model_b",
2614+
kind=IncrementalByTimeRangeKind(
2615+
time_column=TimeColumn(column="ds"),
2616+
disable_restatement=True,
2617+
),
2618+
cron="@daily",
2619+
query=parse_one("SELECT ds FROM test_model_a"),
2620+
)
2621+
snapshot_b = make_snapshot(model_b, nodes={model_a.fqn: model_a}, version="2")
2622+
snapshot_b.add_interval("2020-01-01", "2020-01-05")
2623+
assert snapshot_a.snapshot_id in snapshot_b.parents
2624+
2625+
restated_intervals = apply_auto_restatements(
2626+
{
2627+
snapshot_a.snapshot_id: snapshot_a,
2628+
snapshot_b.snapshot_id: snapshot_b,
2629+
},
2630+
"2020-01-06 10:01:00",
2631+
)
2632+
assert sorted(restated_intervals, key=lambda x: x.name) == [
2633+
SnapshotIntervals(
2634+
name=snapshot_a.name,
2635+
identifier=snapshot_a.identifier,
2636+
version=snapshot_a.version,
2637+
intervals=[],
2638+
dev_intervals=[],
2639+
pending_restatement_intervals=[
2640+
(to_timestamp("2020-01-05 10:00:00"), to_timestamp("2020-01-06 10:00:00"))
2641+
],
2642+
),
2643+
]
2644+
2645+
assert snapshot_a.next_auto_restatement_ts == to_timestamp("2020-01-07 10:00:00")
2646+
assert snapshot_b.next_auto_restatement_ts is None
2647+
2648+
assert snapshot_a.intervals == [
2649+
(to_timestamp("2020-01-01"), to_timestamp("2020-01-05 10:00:00")),
2650+
]
2651+
assert snapshot_b.intervals == [
2652+
(to_timestamp("2020-01-01"), to_timestamp("2020-01-06")),
2653+
]
2654+
2655+
snapshot_b.pending_restatement_intervals = [
2656+
(to_timestamp("2020-01-03"), to_timestamp("2020-01-06"))
2657+
]
2658+
snapshot_b.apply_pending_restatement_intervals()
2659+
assert snapshot_b.intervals == [
2660+
(to_timestamp("2020-01-01"), to_timestamp("2020-01-06")),
2661+
]

0 commit comments

Comments
 (0)