Skip to content

Commit 14bbe78

Browse files
committed
Add triggers class and selected snapshot triggers
1 parent 091de2e commit 14bbe78

5 files changed

Lines changed: 80 additions & 19 deletions

File tree

sqlmesh/core/console.py

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,12 @@
3737
SnapshotId,
3838
SnapshotInfoLike,
3939
)
40-
from sqlmesh.core.snapshot.definition import Interval, Intervals, SnapshotTableInfo
40+
from sqlmesh.core.snapshot.definition import (
41+
Interval,
42+
Intervals,
43+
SnapshotTableInfo,
44+
SnapshotEvaluationTriggers,
45+
)
4146
from sqlmesh.core.test import ModelTest
4247
from sqlmesh.utils import rich as srich
4348
from sqlmesh.utils import Verbosity
@@ -428,7 +433,7 @@ def update_snapshot_evaluation_progress(
428433
num_audits_passed: int,
429434
num_audits_failed: int,
430435
audit_only: bool = False,
431-
auto_restatement_triggers: t.Optional[t.List[SnapshotId]] = None,
436+
snapshot_evaluation_triggers: t.Optional[SnapshotEvaluationTriggers] = None,
432437
) -> None:
433438
"""Updates the snapshot evaluation progress."""
434439

@@ -576,7 +581,7 @@ def update_snapshot_evaluation_progress(
576581
num_audits_passed: int,
577582
num_audits_failed: int,
578583
audit_only: bool = False,
579-
auto_restatement_triggers: t.Optional[t.List[SnapshotId]] = None,
584+
snapshot_evaluation_triggers: t.Optional[SnapshotEvaluationTriggers] = None,
580585
) -> None:
581586
pass
582587

