1616)
1717from sqlmesh .core .context_diff import ContextDiff
1818from sqlmesh .core .environment import EnvironmentNamingInfo
19+ from sqlmesh .core .plan .common import should_force_rebuild
1920from sqlmesh .core .plan .definition import (
2021 Plan ,
2122 SnapshotMapping ,
@@ -276,7 +277,7 @@ def build(self) -> Plan:
276277
277278 self ._check_destructive_changes (directly_modified )
278279 self ._categorize_snapshots (dag , indirectly_modified )
279- self ._adjust_new_snapshot_intervals ()
280+ self ._adjust_snapshot_intervals ()
280281
281282 deployability_index = (
282283 DeployabilityIndex .create (
@@ -508,21 +509,22 @@ def _build_models_to_backfill(
508509 ).sorted
509510 }
510511
511- def _adjust_new_snapshot_intervals (self ) -> None :
512- old_snapshots = {
513- (old .name , old .version_get_or_generate ()): old
514- for _ , old in self ._context_diff .modified_snapshots .values ()
515- }
516-
517- for new in self ._context_diff .new_snapshots .values ():
518- new .intervals = []
519- new .dev_intervals = []
520- old = old_snapshots .get ((new .name , new .version_get_or_generate ()))
521- if not old :
512+ def _adjust_snapshot_intervals (self ) -> None :
513+ for new , old in self ._context_diff .modified_snapshots .values ():
514+ if not new .is_model or not old .is_model :
522515 continue
523- new .merge_intervals (old )
524- if new .is_forward_only :
525- new .dev_intervals = new .intervals .copy ()
516+ is_same_version = old .version_get_or_generate () == new .version_get_or_generate ()
517+ if is_same_version and should_force_rebuild (old , new ):
518+ # If the difference between 2 snapshots requires a full rebuild,
519+ # then clear the intervals for the new snapshot.
520+ self ._context_diff .snapshots [new .snapshot_id ].intervals = []
521+ elif new .snapshot_id in self ._context_diff .new_snapshots :
522+ new .intervals = []
523+ new .dev_intervals = []
524+ if is_same_version :
525+ new .merge_intervals (old )
526+ if new .is_forward_only :
527+ new .dev_intervals = new .intervals .copy ()
526528
527529 def _check_destructive_changes (self , directly_modified : t .Set [SnapshotId ]) -> None :
528530 for s_id in sorted (directly_modified ):
@@ -589,7 +591,7 @@ def _categorize_snapshots(
589591 forward_only = self ._forward_only or self ._is_forward_only_change (s_id )
590592 if forward_only and s_id .name in self ._context_diff .modified_snapshots :
591593 new , old = self ._context_diff .modified_snapshots [s_id .name ]
592- if _should_force_rebuild (old , new ) or snapshot .is_seed :
594+ if should_force_rebuild (old , new ) or snapshot .is_seed :
593595 # Breaking kind changes and seed changes can't be forward-only.
594596 forward_only = False
595597
@@ -614,7 +616,7 @@ def _categorize_snapshot(
614616 if self ._context_diff .directly_modified (s_id .name ):
615617 if self ._auto_categorization_enabled :
616618 new , old = self ._context_diff .modified_snapshots [s_id .name ]
617- if _should_force_rebuild (old , new ):
619+ if should_force_rebuild (old , new ):
618620 snapshot .categorize_as (SnapshotChangeCategory .BREAKING , False )
619621 return
620622
@@ -772,7 +774,7 @@ def _is_forward_only_change(self, s_id: SnapshotId) -> bool:
772774 if snapshot .name in self ._context_diff .modified_snapshots :
773775 _ , old = self ._context_diff .modified_snapshots [snapshot .name ]
774776 # If the model kind has changed in a breaking way, then we can't consider this to be a forward-only change.
775- if snapshot .is_model and _should_force_rebuild (old , snapshot ):
777+ if snapshot .is_model and should_force_rebuild (old , snapshot ):
776778 return False
777779 return (
778780 snapshot .is_model and snapshot .model .forward_only and bool (snapshot .previous_versions )
@@ -870,19 +872,3 @@ def _modified_and_added_snapshots(self) -> t.List[Snapshot]:
870872 if snapshot .name in self ._context_diff .modified_snapshots
871873 or snapshot .snapshot_id in self ._context_diff .added
872874 ]
873-
874-
875- def _should_force_rebuild (old : Snapshot , new : Snapshot ) -> bool :
876- if old .virtual_environment_mode != new .virtual_environment_mode :
877- # If the virtual environment mode has changed, then we need to rebuild
878- return True
879- if old .model .kind .name == new .model .kind .name :
880- # If the kind hasn't changed, then we don't need to rebuild
881- return False
882- if not old .is_incremental or not new .is_incremental :
883- # If either is not incremental, then we need to rebuild
884- return True
885- if old .model .partitioned_by == new .model .partitioned_by :
886- # If the partitioning hasn't changed, then we don't need to rebuild
887- return False
888- return True
0 commit comments