SCD_TYPE_2: option to preserve existing physical table rows on new snapshot identifier (is_first_insert truncation)
Maintainer antworten meist innerhalb von 4 Tagen
Dieses Issue hat noch niemand übernommen.
Bewertung
- Schwierigkeit
- 4/5
- Geschätzter Aufwand
- 3-5 Tage
- Anfängerfreundlichkeit
- 35/100
- Issue-Typ
- Feature
- Klarheit
- Größtenteils klar
- Aktivitätsstatus
- Veraltet
- Tech-Stack
- postgresql, python
- Bereich
- data-engineering, databases
Rechercherichtung
Start by reading evaluator.py, especially the is_first_insert calculation and the _scd_type_2() path that applies limit(0). Compare how invalidate_hard_deletes is handled on ordinary runs with first inserts when a physical table already exists. Decide how the proposed preservation behavior should be configured, and add tests covering a new snapshot identifier with existing historical rows; done means those rows are retained in the intended case without changing existing truncation behavior otherwise.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Beschreibung
Summary
SCD_TYPE_2_BY_COLUMN models that accumulate revision history over time lose all historical data when a new snapshot identifier is created (due to a code change or indirect breaking change in a parent model), because the first interval run for the new identifier sets truncate=True via the is_first_insert path.
Current behaviour
In evaluator.py:
is_first_insert = (
not _intervals(snapshot, deployability_index) or not target_table_exists
) and batch_index == 0
When is_first_insert=True, truncate=True is passed to _scd_type_2(), which limits the existing-rows CTE to zero rows:
if truncate:
existing_rows_query = existing_rows_query.limit(0)
This ignores all rows already in the physical table and rebuilds entirely from the source query.
The problem
For SCD_TYPE_2 models used as accumulating history tables — where the source is a rolling window and the SCD table is the only long-term record of how data changed over time — this truncation destroys irreplaceable historical rows (valid_to IS NOT NULL) that cannot be reconstructed from the current source.
The physical table may contain years of accumulated revision history. The source may intentionally only contain the last N months of data (e.g. a vendor API with a rolling window). Truncating and rebuilding from source produces a correct current state but permanently destroys the revision record.
This is not a bug per se — truncation on first insert is correct behaviour for models where the source is the full source of truth. But it is a gap for the common pattern where the SCD table is the source of truth and the input is a rolling window.
Notably, invalidate_hard_deletes=False (the default) already signals that the user does not want source-absence to affect existing rows. It is inconsistent that this option protects against row closure on normal runs but offers no protection against wholesale truncation on identifier changes.
Proposed solution
Add a model-level option — e.g. forward_only_on_new_identifier or preserve_rows_on_first_insert — that, when set, skips the limit(0) branch when an existing physical table is present and already contains rows, even if the snapshot identifier is new.
Alternatively: when invalidate_hard_deletes=False, do not truncate on is_first_insert if the physical table already exists and is non-empty. The intent of invalidate_hard_deletes=False is "trust existing rows"; applying that intent to the first-insert case would be consistent and solve this problem without a new config option.
Workaround
Manually insert interval records into sqlmesh._intervals for the new snapshot identifier before the first run, setting start_ts/end_ts to cover the full historical range. This causes is_first_insert=False and bypasses the truncation. This works but requires awareness of SQLMesh internals and is not a supported workflow.
Version
SQLMesh 0.228.1
PostgreSQL 18.
- Vorherrschende Sprache
- Python
- Sterne
- 3.3k
- Forks
- 471
- Ø Merge
- 6 T. 7 Std.
- Gemergte PRs (30 T.)
- 35
Entwicklungsumgebung
Startet den Dev-Container des Projekts im Browser, mit Ihrem eigenen GitHub-Konto.
- Kein Dockerfile und keine Docker-Compose-Datei
- Hat eine Pull-Request-Vorlage
- Beitragsleitfaden lesen
Erste Schritte
- Lesen Sie das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreiben Sie ins Issue, dass Sie es übernehmen — das erspart doppelte Arbeit.
- Forken Sie das Repository und arbeiten Sie in einem Branch.
- Öffnen Sie einen Pull Request, der die Issue-Nummer nennt.
Mehr aus SQLMesh/sqlmesh
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 86/100
Maintainer antworten meist innerhalb von 4 Tagen
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 68/100
Maintainer antworten meist innerhalb von 4 Tagen
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 67/100
SQLMesh/sqlmesh#6008 · 1 Kommentar ·
Maintainer antworten meist innerhalb von 4 Tagen
-
VSCode Extension: SQLMesh LSP crashes on startup: ImportError: cannot import name 'WorkspaceDiagnosticRefreshRequest' with lsprotocol 2025.xEvtl. vergeben @boomesh11 hat das vor 15 Tagen übernommen. OffenBug
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 66/100
Maintainer antworten meist innerhalb von 4 Tagen
-
Improvement
Schwierigkeit 1/5 Unter einer Stunde Anfängerfreundlichkeit 85/100
SQLMesh/sqlmesh#5696 · 6 Kommentare ·
Maintainer antworten meist innerhalb von 4 Tagen
Alle Issues in SQLMesh/sqlmesh
Ähnliche Issues
-
[BUG][Low–Medium] founder_numbers.py silently reads a SAFE discount or pool target given as a fraction (0.2) as 0.2%Evtl. vergeben @sumitkhond111-del hat das heute übernommen. Offenbug
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 72/100
bmad-code-org/BMAD-METHOD#3358 · 1 Kommentar ·
Maintainer antworten meist innerhalb von 1 Tag
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 86/100
open-mmlab/mmcv#3352 ·
-
Schwierigkeit 1/5 Unter einer Stunde Anfängerfreundlichkeit 68/100
OpenGithubs/weekly#263 ·
Maintainer antworten meist innerhalb von 1 Tag
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 76/100
Maintainer antworten meist innerhalb von 1 Tag
-
comp/cli P3 type/feature
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 76/100
NousResearch/hermes-agent#162784 ·
Maintainer antworten meist innerhalb von 1 Tag