Skip to content

Commit f63da5d

Browse files
committed
Report auto-restatement triggers only
1 parent d3fee71 commit f63da5d

File tree

9 files changed

+96
-147
lines changed

9 files changed

+96
-147
lines changed

sqlmesh/core/console.py

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,7 @@
3737
SnapshotId,
3838
SnapshotInfoLike,
3939
)
40-
from sqlmesh.core.snapshot.definition import (
41-
Interval,
42-
Intervals,
43-
SnapshotTableInfo,
44-
SnapshotEvaluationTriggers,
45-
)
40+
from sqlmesh.core.snapshot.definition import Interval, Intervals, SnapshotTableInfo
4641
from sqlmesh.core.test import ModelTest
4742
from sqlmesh.utils import rich as srich
4843
from sqlmesh.utils import Verbosity
@@ -433,7 +428,7 @@ def update_snapshot_evaluation_progress(
433428
num_audits_passed: int,
434429
num_audits_failed: int,
435430
audit_only: bool = False,
436-
snapshot_evaluation_triggers: t.Optional[SnapshotEvaluationTriggers] = None,
431+
auto_restatement_triggers: t.Optional[t.List[SnapshotId]] = None,
437432
) -> None:
438433
"""Updates the snapshot evaluation progress."""
439434

@@ -581,7 +576,7 @@ def update_snapshot_evaluation_progress(
581576
num_audits_passed: int,
582577
num_audits_failed: int,
583578
audit_only: bool = False,
584-
snapshot_evaluation_triggers: t.Optional[SnapshotEvaluationTriggers] = None,
579+
auto_restatement_triggers: t.Optional[t.List[SnapshotId]] = None,
585580
) -> None:
586581
pass
587582

