Skip to content

Commit 1a1d253

Browse files
committed
Print auto-restated trigger of model evaluation in debug console
1 parent 055562b commit 1a1d253

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 (
@@ -3653,6 +3656,7 @@ def update_snapshot_evaluation_progress(
36533656
num_audits_passed: int,
36543657
num_audits_failed: int,
36553658
audit_only: bool = False,
3659+
auto_restatement_trigger: t.Optional[SnapshotId] = None,
36563660
) -> None:
36573661
view_name, loaded_batches = self.evaluation_batch_progress[snapshot.snapshot_id]
36583662

@@ -3822,9 +3826,13 @@ def update_snapshot_evaluation_progress(
38223826
num_audits_passed: int,
38233827
num_audits_failed: int,
38243828
audit_only: bool = False,
3829+
auto_restatement_trigger: t.Optional[SnapshotId] = None,
38253830
) -> None:
38263831
message = f"Evaluating {snapshot.name} | batch={batch_idx} | duration={duration_ms}ms | num_audits_passed={num_audits_passed} | num_audits_failed={num_audits_failed}"
38273832

3833+
if auto_restatement_trigger:
3834+
message += f" | evaluation_triggered_by={auto_restatement_trigger.name}"
3835+
38283836
if audit_only:
38293837
message = f"Auditing {snapshot.name} duration={duration_ms}ms | num_audits_passed={num_audits_passed} | num_audits_failed={num_audits_failed}"
38303838

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
@@ -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.List[SnapshotIntervals]:
2127+
) -> t.Tuple[t.List[SnapshotIntervals], t.Dict[SnapshotId, 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,6 +2139,8 @@ 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] = {}
21422144
auto_restated_intervals_per_snapshot: t.Dict[SnapshotId, Interval] = {}
21432145
for s_id in dag:
21442146
if s_id not in snapshots:
@@ -2162,6 +2164,23 @@ def apply_auto_restatements(
21622164
)
21632165
auto_restated_intervals.append(next_auto_restated_interval)
21642166

2167+
# 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]
2183+
21652184
if auto_restated_intervals:
21662185
auto_restated_interval_start = sys.maxsize
21672186
auto_restated_interval_end = -sys.maxsize
@@ -2191,20 +2210,22 @@ def apply_auto_restatements(
21912210

21922211
snapshot.apply_pending_restatement_intervals()
21932212
snapshot.update_next_auto_restatement_ts(execution_time)
2194-
2195-
return [
2196-
SnapshotIntervals(
2197-
name=snapshots[s_id].name,
2198-
identifier=None,
2199-
version=snapshots[s_id].version,
2200-
dev_version=None,
2201-
intervals=[],
2202-
dev_intervals=[],
2203-
pending_restatement_intervals=[interval],
2204-
)
2205-
for s_id, interval in auto_restated_intervals_per_snapshot.items()
2206-
if s_id in snapshots
2207-
]
2213+
return (
2214+
[
2215+
SnapshotIntervals(
2216+
name=snapshots[s_id].name,
2217+
identifier=None,
2218+
version=snapshots[s_id].version,
2219+
dev_version=None,
2220+
intervals=[],
2221+
dev_intervals=[],
2222+
pending_restatement_intervals=[interval],
2223+
)
2224+
for s_id, interval in auto_restated_intervals_per_snapshot.items()
2225+
if s_id in snapshots
2226+
],
2227+
auto_restatement_triggers,
2228+
)
22082229

22092230

22102231
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)