66from pathlib import Path
77from collections import defaultdict
88from sqlglot import exp
9- from pydantic import Field
109
1110from sqlmesh .core .engine_adapter import EngineAdapter
1211from sqlmesh .core .state_sync .db .utils import (
2726 SnapshotNameVersion ,
2827 SnapshotInfoLike ,
2928 Snapshot ,
29+ MinimalSnapshot ,
3030 SnapshotId ,
3131 SnapshotFingerprint ,
3232)
3333from sqlmesh .utils .migration import index_text_type , blob_text_type
3434from sqlmesh .utils .date import now_timestamp , TimeLike , to_timestamp
35- from sqlmesh .utils .pydantic import PydanticModel
3635from sqlmesh .utils import unique
3736
3837if t .TYPE_CHECKING :
@@ -215,7 +214,7 @@ def _get_expired_snapshots(
215214 for snapshot in environment .snapshots
216215 }
217216
218- def _is_snapshot_used (snapshot : SharedVersionSnapshot ) -> bool :
217+ def _is_snapshot_used (snapshot : MinimalSnapshot ) -> bool :
219218 return (
220219 snapshot .snapshot_id in promoted_snapshot_ids
221220 or snapshot .snapshot_id not in expired_candidates
@@ -308,12 +307,12 @@ def get_snapshots(
308307 """
309308 return self ._get_snapshots (snapshot_ids )
310309
311- def get_snapshot_ids_by_names (
310+ def get_snapshots_by_names (
312311 self ,
313312 snapshot_names : t .Iterable [str ],
314313 current_ts : t .Optional [int ] = None ,
315314 exclude_expired : bool = True ,
316- ) -> t .Set [SnapshotId ]:
315+ ) -> t .Set [MinimalSnapshot ]:
317316 """Return the snapshot id's for all versions of the specified snapshot names.
318317
319318 Args:
@@ -334,14 +333,20 @@ def get_snapshot_ids_by_names(
334333 unexpired_expr = None
335334
336335 return {
337- SnapshotId (name = name , identifier = identifier )
336+ MinimalSnapshot (
337+ name = name ,
338+ identifier = identifier ,
339+ version = version ,
340+ dev_version = dev_version ,
341+ fingerprint = SnapshotFingerprint .parse_raw (fingerprint ),
342+ )
338343 for where in snapshot_name_filter (
339344 snapshot_names = snapshot_names ,
340345 batch_size = self .SNAPSHOT_BATCH_SIZE ,
341346 )
342- for name , identifier in fetchall (
347+ for name , identifier , version , dev_version , fingerprint in fetchall (
343348 self .engine_adapter ,
344- exp .select ("name" , "identifier" )
349+ exp .select ("name" , "identifier" , "version" , "dev_version" , "fingerprint" )
345350 .from_ (self .snapshots_table )
346351 .where (where )
347352 .and_ (unexpired_expr ),
@@ -631,7 +636,7 @@ def _get_snapshots_with_same_version(
631636 self ,
632637 snapshots : t .Collection [SnapshotNameVersionLike ],
633638 lock_for_update : bool = False ,
634- ) -> t .List [SharedVersionSnapshot ]:
639+ ) -> t .List [MinimalSnapshot ]:
635640 """Fetches all snapshots that share the same version as the snapshots.
636641
637642 The output includes the snapshots with the specified identifiers.
@@ -668,7 +673,7 @@ def _get_snapshots_with_same_version(
668673 snapshot_rows .extend (fetchall (self .engine_adapter , query ))
669674
670675 return [
671- SharedVersionSnapshot (
676+ MinimalSnapshot (
672677 name = name ,
673678 identifier = identifier ,
674679 version = version ,
@@ -751,23 +756,3 @@ def _auto_restatements_to_df(auto_restatements: t.Dict[SnapshotNameVersion, int]
751756 for name_version , ts in auto_restatements .items ()
752757 ]
753758 )
754-
755-
756- class SharedVersionSnapshot (PydanticModel ):
757- """A stripped down version of a snapshot that is used for fetching snapshots that share the same version
758- with a significantly reduced parsing overhead.
759- """
760-
761- name : str
762- version : str
763- dev_version_ : t .Optional [str ] = Field (alias = "dev_version" )
764- identifier : str
765- fingerprint : SnapshotFingerprint
766-
767- @property
768- def snapshot_id (self ) -> SnapshotId :
769- return SnapshotId (name = self .name , identifier = self .identifier )
770-
771- @property
772- def dev_version (self ) -> str :
773- return self .dev_version_ or self .fingerprint .to_version ()
0 commit comments