Skip to content

Commit 67b984f

Browse files
committed
new approach
1 parent cc5276f commit 67b984f

File tree

2 files changed

+27
-24
lines changed

2 files changed

+27
-24
lines changed

sqlmesh/core/linter/rules/builtin.py

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
from sqlglot.expressions import Star
88
from sqlglot.helper import subclasses
99

10-
from sqlmesh.core.node import IntervalUnit
1110
from sqlmesh.core.constants import EXTERNAL_MODELS_YAML
1211
from sqlmesh.core.dialect import normalize_model_name
1312
from sqlmesh.core.linter.helpers import (
@@ -279,9 +278,7 @@ class CronIntervalAlignment(Rule):
279278
"""Upstream model has a cron expression with longer intervals than downstream model."""
280279

281280
def check_model(self, model: Model) -> t.Optional[t.List[RuleViolation]]:
282-
placeholder_start_date = "2020-01-01 10:00:00"
283-
284-
this_model_cron_next = model.cron_next(placeholder_start_date)
281+
placeholder_start_date = "2020-01-01 00:25:00"
285282

286283
violations = []
287284
for upstream_model_name in model.depends_on:
@@ -296,22 +293,23 @@ def check_model(self, model: Model) -> t.Optional[t.List[RuleViolation]]:
296293
if upstream_model.kind.name in skip_kinds:
297294
continue
298295

299-
upstream_model_cron_next = upstream_model.cron_next(placeholder_start_date)
300-
301-
upstream_cron_interval_unit = IntervalUnit.from_cron(upstream_model.cron)
302-
this_cron_interval_unit = IntervalUnit.from_cron(model.cron)
303-
rule_violation = RuleViolation(
304-
rule=self,
305-
violation_msg=f"Upstream model {upstream_model_name} has longer cron interval ({upstream_model.cron}) "
306-
f"than this model ({model.cron})",
307-
)
296+
for _ in range(12):
297+
this_next = model.cron_next(placeholder_start_date)
298+
upstream_next = upstream_model.cron_next(placeholder_start_date)
299+
current_time = this_next
300+
301+
# Find the first iteration pair where upstream runs after downstream
302+
if upstream_next > this_next:
303+
violations.append(
304+
RuleViolation(
305+
rule=self,
306+
violation_msg=f"Upstream model {upstream_model_name} runs less frequently ({upstream_model.cron}) than this model ({model.cron})",
307+
)
308+
)
309+
break # Found violation, stop checking this upstream model
310+
else:
311+
return None
308312

309-
if upstream_model_cron_next > this_model_cron_next:
310-
violations.append(rule_violation)
311-
elif upstream_cron_interval_unit.seconds > this_cron_interval_unit.seconds:
312-
violations.append(rule_violation)
313-
elif upstream_model_cron_next <= this_model_cron_next:
314-
return None
315313
return violations
316314

317315

tests/core/linter/test_builtin.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -187,14 +187,19 @@ def test_no_missing_external_models_with_existing_file_not_ending_in_newline(
187187
"@weekly",
188188
"@daily",
189189
1,
190-
'Upstream model "memory"."sushi"."step_1" has longer cron interval (@weekly) than this model (@daily)',
190+
'Upstream model "memory"."sushi"."step_1" runs less frequently (@weekly) than this model (@daily)',
191+
),
192+
(
193+
"5 * * * *",
194+
"0 * * * *",
195+
1,
196+
'Upstream model "memory"."sushi"."step_1" runs less frequently (5 * * * *) than this model (0 * * * *)',
191197
),
192-
("5 * * * *", "0 * * * *", 0, None),
193198
(
194199
"15 10 * * *",
195200
"0 * * * *",
196201
1,
197-
'Upstream model "memory"."sushi"."step_1" has longer cron interval (15 10 * * *) than this model (0 * * * *)',
202+
'Upstream model "memory"."sushi"."step_1" runs less frequently (15 10 * * *) than this model (0 * * * *)',
198203
),
199204
],
200205
)
@@ -265,8 +270,8 @@ def test_cron_interval_alignment(
265270
"@daily",
266271
2,
267272
[
268-
'Upstream model "memory"."sushi"."step_a" has longer cron interval (@weekly) than this model (@daily)',
269-
'Upstream model "memory"."sushi"."step_b" has longer cron interval (@weekly) than this model (@daily)',
273+
'Upstream model "memory"."sushi"."step_a" runs less frequently (@weekly) than this model (@daily)',
274+
'Upstream model "memory"."sushi"."step_b" runs less frequently (@weekly) than this model (@daily)',
270275
],
271276
),
272277
],

0 commit comments

Comments
 (0)