Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions sqlmesh/core/engine_adapter/mssql.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,18 @@ class MSSQLEngineAdapter(
VARIABLE_LENGTH_DATA_TYPES = {"binary", "varbinary", "char", "varchar", "nchar", "nvarchar"}
INSERT_OVERWRITE_STRATEGY = InsertOverwriteStrategy.MERGE

def normalize_value(
self,
expr: exp.Expr,
type: exp.DataType,
decimal_precision: int = 3,
timestamp_precision: int = RowDiffMixin.MAX_TIMESTAMP_PRECISION,
) -> exp.Expr:
# T-SQL truncates CAST(x AS VARCHAR) to 30 characters when no length is given
normalized = super().normalize_value(expr, type, decimal_precision, timestamp_precision)
normalized.set("to", exp.DataType.build("VARCHAR(MAX)", dialect=self.dialect))
return normalized

@property
def catalog_support(self) -> CatalogSupport:
# MSSQL and AzureSQL both use this engine adapter, but they differ in catalog support.
Expand Down
14 changes: 14 additions & 0 deletions tests/core/engine_adapter/test_mssql.py
Original file line number Diff line number Diff line change
Expand Up @@ -1055,3 +1055,17 @@ def test_comments(make_mocked_engine_adapter: t.Callable, mocker: MockerFixture)
adapter._build_create_comment_table_exp(table, comment),
adapter._build_create_comment_column_exp(table, "a", comment),
]


def test_concat_columns_uses_varchar_max(adapter: MSSQLEngineAdapter):
# CAST(x AS VARCHAR) without a length truncates to 30 characters in T-SQL, which
# breaks the table_diff join key for long or UNIQUEIDENTIFIER key columns
columns_to_types = {
"id": exp.DataType.build("UNIQUEIDENTIFIER", dialect=adapter.dialect),
"name": exp.DataType.build("NVARCHAR(100)", dialect=adapter.dialect),
}

assert (
adapter.concat_columns(columns_to_types).sql(dialect=adapter.dialect)
== "COALESCE(CAST(id AS VARCHAR(MAX)), '') + ',' + COALESCE(CAST(name AS VARCHAR(MAX)), '')"
)