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

Aberta
#6,224 1 comentário 0 reações 0 responsáveis Ver no GitHub

Mantenedores costumam responder em até 4 dias

Ninguém assumiu esta issue ainda.

Avaliação

Dificuldade
4/5
Tempo estimado
3-5 dias
Facilidade para iniciantes
53/100
Tipo de issue
Bug
Clareza
Claramente especificada
Status de atividade
Ativa
Stack de tecnologia
python, sql
Domínio
backend, databases

Direção de pesquisa

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.

Escrita pelo modelo de indexação a partir do texto da issue.

Descrição

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).
Linguagem predominante
Python
Estrelas
3.3k
Forks
471
Merge médio
6d 7h
PRs com merge (30d)
35

Preparar o ambiente

Abrir no Codespaces

Inicia o contêiner de desenvolvimento do projeto no navegador, com a sua própria conta do GitHub.

Primeiros passos

  1. Leia a issue inteira e depois o guia de contribuição do projeto.
  2. Comente na issue dizendo que vai assumir — evita que duas pessoas façam o mesmo trabalho.
  3. Faça um fork do repositório e trabalhe em uma branch.
  4. Abra um pull request que referencie o número da issue.

Mais de SQLMesh/sqlmesh

Todas as issues de SQLMesh/sqlmesh

Issues semelhantes

Mais issues de Python

Receba novas issues na sua caixa de entrada

Um resumo curto de issues do GitHub para quem está começando.