Skip to content

Commit 92f9448

Browse files
committed
List all upstream auto-restated models
1 parent 47141d7 commit 92f9448

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
@@ -371,7 +371,7 @@ def run_merged_intervals(
371371
end: t.Optional[TimeLike] = None,
372372
run_environment_statements: bool = False,
373373
audit_only: bool = False,
374-
auto_restatement_triggers: t.Dict[SnapshotId, SnapshotId] = {},
374+
auto_restatement_triggers: t.Dict[SnapshotId, t.List[SnapshotId]] = {},
375375
) -> t.Tuple[t.List[NodeExecutionFailedError[SchedulingUnit]], t.List[SchedulingUnit]]:
376376
"""Runs precomputed batches of missing intervals.
377377
@@ -469,7 +469,7 @@ def evaluate_node(node: SchedulingUnit) -> None:
469469
evaluation_duration_ms,
470470
num_audits - num_audits_failed,
471471
num_audits_failed,
472-
auto_restatement_trigger=auto_restatement_triggers.get(snapshot.snapshot_id),
472+
auto_restatement_triggers=auto_restatement_triggers.get(snapshot.snapshot_id),
473473
)
474474

475475
try:
@@ -633,7 +633,7 @@ def _run_or_audit(
633633
for s_id, interval in (remove_intervals or {}).items():
634634
self.snapshots[s_id].remove_interval(interval)
635635

636-
auto_restatement_triggers: t.Dict[SnapshotId, SnapshotId] = {}
636+
auto_restatement_triggers: t.Dict[SnapshotId, t.List[SnapshotId]] = {}
637637
if auto_restatement_enabled:
638638
auto_restated_intervals, auto_restatement_triggers = apply_auto_restatements(
639639
self.snapshots, execution_time

sqlmesh/core/snapshot/definition.py

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

21782178
def apply_auto_restatements(
21792179
snapshots: t.Dict[SnapshotId, Snapshot], execution_time: TimeLike
2180-
) -> t.Tuple[t.List[SnapshotIntervals], t.Dict[SnapshotId, SnapshotId]]:
2180+
) -> t.Tuple[t.List[SnapshotIntervals], t.Dict[SnapshotId, t.List[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,8 +2192,7 @@ 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] = {}
2195+
auto_restatement_triggers: t.Dict[SnapshotId, t.List[SnapshotId]] = {}
21972196
auto_restated_intervals_per_snapshot: t.Dict[SnapshotId, Interval] = {}
21982197
for s_id in dag:
21992198
if s_id not in snapshots:
@@ -2208,6 +2207,7 @@ def apply_auto_restatements(
22082207
for parent_s_id in snapshot.parents
22092208
if parent_s_id in auto_restated_intervals_per_snapshot
22102209
]
2210+
upstream_triggers = []
22112211
if next_auto_restated_interval:
22122212
logger.info(
22132213
"Calculated the next auto restated interval (%s, %s) for snapshot %s",
@@ -2218,21 +2218,15 @@ def apply_auto_restatements(
22182218
auto_restated_intervals.append(next_auto_restated_interval)
22192219

22202220
# 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]
2221+
upstream_triggers = [s_id]
2222+
2223+
for parent_s_id in snapshot.parents:
2224+
if parent_s_id in auto_restatement_triggers:
2225+
upstream_triggers.extend(auto_restatement_triggers[parent_s_id])
2226+
2227+
# remove duplicate triggers
2228+
if upstream_triggers:
2229+
auto_restatement_triggers[s_id] = list(dict.fromkeys(upstream_triggers))
22362230

22372231
if auto_restated_intervals:
22382232
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)