Skip to content

Commit 1234946

Browse files
committed
fix: catch up no-gaps plans before promotion
Signed-off-by: mday-io <mdaytn@gmail.com>
1 parent f72a858 commit 1234946

6 files changed

Lines changed: 191 additions & 36 deletions

File tree

‎sqlmesh/core/plan/evaluator.py‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
SnapshotId,
3535
SnapshotInfoLike,
3636
SnapshotCreationFailedError,
37+
missing_intervals_for_no_gaps,
3738
)
3839
from sqlmesh.utils import to_snake_case
3940
from sqlmesh.core.state_sync import StateSync
@@ -363,6 +364,44 @@ def visit_restatement_stage(
363364
def visit_environment_record_update_stage(
364365
self, stage: stages.EnvironmentRecordUpdateStage, plan: EvaluatablePlan
365366
) -> None:
367+
if (
368+
plan.no_gaps
369+
and stage.no_gaps_snapshot_names
370+
and not plan.is_dev
371+
and not plan.skip_backfill
372+
and not plan.empty_backfill
373+
):
374+
# A plan's initial backfill frontier is frozen when the plan is built, but the
375+
# target environment can advance before that plan reaches promotion.
376+
target_environment = self.state_sync.get_environment(plan.environment.name)
377+
if target_environment:
378+
target_snapshots = list(stage.all_snapshots.values())
379+
self.state_sync.refresh_snapshot_intervals(target_snapshots)
380+
381+
previous_snapshot_infos = [
382+
snapshot
383+
for snapshot in target_environment.snapshots
384+
if snapshot.name in stage.no_gaps_snapshot_names
385+
]
386+
previous_snapshots = self.state_sync.get_snapshots(previous_snapshot_infos).values()
387+
snapshot_to_intervals = missing_intervals_for_no_gaps(
388+
target_snapshots,
389+
previous_snapshots,
390+
stage.no_gaps_snapshot_names,
391+
)
392+
if snapshot_to_intervals:
393+
self.visit_backfill_stage(
394+
stages.BackfillStage(
395+
snapshot_to_intervals=snapshot_to_intervals,
396+
selected_snapshot_ids=stage.selected_snapshot_ids,
397+
all_snapshots=stage.all_snapshots,
398+
deployability_index=stage.deployability_index,
399+
),
400+
plan,
401+
)
402+
403+
# Promotion performs the same no-gaps check again, so a target that advances after
404+
# the catch-up read still fails safely instead of being promoted with a gap.
366405
self.state_sync.promote(
367406
plan.environment,
368407
no_gaps_snapshot_names=stage.no_gaps_snapshot_names if plan.no_gaps else set(),

‎sqlmesh/core/plan/stages.py‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,9 +141,15 @@ class EnvironmentRecordUpdateStage:
141141
142142
Args:
143143
no_gaps_snapshot_names: Names of snapshots for which there should be no interval gaps.
144+
all_snapshots: All snapshots in the plan by name.
145+
selected_snapshot_ids: The snapshots to include in a just-in-time no-gaps backfill.
146+
deployability_index: Deployability index for a just-in-time no-gaps backfill.
144147
"""
145148

146149
no_gaps_snapshot_names: t.Set[str]
150+
all_snapshots: t.Dict[str, Snapshot]
151+
selected_snapshot_ids: t.Set[SnapshotId]
152+
deployability_index: DeployabilityIndex
147153

148154

149155
@dataclass
@@ -370,7 +376,10 @@ def build(self, plan: EvaluatablePlan) -> t.List[PlanStage]:
370376

371377
stages.append(
372378
EnvironmentRecordUpdateStage(
373-
no_gaps_snapshot_names={s.name for s in before_promote_snapshots}
379+
no_gaps_snapshot_names={s.name for s in before_promote_snapshots},
380+
all_snapshots=snapshots_by_name,
381+
selected_snapshot_ids=before_promote_snapshots,
382+
deployability_index=deployability_index,
374383
)
375384
)
376385

‎sqlmesh/core/snapshot/__init__.py‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
has_paused_forward_only as has_paused_forward_only,
2525
merge_intervals as merge_intervals,
2626
missing_intervals as missing_intervals,
27+
missing_intervals_for_no_gaps as missing_intervals_for_no_gaps,
2728
snapshots_to_dag as snapshots_to_dag,
2829
start_date as start_date,
2930
table_name as table_name,

‎sqlmesh/core/snapshot/definition.py‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2314,6 +2314,38 @@ def start_date(
23142314
return earliest
23152315

23162316

2317+
def missing_intervals_for_no_gaps(
2318+
target_snapshots: t.Iterable[Snapshot],
2319+
previous_snapshots: t.Iterable[Snapshot],
2320+
snapshot_names: t.Optional[t.Set[str]] = None,
2321+
) -> t.Dict[Snapshot, Intervals]:
2322+
"""Find intervals target snapshots need to reach the previous snapshots' frontiers."""
2323+
target_snapshots_by_name = {snapshot.name: snapshot for snapshot in target_snapshots}
2324+
cache: t.Dict[str, datetime] = {}
2325+
missing_intervals_by_snapshot: t.Dict[Snapshot, Intervals] = {}
2326+
2327+
for previous_snapshot in previous_snapshots:
2328+
target_snapshot = target_snapshots_by_name.get(previous_snapshot.name)
2329+
if (
2330+
target_snapshot is None
2331+
or target_snapshot.version == previous_snapshot.version
2332+
or (snapshot_names is not None and previous_snapshot.name not in snapshot_names)
2333+
or not target_snapshot.is_incremental
2334+
or not previous_snapshot.is_incremental
2335+
or not previous_snapshot.intervals
2336+
):
2337+
continue
2338+
2339+
start = to_timestamp(start_date(target_snapshot, target_snapshots_by_name.values(), cache))
2340+
end = previous_snapshot.intervals[-1][1]
2341+
if start < end:
2342+
missing_intervals = target_snapshot.missing_intervals(start, end, end_bounded=True)
2343+
if missing_intervals:
2344+
missing_intervals_by_snapshot[target_snapshot] = missing_intervals
2345+
2346+
return missing_intervals_by_snapshot
2347+
2348+
23172349
def snapshots_to_dag(snapshots: t.Collection[Snapshot]) -> DAG[SnapshotId]:
23182350
dag: DAG[SnapshotId] = DAG()
23192351
for snapshot in snapshots:

‎sqlmesh/core/state_sync/db/facade.py‎

Lines changed: 16 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import logging
2121
import typing as t
2222
from pathlib import Path
23-
from datetime import datetime
2423

2524

2625
from sqlmesh.core.console import Console, get_console
@@ -36,7 +35,7 @@
3635
SnapshotIntervals,
3736
SnapshotNameVersion,
3837
SnapshotTableInfo,
39-
start_date,
38+
missing_intervals_for_no_gaps,
4039
)
4140
from sqlmesh.core.snapshot.definition import (
4241
Interval,
@@ -62,7 +61,7 @@
6261
from sqlmesh.core.state_sync.db.snapshot import SnapshotState
6362
from sqlmesh.core.state_sync.db.version import VersionState
6463
from sqlmesh.core.state_sync.db.migrator import StateMigrator, _backup_table_name
65-
from sqlmesh.utils.date import TimeLike, to_timestamp, time_like_to_str, now_timestamp
64+
from sqlmesh.utils.date import TimeLike, time_like_to_str, now_timestamp
6665
from sqlmesh.utils.errors import ConflictingPlanError, SQLMeshError
6766

6867
logger = logging.getLogger(__name__)
@@ -586,43 +585,25 @@ def _ensure_no_gaps(
586585
target_environment: Environment,
587586
snapshot_names: t.Optional[t.Set[str]],
588587
) -> None:
588+
target_snapshots = list(target_snapshots)
589589
target_snapshots_by_name = {s.name: s for s in target_snapshots}
590-
591-
changed_version_prev_snapshots_by_name = {
592-
s.name: s
590+
changed_version_prev_snapshots = [
591+
s
593592
for s in target_environment.snapshots
594593
if s.name in target_snapshots_by_name
595594
and target_snapshots_by_name[s.name].version != s.version
596-
}
595+
]
596+
previous_snapshots = self.get_snapshots(changed_version_prev_snapshots).values()
597597

598-
prev_snapshots = self.get_snapshots(
599-
changed_version_prev_snapshots_by_name.values()
600-
).values()
601-
cache: t.Dict[str, datetime] = {}
602-
603-
for prev_snapshot in prev_snapshots:
604-
target_snapshot = target_snapshots_by_name[prev_snapshot.name]
605-
if (
606-
(snapshot_names is None or prev_snapshot.name in snapshot_names)
607-
and target_snapshot.is_incremental
608-
and prev_snapshot.is_incremental
609-
and prev_snapshot.intervals
610-
):
611-
start = to_timestamp(
612-
start_date(target_snapshot, target_snapshots_by_name.values(), cache)
613-
)
614-
end = prev_snapshot.intervals[-1][1]
615-
616-
if start < end:
617-
missing_intervals = target_snapshot.missing_intervals(
618-
start, end, end_bounded=True
619-
)
620-
621-
if missing_intervals:
622-
raise SQLMeshError(
623-
f"Detected missing intervals for model {target_snapshot.name}, interrupting your current plan. "
624-
"Please re-apply your plan to resolve this error."
625-
)
598+
missing_intervals = missing_intervals_for_no_gaps(
599+
target_snapshots, previous_snapshots, snapshot_names
600+
)
601+
if missing_intervals:
602+
target_snapshot = next(iter(missing_intervals))
603+
raise SQLMeshError(
604+
f"Detected missing intervals for model {target_snapshot.name}, interrupting your current plan. "
605+
"Please re-apply your plan to resolve this error."
606+
)
626607

627608
@contextlib.contextmanager
628609
def _transaction(self) -> t.Iterator[None]:

‎tests/core/test_plan_evaluator.py‎

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1+
from datetime import date
2+
from pathlib import Path
3+
14
import pytest
5+
import time_machine
26
from pytest_mock.plugin import MockerFixture
37
from sqlglot import parse_one
48

@@ -82,3 +86,92 @@ def test_builtin_evaluator_push(sushi_context: Context, make_snapshot):
8286
)
8387
assert sushi_context.engine_adapter.table_exists(new_model_snapshot.table_name())
8488
assert sushi_context.engine_adapter.table_exists(new_view_model_snapshot.table_name())
89+
90+
91+
@pytest.mark.slow
92+
@time_machine.travel("2026-08-06 01:00:00 UTC", tick=False)
93+
def test_builtin_evaluator_catches_up_no_gaps_plan_to_live_prod_frontier(
94+
tmp_path: Path,
95+
) -> None:
96+
def project_for(value: int) -> Path:
97+
project_path = tmp_path / f"project_{value}"
98+
models_path = project_path / "models"
99+
models_path.mkdir(parents=True)
100+
(project_path / "config.yaml").write_text(
101+
f"""
102+
default_gateway: local
103+
gateways:
104+
local:
105+
connection:
106+
type: duckdb
107+
database: {tmp_path / "warehouse.db"}
108+
model_defaults:
109+
dialect: duckdb
110+
"""
111+
)
112+
(models_path / "daily.sql").write_text(
113+
f"""
114+
MODEL (
115+
name repro.daily,
116+
kind INCREMENTAL_BY_TIME_RANGE (
117+
time_column ds,
118+
lookback 1,
119+
batch_size 1
120+
),
121+
cron '@daily',
122+
start '2026-08-04'
123+
);
124+
125+
SELECT
126+
@start_date AS ds,
127+
{value} AS value
128+
;
129+
"""
130+
)
131+
return project_path
132+
133+
initial_project = project_for(1)
134+
changed_project = project_for(2)
135+
136+
initial_context = Context(paths=[initial_project])
137+
initial_plan = initial_context.plan_builder(
138+
"prod",
139+
no_gaps=True,
140+
skip_tests=True,
141+
skip_linter=True,
142+
).build()
143+
initial_context.apply(initial_plan)
144+
initial_context.close()
145+
146+
context = Context(paths=[changed_project])
147+
try:
148+
stale_plan = context.plan_builder(
149+
"prod",
150+
no_gaps=True,
151+
skip_tests=True,
152+
skip_linter=True,
153+
).build()
154+
155+
with time_machine.travel("2026-08-07 01:00:00 UTC", tick=False):
156+
context.run("prod")
157+
assert context.engine_adapter.fetchall(
158+
'SELECT ds, value FROM "warehouse"."repro"."daily" ORDER BY ds'
159+
) == [
160+
(date(2026, 8, 4), 1),
161+
(date(2026, 8, 5), 1),
162+
(date(2026, 8, 6), 1),
163+
]
164+
context.apply(stale_plan)
165+
166+
prod_environment = context.state_sync.get_environment("prod")
167+
assert prod_environment
168+
assert prod_environment.plan_id == stale_plan.plan_id
169+
assert context.engine_adapter.fetchall(
170+
'SELECT ds, value FROM "warehouse"."repro"."daily" ORDER BY ds'
171+
) == [
172+
(date(2026, 8, 4), 2),
173+
(date(2026, 8, 5), 2),
174+
(date(2026, 8, 6), 2),
175+
]
176+
finally:
177+
context.close()

0 commit comments

Comments
 (0)