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

Abierto
#6,224 1 comentario 0 reacciones 0 asignados Ver en GitHub

Los mantenedores suelen responder en 4 días

Nadie ha tomado este issue todavía.

Evaluación

Dificultad
4/5
Tiempo estimado
3-5 días
Aptitud para principiantes
53/100
Tipo de issue
Error
Claridad
Bien especificado
Estado de actividad
Activo
Stack tecnológico
python, sql

Línea de trabajo

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.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

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).
Lenguaje dominante
Python
Estrellas
3.3k
Forks
471
Merge medio
6 d 7 h
PR fusionados (30 d)
35

Preparar el entorno

Abrir en Codespaces

Inicia el contenedor de desarrollo del proyecto en tu navegador, con tu propia cuenta de GitHub.

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de SQLMesh/sqlmesh

Todos los issues de SQLMesh/sqlmesh

Issues similares

Más issues de Python

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.