Skip to content

Commit c7607e3

Browse files
fix(snapshot): check full_history_restatement_only before depends_on_self
Snapshot.depends_on_past evaluated depends_on_self first, which renders the model query and can fail to hydrate user code when computing removal intervals for restatement. Full history restatement models already depend on past intervals, so evaluate that check first and short-circuit. Only the evaluation order changes. Signed-off-by: Alex Castillo <alex.castillog33@gmail.com>
1 parent f0ed98e commit c7607e3

2 files changed

Lines changed: 26 additions & 1 deletion

File tree

‎sqlmesh/core/snapshot/definition.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1472,7 +1472,7 @@ def depends_on_past(self) -> bool:
14721472
An example of this can be an INCREMENTAL_BY_TIME_RANGE model that references previous records from itself.
14731473
2. Models that can only be restated from the beginning of history *and* their interval batches must be processed sequentially.
14741474
"""
1475-
return self.depends_on_self or self.full_history_restatement_only
1475+
return self.full_history_restatement_only or self.depends_on_self
14761476

14771477
@property
14781478
def depends_on_self(self) -> bool:

‎tests/core/test_snapshot.py‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -919,6 +919,31 @@ def test_get_removal_intervals_full_history_restatement_model(make_snapshot):
919919
assert interval == (to_timestamp("2023-01-01"), execution_time)
920920

921921

922+
def test_get_removal_interval_full_history_does_not_render_model(
923+
make_snapshot: t.Callable[..., Snapshot], mocker: MockerFixture
924+
):
925+
# Full history restatement models are already known to depend on past intervals, so computing
926+
# the removal interval must not render the model query (which may fail to hydrate user code).
927+
model = SqlModel(
928+
name="name",
929+
kind=IncrementalUnmanagedKind(),
930+
query=parse_one("SELECT 1"),
931+
)
932+
snapshot = make_snapshot(model)
933+
snapshot.add_interval("2020-01-01", "2024-01-01")
934+
# Drop the cached dependencies, as for a snapshot loaded from the state sync.
935+
snapshot.model._full_depends_on = None
936+
937+
render_query = mocker.patch.object(SqlModel, "render_query", side_effect=ModuleNotFoundError)
938+
939+
execution_time = to_timestamp("2024-01-02")
940+
interval = snapshot.get_removal_interval(
941+
"2023-01-01", "2023-01-01", execution_time=execution_time
942+
)
943+
assert interval == (to_timestamp("2020-01-01"), execution_time)
944+
render_query.assert_not_called()
945+
946+
922947
def test_get_removal_intervals_warns_when_requested_range_automatically_widened(
923948
make_snapshot: t.Callable[..., Snapshot], mocker: MockerFixture
924949
):

0 commit comments

Comments
 (0)