Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 33 additions & 1 deletion sqlmesh/core/plan/evaluator.py
Original file line number Diff line number Diff line change
Expand Up @@ -273,16 +273,48 @@ def visit_audit_only_run_stage(
if not audit_snapshots:
return

all_snapshots_by_id = {
**{s.snapshot_id: s for s in stage.all_snapshots.values()},
**{s.snapshot_id: s for s in audit_snapshots},
}
relevant_snapshots: t.Dict[SnapshotId, Snapshot] = {}
queue = [s.snapshot_id for s in audit_snapshots]
while queue:
s_id = queue.pop()
if s_id in relevant_snapshots or s_id not in all_snapshots_by_id:
continue
snapshot = all_snapshots_by_id[s_id]
relevant_snapshots[s_id] = snapshot
queue.extend(snapshot.parents)

# Copy only the audited snapshots and their upstream ancestors so scheduler.audit's state
# refresh cannot mutate shared Snapshot objects held by subsequent stages or
# CachingStateSync, and avoids refreshing unrelated project snapshots.
snapshots = [
s.copy(
update={
"intervals": s.intervals.copy(),
"dev_intervals": s.dev_intervals.copy(),
"pending_restatement_intervals": s.pending_restatement_intervals.copy(),
}
)
for s in relevant_snapshots.values()
]

# If there are any snapshots to be audited, we'll reuse the scheduler's internals to audit them
scheduler = self.create_scheduler(audit_snapshots, self.snapshot_evaluator)
scheduler = self.create_scheduler(snapshots, self.snapshot_evaluator)
completion_status = scheduler.audit(
plan.environment,
plan.start,
plan.end,
execution_time=plan.execution_time,
end_bounded=plan.end_bounded,
ignore_cron=plan.ignore_cron,
start_override_per_model=plan.start_override_per_model,
end_override_per_model=plan.end_override_per_model,
selected_snapshots={s.name for s in audit_snapshots},
deployability_index=stage.deployability_index,
circuit_breaker=self._circuit_breaker,
)

if completion_status.is_failure:
Expand Down
22 changes: 19 additions & 3 deletions sqlmesh/core/plan/stages.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,13 @@ class AuditOnlyRunStage:

Args:
snapshots: Snapshots to run audits for.
all_snapshots: All snapshots in the plan by name.
deployability_index: Deployability index for this stage.
"""

snapshots: t.List[Snapshot]
all_snapshots: t.Dict[str, Snapshot]
deployability_index: DeployabilityIndex


@dataclass
Expand Down Expand Up @@ -344,9 +348,15 @@ def build(self, plan: EvaluatablePlan) -> t.List[PlanStage]:
)
)

audit_only_snapshots = self._get_audit_only_snapshots(new_snapshots)
audit_only_snapshots = self._get_audit_only_snapshots(new_snapshots, deployability_index)
if audit_only_snapshots:
stages.append(AuditOnlyRunStage(snapshots=list(audit_only_snapshots.values())))
stages.append(
AuditOnlyRunStage(
snapshots=list(audit_only_snapshots.values()),
all_snapshots=snapshots_by_name,
deployability_index=deployability_index,
)
)

if missing_intervals_before_promote:
stages.append(
Expand Down Expand Up @@ -604,7 +614,9 @@ def _missing_intervals(
)

def _get_audit_only_snapshots(
self, new_snapshots: t.Dict[SnapshotId, Snapshot]
self,
new_snapshots: t.Dict[SnapshotId, Snapshot],
deployability_index: DeployabilityIndex,
) -> t.Dict[SnapshotId, Snapshot]:
metadata_snapshots = []
for snapshot in new_snapshots.values():
Expand All @@ -613,6 +625,10 @@ def _get_audit_only_snapshots(
or not snapshot.is_model
or not snapshot.evaluatable
or not snapshot.previous_version
or (
not deployability_index.is_representative(snapshot)
and not snapshot.dev_intervals
)
):
continue

Expand Down
48 changes: 32 additions & 16 deletions sqlmesh/core/scheduler.py
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,10 @@ def audit(
# by _run_or_audit as "missing intervals" to reuse the rest of it's logic
remove_intervals = {}
for snapshot in self.snapshots.values():
if not snapshot.evaluatable or (
selected_snapshots is not None and snapshot.name not in selected_snapshots
):
continue
removal_intervals = snapshot.get_removal_interval(
start, end, execution_time, is_preview=True
)
Expand Down Expand Up @@ -505,19 +509,22 @@ def run_merged_intervals(
selected_models=selected_models,
)

# We only need to create physical tables if the snapshot is not representative or if it
# needs backfill
snapshots_to_create_candidates = [
s
for s in selected_snapshots
if not deployability_index.is_representative(s) or s in batched_intervals
]
snapshots_to_create = {
s.snapshot_id
for s in self.snapshot_evaluator.get_snapshots_to_create(
snapshots_to_create_candidates, deployability_index
)
}
if audit_only:
snapshots_to_create: t.Set[SnapshotId] = set()
else:
# We only need to create physical tables if the snapshot is not representative or if it
# needs backfill
snapshots_to_create_candidates = [
s
for s in selected_snapshots
if not deployability_index.is_representative(s) or s in batched_intervals
]
snapshots_to_create = {
s.snapshot_id
for s in self.snapshot_evaluator.get_snapshots_to_create(
snapshots_to_create_candidates, deployability_index
)
}

dag = self._dag(
batched_intervals,
Expand Down Expand Up @@ -560,11 +567,19 @@ def execute_node(node: SchedulingUnit) -> None:
assert deployability_index # mypy

if audit_only:
audit_snapshots = {
snapshot.name: snapshot,
**{
self.snapshots[p_id].name: self.snapshots[p_id]
for p_id in snapshot.parents
if p_id in self.snapshots
},
}
audit_results = self._audit_snapshot(
snapshot=snapshot,
environment_naming_info=environment_naming_info,
deployability_index=deployability_index,
snapshots=self.snapshots_by_name,
snapshots=audit_snapshots,
start=start,
end=end,
execution_time=execution_time,
Expand Down Expand Up @@ -859,8 +874,9 @@ def _run_or_audit(
execution_time = execution_time or now_timestamp()

self.state_sync.refresh_snapshot_intervals(self.snapshots.values())
for s_id, interval in (remove_intervals or {}).items():
self.snapshots[s_id].remove_interval(interval)
if not audit_only:
for s_id, interval in (remove_intervals or {}).items():
self.snapshots[s_id].remove_interval(interval)

all_auto_restatement_triggers: t.Dict[SnapshotId, t.List[SnapshotId]] = {}
if auto_restatement_enabled:
Expand Down
Loading