Skip to content

Commit a313f34

Browse files
committed
List all upstream auto-restated models
1 parent e94840f commit a313f34

5 files changed

Lines changed: 128 additions & 29 deletions

File tree

sqlmesh/core/console.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -428,7 +428,7 @@ def update_snapshot_evaluation_progress(
428428
num_audits_passed: int,
429429
num_audits_failed: int,
430430
audit_only: bool = False,
431-
auto_restatement_trigger: t.Optional[SnapshotId] = None,
431+
auto_restatement_triggers: t.Optional[t.List[SnapshotId]] = None,
432432
) -> None:
433433
"""Updates the snapshot evaluation progress."""
434434

@@ -576,7 +576,7 @@ def update_snapshot_evaluation_progress(
576576
num_audits_passed: int,
577577
num_audits_failed: int,
578578
audit_only: bool = False,
579-
auto_restatement_trigger: t.Optional[SnapshotId] = None,
579+
auto_restatement_triggers: t.Optional[t.List[SnapshotId]] = None,
580580
) -> None:
581581
pass
582582

@@ -1058,7 +1058,7 @@ def update_snapshot_evaluation_progress(
10581058
num_audits_passed: int,
10591059
num_audits_failed: int,
10601060
audit_only: bool = False,
1061-
auto_restatement_trigger: t.Optional[SnapshotId] = None,
1061+
auto_restatement_triggers: t.Optional[t.List[SnapshotId]] = None,
10621062
) -> None:
10631063
"""Update the snapshot evaluation progress."""
10641064
if (
@@ -3642,7 +3642,7 @@ def update_snapshot_evaluation_progress(
36423642
num_audits_passed: int,
36433643
num_audits_failed: int,
36443644
audit_only: bool = False,
3645-
auto_restatement_trigger: t.Optional[SnapshotId] = None,
3645+
auto_restatement_triggers: t.Optional[t.List[SnapshotId]] = None,
36463646
) -> None:
36473647
view_name, loaded_batches = self.evaluation_batch_progress[snapshot.snapshot_id]
36483648

@@ -3812,12 +3812,12 @@ def update_snapshot_evaluation_progress(
38123812
num_audits_passed: int,
38133813
num_audits_failed: int,
38143814
audit_only: bool = False,
3815-
auto_restatement_trigger: t.Optional[SnapshotId] = None,
3815+
auto_restatement_triggers: t.Optional[t.List[SnapshotId]] = None,
38163816
) -> None:
38173817
message = f"Evaluating {snapshot.name} | batch={batch_idx} | duration={duration_ms}ms | num_audits_passed={num_audits_passed} | num_audits_failed={num_audits_failed}"
38183818

3819-
if auto_restatement_trigger:
3820-
message += f" | evaluation_triggered_by={auto_restatement_trigger.name}"
3819+
if auto_restatement_triggers:
3820+
message += f" | auto_restatement_triggers={','.join(trigger.name for trigger in auto_restatement_triggers)}"
38213821

38223822
if audit_only:
38233823
message = f"Auditing {snapshot.name} duration={duration_ms}ms | num_audits_passed={num_audits_passed} | num_audits_failed={num_audits_failed}"

sqlmesh/core/scheduler.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,7 @@ def run_merged_intervals(
374374
run_environment_statements: bool = False,
375375
audit_only: bool = False,
376376
restatements: t.Optional[t.Dict[SnapshotId, Interval]] = None,
377-
auto_restatement_triggers: t.Dict[SnapshotId, SnapshotId] = {},
377+
auto_restatement_triggers: t.Dict[SnapshotId, t.List[SnapshotId]] = {},
378378
) -> t.Tuple[t.List[NodeExecutionFailedError[SchedulingUnit]], t.List[SchedulingUnit]]:
379379
"""Runs precomputed batches of missing intervals.
380380
@@ -477,7 +477,7 @@ def evaluate_node(node: SchedulingUnit) -> None:
477477
evaluation_duration_ms,
478478
num_audits - num_audits_failed,
479479
num_audits_failed,
480-
auto_restatement_trigger=auto_restatement_triggers.get(snapshot.snapshot_id),
480+
auto_restatement_triggers=auto_restatement_triggers.get(snapshot.snapshot_id),
481481
)
482482

483483
try:
@@ -641,7 +641,7 @@ def _run_or_audit(
641641
for s_id, interval in (remove_intervals or {}).items():
642642
self.snapshots[s_id].remove_interval(interval)
643643

644-
auto_restatement_triggers: t.Dict[SnapshotId, SnapshotId] = {}
644+
auto_restatement_triggers: t.Dict[SnapshotId, t.List[SnapshotId]] = {}
645645
if auto_restatement_enabled:
646646
auto_restated_intervals, auto_restatement_triggers = apply_auto_restatements(
647647
self.snapshots, execution_time

sqlmesh/core/snapshot/definition.py

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2192,7 +2192,7 @@ def snapshots_to_dag(snapshots: t.Collection[Snapshot]) -> DAG[SnapshotId]:
21922192

21932193
def apply_auto_restatements(
21942194
snapshots: t.Dict[SnapshotId, Snapshot], execution_time: TimeLike
2195-
) -> t.Tuple[t.List[SnapshotIntervals], t.Dict[SnapshotId, SnapshotId]]:
2195+
) -> t.Tuple[t.List[SnapshotIntervals], t.Dict[SnapshotId, t.List[SnapshotId]]]:
21962196
"""Applies auto restatements to the snapshots.
21972197
21982198
This operation results in the removal of intervals for snapshots that are ready to be restated based
@@ -2207,8 +2207,7 @@ def apply_auto_restatements(
22072207
A list of SnapshotIntervals with **new** intervals that need to be restated.
22082208
"""
22092209
dag = snapshots_to_dag(snapshots.values())
2210-
snapshots_with_auto_restatements: t.List[SnapshotId] = []
2211-
auto_restatement_triggers: t.Dict[SnapshotId, SnapshotId] = {}
2210+
auto_restatement_triggers: t.Dict[SnapshotId, t.List[SnapshotId]] = {}
22122211
auto_restated_intervals_per_snapshot: t.Dict[SnapshotId, Interval] = {}
22132212
for s_id in dag:
22142213
if s_id not in snapshots:
@@ -2223,6 +2222,7 @@ def apply_auto_restatements(
22232222
for parent_s_id in snapshot.parents
22242223
if parent_s_id in auto_restated_intervals_per_snapshot
22252224
]
2225+
upstream_triggers = []
22262226
if next_auto_restated_interval:
22272227
logger.info(
22282228
"Calculated the next auto restated interval (%s, %s) for snapshot %s",
@@ -2233,21 +2233,15 @@ def apply_auto_restatements(
22332233
auto_restated_intervals.append(next_auto_restated_interval)
22342234

22352235
# auto-restated snapshot is its own trigger
2236-
snapshots_with_auto_restatements.append(s_id)
2237-
auto_restatement_triggers[s_id] = s_id
2238-
else:
2239-
for parent_s_id in snapshot.parents:
2240-
# first auto-restated parent is the trigger
2241-
if parent_s_id in snapshots_with_auto_restatements:
2242-
auto_restatement_triggers[s_id] = parent_s_id
2243-
break
2244-
# if no trigger yet and parent has trigger, inherit their trigger
2245-
# - will be overwritten if a different parent is auto-restated
2246-
if (
2247-
parent_s_id in auto_restatement_triggers
2248-
and s_id not in auto_restatement_triggers
2249-
):
2250-
auto_restatement_triggers[s_id] = auto_restatement_triggers[parent_s_id]
2236+
upstream_triggers = [s_id]
2237+
2238+
for parent_s_id in snapshot.parents:
2239+
if parent_s_id in auto_restatement_triggers:
2240+
upstream_triggers.extend(auto_restatement_triggers[parent_s_id])
2241+
2242+
# remove duplicate triggers
2243+
if upstream_triggers:
2244+
auto_restatement_triggers[s_id] = list(dict.fromkeys(upstream_triggers))
22512245

22522246
if auto_restated_intervals:
22532247
auto_restated_interval_start = sys.maxsize

tests/core/test_snapshot.py

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3278,6 +3278,111 @@ def test_apply_auto_restatements_disable_restatement_downstream(make_snapshot):
32783278
]
32793279

32803280

3281+
def test_auto_restatement_triggers(make_snapshot):
3282+
model_a = SqlModel(
3283+
name="test_model_a",
3284+
kind=IncrementalByTimeRangeKind(
3285+
time_column=TimeColumn(column="ds"),
3286+
auto_restatement_cron="0 10 * * *",
3287+
auto_restatement_intervals=24,
3288+
),
3289+
start="2020-01-01",
3290+
cron="@daily",
3291+
query=parse_one("SELECT 1 as ds"),
3292+
)
3293+
snapshot_a = make_snapshot(model_a, version="1")
3294+
snapshot_a.add_interval("2020-01-01", "2020-01-05")
3295+
snapshot_a.next_auto_restatement_ts = to_timestamp("2020-01-06 10:00:00")
3296+
3297+
model_b = SqlModel(
3298+
name="test_model_b",
3299+
kind=IncrementalByTimeRangeKind(
3300+
time_column=TimeColumn(column="ds"),
3301+
),
3302+
start="2020-01-01",
3303+
cron="@daily",
3304+
query=parse_one("SELECT ds FROM test_model_a"),
3305+
)
3306+
snapshot_b = make_snapshot(model_b, nodes={model_a.fqn: model_a}, version="1")
3307+
snapshot_b.add_interval("2020-01-01", "2020-01-05")
3308+
3309+
model_c = SqlModel(
3310+
name="test_model_c",
3311+
kind=IncrementalByTimeRangeKind(
3312+
time_column=TimeColumn(column="ds"),
3313+
auto_restatement_cron="0 10 * * *",
3314+
auto_restatement_intervals=24,
3315+
),
3316+
start="2020-01-01",
3317+
cron="@daily",
3318+
query=parse_one("SELECT ds FROM test_model_a"),
3319+
)
3320+
snapshot_c = make_snapshot(model_c, nodes={model_a.fqn: model_a}, version="1")
3321+
snapshot_c.add_interval("2020-01-01", "2020-01-05")
3322+
snapshot_c.next_auto_restatement_ts = to_timestamp("2020-01-06 10:00:00")
3323+
3324+
model_d = SqlModel(
3325+
name="test_model_d",
3326+
kind=IncrementalByTimeRangeKind(
3327+
time_column=TimeColumn(column="ds"),
3328+
auto_restatement_cron="0 10 * * *",
3329+
auto_restatement_intervals=24,
3330+
),
3331+
start="2020-01-01",
3332+
cron="@daily",
3333+
query=parse_one("SELECT 1 as ds"),
3334+
)
3335+
snapshot_d = make_snapshot(model_d, version="1")
3336+
snapshot_d.add_interval("2020-01-01", "2020-01-05")
3337+
snapshot_d.next_auto_restatement_ts = to_timestamp("2020-01-06 10:00:00")
3338+
3339+
model_e = SqlModel(
3340+
name="test_model_e",
3341+
kind=IncrementalByTimeRangeKind(
3342+
time_column=TimeColumn(column="ds"),
3343+
),
3344+
start="2020-01-01",
3345+
cron="@daily",
3346+
query=parse_one(
3347+
"SELECT ds from test_model_b UNION ALL SELECT ds from test_model_c UNION ALL SELECT ds from test_model_d"
3348+
),
3349+
)
3350+
snapshot_e = make_snapshot(
3351+
model_e,
3352+
nodes={
3353+
model_a.fqn: model_a,
3354+
model_b.fqn: model_b,
3355+
model_c.fqn: model_c,
3356+
model_d.fqn: model_d,
3357+
},
3358+
version="1",
3359+
)
3360+
snapshot_e.add_interval("2020-01-01", "2020-01-05")
3361+
3362+
_, auto_restatement_triggers = apply_auto_restatements(
3363+
{
3364+
snapshot_a.snapshot_id: snapshot_a,
3365+
snapshot_b.snapshot_id: snapshot_b,
3366+
snapshot_c.snapshot_id: snapshot_c,
3367+
snapshot_d.snapshot_id: snapshot_d,
3368+
snapshot_e.snapshot_id: snapshot_e,
3369+
},
3370+
"2020-01-06 10:01:00",
3371+
)
3372+
3373+
assert auto_restatement_triggers == {
3374+
snapshot_a.snapshot_id: [snapshot_a.snapshot_id],
3375+
snapshot_d.snapshot_id: [snapshot_d.snapshot_id],
3376+
snapshot_b.snapshot_id: [snapshot_a.snapshot_id],
3377+
snapshot_c.snapshot_id: [snapshot_c.snapshot_id, snapshot_a.snapshot_id],
3378+
snapshot_e.snapshot_id: [
3379+
snapshot_d.snapshot_id,
3380+
snapshot_c.snapshot_id,
3381+
snapshot_a.snapshot_id,
3382+
],
3383+
}
3384+
3385+
32813386
def test_render_signal(make_snapshot, mocker):
32823387
@signal()
32833388
def check_types(batch, env: str, sql: list[SQL], table: exp.Table, default: int = 0):

web/server/console.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ def update_snapshot_evaluation_progress(
142142
num_audits_passed: int,
143143
num_audits_failed: int,
144144
audit_only: bool = False,
145-
auto_restatement_trigger: t.Optional[SnapshotId] = None,
145+
auto_restatement_triggers: t.Optional[t.List[SnapshotId]] = None,
146146
) -> None:
147147
if audit_only:
148148
return

0 commit comments

Comments
 (0)