@@ -1058,7 +1063,7 @@ def update_snapshot_evaluation_progress(
10581063
num_audits_passed: int,
10591064
num_audits_failed: int,
10601065
audit_only: bool = False,
1061-
auto_restatement_triggers: t.Optional[t.List[SnapshotId]] = None,
1066+
snapshot_evaluation_triggers: t.Optional[SnapshotEvaluationTriggers] = None,
10621067
) -> None:
10631068
"""Update the snapshot evaluation progress."""
10641069
if (
@@ -3642,7 +3647,7 @@ def update_snapshot_evaluation_progress(
36423647
num_audits_passed: int,
36433648
num_audits_failed: int,
36443649
audit_only: bool = False,
3645-
auto_restatement_triggers: t.Optional[t.List[SnapshotId]] = None,
3650+
snapshot_evaluation_triggers: t.Optional[SnapshotEvaluationTriggers] = None,
36463651
) -> None:
36473652
view_name, loaded_batches = self.evaluation_batch_progress[snapshot.snapshot_id]
36483653

@@ -3812,12 +3817,15 @@ def update_snapshot_evaluation_progress(
38123817
num_audits_passed: int,
38133818
num_audits_failed: int,
38143819
audit_only: bool = False,
3815-
auto_restatement_triggers: t.Optional[t.List[SnapshotId]] = None,
3820+
snapshot_evaluation_triggers: t.Optional[SnapshotEvaluationTriggers] = None,
38163821
) -> None:
38173822
message = f"Evaluating {snapshot.name} | batch={batch_idx} | duration={duration_ms}ms | num_audits_passed={num_audits_passed} | num_audits_failed={num_audits_failed}"
38183823

3819-
if auto_restatement_triggers:
3820-
message += f" | auto_restatement_triggers={','.join(trigger.name for trigger in auto_restatement_triggers)}"
3824+
if snapshot_evaluation_triggers:
3825+
if snapshot_evaluation_triggers.auto_restatement_triggers:
3826+
message += f" | auto_restatement_triggers={','.join(trigger.name for trigger in snapshot_evaluation_triggers.auto_restatement_triggers)}"
3827+
if snapshot_evaluation_triggers.select_snapshot_triggers:
3828+
message += f" | select_snapshot_triggers={','.join(trigger.name for trigger in snapshot_evaluation_triggers.select_snapshot_triggers)}"
38213829

38223830
if audit_only:
38233831
message = f"Auditing {snapshot.name} duration={duration_ms}ms | num_audits_passed={num_audits_passed} | num_audits_failed={num_audits_failed}"

sqlmesh/core/context.py

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

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

23082306
results = {}
23092307
execution_context = self.execution_context(snapshots=snapshots)
@@ -2453,8 +2451,9 @@ def _run(
24532451
scheduler = self.scheduler(environment=environment)
24542452
snapshots = scheduler.snapshots
24552453

2454+
select_models_auto_upstream = None
24562455
if select_models is not None:
2457-
select_models = self._select_models_for_run(
2456+
select_models, select_models_auto_upstream = self._select_models_for_run(
24582457
select_models, no_auto_upstream, snapshots.values()
24592458
)
24602459

@@ -2466,6 +2465,7 @@ def _run(
24662465
ignore_cron=ignore_cron,
24672466
circuit_breaker=circuit_breaker,
24682467
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.Set[str]:
2884+
) -> t.Tuple[t.Set[str], t.Set[str]]:
28852885
models: UniqueKeyDict[str, Model] = UniqueKeyDict(
28862886
"models", **{s.name: s.model for s in snapshots if s.is_model}
28872887
)
@@ -2891,8 +2891,8 @@ def _select_models_for_run(
28912891
model_selector = self._new_selector(models=models, dag=dag)
28922892
result = set(model_selector.expand_model_selections(select_models))
28932893
if not no_auto_upstream:
2894-
result = set(dag.subdag(*result))
2895-
return result
2894+
result_with_upstream = set(dag.subdag(*result))
2895+
return result, result_with_upstream - result
28962896

28972897
@cached_property
28982898
def _project_type(self) -> str:

sqlmesh/core/scheduler.py

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
from sqlmesh.core.snapshot.definition import check_ready_intervals
3030
from sqlmesh.core.snapshot.definition import (
3131
Interval,
32+
SnapshotEvaluationTriggers,
3233
expand_range,
3334
parent_snapshots_by_name,
3435
)
@@ -221,6 +222,7 @@ def run(
221222
ignore_cron: bool = False,
222223
end_bounded: bool = False,
223224
selected_snapshots: t.Optional[t.Set[str]] = None,
225+
selected_snapshots_auto_upstream: t.Optional[t.Set[str]] = None,
224226
circuit_breaker: t.Optional[t.Callable[[], bool]] = None,
225227
deployability_index: t.Optional[DeployabilityIndex] = None,
226228
auto_restatement_enabled: bool = False,
@@ -237,6 +239,7 @@ def run(
237239
ignore_cron=ignore_cron,
238240
end_bounded=end_bounded,
239241
selected_snapshots=selected_snapshots,
242+
selected_snapshots_auto_upstream=selected_snapshots_auto_upstream,
240243
circuit_breaker=circuit_breaker,
241244
deployability_index=deployability_index,
242245
auto_restatement_enabled=auto_restatement_enabled,
@@ -469,7 +472,9 @@ def evaluate_node(node: SchedulingUnit) -> None:
469472
evaluation_duration_ms,
470473
num_audits - num_audits_failed,
471474
num_audits_failed,
472-
auto_restatement_triggers=auto_restatement_triggers.get(snapshot.snapshot_id),
475+
snapshot_evaluation_triggers=snapshot_evaluation_triggers.get(
476+
snapshot.snapshot_id
477+
),
473478
)
474479

475480
try:
@@ -580,6 +585,7 @@ def _run_or_audit(
580585
ignore_cron: bool = False,
581586
end_bounded: bool = False,
582587
selected_snapshots: t.Optional[t.Set[str]] = None,
588+
selected_snapshots_auto_upstream: t.Optional[t.Set[str]] = None,
583589
circuit_breaker: t.Optional[t.Callable[[], bool]] = None,
584590
deployability_index: t.Optional[DeployabilityIndex] = None,
585591
auto_restatement_enabled: bool = False,
@@ -603,6 +609,7 @@ def _run_or_audit(
603609
end_bounded: If set to true, the evaluated intervals will be bounded by the target end date, disregarding lookback,
604610
allow_partials, and other attributes that could cause the intervals to exceed the target end date.
605611
selected_snapshots: A set of snapshot names to run. If not provided, all snapshots will be run.
612+
selected_snapshots_auto_upstream: The set of selected_snapshots that were automatically added because they're upstream of a selected snapshot.
606613
circuit_breaker: An optional handler which checks if the run should be aborted.
607614
deployability_index: Determines snapshots that are deployable in the context of this render.
608615
auto_restatement_enabled: Whether to enable auto restatements.
@@ -658,6 +665,42 @@ def _run_or_audit(
658665
if not merged_intervals:
659666
return CompletionStatus.NOTHING_TO_DO
660667

668+
merged_intervals_snapshots = {
669+
snapshot.snapshot_id: snapshot for snapshot in merged_intervals.keys()
670+
}
671+
select_snapshot_triggers: t.Dict[SnapshotId, t.List[SnapshotId]] = {}
672+
if selected_snapshots and selected_snapshots_auto_upstream:
673+
# actually selected snapshots are their own triggers
674+
selected_snapshots_no_auto_upstream = (
675+
selected_snapshots - selected_snapshots_auto_upstream
676+
)
677+
select_snapshot_triggers = {
678+
s_id: [s_id]
679+
for s_id in [
680+
snapshot_id
681+
for snapshot_id in merged_intervals_snapshots
682+
if snapshot_id.name in selected_snapshots_no_auto_upstream
683+
]
684+
}
685+
686+
# trace upstream by reversing dag of all snapshots to evaluate
687+
reversed_intervals_dag = snapshots_to_dag(merged_intervals_snapshots.values()).reversed
688+
for s_id in reversed_intervals_dag:
689+
if s_id not in select_snapshot_triggers:
690+
triggers = []
691+
for parent_s_id in merged_intervals_snapshots[s_id].parents:
692+
triggers.extend(select_snapshot_triggers[parent_s_id])
693+
select_snapshot_triggers[s_id] = list(dict.fromkeys(triggers))
694+
695+
all_snapshot_triggers: t.Dict[SnapshotId, SnapshotEvaluationTriggers] = {
696+
s_id: SnapshotEvaluationTriggers(
697+
ignore_cron=ignore_cron,
698+
auto_restatement_triggers=auto_restatement_triggers.get(s_id, []),
699+
select_snapshot_triggers=select_snapshot_triggers.get(s_id, []),
700+
)
701+
for s_id in merged_intervals_snapshots
702+
if ignore_cron or s_id in auto_restatement_triggers or s_id in select_snapshot_triggers
703+
}
661704
errors, _ = self.run_merged_intervals(
662705
merged_intervals=merged_intervals,
663706
deployability_index=deployability_index,
@@ -668,6 +711,7 @@ def _run_or_audit(
668711
end=end,
669712
run_environment_statements=run_environment_statements,
670713
audit_only=audit_only,
714+
restatements=remove_intervals,
671715
auto_restatement_triggers=auto_restatement_triggers,
672716
)
673717

sqlmesh/core/snapshot/definition.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,14 @@ def table_name_for_environment(
327327
return table
328328

329329

330+
class SnapshotEvaluationTriggers(PydanticModel):
331+
ignore_cron: bool
332+
auto_restatement_triggers: t.List[SnapshotId] = []
333+
select_snapshot_triggers: t.List[SnapshotId] = []
334+
directly_modified_triggers: t.List[SnapshotId] = []
335+
manual_restatement_triggers: t.List[SnapshotId] = []
336+
337+
330338
class SnapshotInfoMixin(ModelKindMixin):
331339
name: str
332340
dev_version_: t.Optional[str]

web/server/console.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
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, SnapshotId
12+
from sqlmesh.core.snapshot import Snapshot, SnapshotInfoLike, SnapshotTableInfo
13+
from sqlmesh.core.snapshot.definition import SnapshotEvaluationTriggers
1314
from sqlmesh.core.test import ModelTest
1415
from sqlmesh.core.test.result import ModelTextTestResult
1516
from sqlmesh.utils.date import now_timestamp
@@ -142,7 +143,7 @@ def update_snapshot_evaluation_progress(
142143
num_audits_passed: int,
143144
num_audits_failed: int,
144145
audit_only: bool = False,
145-
auto_restatement_triggers: t.Optional[t.List[SnapshotId]] = None,
146+
snapshot_evaluation_triggers: t.Optional[SnapshotEvaluationTriggers] = None,
146147
) -> None:
147148
if audit_only:
148149
return

0 commit comments

Comments
 (0)