Skip to content

Commit 555d0c9

Browse files
committed
Print auto-restated trigger of model evaluation in debug console
1 parent 54320bd commit 555d0c9

File tree

5 files changed

+55
-19
lines changed

5 files changed

+55
-19
lines changed

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
@@ -2158,7 +2158,7 @@ def snapshots_to_dag(snapshots: t.Collection[Snapshot]) -> DAG[SnapshotId]:
21582158

21592159
def apply_auto_restatements(
21602160
snapshots: t.Dict[SnapshotId, Snapshot], execution_time: TimeLike
2161-
) -> t.List[SnapshotIntervals]:
2161+
) -> t.Tuple[t.List[SnapshotIntervals], t.Dict[SnapshotId, SnapshotId]]:
21622162
"""Applies auto restatements to the snapshots.
21632163
21642164
This operation results in the removal of intervals for snapshots that are ready to be restated based
@@ -2173,6 +2173,8 @@ def apply_auto_restatements(
21732173
A list of SnapshotIntervals with **new** intervals that need to be restated.
21742174
"""
21752175
dag = snapshots_to_dag(snapshots.values())
2176+
snapshots_with_auto_restatements: t.List[SnapshotId] = []
2177+
auto_restatement_triggers: t.Dict[SnapshotId, SnapshotId] = {}
21762178
auto_restated_intervals_per_snapshot: t.Dict[SnapshotId, Interval] = {}
21772179
for s_id in dag:
21782180
if s_id not in snapshots:
@@ -2196,6 +2198,23 @@ def apply_auto_restatements(
21962198
)
21972199
auto_restated_intervals.append(next_auto_restated_interval)
21982200

2201+
# auto-restated snapshot is its own trigger
2202+
snapshots_with_auto_restatements.append(s_id)
2203+
auto_restatement_triggers[s_id] = s_id
2204+
else:
2205+
for parent_s_id in snapshot.parents:
2206+
# first auto-restated parent is the trigger
2207+
if parent_s_id in snapshots_with_auto_restatements:
2208+
auto_restatement_triggers[s_id] = parent_s_id
2209+
break
2210+
# if no trigger yet and parent has trigger, inherit their trigger
2211+
# - will be overwritten if a different parent is auto-restated
2212+
if (
2213+
parent_s_id in auto_restatement_triggers
2214+
and s_id not in auto_restatement_triggers
2215+
):
2216+
auto_restatement_triggers[s_id] = auto_restatement_triggers[parent_s_id]
2217+
21992218
if auto_restated_intervals:
22002219
auto_restated_interval_start = sys.maxsize
22012220
auto_restated_interval_end = -sys.maxsize
@@ -2225,20 +2244,22 @@ def apply_auto_restatements(
22252244

22262245
snapshot.apply_pending_restatement_intervals()
22272246
snapshot.update_next_auto_restatement_ts(execution_time)
2228-
2229-
return [
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-
]
2247+
return (
2248+
[
2249+
SnapshotIntervals(
2250+
name=snapshots[s_id].name,
2251+
identifier=None,
2252+
version=snapshots[s_id].version,
2253+
dev_version=None,
2254+
intervals=[],
2255+
dev_intervals=[],
2256+
pending_restatement_intervals=[interval],
2257+
)
2258+
for s_id, interval in auto_restated_intervals_per_snapshot.items()
2259+
if s_id in snapshots
2260+
],
2261+
auto_restatement_triggers,
2262+
)
22422263

22432264

22442265
def parent_snapshots_by_name(

tests/core/test_snapshot.py

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

3102-
restated_intervals = apply_auto_restatements(
3102+
restated_intervals, _ = apply_auto_restatements(
31033103
{
31043104
snapshot_a.snapshot_id: snapshot_a,
31053105
snapshot_b.snapshot_id: snapshot_b,
@@ -3236,7 +3236,7 @@ def test_apply_auto_restatements_disable_restatement_downstream(make_snapshot):
32363236
snapshot_b.add_interval("2020-01-01", "2020-01-05")
32373237
assert snapshot_a.snapshot_id in snapshot_b.parents
32383238

3239-
restated_intervals = apply_auto_restatements(
3239+
restated_intervals, _ = apply_auto_restatements(
32403240
{
32413241
snapshot_a.snapshot_id: snapshot_a,
32423242
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)