@@ -1063,7 +1058,7 @@ def update_snapshot_evaluation_progress(
10631058
num_audits_passed: int,
10641059
num_audits_failed: int,
10651060
audit_only: bool = False,
1066-
snapshot_evaluation_triggers: t.Optional[SnapshotEvaluationTriggers] = None,
1061+
auto_restatement_triggers: t.Optional[t.List[SnapshotId]] = None,
10671062
) -> None:
10681063
"""Update the snapshot evaluation progress."""
10691064
if (
@@ -3647,7 +3642,7 @@ def update_snapshot_evaluation_progress(
36473642
num_audits_passed: int,
36483643
num_audits_failed: int,
36493644
audit_only: bool = False,
3650-
snapshot_evaluation_triggers: t.Optional[SnapshotEvaluationTriggers] = None,
3645+
auto_restatement_triggers: t.Optional[t.List[SnapshotId]] = None,
36513646
) -> None:
36523647
view_name, loaded_batches = self.evaluation_batch_progress[snapshot.snapshot_id]
36533648

@@ -3817,22 +3812,15 @@ def update_snapshot_evaluation_progress(
38173812
num_audits_passed: int,
38183813
num_audits_failed: int,
38193814
audit_only: bool = False,
3820-
snapshot_evaluation_triggers: t.Optional[SnapshotEvaluationTriggers] = None,
3815+
auto_restatement_triggers: t.Optional[t.List[SnapshotId]] = None,
38213816
) -> None:
38223817
message = f"Evaluated {snapshot.name} | batch={batch_idx} | duration={duration_ms}ms | num_audits_passed={num_audits_passed} | num_audits_failed={num_audits_failed}"
38233818

3824-
if snapshot_evaluation_triggers:
3825-
if snapshot_evaluation_triggers.ignore_cron_flag is not None:
3826-
message += f" | ignore_cron_flag={snapshot_evaluation_triggers.ignore_cron_flag}"
3827-
if snapshot_evaluation_triggers.cron_ready is not None:
3828-
message += f" | cron_ready={snapshot_evaluation_triggers.cron_ready}"
3829-
if snapshot_evaluation_triggers.auto_restatement_triggers:
3830-
message += f" | auto_restatement_triggers={','.join(trigger.name for trigger in snapshot_evaluation_triggers.auto_restatement_triggers)}"
3831-
if snapshot_evaluation_triggers.select_snapshot_triggers:
3832-
message += f" | select_snapshot_triggers={','.join(trigger.name for trigger in snapshot_evaluation_triggers.select_snapshot_triggers)}"
3819+
if auto_restatement_triggers:
3820+
message += f" | Auto-restatement triggers {', '.join(trigger.name for trigger in auto_restatement_triggers)}"
38333821

38343822
if audit_only:
3835-
message = f"Audited {snapshot.name} duration={duration_ms}ms | num_audits_passed={num_audits_passed} | num_audits_failed={num_audits_failed}"
3823+
message = f"Audited {snapshot.name} | duration={duration_ms}ms | num_audits_passed={num_audits_passed} | num_audits_failed={num_audits_failed}"
38363824

38373825
self._write(message)
38383826

sqlmesh/core/context.py

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2299,9 +2299,11 @@ def check_intervals(
22992299
}
23002300

23012301
if select_models:
2302-
selected, _ = self._select_models_for_run(select_models, True, snapshots.values())
2302+
selected: t.Collection[str] = self._select_models_for_run(
2303+
select_models, True, snapshots.values()
2304+
)
23032305
else:
2304-
selected = set(snapshots.keys())
2306+
selected = snapshots.keys()
23052307

23062308
results = {}
23072309
execution_context = self.execution_context(snapshots=snapshots)
@@ -2451,9 +2453,8 @@ def _run(
24512453
scheduler = self.scheduler(environment=environment)
24522454
snapshots = scheduler.snapshots
24532455

2454-
select_models_auto_upstream = None
24552456
if select_models is not None:
2456-
select_models, select_models_auto_upstream = self._select_models_for_run(
2457+
select_models = self._select_models_for_run(
24572458
select_models, no_auto_upstream, snapshots.values()
24582459
)
24592460

@@ -2465,7 +2466,6 @@ def _run(
24652466
ignore_cron=ignore_cron,
24662467
circuit_breaker=circuit_breaker,
24672468
selected_snapshots=select_models,
2468-
selected_snapshots_auto_upstream=select_models_auto_upstream,
24692469
auto_restatement_enabled=environment.lower() == c.PROD,
24702470
run_environment_statements=True,
24712471
)
@@ -2881,7 +2881,7 @@ def _select_models_for_run(
28812881
select_models: t.Collection[str],
28822882
no_auto_upstream: bool,
28832883
snapshots: t.Collection[Snapshot],
2884-
) -> t.Tuple[t.Set[str], t.Set[str]]:
2884+
) -> t.Set[str]:
28852885
models: UniqueKeyDict[str, Model] = UniqueKeyDict(
28862886
"models", **{s.name: s.model for s in snapshots if s.is_model}
28872887
)
@@ -2890,10 +2890,9 @@ def _select_models_for_run(
28902890
dag.add(fqn, model.depends_on)
28912891
model_selector = self._new_selector(models=models, dag=dag)
28922892
result = set(model_selector.expand_model_selections(select_models))
2893-
if no_auto_upstream:
2894-
return result, set()
2895-
result_with_upstream = set(dag.subdag(*result))
2896-
return result_with_upstream, result_with_upstream - result
2893+
if not no_auto_upstream:
2894+
result = set(dag.subdag(*result))
2895+
return result
28972896

28982897
@cached_property
28992898
def _project_type(self) -> str:

sqlmesh/core/plan/stages.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -513,7 +513,7 @@ def _missing_intervals(
513513
snapshots_by_name: t.Dict[str, Snapshot],
514514
deployability_index: DeployabilityIndex,
515515
) -> SnapshotToIntervals:
516-
missing_intervals = merged_missing_intervals(
516+
return merged_missing_intervals(
517517
snapshots=snapshots_by_name.values(),
518518
start=plan.start,
519519
end=plan.end,
@@ -527,7 +527,6 @@ def _missing_intervals(
527527
start_override_per_model=plan.start_override_per_model,
528528
end_override_per_model=plan.end_override_per_model,
529529
)
530-
return missing_intervals
531530

532531
def _get_audit_only_snapshots(
533532
self, new_snapshots: t.Dict[SnapshotId, Snapshot]

sqlmesh/core/scheduler.py

Lines changed: 5 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,10 @@
2626
snapshots_to_dag,
2727
Intervals,
2828
)
29+
from sqlmesh.core.snapshot.definition import check_ready_intervals
2930
from sqlmesh.core.snapshot.definition import (
3031
Interval,
31-
SnapshotEvaluationTriggers,
3232
SnapshotIntervals,
33-
check_ready_intervals,
3433
expand_range,
3534
parent_snapshots_by_name,
3635
)
@@ -133,9 +132,6 @@ def merged_missing_intervals(
133132
end_bounded: If set to true, the returned intervals will be bounded by the target end date, disregarding lookback,
134133
allow_partials, and other attributes that could cause the intervals to exceed the target end date.
135134
selected_snapshots: A set of snapshot names to run. If not provided, all snapshots will be run.
136-
137-
Returns:
138-
A dict containing all snapshots needing to be run with their associated interval params.
139135
"""
140136
snapshots_to_intervals = merged_missing_intervals(
141137
snapshots=self.snapshot_per_version.values(),
@@ -226,7 +222,6 @@ def run(
226222
ignore_cron: bool = False,
227223
end_bounded: bool = False,
228224
selected_snapshots: t.Optional[t.Set[str]] = None,
229-
selected_snapshots_auto_upstream: t.Optional[t.Set[str]] = None,
230225
circuit_breaker: t.Optional[t.Callable[[], bool]] = None,
231226
deployability_index: t.Optional[DeployabilityIndex] = None,
232227
auto_restatement_enabled: bool = False,
@@ -243,7 +238,6 @@ def run(
243238
ignore_cron=ignore_cron,
244239
end_bounded=end_bounded,
245240
selected_snapshots=selected_snapshots,
246-
selected_snapshots_auto_upstream=selected_snapshots_auto_upstream,
247241
circuit_breaker=circuit_breaker,
248242
deployability_index=deployability_index,
249243
auto_restatement_enabled=auto_restatement_enabled,
@@ -378,6 +372,7 @@ def run_merged_intervals(
378372
end: t.Optional[TimeLike] = None,
379373
run_environment_statements: bool = False,
380374
audit_only: bool = False,
375+
restatements: t.Optional[t.Dict[SnapshotId, Interval]] = None,
381376
auto_restatement_triggers: t.Dict[SnapshotId, t.List[SnapshotId]] = {},
382377
) -> t.Tuple[t.List[NodeExecutionFailedError[SchedulingUnit]], t.List[SchedulingUnit]]:
383378
"""Runs precomputed batches of missing intervals.
@@ -476,9 +471,7 @@ def evaluate_node(node: SchedulingUnit) -> None:
476471
evaluation_duration_ms,
477472
num_audits - num_audits_failed,
478473
num_audits_failed,
479-
snapshot_evaluation_triggers=snapshot_evaluation_triggers.get(
480-
snapshot.snapshot_id
481-
),
474+
auto_restatement_triggers=auto_restatement_triggers.get(snapshot.snapshot_id),
482475
)
483476

484477
try:
@@ -589,7 +582,6 @@ def _run_or_audit(
589582
ignore_cron: bool = False,
590583
end_bounded: bool = False,
591584
selected_snapshots: t.Optional[t.Set[str]] = None,
592-
selected_snapshots_auto_upstream: t.Optional[t.Set[str]] = None,
593585
circuit_breaker: t.Optional[t.Callable[[], bool]] = None,
594586
deployability_index: t.Optional[DeployabilityIndex] = None,
595587
auto_restatement_enabled: bool = False,
@@ -613,7 +605,6 @@ def _run_or_audit(
613605
end_bounded: If set to true, the evaluated intervals will be bounded by the target end date, disregarding lookback,
614606
allow_partials, and other attributes that could cause the intervals to exceed the target end date.
615607
selected_snapshots: A set of snapshot names to run. If not provided, all snapshots will be run.
616-
selected_snapshots_auto_upstream: The set of selected_snapshots that were automatically added because they're upstream of a selected snapshot.
617608
circuit_breaker: An optional handler which checks if the run should be aborted.
618609
deployability_index: Determines snapshots that are deployable in the context of this render.
619610
auto_restatement_enabled: Whether to enable auto restatements.
@@ -672,38 +663,9 @@ def _run_or_audit(
672663
return CompletionStatus.NOTHING_TO_DO
673664

674665
merged_intervals_snapshots = {snapshot.snapshot_id for snapshot in merged_intervals}
675-
select_snapshot_triggers: t.Dict[SnapshotId, t.List[SnapshotId]] = {}
676-
if selected_snapshots and selected_snapshots_auto_upstream:
677-
# actually selected snapshots are their own triggers
678-
selected_snapshots_no_auto_upstream = (
679-
selected_snapshots - selected_snapshots_auto_upstream
680-
)
681-
select_snapshot_triggers = {
682-
s_id: [s_id]
683-
for s_id in [
684-
snapshot_id
685-
for snapshot_id in merged_intervals_snapshots
686-
if snapshot_id.name in selected_snapshots_no_auto_upstream
687-
]
688-
}
689666

690-
# trace upstream by walking downstream on reversed dag
691-
reversed_dag = snapshots_to_dag(self.snapshots.values()).reversed
692-
for s_id in reversed_dag:
693-
if s_id in merged_intervals_snapshots:
694-
triggers = select_snapshot_triggers.get(s_id, [])
695-
for parent_s_id in reversed_dag.graph.get(s_id, set()):
696-
triggers.extend(select_snapshot_triggers.get(parent_s_id, []))
697-
select_snapshot_triggers[s_id] = list(dict.fromkeys(triggers))
698-
699-
all_snapshot_triggers: t.Dict[SnapshotId, SnapshotEvaluationTriggers] = {
700-
s_id: SnapshotEvaluationTriggers(
701-
ignore_cron_flag=ignore_cron,
702-
cron_ready=s_id not in auto_restated_snapshots,
703-
auto_restatement_triggers=auto_restatement_triggers.get(s_id, []),
704-
select_snapshot_triggers=select_snapshot_triggers.get(s_id, []),
705-
)
706-
for s_id in merged_intervals_snapshots
667+
auto_restatement_triggers_dict: t.Dict[SnapshotId, t.List[SnapshotId]] = {
668+
s_id: auto_restatement_triggers.get(s_id, []) for s_id in merged_intervals_snapshots
707669
}
708670

709671
errors, _ = self.run_merged_intervals(

sqlmesh/core/snapshot/definition.py

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
from sqlmesh.core.model import Model, ModelKindMixin, ModelKindName, ViewKind, CustomKind
2222
from sqlmesh.core.model.definition import _Model
2323
from sqlmesh.core.node import IntervalUnit, NodeType
24-
from sqlmesh.utils import sanitize_name
24+
from sqlmesh.utils import sanitize_name, unique
2525
from sqlmesh.utils.dag import DAG
2626
from sqlmesh.utils.date import (
2727
TimeLike,
@@ -327,13 +327,6 @@ def table_name_for_environment(
327327
return table
328328

329329

330-
class SnapshotEvaluationTriggers(PydanticModel):
331-
ignore_cron_flag: t.Optional[bool] = None
332-
cron_ready: t.Optional[bool] = None
333-
auto_restatement_triggers: t.List[SnapshotId] = []
334-
select_snapshot_triggers: t.List[SnapshotId] = []
335-
336-
337330
class SnapshotInfoMixin(ModelKindMixin):
338331
name: str
339332
dev_version_: t.Optional[str]
@@ -2226,14 +2219,15 @@ def apply_auto_restatements(
22262219

22272220
# auto-restated snapshot is its own trigger
22282221
upstream_triggers = [s_id]
2222+
else:
2223+
# inherit each parent's auto-restatement triggers (if any)
2224+
for parent_s_id in snapshot.parents:
2225+
if parent_s_id in auto_restatement_triggers:
2226+
upstream_triggers.extend(auto_restatement_triggers[parent_s_id])
22292227

2230-
for parent_s_id in snapshot.parents:
2231-
if parent_s_id in auto_restatement_triggers:
2232-
upstream_triggers.extend(auto_restatement_triggers[parent_s_id])
2233-
2234-
# remove duplicate triggers
2228+
# remove duplicate triggers, retaining order and keeping first seen of duplicates
22352229
if upstream_triggers:
2236-
auto_restatement_triggers[s_id] = list(dict.fromkeys(upstream_triggers))
2230+
auto_restatement_triggers[s_id] = unique(upstream_triggers)
22372231

22382232
if auto_restated_intervals:
22392233
auto_restated_interval_start = sys.maxsize

0 commit comments

Comments
 (0)