diff --git a/sqlmesh/core/context_diff.py b/sqlmesh/core/context_diff.py index 048ab3612b..2857497a42 100644 --- a/sqlmesh/core/context_diff.py +++ b/sqlmesh/core/context_diff.py @@ -20,7 +20,12 @@ from sqlmesh.core.console import get_console from sqlmesh.core.macros import RuntimeStage from sqlmesh.core.model.common import sorted_python_env_payloads -from sqlmesh.core.snapshot import Snapshot, SnapshotId, SnapshotTableInfo +from sqlmesh.core.snapshot import ( + Snapshot, + SnapshotChangeCategory, + SnapshotId, + SnapshotTableInfo, +) from sqlmesh.utils.errors import SQLMeshError from sqlmesh.utils.pydantic import PydanticModel @@ -444,11 +449,17 @@ def directly_modified(self, name: str) -> bool: current, previous = self.modified_snapshots[name] return current.is_directly_modified(previous) - def indirectly_modified(self, name: str) -> bool: + def indirectly_modified(self, name: str, include_metadata_only: bool = False) -> bool: """Returns whether or not a node was indirectly modified in this context. + By default, a node whose upstream changes were all categorized as metadata-only (it is + METADATA and keeps the previous version) is not reported as indirectly modified. This is + meant for display callers (console, CI bot, web UI). Plan building passes + `include_metadata_only=True` so that its answer doesn't depend on categorization state. + Args: name: The snapshot name to check. + include_metadata_only: Whether to also count such metadata-only nodes. Returns: Whether or not the node was indirectly modified. @@ -458,7 +469,16 @@ def indirectly_modified(self, name: str) -> bool: return False current, previous = self.modified_snapshots[name] - return current.is_indirectly_modified(previous) + if not current.is_indirectly_modified(previous): + return False + + # A snapshot whose upstream changes are all metadata-only (eg. whitespace in a parent's query, + # which still changes the parent's data hash) is categorized as METADATA and keeps the previous + # version, so its data isn't affected. + return include_metadata_only or not ( + current.change_category == SnapshotChangeCategory.METADATA + and current.version == previous.version + ) def metadata_updated(self, name: str) -> bool: """Returns whether or not the given node's metadata has been updated. diff --git a/sqlmesh/core/plan/builder.py b/sqlmesh/core/plan/builder.py index 89b7fdabcd..c8fe0123e5 100644 --- a/sqlmesh/core/plan/builder.py +++ b/sqlmesh/core/plan/builder.py @@ -420,7 +420,7 @@ def _build_restatements( and ( # Metadata changes should not be previewed. self._context_diff.directly_modified(s.name) - or self._context_diff.indirectly_modified(s.name) + or self._context_diff.indirectly_modified(s.name, include_metadata_only=True) ) } is_preview = True @@ -724,7 +724,7 @@ def _categorize_snapshot( ) if mode == AutoCategorizationMode.FULL: snapshot.categorize_as(SnapshotChangeCategory.BREAKING, forward_only) - elif self._context_diff.indirectly_modified(snapshot.name): + elif self._context_diff.indirectly_modified(snapshot.name, include_metadata_only=True): if snapshot.is_materialized_view and not forward_only: # We categorize changes as breaking to allow for instantaneous switches in a virtual layer. # Otherwise, there might be a potentially long downtime during MVs recreation. @@ -985,7 +985,7 @@ def _apply_effective_from(self) -> None: def _is_forward_only_change(self, s_id: SnapshotId) -> bool: if not self._context_diff.directly_modified( s_id.name - ) and not self._context_diff.indirectly_modified(s_id.name): + ) and not self._context_diff.indirectly_modified(s_id.name, include_metadata_only=True): return False snapshot = self._context_diff.snapshots[s_id] if snapshot.name in self._context_diff.modified_snapshots: diff --git a/tests/core/integration/test_change_scenarios.py b/tests/core/integration/test_change_scenarios.py index 08e5aaef84..dcc9776f84 100644 --- a/tests/core/integration/test_change_scenarios.py +++ b/tests/core/integration/test_change_scenarios.py @@ -696,6 +696,91 @@ def test_no_backfill_for_model_downstream_of_metadata_change(init_and_plan_conte ) +def test_metadata_only_parent_change_is_not_indirect(init_and_plan_context: t.Callable): + context, plan = init_and_plan_context("examples/sushi") + context.apply(plan) + + parent_name, child_name = "sushi.waiter_revenue_by_day", "sushi.top_waiters" + parent = t.cast(SqlModel, context.get_model(parent_name)) + child_snapshot = context.get_snapshot(child_name, raise_if_missing=True) + + # Whitespace changes the raw query text, and therefore the parent's data hash, but not its + # rendered query, so it is a metadata-only change. + context.upsert_model(parent.copy(update={"query_": ParsableSql(sql=parent.query_.sql + " ")})) + + plan = context.plan_builder("dev", skip_tests=True).build() + diff = plan.context_diff + assert diff.metadata_updated(parent.fqn) + assert not diff.directly_modified(parent.fqn) + assert not diff.indirectly_modified(parent.fqn) + + new_child = diff.snapshots[ + next(s_id for s_id in diff.snapshots if s_id.name == child_snapshot.name) + ] + assert new_child.fingerprint.parent_data_hash != child_snapshot.fingerprint.parent_data_hash + assert not diff.indirectly_modified(child_snapshot.name) + assert new_child.change_category == SnapshotChangeCategory.METADATA + assert new_child.version == child_snapshot.version + assert not plan.missing_intervals + + +def test_data_change_in_parent_is_indirect(init_and_plan_context: t.Callable): + context, plan = init_and_plan_context("examples/sushi") + context.apply(plan) + + parent = t.cast(SqlModel, context.get_model("sushi.waiter_revenue_by_day")) + child_name = context.get_snapshot("sushi.top_waiters", raise_if_missing=True).name + context.upsert_model(add_projection_to_model(parent)) + + diff = context.plan_builder("dev", skip_tests=True).build().context_diff + assert diff.directly_modified(parent.fqn) + assert diff.indirectly_modified(child_name) + + +def test_metadata_only_parent_change_rebuild_keeps_forward_only_child( + init_and_plan_context: t.Callable, +): + context, plan = init_and_plan_context("examples/sushi") + context.apply(plan) + + parent = t.cast(SqlModel, context.get_model("sushi.waiter_revenue_by_day")) + context.upsert_model( + load_sql_based_model( + d.parse( + """ + MODEL ( + name memory.sushi.forward_only_child, + kind INCREMENTAL_BY_TIME_RANGE (time_column event_date, forward_only true), + start '2023-01-01', + ); + SELECT waiter_id, revenue, event_date + FROM memory.sushi.waiter_revenue_by_day + WHERE event_date BETWEEN @start_ds AND @end_ds + """ + ) + ) + ) + context.plan("prod", auto_apply=True, no_prompts=True, skip_tests=True) + + context.upsert_model(parent.copy(update={"query_": ParsableSql(sql=parent.query_.sql + " ")})) + plan_builder = context.plan_builder("dev", skip_tests=True) + + def child_state(plan: Plan) -> t.Tuple[SnapshotChangeCategory, bool]: + child = next( + s + for s in plan.context_diff.snapshots.values() + if s.name.endswith('forward_only_child"') + ) + return child.change_category, child.is_forward_only # type: ignore + + first = child_state(plan_builder.build()) + # Rebuilding sees the child already categorized by the first build. + second = child_state(plan_builder.set_start("2023-01-01").build()) + + assert first == (SnapshotChangeCategory.METADATA, True) + assert second == first + + @time_machine.travel("2023-01-08 15:00:00 UTC") def test_plan_set_choice_is_reflected_in_missing_intervals(init_and_plan_context: t.Callable): context, _ = init_and_plan_context("examples/sushi")