Skip to content

Commit 5fd3c2a

Browse files
authored
Fix: Only construct snapshot mapping once when computing missing intervals and the earliest start date (#4012)
1 parent 92e8030 commit 5fd3c2a

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed

sqlmesh/core/snapshot/definition.py

+12-6
Original file line numberDiff line numberDiff line change
@@ -329,11 +329,11 @@ class SnapshotInfoMixin(ModelKindMixin):
329329
base_table_name_override: t.Optional[str]
330330
dev_table_suffix: str
331331

332-
@property
332+
@cached_property
333333
def identifier(self) -> str:
334334
return self.fingerprint.to_identifier()
335335

336-
@property
336+
@cached_property
337337
def snapshot_id(self) -> SnapshotId:
338338
return SnapshotId(name=self.name, identifier=self.identifier)
339339

@@ -1748,7 +1748,7 @@ def has_paused_forward_only(
17481748

17491749

17501750
def missing_intervals(
1751-
snapshots: t.Collection[Snapshot],
1751+
snapshots: t.Union[t.Collection[Snapshot], t.Dict[SnapshotId, Snapshot]],
17521752
start: t.Optional[TimeLike] = None,
17531753
end: t.Optional[TimeLike] = None,
17541754
execution_time: t.Optional[TimeLike] = None,
@@ -1759,6 +1759,9 @@ def missing_intervals(
17591759
end_bounded: bool = False,
17601760
) -> t.Dict[Snapshot, Intervals]:
17611761
"""Returns all missing intervals given a collection of snapshots."""
1762+
if not isinstance(snapshots, dict):
1763+
# Make sure that the mapping is only constructed once
1764+
snapshots = {snapshot.snapshot_id: snapshot for snapshot in snapshots}
17621765
missing = {}
17631766
cache: t.Dict[str, datetime] = {}
17641767
end_date = end or now_timestamp()
@@ -1771,7 +1774,7 @@ def missing_intervals(
17711774
interval_end_per_model = interval_end_per_model or {}
17721775
deployability_index = deployability_index or DeployabilityIndex.all_deployable()
17731776

1774-
for snapshot in snapshots:
1777+
for snapshot in snapshots.values():
17751778
if not snapshot.evaluatable:
17761779
continue
17771780
snapshot_start_date = start_dt
@@ -1944,7 +1947,7 @@ def inclusive_exclusive(
19441947

19451948

19461949
def earliest_start_date(
1947-
snapshots: t.Collection[Snapshot],
1950+
snapshots: t.Union[t.Collection[Snapshot], t.Dict[SnapshotId, Snapshot]],
19481951
cache: t.Optional[t.Dict[str, datetime]] = None,
19491952
relative_to: t.Optional[TimeLike] = None,
19501953
) -> datetime:
@@ -1959,9 +1962,12 @@ def earliest_start_date(
19591962
"""
19601963
cache = {} if cache is None else cache
19611964
if snapshots:
1965+
if not isinstance(snapshots, dict):
1966+
# Make sure that the mapping is only constructed once
1967+
snapshots = {snapshot.snapshot_id: snapshot for snapshot in snapshots}
19621968
return min(
19631969
start_date(snapshot, snapshots, cache=cache, relative_to=relative_to)
1964-
for snapshot in snapshots
1970+
for snapshot in snapshots.values()
19651971
)
19661972
return yesterday()
19671973

0 commit comments

Comments
 (0)