diff --git a/sqlmesh/core/plan/stages.py b/sqlmesh/core/plan/stages.py index 3d034cf6a1..b4e1a7e555 100644 --- a/sqlmesh/core/plan/stages.py +++ b/sqlmesh/core/plan/stages.py @@ -326,7 +326,7 @@ def build(self, plan: EvaluatablePlan) -> t.List[PlanStage]: if plan.new_snapshots: stages.append(CreateSnapshotRecordsStage(snapshots=plan.new_snapshots)) - snapshots_to_create = self._get_snapshots_to_create(plan, snapshots) + snapshots_to_create = self._get_snapshots_to_create(plan, snapshots, deployability_index) if snapshots_to_create: stages.append( PhysicalLayerSchemaCreationStage( @@ -642,7 +642,10 @@ def _get_audit_only_snapshots( return audit_snapshots def _get_snapshots_to_create( - self, plan: EvaluatablePlan, snapshots: t.Dict[SnapshotId, Snapshot] + self, + plan: EvaluatablePlan, + snapshots: t.Dict[SnapshotId, Snapshot], + deployability_index: DeployabilityIndex, ) -> t.List[Snapshot]: promoted_snapshot_ids = ( set(plan.environment.promoted_snapshot_ids) @@ -653,6 +656,11 @@ def _get_snapshots_to_create( def _should_create(s: Snapshot) -> bool: if not s.is_model or s.is_symbolic: return False + + # Do not create snapshots that contain production data but can never be deployed. + if not deployability_index.is_eligible_for_physical_layer(s): + return False + # Only create tables for snapshots that we're planning to promote or that were selected for backfill return ( plan.is_selected_for_backfill(s.name) diff --git a/sqlmesh/core/scheduler.py b/sqlmesh/core/scheduler.py index 7345d92888..5f3dd0bd7c 100644 --- a/sqlmesh/core/scheduler.py +++ b/sqlmesh/core/scheduler.py @@ -185,6 +185,7 @@ def merged_missing_intervals( ignore_cron=ignore_cron, end_bounded=end_bounded, ) + # Filtering snapshots after computing missing intervals because we need all snapshots in order # to correctly infer start dates. if selected_snapshots is not None: @@ -1083,7 +1084,9 @@ def merged_missing_intervals( restatements = restatements or {} validate_date_range(start, end) - return compute_interval_params( + deployability_index = deployability_index or DeployabilityIndex.all_deployable() + + snapshots_to_intervals = compute_interval_params( snapshots, start=start or earliest_start_date(snapshots), end=end or now_timestamp(), @@ -1096,6 +1099,15 @@ def merged_missing_intervals( end_bounded=end_bounded, ) + if snapshots: + snapshots_to_intervals = { + snapshot: interval + for snapshot, interval in snapshots_to_intervals.items() + if deployability_index.is_eligible_for_physical_layer(snapshot) + } + + return snapshots_to_intervals + def compute_interval_params( snapshots: t.Collection[Snapshot], diff --git a/sqlmesh/core/snapshot/definition.py b/sqlmesh/core/snapshot/definition.py index 2d813a06b1..c0e28ce27b 100644 --- a/sqlmesh/core/snapshot/definition.py +++ b/sqlmesh/core/snapshot/definition.py @@ -1048,6 +1048,7 @@ def missing_intervals( return [] deployability_index = deployability_index or DeployabilityIndex.all_deployable() + intervals = ( self.intervals if deployability_index.is_representative(self) else self.dev_intervals ) @@ -1610,10 +1611,9 @@ def is_deployable(self, snapshot: SnapshotIdLike) -> bool: ) def is_representative(self, snapshot: SnapshotIdLike) -> bool: - """Returns true if the deployable (non-dev) table of the given snapshot should be used for reading, table mapping, and - computing missing intervals. + """Returns true if the deployable (non-dev) table of the given snapshot should be used for reading and table mapping. - Note, that deployable snapshots are also representative, but the reverse is not always true. + Note, that deployable snapshots are always representative, but the reverse is not always true. Unlike `is_deployable`, this variant also captures FORWARD_ONLY and INDIRECT_NON_BREAKING snapshots that are not deployable by their nature but are currently promoted in production. Therefore, it's safe to consider @@ -1629,6 +1629,23 @@ def is_representative(self, snapshot: SnapshotIdLike) -> bool: representative = snapshot_id in self.representative_shared_version_ids return representative or self.is_deployable(snapshot) + def is_eligible_for_physical_layer(self, snapshot: SnapshotIdLike) -> bool: + """Returns true if the snapshot eligible for the physical layer. This will be true unless it is is representative + (looks like prod) but not deployable (can never become prod), and is used for computing missing intervals and + physical layer creation. + + This will only return false for FORWARD_ONLY and INDIRECT_NON_BREAKING snapshots in non-production environments. + + Args: + snapshot: The snapshot to check. + + Returns: + True if the snapshot is not representative, or if it is deployable. False otherwise. + """ + return not self.is_representative(snapshot=snapshot) or self.is_deployable( + snapshot=snapshot + ) + def with_non_deployable(self, snapshot: SnapshotIdLike) -> DeployabilityIndex: """Creates a new index with the given snapshot marked as non-deployable.""" return self._add_snapshot(snapshot, False) @@ -1731,7 +1748,11 @@ def create( children_deployable = False else: children_deployable = False - if not snapshots[node].is_paused: + if not snapshot.is_paused or ( + not snapshot.virtual_environment_mode.is_full + and snapshot.is_indirect_non_breaking + and snapshot.intervals + ): representative_shared_version_ids.add(node) deployability_mapping[node] = this_deployable diff --git a/tests/core/integration/test_config.py b/tests/core/integration/test_config.py index 5d571cd7c5..4bc820629d 100644 --- a/tests/core/integration/test_config.py +++ b/tests/core/integration/test_config.py @@ -280,15 +280,23 @@ def get_prod_dev_views(metadata: DuckDBMetadata) -> t.Tuple[t.Set[exp.Table], t. dev_views = {x for x in views if x.catalog == "dev_catalog" if x.db in environments_schemas} return prod_views, dev_views - def get_default_catalog_and_non_tables( + def get_default_catalog_and_non_sql_objects( metadata: DuckDBMetadata, default_catalog: t.Optional[str] - ) -> t.Tuple[t.Set[exp.Table], t.Set[exp.Table]]: + ) -> t.Tuple[t.Set[exp.Table], t.Set[exp.Table], t.Set[exp.Table], t.Set[exp.Table]]: tables = metadata.qualified_tables + views = metadata.qualified_views user_default_tables = { x for x in tables if x.catalog == default_catalog and x.db != "sqlmesh" } + user_default_views = { + x + for x in views + if x.catalog == default_catalog + and x.db not in (list(environments_schemas) + ["sqlmesh"]) + } non_default_tables = {x for x in tables if x.catalog != default_catalog} - return user_default_tables, non_default_tables + non_default_views = {x for x in views if x.catalog != default_catalog} + return user_default_tables, user_default_views, non_default_tables, non_default_views context, plan = init_and_plan_context( "examples/sushi", config="environment_catalog_mapping_config" @@ -299,11 +307,16 @@ def get_default_catalog_and_non_tables( prod_views, dev_views = get_prod_dev_views(metadata) ( user_default_tables, + user_default_views, non_default_tables, - ) = get_default_catalog_and_non_tables(metadata, context.default_catalog) + non_default_views, + ) = get_default_catalog_and_non_sql_objects(metadata, context.default_catalog) assert len(prod_views) == 16 assert len(dev_views) == 0 - assert len(user_default_tables) == 15 + assert ( + len(user_default_tables) == 15 + ) # One of the snapshots (top_waiters) is a view as opposed to a table. + assert len(user_default_views) == 1 assert state_metadata.schemas == ["sqlmesh"] assert {x.sql() for x in state_metadata.qualified_tables}.issuperset( { @@ -317,12 +330,16 @@ def get_default_catalog_and_non_tables( prod_views, dev_views = get_prod_dev_views(metadata) ( user_default_tables, + user_default_views, non_default_tables, - ) = get_default_catalog_and_non_tables(metadata, context.default_catalog) + non_default_views, + ) = get_default_catalog_and_non_sql_objects(metadata, context.default_catalog) assert len(prod_views) == 16 assert len(dev_views) == 16 - assert len(user_default_tables) == 16 + assert len(user_default_tables) == 15 + assert len(user_default_views) == 1 assert len(non_default_tables) == 0 + assert len(non_default_views) == 32 assert state_metadata.schemas == ["sqlmesh"] assert {x.sql() for x in state_metadata.qualified_tables}.issuperset( { @@ -336,12 +353,16 @@ def get_default_catalog_and_non_tables( prod_views, dev_views = get_prod_dev_views(metadata) ( user_default_tables, + user_default_views, non_default_tables, - ) = get_default_catalog_and_non_tables(metadata, context.default_catalog) + non_default_views, + ) = get_default_catalog_and_non_sql_objects(metadata, context.default_catalog) assert len(prod_views) == 16 assert len(dev_views) == 32 - assert len(user_default_tables) == 16 + assert len(user_default_tables) == 15 + assert len(user_default_views) == 1 assert len(non_default_tables) == 0 + assert len(non_default_views) == 48 assert state_metadata.schemas == ["sqlmesh"] assert {x.sql() for x in state_metadata.qualified_tables}.issuperset( { @@ -356,12 +377,16 @@ def get_default_catalog_and_non_tables( prod_views, dev_views = get_prod_dev_views(metadata) ( user_default_tables, + user_default_views, non_default_tables, - ) = get_default_catalog_and_non_tables(metadata, context.default_catalog) + non_default_views, + ) = get_default_catalog_and_non_sql_objects(metadata, context.default_catalog) assert len(prod_views) == 16 assert len(dev_views) == 16 - assert len(user_default_tables) == 16 + assert len(user_default_tables) == 15 + assert len(user_default_views) == 1 assert len(non_default_tables) == 0 + assert len(non_default_views) == 32 assert state_metadata.schemas == ["sqlmesh"] assert {x.sql() for x in state_metadata.qualified_tables}.issuperset( { diff --git a/tests/core/test_plan_stages.py b/tests/core/test_plan_stages.py index 5bfa3e2fc9..dce4ea2f99 100644 --- a/tests/core/test_plan_stages.py +++ b/tests/core/test_plan_stages.py @@ -78,6 +78,34 @@ def snapshot_c(make_snapshot, snapshot_a: Snapshot) -> Snapshot: return snapshot +@pytest.fixture +def snapshot_d(make_snapshot, snapshot_a: Snapshot) -> Snapshot: + snapshot = make_snapshot( + SqlModel( + name="d", + query=parse_one("select * from a"), + kind=dict(name=ModelKindName.VIEW), + ), + nodes={'"a"': snapshot_a.model}, + ) + snapshot.categorize_as(SnapshotChangeCategory.NON_BREAKING) + return snapshot + + +@pytest.fixture +def snapshot_e(make_snapshot, snapshot_d: Snapshot) -> Snapshot: + snapshot = make_snapshot( + SqlModel( + name="e", + query=parse_one("select * from d"), + kind=dict(name=ModelKindName.VIEW), + ), + nodes={'"d"': snapshot_d.model}, + ) + snapshot.categorize_as(SnapshotChangeCategory.INDIRECT_NON_BREAKING) + return snapshot + + def test_build_plan_stages_basic( snapshot_a: Snapshot, snapshot_b: Snapshot, mocker: MockerFixture ) -> None: @@ -1767,6 +1795,87 @@ def test_build_plan_stages_migrates_views_downstream_of_migrated_view( assert {s.snapshot_id for s in migrate_stage.snapshots} == {s.snapshot_id for s in expected} +def test_build_plan_stages_indirect_non_breaking_view_dev( + snapshot_d: Snapshot, snapshot_e: Snapshot, make_snapshot, mocker: MockerFixture +) -> None: + # Setup Snapshots + snapshot_e.intervals = [(to_timestamp("2023-01-01"), to_timestamp("2023-01-02"))] + + # Create environment + state_reader = mocker.Mock(spec=StateReader) + state_reader.get_snapshots.return_value = {} + existing_environment = Environment( + name="prod", + snapshots=[snapshot_d.table_info, snapshot_e.table_info], + start_at="2023-01-01", + end_at="2023-01-02", + plan_id="previous_plan", + previous_plan_id=None, + promoted_snapshot_ids=[snapshot_d.snapshot_id, snapshot_e.snapshot_id], + finalized_ts=to_timestamp("2023-01-02"), + ) + state_reader.get_environment.return_value = existing_environment + + environment = Environment( + name="dev", + snapshots=[snapshot_d.table_info, snapshot_e.table_info], + start_at="2023-01-01", + end_at="2023-01-02", + plan_id="test_plan", + previous_plan_id="previous_plan", + promoted_snapshot_ids=[snapshot_d.snapshot_id, snapshot_e.snapshot_id], + ) + + # Create evaluatable plan + plan = EvaluatablePlan( + start="2023-01-01", + end="2023-01-02", + new_snapshots=[snapshot_d, snapshot_e], + environment=environment, + no_gaps=False, + skip_backfill=False, + empty_backfill=False, + restatements={}, + restate_all_snapshots=False, + is_dev=True, + allow_destructive_models=set(), + allow_additive_models=set(), + forward_only=False, + end_bounded=False, + ensure_finalized_snapshots=False, + ignore_cron=False, + directly_modified_snapshots=[snapshot_d.snapshot_id], + indirectly_modified_snapshots={ + snapshot_d.name: [snapshot_e.snapshot_id], + }, + metadata_updated_snapshots=[], + removed_snapshots=[], + requires_backfill=True, + models_to_backfill=None, + execution_time="2023-01-02", + disabled_restatement_models=set(), + environment_statements=None, + user_provided_flags=None, + ) + + # Build plan stages + stages = build_plan_stages(plan, state_reader, None) + + # Verify stages + assert len(stages) == 6 + + assert isinstance(stages[0], CreateSnapshotRecordsStage) + assert isinstance(stages[1], PhysicalLayerSchemaCreationStage) + assert isinstance(stages[2], BackfillStage) + assert isinstance(stages[3], EnvironmentRecordUpdateStage) + assert isinstance(stages[4], VirtualLayerUpdateStage) + assert isinstance(stages[5], FinalizeEnvironmentStage) + + ## Verify that indirect non breaking change does not trigger a schema creation or backfill + assert snapshot_e not in stages[1].snapshots + assert snapshot_e not in stages[2].snapshot_to_intervals + + def test_build_plan_stages_virtual_environment_mode_filtering( make_snapshot, mocker: MockerFixture ) -> None: diff --git a/tests/core/test_scheduler.py b/tests/core/test_scheduler.py index cd32d2451d..46b2667cd4 100644 --- a/tests/core/test_scheduler.py +++ b/tests/core/test_scheduler.py @@ -1213,3 +1213,75 @@ def test_dag_upstream_dependency_caching_with_complex_diamond(mocker: MockerFixt expected_g_node: {expected_a_node}, expected_h_node: {expected_a_node}, } + + +def test_dev_run_skips_representative_non_deployable_snapshot(mocker: MockerFixture, make_snapshot): + r""" + Tests that snapshots that are ineligible for the physical layer do not have any missing intervals in dev runs. + """ + + snapshot_non_breaking = make_snapshot( + SqlModel( + name="non_breaking", + kind=IncrementalByTimeRangeKind(time_column="ds"), + cron="@daily", + start="2020-01-01", + query=parse_one("SELECT 1 AS ds"), + ) + ) + snapshot_non_breaking.categorize_as(SnapshotChangeCategory.NON_BREAKING) + + snapshot_indirect_non_breaking = make_snapshot( + SqlModel( + name="indirect_non_breaking", + kind=IncrementalByTimeRangeKind(time_column="ds"), + cron="@daily", + start="2020-01-01", + query=parse_one("SELECT ds FROM non_breaking"), + ), + nodes={'"non_breaking"': snapshot_non_breaking.model}, + ) + snapshot_indirect_non_breaking.categorize_as(SnapshotChangeCategory.INDIRECT_NON_BREAKING) + snapshot_indirect_non_breaking.add_interval(start="2020-01-01", end="2020-01-01", is_dev=False) + snapshot_indirect_non_breaking.add_interval(start="2020-01-02", end="2020-01-02", is_dev=False) + + snapshots = [snapshot_non_breaking, snapshot_indirect_non_breaking] + dev_deployability_index = DeployabilityIndex.create(snapshots) + prod_deployability_index = DeployabilityIndex.all_deployable() + + scheduler = Scheduler( + snapshots=snapshots, + snapshot_evaluator=mocker.Mock(), + state_sync=mocker.Mock(), + default_catalog=None, + ) + + dev_intervals = scheduler.merged_missing_intervals( + start="2020-01-01", + end="2020-01-03", + execution_time="2020-01-04", + deployability_index=dev_deployability_index, + ignore_cron=True, + ) + + prod_intervals = scheduler.merged_missing_intervals( + start="2020-01-01", + end="2020-01-03", + execution_time="2020-01-04", + deployability_index=DeployabilityIndex.all_deployable(), + ignore_cron=True, + ) + + assert snapshot_indirect_non_breaking not in dev_intervals + assert not dev_deployability_index.is_eligible_for_physical_layer( + snapshot_indirect_non_breaking + ) + + assert snapshot_non_breaking in dev_intervals + assert dev_deployability_index.is_eligible_for_physical_layer(snapshot_non_breaking) + + assert snapshot_indirect_non_breaking in prod_intervals + assert prod_deployability_index.is_eligible_for_physical_layer(snapshot_indirect_non_breaking) + + assert snapshot_non_breaking in prod_intervals + assert prod_deployability_index.is_eligible_for_physical_layer(snapshot_non_breaking) diff --git a/tests/core/test_snapshot.py b/tests/core/test_snapshot.py index 64bee7f472..b6c0d2d6e2 100644 --- a/tests/core/test_snapshot.py +++ b/tests/core/test_snapshot.py @@ -638,6 +638,49 @@ def test_missing_intervals_start_override_per_model(make_snapshot: t.Callable[.. ] +def test__missing_intervals__representative_snapshot_prod_intervals_returned(snapshot: Snapshot): + # Arrange + snapshot.add_interval(start="2020-01-01", end="2020-01-01", is_dev=False) + snapshot.add_interval(start="2020-01-02", end="2020-01-02", is_dev=False) + snapshot.add_interval(start="2020-01-01", end="2020-01-01", is_dev=True) + + snapshot.categorize_as(SnapshotChangeCategory.BREAKING) + deployability_index = DeployabilityIndex.create([snapshot]) + + # Act + missing_intervals = snapshot.missing_intervals( + start="2020-01-01", end="2020-01-03", deployability_index=deployability_index + ) + + # Assert + assert deployability_index.is_representative(snapshot) + assert missing_intervals == [ + (to_timestamp("2020-01-03"), to_timestamp("2020-01-04")), + ] + + +def test__missing_intervals__non_representative_snapshot_dev_intervals_returned(snapshot: Snapshot): + # Arrange + snapshot.add_interval(start="2020-01-01", end="2020-01-01", is_dev=False) + snapshot.add_interval(start="2020-01-02", end="2020-01-02", is_dev=False) + snapshot.add_interval(start="2020-01-01", end="2020-01-01", is_dev=True) + + snapshot.categorize_as(SnapshotChangeCategory.BREAKING, forward_only=True) + deployability_index = DeployabilityIndex.create([snapshot]) + + # Act + missing_intervals = snapshot.missing_intervals( + start="2020-01-01", end="2020-01-03", deployability_index=deployability_index + ) + + # Assert + assert not deployability_index.is_representative(snapshot) + assert missing_intervals == [ + (to_timestamp("2020-01-02"), to_timestamp("2020-01-03")), # Not missing from prod intervals + (to_timestamp("2020-01-03"), to_timestamp("2020-01-04")), + ] + + def test_incremental_time_self_reference(make_snapshot): snapshot = make_snapshot( SqlModel( @@ -2407,71 +2450,124 @@ def test_earliest_start_date(sushi_context: Context): def test_deployability_index(make_snapshot): - snapshot_a = make_snapshot(SqlModel(name="a", query=parse_one("SELECT 1"))) - snapshot_a.categorize_as(SnapshotChangeCategory.BREAKING) + # Breaking change - should be both deployable / representative + snapshot_breaking = make_snapshot(SqlModel(name="breaking", query=parse_one("SELECT 1"))) + snapshot_breaking.categorize_as(SnapshotChangeCategory.BREAKING) - snapshot_b = make_snapshot(SqlModel(name="b", query=parse_one("SELECT 1"))) - snapshot_b.categorize_as(SnapshotChangeCategory.BREAKING, forward_only=True) - snapshot_b.parents = (snapshot_a.snapshot_id,) + # Forward only breaking change - cannot be deployable / representative due to forward only + snapshot_breaking_forward_only = make_snapshot( + SqlModel(name="forward_only", query=parse_one("SELECT 1")) + ) + snapshot_breaking_forward_only.categorize_as(SnapshotChangeCategory.BREAKING, forward_only=True) + snapshot_breaking_forward_only.parents = (snapshot_breaking.snapshot_id,) - snapshot_c = make_snapshot(SqlModel(name="c", query=parse_one("SELECT 1"))) - snapshot_c.categorize_as(SnapshotChangeCategory.INDIRECT_BREAKING) - snapshot_c.parents = (snapshot_b.snapshot_id,) + # Indirect breaking - cannot be deployable / representative due to forward only non-representative parent + snapshot_indirect_breaking_non_representative_parent = make_snapshot( + SqlModel(name="indirect_breaking_non_representative_parent", query=parse_one("SELECT 1")) + ) + snapshot_indirect_breaking_non_representative_parent.categorize_as( + SnapshotChangeCategory.INDIRECT_BREAKING + ) + snapshot_indirect_breaking_non_representative_parent.parents = ( + snapshot_breaking_forward_only.snapshot_id, + ) - snapshot_d = make_snapshot(SqlModel(name="d", query=parse_one("SELECT 1"))) - snapshot_d.categorize_as(SnapshotChangeCategory.INDIRECT_BREAKING) - snapshot_d.parents = (snapshot_b.snapshot_id, snapshot_a.snapshot_id) + # Indirect breaking - can be deployable / representative due to forward only representative parent + snapshot_indirect_breaking_representative_parent = make_snapshot( + SqlModel(name="indirect_breaking_representative_parent", query=parse_one("SELECT 1")) + ) + snapshot_indirect_breaking_representative_parent.categorize_as( + SnapshotChangeCategory.INDIRECT_BREAKING + ) + snapshot_indirect_breaking_representative_parent.parents = (snapshot_breaking.snapshot_id,) - snapshot_e = make_snapshot(SqlModel(name="e", query=parse_one("SELECT 1"))) - snapshot_e.categorize_as(SnapshotChangeCategory.NON_BREAKING) + # Non breaking - deployable / representative due to no breaking changes + snapshot_non_breaking = make_snapshot( + SqlModel(name="non_breaking", query=parse_one("SELECT 1")) + ) + snapshot_non_breaking.categorize_as(SnapshotChangeCategory.NON_BREAKING) - snapshot_f = make_snapshot(SqlModel(name="f", query=parse_one("SELECT 1"))) - snapshot_f.categorize_as(SnapshotChangeCategory.INDIRECT_BREAKING) - snapshot_f.parents = (snapshot_e.snapshot_id, snapshot_a.snapshot_id) + # Indirect non breaking - can be representative but not deployable + snapshot_indirect_non_breaking = make_snapshot( + SqlModel(name="indirect_non_breaking", query=parse_one("SELECT 1")) + ) + snapshot_indirect_non_breaking.intervals = [ + (to_timestamp("2023-01-01"), to_timestamp("2023-01-02")) + ] + snapshot_indirect_non_breaking.categorize_as(SnapshotChangeCategory.INDIRECT_NON_BREAKING) + snapshot_indirect_non_breaking.parents = (snapshot_non_breaking.snapshot_id,) - snapshot_g = make_snapshot(SqlModel(name="g", query=parse_one("SELECT 1"))) - snapshot_g.intervals = [(to_timestamp("2023-01-01"), to_timestamp("2023-01-02"))] - snapshot_g.categorize_as(SnapshotChangeCategory.INDIRECT_NON_BREAKING) - snapshot_g.parents = (snapshot_e.snapshot_id,) + # Breaking with non-representative parent - cannot be deployable due to non-representative parent + snapshot_breaking_non_representative_parent = make_snapshot( + SqlModel(name="breaking_non_deployable_parents", query=parse_one("SELECT 1")) + ) + snapshot_breaking_non_representative_parent.categorize_as(SnapshotChangeCategory.BREAKING) + snapshot_breaking_non_representative_parent.parents = ( + snapshot_breaking_forward_only.snapshot_id, + ) snapshots = { s.snapshot_id: s for s in [ - snapshot_a, - snapshot_b, - snapshot_c, - snapshot_d, - snapshot_e, - snapshot_f, - snapshot_g, + snapshot_breaking, + snapshot_breaking_forward_only, + snapshot_indirect_breaking_non_representative_parent, + snapshot_indirect_breaking_representative_parent, + snapshot_non_breaking, + snapshot_indirect_non_breaking, + snapshot_breaking_non_representative_parent, ] } deployability_index = DeployabilityIndex.create(snapshots) - assert deployability_index.is_deployable(snapshot_a) - assert deployability_index.is_deployable(snapshot_e) - assert deployability_index.is_deployable(snapshot_f) - assert not deployability_index.is_deployable(snapshot_g) - assert not deployability_index.is_deployable(snapshot_b) - assert not deployability_index.is_deployable(snapshot_c) - assert not deployability_index.is_deployable(snapshot_d) + assert deployability_index.is_deployable(snapshot_breaking) + assert deployability_index.is_representative(snapshot_breaking) + assert deployability_index.is_eligible_for_physical_layer(snapshot_breaking) - assert deployability_index.is_representative(snapshot_a) - assert deployability_index.is_representative(snapshot_e) - assert deployability_index.is_representative(snapshot_f) - assert deployability_index.is_representative(snapshot_g) - assert not deployability_index.is_representative(snapshot_b) - assert not deployability_index.is_representative(snapshot_c) - assert not deployability_index.is_representative(snapshot_d) + assert not deployability_index.is_deployable(snapshot_breaking_forward_only) + assert not deployability_index.is_representative(snapshot_breaking_forward_only) + assert deployability_index.is_eligible_for_physical_layer(snapshot_breaking_forward_only) + + assert deployability_index.is_deployable(snapshot_non_breaking) + assert deployability_index.is_representative(snapshot_non_breaking) + assert deployability_index.is_eligible_for_physical_layer(snapshot_non_breaking) + + assert not deployability_index.is_deployable( + snapshot_indirect_breaking_non_representative_parent + ) + assert not deployability_index.is_representative( + snapshot_indirect_breaking_non_representative_parent + ) + assert deployability_index.is_eligible_for_physical_layer( + snapshot_indirect_breaking_non_representative_parent + ) + + assert deployability_index.is_deployable(snapshot_indirect_breaking_representative_parent) + assert deployability_index.is_representative(snapshot_indirect_breaking_representative_parent) + assert deployability_index.is_eligible_for_physical_layer( + snapshot_indirect_breaking_representative_parent + ) + + assert not deployability_index.is_deployable(snapshot_indirect_non_breaking) + assert deployability_index.is_representative(snapshot_indirect_non_breaking) + assert not deployability_index.is_eligible_for_physical_layer(snapshot_indirect_non_breaking) + + assert not deployability_index.is_deployable(snapshot_breaking_non_representative_parent) + assert not deployability_index.is_representative(snapshot_breaking_non_representative_parent) + assert deployability_index.is_eligible_for_physical_layer( + snapshot_breaking_non_representative_parent + ) all_deployable_index = deployability_index.all_deployable() assert all(all_deployable_index.is_deployable(s) for s in snapshots.values()) assert all(all_deployable_index.is_representative(s) for s in snapshots.values()) + assert all(all_deployable_index.is_eligible_for_physical_layer(s) for s in snapshots.values()) none_deployable_index = deployability_index.none_deployable() assert all(not none_deployable_index.is_deployable(s) for s in snapshots.values()) assert all(not none_deployable_index.is_representative(s) for s in snapshots.values()) + assert all(none_deployable_index.is_eligible_for_physical_layer(s) for s in snapshots.values()) def test_deployability_index_unpaused_forward_only(make_snapshot): @@ -2483,15 +2579,18 @@ def test_deployability_index_unpaused_forward_only(make_snapshot): snapshot_b.categorize_as(SnapshotChangeCategory.BREAKING) snapshot_b.parents = (snapshot_a.snapshot_id,) - deplyability_index = DeployabilityIndex.create( + deployability_index = DeployabilityIndex.create( {s.snapshot_id: s for s in [snapshot_a, snapshot_b]} ) - assert not deplyability_index.is_deployable(snapshot_a) - assert deplyability_index.is_deployable(snapshot_b) + assert not deployability_index.is_deployable(snapshot_a) + assert deployability_index.is_deployable(snapshot_b) + + assert deployability_index.is_representative(snapshot_a) + assert deployability_index.is_representative(snapshot_b) - assert deplyability_index.is_representative(snapshot_a) - assert deplyability_index.is_representative(snapshot_b) + assert not deployability_index.is_eligible_for_physical_layer(snapshot_a) + assert deployability_index.is_eligible_for_physical_layer(snapshot_b) def test_deployability_index_unpaused_auto_restatement(make_snapshot): @@ -2518,17 +2617,21 @@ def test_deployability_index_unpaused_auto_restatement(make_snapshot): snapshot_c.categorize_as(SnapshotChangeCategory.BREAKING) snapshot_c.parents = (snapshot_b.snapshot_id,) - deplyability_index = DeployabilityIndex.create( + deployability_index = DeployabilityIndex.create( {s.snapshot_id: s for s in [snapshot_a, snapshot_b, snapshot_c]} ) - assert not deplyability_index.is_deployable(snapshot_a) - assert not deplyability_index.is_deployable(snapshot_b) - assert not deplyability_index.is_deployable(snapshot_c) + assert not deployability_index.is_deployable(snapshot_a) + assert not deployability_index.is_deployable(snapshot_b) + assert not deployability_index.is_deployable(snapshot_c) + + assert deployability_index.is_representative(snapshot_a) + assert deployability_index.is_representative(snapshot_b) + assert not deployability_index.is_representative(snapshot_c) - assert deplyability_index.is_representative(snapshot_a) - assert deplyability_index.is_representative(snapshot_b) - assert not deplyability_index.is_representative(snapshot_c) + assert not deployability_index.is_eligible_for_physical_layer(snapshot_a) + assert not deployability_index.is_eligible_for_physical_layer(snapshot_b) + assert deployability_index.is_eligible_for_physical_layer(snapshot_c) def test_deployability_index_uncategorized_forward_only_model(make_snapshot): @@ -2557,6 +2660,9 @@ def test_deployability_index_uncategorized_forward_only_model(make_snapshot): assert not deployability_index.is_representative(snapshot_a) assert not deployability_index.is_representative(snapshot_b) + assert deployability_index.is_eligible_for_physical_layer(snapshot_a) + assert deployability_index.is_eligible_for_physical_layer(snapshot_b) + def test_deployability_index_auto_restatement_model(make_snapshot): model_a = SqlModel( @@ -2586,6 +2692,9 @@ def test_deployability_index_auto_restatement_model(make_snapshot): assert not deployability_index.is_representative(snapshot_a) assert not deployability_index.is_representative(snapshot_b) + assert deployability_index.is_eligible_for_physical_layer(snapshot_a) + assert deployability_index.is_eligible_for_physical_layer(snapshot_b) + def test_deployability_index_categorized_forward_only_model(make_snapshot): model_a = SqlModel( @@ -2615,6 +2724,9 @@ def test_deployability_index_categorized_forward_only_model(make_snapshot): assert not deployability_index.is_representative(snapshot_a) assert not deployability_index.is_representative(snapshot_b) + assert deployability_index.is_eligible_for_physical_layer(snapshot_a) + assert deployability_index.is_eligible_for_physical_layer(snapshot_b) + def test_deployability_index_missing_parent(make_snapshot): snapshot_a = make_snapshot(SqlModel(name="a", query=parse_one("SELECT 1"))) @@ -2624,10 +2736,10 @@ def test_deployability_index_missing_parent(make_snapshot): snapshot_b.categorize_as(SnapshotChangeCategory.BREAKING, forward_only=True) snapshot_b.parents = (snapshot_a.snapshot_id,) - deplyability_index = DeployabilityIndex.create({snapshot_b.snapshot_id: snapshot_b}) + deployability_index = DeployabilityIndex.create({snapshot_b.snapshot_id: snapshot_b}) - assert not deplyability_index.is_deployable(snapshot_b) - assert not deplyability_index.is_deployable(snapshot_a) + assert not deployability_index.is_deployable(snapshot_b) + assert not deployability_index.is_deployable(snapshot_a) @pytest.mark.parametrize( diff --git a/tests/core/test_snapshot_evaluator.py b/tests/core/test_snapshot_evaluator.py index 0955af331d..fd84629e84 100644 --- a/tests/core/test_snapshot_evaluator.py +++ b/tests/core/test_snapshot_evaluator.py @@ -424,7 +424,7 @@ def increment_stage_counter(evaluator) -> None: ) -def test_promote(mocker: MockerFixture, adapter_mock, make_snapshot): +def test_promote__representative__prod_physical(mocker: MockerFixture, adapter_mock, make_snapshot): evaluator = SnapshotEvaluator(adapter_mock) model = SqlModel( @@ -436,9 +436,15 @@ def test_promote(mocker: MockerFixture, adapter_mock, make_snapshot): snapshot = make_snapshot(model) snapshot.categorize_as(SnapshotChangeCategory.BREAKING) + deployability_index = DeployabilityIndex.create([snapshot]) - evaluator.promote([snapshot], EnvironmentNamingInfo(name="test_env")) + evaluator.promote( + target_snapshots=[snapshot], + environment_naming_info=EnvironmentNamingInfo(name="test_env"), + deployability_index=deployability_index, + ) + assert deployability_index.is_representative(snapshot) adapter_mock.transaction.assert_called() adapter_mock.session.assert_called() adapter_mock.create_schema.assert_called_once_with(to_schema("test_schema__test_env")) @@ -453,6 +459,43 @@ def test_promote(mocker: MockerFixture, adapter_mock, make_snapshot): ) +def test_promote__non_representative__dev_physical( + mocker: MockerFixture, adapter_mock, make_snapshot +): + evaluator = SnapshotEvaluator(adapter_mock) + + model = SqlModel( + name="test_schema.test_model", + kind=IncrementalByTimeRangeKind(time_column="a"), + storage_format="parquet", + query=parse_one("SELECT a FROM tbl WHERE ds BETWEEN @start_ds and @end_ds"), + ) + + snapshot = make_snapshot(model) + snapshot.categorize_as(SnapshotChangeCategory.BREAKING, forward_only=True) + deployability_index = DeployabilityIndex.create([snapshot]) + + evaluator.promote( + target_snapshots=[snapshot], + environment_naming_info=EnvironmentNamingInfo(name="test_env"), + deployability_index=deployability_index, + ) + + assert not deployability_index.is_representative(snapshot) + adapter_mock.transaction.assert_called() + adapter_mock.session.assert_called() + adapter_mock.create_schema.assert_called_once_with(to_schema("test_schema__test_env")) + adapter_mock.create_view.assert_called_once_with( + "test_schema__test_env.test_model", + parse_one( + f"SELECT * FROM sqlmesh__test_schema.test_schema__test_model__{snapshot.version}__dev" + ), + table_description=None, + column_descriptions=None, + view_properties={}, + ) + + def test_demote(mocker: MockerFixture, adapter_mock, make_snapshot): evaluator = SnapshotEvaluator(adapter_mock)