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..463995b53a 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,23 @@ 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 + 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: is_coerceable = new_type in self.coerceable_types[current_type] @@ -702,7 +724,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..e8f1e7f556 100644 --- a/tests/core/test_schema_diff.py +++ b/tests/core/test_schema_diff.py @@ -1960,6 +1960,42 @@ def test_get_schema_differ(): assert schema_differ_with_override.drop_cascade is False +@pytest.mark.parametrize( + "current_type, new_type", + [ + ("INT", "BIGINT"), + ("INTEGER", "SMALLINT"), + ("TINYINT", "BYTEINT"), + ("INT", "NUMBER(38, 0)"), + ("INT", "NUMBER(38)"), + ("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 == [] + + +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("INT", dialect="snowflake")}, + {"id": exp.DataType.build("NUMBER(10, 0)", dialect="snowflake")}, + ) + assert [op.expression.sql(dialect="snowflake") for op in operations] == [ + "ALTER TABLE apply_to_table DROP COLUMN id", + "ALTER TABLE apply_to_table ADD id DECIMAL(10, 0)", + ] + + def test_ignore_destructive_edge_cases(): """Test edge cases for ignore_destructive behavior.""" schema_differ = SchemaDiffer(support_positional_add=True)