Fabric: false destructive change on SCD2 `valid_from`/`valid_to` because `TIMESTAMP` and `DATETIME2(6)` are seen as different types

オープン
#6,224 コメント 1 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 4 日以内に返信

まだ誰も着手していません。

評価

難易度
4/5
見積もり時間
3〜5日
初心者へのやさしさ
53/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
活発
技術スタック
python, sql
領域
backend, databases

調査の方向性

Start with sqlmesh/core/schema_diff.py and the Fabric adapter’s MSSQLEngineAdapter.columns() implementation. Reproduce the issue with the provided get_schema_differ("fabric") snippet, then check how dialect-rendered types are compared and how DATETIME_PRECISION is queried. Done means the comparison emits only ADD c for the repro and the existing datetime2 precision is read correctly.

索引モデルが issue の本文から書いたものです。

説明

Problem
On Fabric (SQLMesh 0.236.4), adding a nullable column to an existing SCD_TYPE_2_BY_COLUMN model fails the plan:

ALTER TABLE .. ADD cancelled_flag BIT
ALTER TABLE .. DROP COLUMN _dwh_valid_from
ALTER TABLE .. ADD _dwh_valid_from DATETIME2(6)
ALTER TABLE .. DROP COLUMN _dwh_valid_to
ALTER TABLE .. ADD _dwh_valid_to DATETIME2(6)
Error: Plan requires a destructive change to a forward-only model.

Allowing it would wipe all SCD2 history.

Cause

  • The plan's destructive check compares the old and new columns_to_types, including the SCD2 managed columns.
  • The stored snapshot has DATETIME2(6), while the new model keeps the generic TIMESTAMP. Both render as DATETIME2(6) in Fabric.
  • SchemaDiffer compares the raw types, sees a type change, and plans DROP + ADD.

Repro

from sqlglot import exp
from sqlmesh.core.schema_diff import get_schema_differ

sd = get_schema_differ("fabric")
cur = {"v": exp.DataType.build("DATETIME2(6)", dialect="fabric")}
new = {"v": exp.DataType.build("TIMESTAMP"), "c": exp.DataType.build("BIT")}
print([o.expression.sql("fabric") for o in sd.compare_columns("t", cur, new)])
# actual:   ADD c, DROP v, ADD v DATETIME2(6)
# expected: ADD c

Proposed fix

  1. Treat types that render identically in the dialect as unchanged. In SchemaDiffer (or in PlanBuilder before calling compare_columns):
    exp.DataType.build(t.sql(dialect), dialect=dialect)
  2. Add DATETIME_PRECISION to the column query in MSSQLEngineAdapter.columns(). Today an existing datetime2(6) column is read back as datetime2, which counts as (7).
主要言語
Python
スター
3.3k
フォーク
471
平均マージ
6日 7時間
マージ済み PR(30日)
35

環境構築

Codespaces で開く

このプロジェクトの開発コンテナを、あなたの GitHub アカウントでブラウザ上に起動します。

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

SQLMesh/sqlmesh のほかの issue

SQLMesh/sqlmesh の issue をすべて見る

似ている issue

Python の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。