Skip to content

Commit bbed5ac

Browse files
committed
Add triggers class and selected snapshot triggers
1 parent 30a6b73 commit bbed5ac

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
@@ -2307,11 +2307,9 @@ def check_intervals(
23072307
}
23082308

23092309
if select_models:
2310-
selected: t.Collection[str] = self._select_models_for_run(
2311-
select_models, True, snapshots.values()
2312-
)
2310+
selected, _ = self._select_models_for_run(select_models, True, snapshots.values())
23132311
else:
2314-
selected = snapshots.keys()
2312+
selected = t.cast(t.Set[str], snapshots.keys())
23152313

23162314
results = {}
23172315
execution_context = self.execution_context(snapshots=snapshots)
@@ -2461,8 +2459,9 @@ def _run(
24612459
scheduler = self.scheduler(environment=environment)
24622460
snapshots = scheduler.snapshots
24632461

2462+
select_models_auto_upstream = None
24642463
if select_models is not None:
2465-
select_models = self._select_models_for_run(
2464+
select_models, select_models_auto_upstream = self._select_models_for_run(
24662465
select_models, no_auto_upstream, snapshots.values()
24672466
)
24682467

@@ -2474,6 +2473,7 @@ def _run(
24742473
ignore_cron=ignore_cron,
24752474
circuit_breaker=circuit_breaker,
24762475
selected_snapshots=select_models,
2476+
selected_snapshots_auto_upstream=select_models_auto_upstream,
24772477
auto_restatement_enabled=environment.lower() == c.PROD,
24782478
run_environment_statements=True,
24792479
)
@@ -2889,7 +2889,7 @@ def _select_models_for_run(
28892889
select_models: t.Collection[str],
28902890
no_auto_upstream: bool,
28912891
snapshots: t.Collection[Snapshot],
2892-
) -> t.Set[str]:
2892+
) -> t.Tuple[t.Set[str], t.Set[str]]:
28932893
models: UniqueKeyDict[str, Model] = UniqueKeyDict(
28942894
"models", **{s.name: s.model for s in snapshots if s.is_model}
28952895
)
@@ -2899,8 +2899,8 @@ def _select_models_for_run(
28992899
model_selector = self._new_selector(models=models, dag=dag)
29002900
result = set(model_selector.expand_model_selections(select_models))
29012901
if not no_auto_upstream:
2902-
result = set(dag.subdag(*result))
2903-
return result
2902+
result_with_upstream = set(dag.subdag(*result))
2903+
return result, result_with_upstream - result
29042904

29052905
@cached_property
29062906
def _project_type(self) -> str:

sqlmesh/core/scheduler.py

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
from sqlmesh.core.snapshot.definition import check_ready_intervals
3232
from sqlmesh.core.snapshot.definition import (
3333
Interval,
34+
SnapshotEvaluationTriggers,
3435
expand_range,
3536
parent_snapshots_by_name,
3637
)
@@ -262,6 +263,7 @@ def run(
262263
ignore_cron: bool = False,
263264
end_bounded: bool = False,
264265
selected_snapshots: t.Optional[t.Set[str]] = None,
266+
selected_snapshots_auto_upstream: t.Optional[t.Set[str]] = None,
265267
circuit_breaker: t.Optional[t.Callable[[], bool]] = None,
266268
deployability_index: t.Optional[DeployabilityIndex] = None,
267269
auto_restatement_enabled: bool = False,
@@ -278,6 +280,7 @@ def run(
278280
ignore_cron=ignore_cron,
279281
end_bounded=end_bounded,
280282
selected_snapshots=selected_snapshots,
283+
selected_snapshots_auto_upstream=selected_snapshots_auto_upstream,
281284
circuit_breaker=circuit_breaker,
282285
deployability_index=deployability_index,
283286
auto_restatement_enabled=auto_restatement_enabled,
@@ -532,7 +535,9 @@ def run_node(node: SchedulingUnit) -> None:
532535
evaluation_duration_ms,
533536
num_audits - num_audits_failed,
534537
num_audits_failed,
535-
auto_restatement_triggers=auto_restatement_triggers.get(snapshot.snapshot_id),
538+
snapshot_evaluation_triggers=snapshot_evaluation_triggers.get(
539+
snapshot.snapshot_id
540+
),
536541
)
537542
elif isinstance(node, CreateNode):
538543
self.snapshot_evaluator.create_snapshot(
@@ -685,6 +690,7 @@ def _run_or_audit(
685690
ignore_cron: bool = False,
686691
end_bounded: bool = False,
687692
selected_snapshots: t.Optional[t.Set[str]] = None,
693+
selected_snapshots_auto_upstream: t.Optional[t.Set[str]] = None,
688694
circuit_breaker: t.Optional[t.Callable[[], bool]] = None,
689695
deployability_index: t.Optional[DeployabilityIndex] = None,
690696
auto_restatement_enabled: bool = False,
@@ -708,6 +714,7 @@ def _run_or_audit(
708714
end_bounded: If set to true, the evaluated intervals will be bounded by the target end date, disregarding lookback,
709715
allow_partials, and other attributes that could cause the intervals to exceed the target end date.
710716
selected_snapshots: A set of snapshot names to run. If not provided, all snapshots will be run.
717+
selected_snapshots_auto_upstream: The set of selected_snapshots that were automatically added because they're upstream of a selected snapshot.
711718
circuit_breaker: An optional handler which checks if the run should be aborted.
712719
deployability_index: Determines snapshots that are deployable in the context of this render.
713720
auto_restatement_enabled: Whether to enable auto restatements.
@@ -763,6 +770,42 @@ def _run_or_audit(
763770
if not merged_intervals:
764771
return CompletionStatus.NOTHING_TO_DO
765772

773+
merged_intervals_snapshots = {
774+
snapshot.snapshot_id: snapshot for snapshot in merged_intervals.keys()
775+
}
776+
select_snapshot_triggers: t.Dict[SnapshotId, t.List[SnapshotId]] = {}
777+
if selected_snapshots and selected_snapshots_auto_upstream:
778+
# actually selected snapshots are their own triggers
779+
selected_snapshots_no_auto_upstream = (
780+
selected_snapshots - selected_snapshots_auto_upstream
781+
)
782+
select_snapshot_triggers = {
783+
s_id: [s_id]
784+
for s_id in [
785+
snapshot_id
786+
for snapshot_id in merged_intervals_snapshots
787+
if snapshot_id.name in selected_snapshots_no_auto_upstream
788+
]
789+
}
790+
791+
# trace upstream by reversing dag of all snapshots to evaluate
792+
reversed_intervals_dag = snapshots_to_dag(merged_intervals_snapshots.values()).reversed
793+
for s_id in reversed_intervals_dag:
794+
if s_id not in select_snapshot_triggers:
795+
triggers = []
796+
for parent_s_id in merged_intervals_snapshots[s_id].parents:
797+
triggers.extend(select_snapshot_triggers[parent_s_id])
798+
select_snapshot_triggers[s_id] = list(dict.fromkeys(triggers))
799+
800+
all_snapshot_triggers: t.Dict[SnapshotId, SnapshotEvaluationTriggers] = {
801+
s_id: SnapshotEvaluationTriggers(
802+
ignore_cron=ignore_cron,
803+
auto_restatement_triggers=auto_restatement_triggers.get(s_id, []),
804+
select_snapshot_triggers=select_snapshot_triggers.get(s_id, []),
805+
)
806+
for s_id in merged_intervals_snapshots
807+
if ignore_cron or s_id in auto_restatement_triggers or s_id in select_snapshot_triggers
808+
}
766809
errors, _ = self.run_merged_intervals(
767810
merged_intervals=merged_intervals,
768811
deployability_index=deployability_index,
@@ -773,6 +816,7 @@ def _run_or_audit(
773816
end=end,
774817
run_environment_statements=run_environment_statements,
775818
audit_only=audit_only,
819+
restatements=remove_intervals,
776820
auto_restatement_triggers=auto_restatement_triggers,
777821
)
778822

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)