77from sqlglot .expressions import Star
88from sqlglot .helper import subclasses
99
10- from sqlmesh .core .node import IntervalUnit
1110from sqlmesh .core .constants import EXTERNAL_MODELS_YAML
1211from sqlmesh .core .dialect import normalize_model_name
1312from 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
0 commit comments