Skip to content

Commit 13ed9ee

Browse files
authored
fix: don't load models if start is ahead of end (#2368)
1 parent 526a263 commit 13ed9ee

3 files changed

Lines changed: 50 additions & 3 deletions

File tree

sqlmesh/core/snapshot/definition.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -772,7 +772,10 @@ def missing_intervals(
772772
A list of all the missing intervals as epoch timestamps.
773773
"""
774774
# If the node says that it has an end, and we are wanting to load past it, then we can return no empty intervals
775-
if self.node.end and to_datetime(start) > to_datetime(self.node.end):
775+
# Also if a node's start is after the end of the range we are checking then we can return no empty intervals
776+
if (self.node.end and to_datetime(start) > to_datetime(self.node.end)) or (
777+
self.node.start and to_datetime(self.node.start) > to_datetime(end)
778+
):
776779
return []
777780
# If the amount of time being checked is less than the size of a single interval then we
778781
# know that there can't being missing intervals within that range and return

tests/core/test_context.py

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@
55
from tempfile import TemporaryDirectory
66
from unittest.mock import PropertyMock, call, patch
77

8+
import freezegun
89
import pytest
910
from pytest_mock.plugin import MockerFixture
1011
from sqlglot import parse_one
1112
from sqlglot.errors import SchemaError
1213

1314
import sqlmesh.core.constants
15+
import sqlmesh.core.dialect as d
1416
from sqlmesh.core.config import (
1517
Config,
1618
DuckDBConnectionConfig,
@@ -24,7 +26,13 @@
2426
from sqlmesh.core.environment import Environment
2527
from sqlmesh.core.model import load_sql_based_model
2628
from sqlmesh.core.plan import BuiltInPlanEvaluator, PlanBuilder
27-
from sqlmesh.utils.date import make_inclusive_end, now, to_date, yesterday_ds
29+
from sqlmesh.utils.date import (
30+
make_inclusive_end,
31+
now,
32+
to_date,
33+
to_timestamp,
34+
yesterday_ds,
35+
)
2836
from sqlmesh.utils.errors import ConfigError
2937
from tests.utils.test_filesystem import create_temp_file
3038

@@ -482,6 +490,42 @@ def test_plan_default_end(sushi_context_pre_scheduling: Context):
482490
assert forward_only_dev_plan.start == plan_end
483491

484492

493+
@pytest.mark.slow
494+
def test_plan_start_ahead_of_end(copy_to_temp_path):
495+
path = copy_to_temp_path("examples/sushi")
496+
with freezegun.freeze_time("2024-01-02 00:00:00"):
497+
context = Context(paths=path, config="local_config")
498+
context.plan("prod", no_prompts=True, auto_apply=True)
499+
assert context.state_sync.max_interval_end_for_environment("prod") == to_timestamp(
500+
"2024-01-02"
501+
)
502+
context.close()
503+
with freezegun.freeze_time("2024-01-03 00:00:00"):
504+
context = Context(paths=path, config="local_config")
505+
expression = d.parse(
506+
"""
507+
MODEL(
508+
name sushi.hourly,
509+
KIND FULL,
510+
cron '@hourly',
511+
start '2024-01-02 12:00:00',
512+
);
513+
514+
SELECT 1"""
515+
)
516+
model = load_sql_based_model(expression, default_catalog=context.default_catalog)
517+
context.upsert_model(model)
518+
context.plan("prod", no_prompts=True, auto_apply=True)
519+
# Since the new start is ahead of the latest end loaded for prod, the table is deployed as empty
520+
# This isn't considered a gap since prod has not loaded these intervals yet
521+
# As a results the max interval end is unchanged and the table is empty
522+
assert context.state_sync.max_interval_end_for_environment("prod") == to_timestamp(
523+
"2024-01-02"
524+
)
525+
assert context.engine_adapter.fetchone("SELECT COUNT(*) FROM sushi.hourly")[0] == 0
526+
context.close()
527+
528+
485529
@pytest.mark.slow
486530
def test_schema_error_no_default(sushi_context_pre_scheduling) -> None:
487531
context = sushi_context_pre_scheduling

tests/core/test_snapshot.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1249,7 +1249,7 @@ def test_inclusive_exclusive_hourly(make_snapshot):
12491249
owner="owner",
12501250
dialect="",
12511251
cron="@hourly",
1252-
start="1 week ago",
1252+
start="2023-01-29",
12531253
query=parse_one("SELECT id, @end_ds as ds FROM name"),
12541254
)
12551255
)

0 commit comments

Comments
 (0)