Skip to content

Commit 35e9e59

Browse files
committed
List all upstream auto-restated models
1 parent 1a1d253 commit 35e9e59

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 (
@@ -3656,7 +3656,7 @@ def update_snapshot_evaluation_progress(
36563656
num_audits_passed: int,
36573657
num_audits_failed: int,
36583658
audit_only: bool = False,
3659-
auto_restatement_trigger: t.Optional[SnapshotId] = None,
3659+
auto_restatement_triggers: t.Optional[t.List[SnapshotId]] = None,
36603660
) -> None:
36613661
view_name, loaded_batches = self.evaluation_batch_progress[snapshot.snapshot_id]
36623662

@@ -3826,12 +3826,12 @@ def update_snapshot_evaluation_progress(
38263826
num_audits_passed: int,
38273827
num_audits_failed: int,
38283828
audit_only: bool = False,
3829-
auto_restatement_trigger: t.Optional[SnapshotId] = None,
3829+
auto_restatement_triggers: t.Optional[t.List[SnapshotId]] = None,
38303830
) -> None:
38313831
message = f"Evaluating {snapshot.name} | batch={batch_idx} | duration={duration_ms}ms | num_audits_passed={num_audits_passed} | num_audits_failed={num_audits_failed}"
38323832

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

38363836
if audit_only:
38373837
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
@@ -2124,7 +2124,7 @@ def snapshots_to_dag(snapshots: t.Collection[Snapshot]) -> DAG[SnapshotId]:
21242124

21252125
def apply_auto_restatements(
21262126
snapshots: t.Dict[SnapshotId, Snapshot], execution_time: TimeLike
2127-
) -> t.Tuple[t.List[SnapshotIntervals], t.Dict[SnapshotId, SnapshotId]]:
2127+
) -> t.Tuple[t.List[SnapshotIntervals], t.Dict[SnapshotId, t.List[SnapshotId]]]:
21282128
"""Applies auto restatements to the snapshots.
21292129
21302130
This operation results in the removal of intervals for snapshots that are ready to be restated based
@@ -2139,8 +2139,7 @@ def apply_auto_restatements(
21392139
A list of SnapshotIntervals with **new** intervals that need to be restated.
21402140
"""
21412141
dag = snapshots_to_dag(snapshots.values())
2142-
snapshots_with_auto_restatements: t.List[SnapshotId] = []
2143-
auto_restatement_triggers: t.Dict[SnapshotId, SnapshotId] = {}
2142+
auto_restatement_triggers: t.Dict[SnapshotId, t.List[SnapshotId]] = {}
21442143
auto_restated_intervals_per_snapshot: t.Dict[SnapshotId, Interval] = {}
21452144
for s_id in dag:
21462145
if s_id not in snapshots:
@@ -2155,6 +2154,7 @@ def apply_auto_restatements(
21552154
for parent_s_id in snapshot.parents
21562155
if parent_s_id in auto_restated_intervals_per_snapshot
21572156
]
2157+
upstream_triggers = []
21582158
if next_auto_restated_interval:
21592159
logger.info(
21602160
"Calculated the next auto restated interval (%s, %s) for snapshot %s",
@@ -2165,21 +2165,15 @@ def apply_auto_restatements(
21652165
auto_restated_intervals.append(next_auto_restated_interval)
21662166

21672167
# auto-restated snapshot is its own trigger
2168-
snapshots_with_auto_restatements.append(s_id)
2169-
auto_restatement_triggers[s_id] = s_id
2170-
else:
2171-
for parent_s_id in snapshot.parents:
2172-
# first auto-restated parent is the trigger
2173-
if parent_s_id in snapshots_with_auto_restatements:
2174-
auto_restatement_triggers[s_id] = parent_s_id
2175-
break
2176-
# if no trigger yet and parent has trigger, inherit their trigger
2177-
# - will be overwritten if a different parent is auto-restated
2178-
if (
2179-
parent_s_id in auto_restatement_triggers
2180-
and s_id not in auto_restatement_triggers
2181-
):
2182-
auto_restatement_triggers[s_id] = auto_restatement_triggers[parent_s_id]
2168+
upstream_triggers = [s_id]
2169+
2170+
for parent_s_id in snapshot.parents:
2171+
if parent_s_id in auto_restatement_triggers:
2172+
upstream_triggers.extend(auto_restatement_triggers[parent_s_id])
2173+
2174+
# remove duplicate triggers
2175+
if upstream_triggers:
2176+
auto_restatement_triggers[s_id] = list(dict.fromkeys(upstream_triggers))
21832177

21842178
if auto_restated_intervals:
21852179
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
@@ -3274,6 +3274,111 @@ def test_apply_auto_restatements_disable_restatement_downstream(make_snapshot):
32743274
]
32753275

32763276

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