Skip to content

fix(plan): avoid rebuilding children after forward-only parent history expires - #6325

Open
vinhky123 wants to merge 1 commit into
SQLMesh:mainfrom
vinhky123:fix-plan-forward-only-history-6307
Open

vinhky123 wants to merge 1 commit into
SQLMesh:mainfrom
vinhky123:fix-plan-forward-only-history-6307

Conversation

@vinhky123

Copy link
Copy Markdown
Contributor

Description

Fixes #6307.

After two forward-only changes to one parent, a metadata change to another parent could trigger a full production backfill of their child. Running the janitor between the two plans was enough to change the outcome.

SQLMesh now records whether each parent version was applied forward-only in its version history. For older history, the planner still consults the stored snapshot. If both sources are missing, it keeps the conservative rebuild decision.

Upgrade note

Run sqlmesh migrate when upgrading. The migration advances the state schema version so older clients cannot accidentally load the new field. It does not reconstruct history that was already deleted.

Test Plan

The DuckDB regression applies two forward-only changes in dev and verifies the janitor deleted the intermediate snapshot. The production plan then retains the child's version and schedules no child backfill. The same scenario failed on upstream 3135e61.

Checklist

  • I have run make style and fixed any issues
  • I have added tests for my changes
  • All existing tests pass (make fast-test)
  • My commits are signed off (git commit -s) per the DCO

…y expires

Signed-off-by: Ky Le <vinhkyle123@gmail.com>
@coderabbitai

coderabbitai Bot commented Dec 6, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Summary

This PR stores forward-only provenance on each parent data version. SnapshotDataVersion gets an optional forward_only field. SnapshotTableInfo.data_version and Snapshot.data_version fill it in.

PlanBuilder._was_applied_forward_only checks this field first. Older records without the field use the previous fallback to the stored snapshot.

_get_stored_parent_forward_only adds historical parent versions to its lookup only when forward_only is None.

A new migration, v0103_add_forward_only_data_version_provenance, defines no-op migrate_schemas and migrate_rows functions.

Tests cover the janitor scenario, the legacy fallback, and the new field.

Scope: +28/-3 lines, excluding tests and docs. Changed files under sqlmesh/core: sqlmesh/core/context_diff.py, sqlmesh/core/plan/builder.py, sqlmesh/core/snapshot/definition.py.

Snapshot data versions now record forward-only state. Planning uses that provenance when available and retains existing lookups for legacy records. Tests cover legacy data, stored-parent history, and janitor removal of an intermediate snapshot.

Changes: Forward-only parent history

Layer / File(s) Summary
Record forward-only provenance
sqlmesh/core/snapshot/definition.py, sqlmesh/migrations/v0103_add_forward_only_data_version_provenance.py, tests/core/test_snapshot.py
SnapshotDataVersion adds an optional forward_only field. Both data-version properties populate it from is_forward_only. Tests cover true, false, and legacy data without the field.
Apply provenance to parent history
sqlmesh/core/context_diff.py, sqlmesh/core/plan/builder.py, tests/core/test_plan.py, tests/core/integration/test_change_scenarios.py
Historical parent lookup skips versions that already have provenance. Plan evaluation uses the recorded value when available and retains the prior lookup for legacy records. Tests cover stored-parent lookup and janitor removal of an intermediate snapshot.

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix · Severity of issue fixed: Medium

Possibly related PRs

  • SQLMesh/sqlmesh#6214: Changes indirect snapshot categorization and stored-parent forward-only lookups.
  • SQLMesh/sqlmesh#6276: Changes how metadata-only upstream hash changes affect indirect snapshot categorization.

Suggested reviewers: dcohen24

Merge Risk: Minimal · up to 4e550

This change keeps a child model on its current version after a parent's older forward-only snapshot is cleaned up, so it no longer triggers a full backfill. Legacy records keep their existing behavior, and the regression test covers the reported scenario. No merge-blocking risk is evident.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title uses the conventional fix(plan): prefix and names the main change: avoiding child rebuilds after forward-only parent history expires. The changeset adds forward-only provenance to version hi…
Description check Passed The description follows the template. It has a Description section that explains the problem (fixes #6307), the approach (forward-only provenance in version history, stored-snapshot fallback, conserva…
Linked Issues check Passed [#6307] requires that a prod plan without --forward-only keep child c on its current version and skip its backfill after two forward-only parent changes, janitor cleanup of the intermediate parent s…
Out of Scope Changes check Passed All changed files relate to the #6307 fix. The changes are: the new optional forward_only field and its population in SnapshotTableInfo.data_version and Snapshot.data_version; the planner preced…
Full details: Title check

Explanation

The title uses the conventional fix(plan): prefix and names the main change: avoiding child rebuilds after forward-only parent history expires. The changeset adds forward-only provenance to version history and falls back to stored snapshots, which matches the title's described effect on the planner. The title is specific and concise.

Full details: Description check

Explanation

The description follows the template. It has a Description section that explains the problem (fixes #6307), the approach (forward-only provenance in version history, stored-snapshot fallback, conservative rebuild when both are missing), and an upgrade note. It has a Test Plan section that describes a DuckDB regression test and the upstream failure. The Checklist is present with items checked. The description does not explicitly list the test file names, but it is mostly complete.

Full details: Linked Issues check

Explanation

[#6307] requires that a prod plan without --forward-only keep child c on its current version and skip its backfill after two forward-only parent changes, janitor cleanup of the intermediate parent snapshot, and a metadata-only change to parent b. The diff at head implements this. SnapshotDataVersion gains forward_only (sqlmesh/core/snapshot/definition.py, per summary), and PlanBuilder._was_applied_forward_only in sqlmesh/core/plan/builder.py now returns version.forward_only when it is set, before it reads any stored snapshot. The janitor cannot remove this record, so the planner no longer treats a deleted intermediate version as not forward-only. In sqlmesh/core/context_diff.py, _get_stored_parent_forward_only fetches stored snapshots only for legacy entries with forward_only is None. The history-boundary check still runs first. The reported DuckDB regression test covers the issue scenario. Limitation: parent versions recorded before this upgrade have no provenance field. For those versions, the conservative rebuild decision still applies once the snapshot is deleted. The PR description states this limitation, and the issue's reproduction steps run on new state, so the requirement is met for them.

Full details: Out of Scope Changes check

Explanation

All changed files relate to the #6307 fix. The changes are: the new optional forward_only field and its population in SnapshotTableInfo.data_version and Snapshot.data_version; the planner precedence change in builder.py; the context_diff.py lookup narrowing; the no-op schema-gate migration v0103 that the upgrade note requires for the new field; and tests in test_change_scenarios.py, test_plan.py, and test_snapshot.py that cover the new behavior. No unrelated behavior change appears in the summary or in the inspected builder.py and context_diff.py hunks.

Finishing Touches

Generate unit tests (beta)

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

Commits

Reviewing files that changed from the base of the PR and between 3135e61 and 4e550f1.

Files selected for processing: 7

  • sqlmesh/core/context_diff.py
  • sqlmesh/core/plan/builder.py
  • sqlmesh/core/snapshot/definition.py
  • sqlmesh/migrations/v0103_add_forward_only_data_version_provenance.py
  • tests/core/integration/test_change_scenarios.py
  • tests/core/test_plan.py
  • tests/core/test_snapshot.py

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 7 remain after this review.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Prod plan rebuilds a child model after the janitor deletes an old forward-only parent version

1 participant