SCD_TYPE_2: option to preserve existing physical table rows on new snapshot identifier (is_first_insert truncation)
メンテナーはふだん 4 日以内に返信
まだ誰も着手していません。
評価
- 難易度
- 4/5
- 見積もり時間
- 3〜5日
- 初心者へのやさしさ
- 35/100
- issue の種類
- 機能追加
- 明瞭さ
- おおむね明確
- 活発さ
- 停滞
- 技術スタック
- postgresql, python
調査の方向性
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.
索引モデルが issue の本文から書いたものです。
説明
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.
- 主要言語
- Python
- スター
- 3.3k
- フォーク
- 471
- 平均マージ
- 6日 7時間
- マージ済み PR(30日)
- 35
環境構築
このプロジェクトの開発コンテナを、あなたの GitHub アカウントでブラウザ上に起動します。
- Dockerfile・Docker Compose ファイルなし
- プルリクエストのテンプレートあり
- コントリビューションガイドを読む
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
SQLMesh/sqlmesh のほかの issue
-
難易度 2/5 1〜3時間 初心者へのやさしさ 86/100
メンテナーはふだん 4 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 68/100
メンテナーはふだん 4 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 67/100
SQLMesh/sqlmesh#6008 · コメント 1 件 ·
メンテナーはふだん 4 日以内に返信
-
VSCode Extension: SQLMesh LSP crashes on startup: ImportError: cannot import name 'WorkspaceDiagnosticRefreshRequest' with lsprotocol 2025.x対応中かも @boomesh11 が 15 日前に担当しました。 オープンBug
難易度 2/5 1〜3時間 初心者へのやさしさ 66/100
メンテナーはふだん 4 日以内に返信
-
Improvement
難易度 1/5 1時間未満 初心者へのやさしさ 85/100
SQLMesh/sqlmesh#5696 · コメント 6 件 ·
メンテナーはふだん 4 日以内に返信
SQLMesh/sqlmesh の issue をすべて見る
似ている issue
-
[BUG][Low–Medium] founder_numbers.py silently reads a SAFE discount or pool target given as a fraction (0.2) as 0.2%対応中かも @sumitkhond111-del が今日担当しました。 オープンbug
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
bmad-code-org/BMAD-METHOD#3358 · コメント 1 件 ·
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 86/100
open-mmlab/mmcv#3352 ·
-
難易度 1/5 1時間未満 初心者へのやさしさ 68/100
OpenGithubs/weekly#263 ·
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 76/100
メンテナーはふだん 1 日以内に返信
-
comp/cli P3 type/feature
難易度 2/5 1〜3時間 初心者へのやさしさ 76/100
NousResearch/hermes-agent#162784 ·
メンテナーはふだん 1 日以内に返信