From c9e45ce28684696d11ec43d18fd0696e571f39cf Mon Sep 17 00:00:00 2001 From: Dhruv Gupta Date: Thu, 26 Nov 2026 06:27:59 -0500 Subject: [PATCH 1/2] fix(snowflake): treat integer type synonyms as the same type in schema diffs Snowflake stores INT, INTEGER, BIGINT, SMALLINT, TINYINT and BYTEINT as NUMBER(38, 0). The schema differ compared the sqlglot types directly, so changing a column from INT to BIGINT (or to NUMBER(38, 0)) was planned as DROP COLUMN plus ADD COLUMN. For forward-only models the plan then failed with "Plan requires a destructive change to a forward-only model" even though the column type does not change in Snowflake. Add a `type_synonyms` option to SchemaDiffer and map the Snowflake integer types to NUMBER(38, 0), so these changes produce no alter operations. Fixes #ISSUE Signed-off-by: Dhruv Gupta --- sqlmesh/core/engine_adapter/snowflake.py | 16 ++++++ sqlmesh/core/schema_diff.py | 17 +++++- tests/core/test_schema_diff.py | 68 ++++++++++++++++++++++++ 3 files changed, 100 insertions(+), 1 deletion(-) diff --git a/sqlmesh/core/engine_adapter/snowflake.py b/sqlmesh/core/engine_adapter/snowflake.py index 7b67976f66..8c2083ac9a 100644 --- a/sqlmesh/core/engine_adapter/snowflake.py +++ b/sqlmesh/core/engine_adapter/snowflake.py @@ -83,6 +83,22 @@ class SnowflakeEngineAdapter( exp.DataType.build("TIMESTAMP_NTZ", dialect=DIALECT).this: [(9,)], exp.DataType.build("TIMESTAMP_TZ", dialect=DIALECT).this: [(9,)], }, + # INT, INTEGER, BIGINT, SMALLINT, TINYINT and BYTEINT are all NUMBER(38, 0) in Snowflake (INTEGER and BYTEINT + # parse to INT): https://docs.snowflake.com/en/sql-reference/data-types-numeric#int-integer-bigint-smallint-tinyint-byteint + "type_synonyms": { + exp.DataType.build("INT", dialect=DIALECT).this: exp.DataType.build( + "NUMBER(38, 0)", dialect=DIALECT + ), + exp.DataType.build("BIGINT", dialect=DIALECT).this: exp.DataType.build( + "NUMBER(38, 0)", dialect=DIALECT + ), + exp.DataType.build("SMALLINT", dialect=DIALECT).this: exp.DataType.build( + "NUMBER(38, 0)", dialect=DIALECT + ), + exp.DataType.build("TINYINT", dialect=DIALECT).this: exp.DataType.build( + "NUMBER(38, 0)", dialect=DIALECT + ), + }, } MANAGED_TABLE_KIND = "DYNAMIC TABLE" SNOWPARK = "snowpark" diff --git a/sqlmesh/core/schema_diff.py b/sqlmesh/core/schema_diff.py index ecf38b18a8..15e8f58ac5 100644 --- a/sqlmesh/core/schema_diff.py +++ b/sqlmesh/core/schema_diff.py @@ -350,6 +350,10 @@ class SchemaDiffer(PydanticModel): max_parameter_length: Numeric parameter values corresponding to "max". Example: `VARCHAR(max)` -> `VARCHAR(65535)`. types_with_unlimited_length: Data types that accept values of any length up to system limits. Any explicitly parameterized type can ALTER to its unlimited length version, along with different types in some engines. + type_synonyms: Data types that the engine stores as another data type. Dict key is a sqlglot exp.DataType.Type, + built from the dialect string like in `parameterized_type_defaults`, and value is the data type the engine + uses for it. Example: Snowflake stores INT and BIGINT as NUMBER(38,0), so changing a column from INT to BIGINT + or NUMBER(38,0) is not a type change. treat_alter_data_type_as_destructive: The SchemaDiffer will only output change data type operations if it concludes the change is compatible and won't result in data loss. If this flag is set to True, it will flag these data type changes as destructive. This was added for dbt adapter support and likely shouldn't @@ -369,6 +373,7 @@ class SchemaDiffer(PydanticModel): parameterized_type_defaults: t.Dict[exp.DType, t.List[t.Tuple[t.Union[int, float], ...]]] = {} max_parameter_length: t.Dict[exp.DType, t.Union[int, float]] = {} types_with_unlimited_length: t.Dict[exp.DType, t.Set[exp.DType]] = {} + type_synonyms: t.Dict[exp.DType, exp.DataType] = {} treat_alter_data_type_as_destructive: bool = False _coerceable_types: t.Dict[exp.DataType, t.Set[exp.DataType]] = {} @@ -402,6 +407,16 @@ def _is_compatible_type(self, current_type: exp.DataType, new_type: exp.DataType return current_type.this in self.types_with_unlimited_length[new_type.this] return False + def _resolve_type_synonym(self, type: exp.DataType) -> exp.DataType: + if type.expressions: + return type + return self.type_synonyms.get(type.this, type) + + def _is_same_type(self, current_type: exp.DataType, new_type: exp.DataType) -> bool: + if current_type == new_type: + return True + return self._resolve_type_synonym(current_type) == self._resolve_type_synonym(new_type) + def _is_coerceable_type(self, current_type: exp.DataType, new_type: exp.DataType) -> bool: if current_type in self.coerceable_types: is_coerceable = new_type in self.coerceable_types[current_type] @@ -702,7 +717,7 @@ def _resolve_alter_operations( _, new_type = _get_name_and_type(new_kwarg) _, current_type = _get_name_and_type(current_kwarg) columns = parent_columns + [TableAlterColumn.from_struct_kwarg(current_kwarg)] - if new_type == current_type: + if self._is_same_type(current_type, new_type): continue operations.extend( self._alter_operation( diff --git a/tests/core/test_schema_diff.py b/tests/core/test_schema_diff.py index 52bd6bb606..cc1b0e9831 100644 --- a/tests/core/test_schema_diff.py +++ b/tests/core/test_schema_diff.py @@ -1960,6 +1960,74 @@ def test_get_schema_differ(): assert schema_differ_with_override.drop_cascade is False +@pytest.mark.parametrize( + "current_type, new_type", + [ + ("INT", "BIGINT"), + ("BIGINT", "INT"), + ("INTEGER", "SMALLINT"), + ("TINYINT", "BYTEINT"), + ("INT", "NUMBER(38, 0)"), + ("NUMBER(38, 0)", "BIGINT"), + ("NUMBER", "INT"), + ], +) +def test_snowflake_integer_synonyms_are_not_a_type_change(current_type: str, new_type: str): + # Snowflake stores all of these as NUMBER(38, 0), so there is nothing to alter + differ = get_schema_differ("snowflake") + operations = differ.compare_columns( + "apply_to_table", + {"id": exp.DataType.build(current_type, dialect="snowflake")}, + {"id": exp.DataType.build(new_type, dialect="snowflake")}, + ) + assert operations == [] + + +@pytest.mark.parametrize( + "current_type, new_type, expected", + [ + ( + "INT", + "NUMBER(10, 0)", + [ + "ALTER TABLE apply_to_table DROP COLUMN id", + "ALTER TABLE apply_to_table ADD id DECIMAL(10, 0)", + ], + ), + ( + "BIGINT", + "VARCHAR", + [ + "ALTER TABLE apply_to_table DROP COLUMN id", + "ALTER TABLE apply_to_table ADD id VARCHAR", + ], + ), + ], +) +def test_snowflake_integer_synonyms_real_type_change( + current_type: str, new_type: str, expected: t.List[str] +): + differ = get_schema_differ("snowflake") + operations = differ.compare_columns( + "apply_to_table", + {"id": exp.DataType.build(current_type, dialect="snowflake")}, + {"id": exp.DataType.build(new_type, dialect="snowflake")}, + ) + assert [op.expression.sql(dialect="snowflake") for op in operations] == expected + + +def test_integer_types_are_distinct_without_synonyms(): + operations = SchemaDiffer().compare_columns( + "apply_to_table", + {"id": exp.DataType.build("INT")}, + {"id": exp.DataType.build("BIGINT")}, + ) + assert [op.expression.sql() for op in operations] == [ + "ALTER TABLE apply_to_table DROP COLUMN id", + "ALTER TABLE apply_to_table ADD COLUMN id BIGINT", + ] + + def test_ignore_destructive_edge_cases(): """Test edge cases for ignore_destructive behavior.""" schema_differ = SchemaDiffer(support_positional_add=True) From bf454f23368151f2d40492687fca37093b6913a8 Mon Sep 17 00:00:00 2001 From: Dhruv Gupta Date: Tue, 1 Dec 2026 10:02:34 -0500 Subject: [PATCH 2/2] fix(snowflake): match NUMBER(38) to integer synonyms and trim tests NUMBER(38) leaves out the scale, so it was compared as DECIMAL(38) and INT -> NUMBER(38) still came out as DROP COLUMN plus ADD COLUMN. When a synonym is involved, compare the types with omitted parameters filled in, the same way the precision checks do. Drop the test cases that the other cases already cover. Signed-off-by: Dhruv Gupta --- sqlmesh/core/schema_diff.py | 9 ++++++- tests/core/test_schema_diff.py | 46 ++++++---------------------------- 2 files changed, 15 insertions(+), 40 deletions(-) diff --git a/sqlmesh/core/schema_diff.py b/sqlmesh/core/schema_diff.py index 15e8f58ac5..463995b53a 100644 --- a/sqlmesh/core/schema_diff.py +++ b/sqlmesh/core/schema_diff.py @@ -415,7 +415,14 @@ def _resolve_type_synonym(self, type: exp.DataType) -> exp.DataType: def _is_same_type(self, current_type: exp.DataType, new_type: exp.DataType) -> bool: if current_type == new_type: return True - return self._resolve_type_synonym(current_type) == self._resolve_type_synonym(new_type) + resolved_current = self._resolve_type_synonym(current_type) + resolved_new = self._resolve_type_synonym(new_type) + if resolved_current is current_type and resolved_new is new_type: + return False + # Compare with omitted parameters filled in, so that NUMBER(38) matches INT's NUMBER(38, 0) + return resolved_current.this == resolved_new.this and self.get_type_parameters( + resolved_current + ) == self.get_type_parameters(resolved_new) def _is_coerceable_type(self, current_type: exp.DataType, new_type: exp.DataType) -> bool: if current_type in self.coerceable_types: diff --git a/tests/core/test_schema_diff.py b/tests/core/test_schema_diff.py index cc1b0e9831..e8f1e7f556 100644 --- a/tests/core/test_schema_diff.py +++ b/tests/core/test_schema_diff.py @@ -1964,11 +1964,10 @@ def test_get_schema_differ(): "current_type, new_type", [ ("INT", "BIGINT"), - ("BIGINT", "INT"), ("INTEGER", "SMALLINT"), ("TINYINT", "BYTEINT"), ("INT", "NUMBER(38, 0)"), - ("NUMBER(38, 0)", "BIGINT"), + ("INT", "NUMBER(38)"), ("NUMBER", "INT"), ], ) @@ -1983,48 +1982,17 @@ def test_snowflake_integer_synonyms_are_not_a_type_change(current_type: str, new assert operations == [] -@pytest.mark.parametrize( - "current_type, new_type, expected", - [ - ( - "INT", - "NUMBER(10, 0)", - [ - "ALTER TABLE apply_to_table DROP COLUMN id", - "ALTER TABLE apply_to_table ADD id DECIMAL(10, 0)", - ], - ), - ( - "BIGINT", - "VARCHAR", - [ - "ALTER TABLE apply_to_table DROP COLUMN id", - "ALTER TABLE apply_to_table ADD id VARCHAR", - ], - ), - ], -) -def test_snowflake_integer_synonyms_real_type_change( - current_type: str, new_type: str, expected: t.List[str] -): +def test_snowflake_integer_synonyms_real_type_change(): + # NUMBER(10, 0) is not what Snowflake stores INT as, so synonyms must not match it differ = get_schema_differ("snowflake") operations = differ.compare_columns( "apply_to_table", - {"id": exp.DataType.build(current_type, dialect="snowflake")}, - {"id": exp.DataType.build(new_type, dialect="snowflake")}, - ) - assert [op.expression.sql(dialect="snowflake") for op in operations] == expected - - -def test_integer_types_are_distinct_without_synonyms(): - operations = SchemaDiffer().compare_columns( - "apply_to_table", - {"id": exp.DataType.build("INT")}, - {"id": exp.DataType.build("BIGINT")}, + {"id": exp.DataType.build("INT", dialect="snowflake")}, + {"id": exp.DataType.build("NUMBER(10, 0)", dialect="snowflake")}, ) - assert [op.expression.sql() for op in operations] == [ + assert [op.expression.sql(dialect="snowflake") for op in operations] == [ "ALTER TABLE apply_to_table DROP COLUMN id", - "ALTER TABLE apply_to_table ADD COLUMN id BIGINT", + "ALTER TABLE apply_to_table ADD id DECIMAL(10, 0)", ]