@@ -2177,7 +2177,7 @@ def snapshots_to_dag(snapshots: t.Collection[Snapshot]) -> DAG[SnapshotId]:
21772177
21782178def apply_auto_restatements (
21792179 snapshots : t .Dict [SnapshotId , Snapshot ], execution_time : TimeLike
2180- ) -> t .List [SnapshotIntervals ]:
2180+ ) -> t .Tuple [ t . List [SnapshotIntervals ], t . Dict [ SnapshotId , SnapshotId ] ]:
21812181 """Applies auto restatements to the snapshots.
21822182
21832183 This operation results in the removal of intervals for snapshots that are ready to be restated based
@@ -2192,6 +2192,8 @@ def apply_auto_restatements(
21922192 A list of SnapshotIntervals with **new** intervals that need to be restated.
21932193 """
21942194 dag = snapshots_to_dag (snapshots .values ())
2195+ snapshots_with_auto_restatements : t .List [SnapshotId ] = []
2196+ auto_restatement_triggers : t .Dict [SnapshotId , SnapshotId ] = {}
21952197 auto_restated_intervals_per_snapshot : t .Dict [SnapshotId , Interval ] = {}
21962198 for s_id in dag :
21972199 if s_id not in snapshots :
@@ -2215,6 +2217,23 @@ def apply_auto_restatements(
22152217 )
22162218 auto_restated_intervals .append (next_auto_restated_interval )
22172219
2220+ # auto-restated snapshot is its own trigger
2221+ snapshots_with_auto_restatements .append (s_id )
2222+ auto_restatement_triggers [s_id ] = s_id
2223+ else :
2224+ for parent_s_id in snapshot .parents :
2225+ # first auto-restated parent is the trigger
2226+ if parent_s_id in snapshots_with_auto_restatements :
2227+ auto_restatement_triggers [s_id ] = parent_s_id
2228+ break
2229+ # if no trigger yet and parent has trigger, inherit their trigger
2230+ # - will be overwritten if a different parent is auto-restated
2231+ if (
2232+ parent_s_id in auto_restatement_triggers
2233+ and s_id not in auto_restatement_triggers
2234+ ):
2235+ auto_restatement_triggers [s_id ] = auto_restatement_triggers [parent_s_id ]
2236+
22182237 if auto_restated_intervals :
22192238 auto_restated_interval_start = sys .maxsize
22202239 auto_restated_interval_end = - sys .maxsize
@@ -2244,20 +2263,22 @@ def apply_auto_restatements(
22442263
22452264 snapshot .apply_pending_restatement_intervals ()
22462265 snapshot .update_next_auto_restatement_ts (execution_time )
2247-
2248- return [
2249- SnapshotIntervals (
2250- name = snapshots [s_id ].name ,
2251- identifier = None ,
2252- version = snapshots [s_id ].version ,
2253- dev_version = None ,
2254- intervals = [],
2255- dev_intervals = [],
2256- pending_restatement_intervals = [interval ],
2257- )
2258- for s_id , interval in auto_restated_intervals_per_snapshot .items ()
2259- if s_id in snapshots
2260- ]
2266+ return (
2267+ [
2268+ SnapshotIntervals (
2269+ name = snapshots [s_id ].name ,
2270+ identifier = None ,
2271+ version = snapshots [s_id ].version ,
2272+ dev_version = None ,
2273+ intervals = [],
2274+ dev_intervals = [],
2275+ pending_restatement_intervals = [interval ],
2276+ )
2277+ for s_id , interval in auto_restated_intervals_per_snapshot .items ()
2278+ if s_id in snapshots
2279+ ],
2280+ auto_restatement_triggers ,
2281+ )
22612282
22622283
22632284def parent_snapshots_by_name (
0 commit comments