Skip to content

Commit bf90fc1

Browse files
committed
Print auto-restated trigger of model evaluation in debug console
1 parent 94b1357 commit bf90fc1

5 files changed

Lines changed: 55 additions & 19 deletions

File tree

sqlmesh/core/console.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -428,6 +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,
431432
) -> None:
432433
"""Updates the snapshot evaluation progress."""
433434

@@ -575,6 +576,7 @@ def update_snapshot_evaluation_progress(
575576
num_audits_passed: int,
576577
num_audits_failed: int,
577578
audit_only: bool = False,
579+
auto_restatement_trigger: t.Optional[SnapshotId] = None,
578580
) -> None:
579581
pass
580582

@@ -1056,6 +1058,7 @@ def update_snapshot_evaluation_progress(
10561058
num_audits_passed: int,
10571059
num_audits_failed: int,
10581060
audit_only: bool = False,
1061+
auto_restatement_trigger: t.Optional[SnapshotId] = None,
10591062
) -> None:
10601063
"""Update the snapshot evaluation progress."""
10611064
if (
@@ -3639,6 +3642,7 @@ def update_snapshot_evaluation_progress(
36393642
num_audits_passed: int,
36403643
num_audits_failed: int,
36413644
audit_only: bool = False,
3645+
auto_restatement_trigger: t.Optional[SnapshotId] = None,
36423646
) -> None:
36433647
view_name, loaded_batches = self.evaluation_batch_progress[snapshot.snapshot_id]
36443648

@@ -3808,9 +3812,13 @@ def update_snapshot_evaluation_progress(
38083812
num_audits_passed: int,
38093813
num_audits_failed: int,
38103814
audit_only: bool = False,
3815+
auto_restatement_trigger: t.Optional[SnapshotId] = None,
38113816
) -> None:
38123817
message = f"Evaluating {snapshot.name} | batch={batch_idx} | duration={duration_ms}ms | num_audits_passed={num_audits_passed} | num_audits_failed={num_audits_failed}"
38133818

3819+
if auto_restatement_trigger:
3820+
message += f" | evaluation_triggered_by={auto_restatement_trigger.name}"
3821+
38143822
if audit_only:
38153823
message = f"Auditing {snapshot.name} duration={duration_ms}ms | num_audits_passed={num_audits_passed} | num_audits_failed={num_audits_failed}"
38163824

sqlmesh/core/scheduler.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -374,6 +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] = {},
377378
) -> t.Tuple[t.List[NodeExecutionFailedError[SchedulingUnit]], t.List[SchedulingUnit]]:
378379
"""Runs precomputed batches of missing intervals.
379380
@@ -476,6 +477,7 @@ def evaluate_node(node: SchedulingUnit) -> None:
476477
evaluation_duration_ms,
477478
num_audits - num_audits_failed,
478479
num_audits_failed,
480+
auto_restatement_trigger=auto_restatement_triggers.get(snapshot.snapshot_id),
479481
)
480482

481483
try:
@@ -639,8 +641,11 @@ def _run_or_audit(
639641
for s_id, interval in (remove_intervals or {}).items():
640642
self.snapshots[s_id].remove_interval(interval)
641643

644+
auto_restatement_triggers: t.Dict[SnapshotId, SnapshotId] = {}
642645
if auto_restatement_enabled:
643-
auto_restated_intervals = apply_auto_restatements(self.snapshots, execution_time)
646+
auto_restated_intervals, auto_restatement_triggers = apply_auto_restatements(
647+
self.snapshots, execution_time
648+
)
644649
self.state_sync.add_snapshots_intervals(auto_restated_intervals)
645650
self.state_sync.update_auto_restatements(
646651
{s.name_version: s.next_auto_restatement_ts for s in self.snapshots.values()}
@@ -672,6 +677,7 @@ def _run_or_audit(
672677
run_environment_statements=run_environment_statements,
673678
audit_only=audit_only,
674679
restatements=remove_intervals,
680+
auto_restatement_triggers=auto_restatement_triggers,
675681
)
676682

677683
return CompletionStatus.FAILURE if errors else CompletionStatus.SUCCESS

sqlmesh/core/snapshot/definition.py

Lines changed: 36 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2139,7 +2139,7 @@ def snapshots_to_dag(snapshots: t.Collection[Snapshot]) -> DAG[SnapshotId]:
21392139

21402140
def apply_auto_restatements(
21412141
snapshots: t.Dict[SnapshotId, Snapshot], execution_time: TimeLike
2142-
) -> t.List[SnapshotIntervals]:
2142+
) -> t.Tuple[t.List[SnapshotIntervals], t.Dict[SnapshotId, SnapshotId]]:
21432143
"""Applies auto restatements to the snapshots.
21442144
21452145
This operation results in the removal of intervals for snapshots that are ready to be restated based
@@ -2154,6 +2154,8 @@ def apply_auto_restatements(
21542154
A list of SnapshotIntervals with **new** intervals that need to be restated.
21552155
"""
21562156
dag = snapshots_to_dag(snapshots.values())
2157+
snapshots_with_auto_restatements: t.List[SnapshotId] = []
2158+
auto_restatement_triggers: t.Dict[SnapshotId, SnapshotId] = {}
21572159
auto_restated_intervals_per_snapshot: t.Dict[SnapshotId, Interval] = {}
21582160
for s_id in dag:
21592161
if s_id not in snapshots:
@@ -2177,6 +2179,23 @@ def apply_auto_restatements(
21772179
)
21782180
auto_restated_intervals.append(next_auto_restated_interval)
21792181

2182+
# auto-restated snapshot is its own trigger
2183+
snapshots_with_auto_restatements.append(s_id)
2184+
auto_restatement_triggers[s_id] = s_id
2185+
else:
2186+
for parent_s_id in snapshot.parents:
2187+
# first auto-restated parent is the trigger
2188+
if parent_s_id in snapshots_with_auto_restatements:
2189+
auto_restatement_triggers[s_id] = parent_s_id
2190+
break
2191+
# if no trigger yet and parent has trigger, inherit their trigger
2192+
# - will be overwritten if a different parent is auto-restated
2193+
if (
2194+
parent_s_id in auto_restatement_triggers
2195+
and s_id not in auto_restatement_triggers
2196+
):
2197+
auto_restatement_triggers[s_id] = auto_restatement_triggers[parent_s_id]
2198+
21802199
if auto_restated_intervals:
21812200
auto_restated_interval_start = sys.maxsize
21822201
auto_restated_interval_end = -sys.maxsize
@@ -2206,20 +2225,22 @@ def apply_auto_restatements(
22062225

22072226
snapshot.apply_pending_restatement_intervals()
22082227
snapshot.update_next_auto_restatement_ts(execution_time)
2209-
2210-
return [
2211-
SnapshotIntervals(
2212-
name=snapshots[s_id].name,
2213-
identifier=None,
2214-
version=snapshots[s_id].version,
2215-
dev_version=None,
2216-
intervals=[],
2217-
dev_intervals=[],
2218-
pending_restatement_intervals=[interval],
2219-
)
2220-
for s_id, interval in auto_restated_intervals_per_snapshot.items()
2221-
if s_id in snapshots
2222-
]
2228+
return (
2229+
[
2230+
SnapshotIntervals(
2231+
name=snapshots[s_id].name,
2232+
identifier=None,
2233+
version=snapshots[s_id].version,
2234+
dev_version=None,
2235+
intervals=[],
2236+
dev_intervals=[],
2237+
pending_restatement_intervals=[interval],
2238+
)
2239+
for s_id, interval in auto_restated_intervals_per_snapshot.items()
2240+
if s_id in snapshots
2241+
],
2242+
auto_restatement_triggers,
2243+
)
22232244

22242245

22252246
def parent_snapshots_by_name(

tests/core/test_snapshot.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3097,7 +3097,7 @@ def test_apply_auto_restatements(make_snapshot):
30973097
(to_timestamp("2020-01-01"), to_timestamp("2020-01-06")),
30983098
]
30993099

3100-
restated_intervals = apply_auto_restatements(
3100+
restated_intervals, _ = apply_auto_restatements(
31013101
{
31023102
snapshot_a.snapshot_id: snapshot_a,
31033103
snapshot_b.snapshot_id: snapshot_b,
@@ -3234,7 +3234,7 @@ def test_apply_auto_restatements_disable_restatement_downstream(make_snapshot):
32343234
snapshot_b.add_interval("2020-01-01", "2020-01-05")
32353235
assert snapshot_a.snapshot_id in snapshot_b.parents
32363236

3237-
restated_intervals = apply_auto_restatements(
3237+
restated_intervals, _ = apply_auto_restatements(
32383238
{
32393239
snapshot_a.snapshot_id: snapshot_a,
32403240
snapshot_b.snapshot_id: snapshot_b,

web/server/console.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from sqlmesh.core.console import TerminalConsole
1010
from sqlmesh.core.environment import EnvironmentNamingInfo
1111
from sqlmesh.core.plan.definition import EvaluatablePlan
12-
from sqlmesh.core.snapshot import Snapshot, SnapshotInfoLike, SnapshotTableInfo
12+
from sqlmesh.core.snapshot import Snapshot, SnapshotInfoLike, SnapshotTableInfo, SnapshotId
1313
from sqlmesh.core.test import ModelTest
1414
from sqlmesh.core.test.result import ModelTextTestResult
1515
from sqlmesh.utils.date import now_timestamp
@@ -142,6 +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,
145146
) -> None:
146147
if audit_only:
147148
return

0 commit comments

Comments
 (0)