Skip to content

Commit f9a0d0c

Browse files
authored
Merge pull request #14 from axellpadilla/patch/sqlserver-column-n-type-expansion
Enhance SQLServerColumn to support fixed numeric types and update tests
2 parents 5a81688 + ddc913c commit f9a0d0c

File tree

2 files changed

+49
-4
lines changed

2 files changed

+49
-4
lines changed

dbt/adapters/sqlserver/sqlserver_column.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,9 @@ def is_string(self) -> bool:
7676
return self.dtype.lower() in ["varchar", "char", "nvarchar", "nchar"]
7777

7878
def is_number(self):
79-
return any([self.is_integer(), self.is_numeric(), self.is_float()])
79+
return any(
80+
[self.is_integer(), self.is_numeric(), self.is_float(), self.is_fixed_numeric()]
81+
)
8082

8183
def is_float(self):
8284
return self.dtype.lower() in ["float", "real"]
@@ -87,7 +89,10 @@ def is_integer(self) -> bool:
8789
return self.dtype.lower() in ["int", "integer", "bigint", "smallint", "tinyint", "bit"]
8890

8991
def is_numeric(self) -> bool:
90-
return self.dtype.lower() in ["numeric", "decimal", "money", "smallmoney"]
92+
return self.dtype.lower() in ["numeric", "decimal"]
93+
94+
def is_fixed_numeric(self) -> bool:
95+
return self.dtype.lower() in ["money", "smallmoney"]
9196

9297
def string_size(self) -> int:
9398
if not self.is_string():
@@ -142,13 +147,15 @@ def can_expand_to(
142147

143148
# Numeric/Decimal promotions: allow when target precision >= source precision
144149
# and target scale >= source scale (so we don't lose fractional digits).
145-
if self.is_numeric() and other_column.is_numeric():
150+
if (self.is_numeric() or self.is_fixed_numeric()) and (
151+
other_column.is_numeric() or other_column.is_fixed_numeric()
152+
):
146153
# Access precision/scale directly from columns. Fall back to 0 when missing.
147154
self_scale = int(self.numeric_scale or 0)
148155
other_scale = int(other_column.numeric_scale or 0)
149156

150157
if other_prec >= self_prec and other_scale >= self_scale:
151-
if other_prec > self_prec or other_scale > self_scale:
158+
if other_prec > self_prec or other_scale > self_scale or self_dtype != other_dtype:
152159
return True
153160

154161
return False

tests/unit/adapters/mssql/test_can_expand_to.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,44 @@ def col_kwargs(dtype, char_size=None, numeric_precision=0, numeric_scale=0):
3838
# String family change (VARCHAR -> NVARCHAR) is only allowed when the
3939
# feature flag is set and capacity is sufficient
4040
(col_kwargs("varchar", char_size=10), col_kwargs("nvarchar", char_size=10), True, False),
41+
# Fixed-money types (MONEY/SMALLMONEY) behaviour
42+
# SMALLMONEY -> MONEY (widening) requires the feature flag
43+
(
44+
col_kwargs("smallmoney", numeric_precision=10, numeric_scale=4),
45+
col_kwargs("money", numeric_precision=19, numeric_scale=4),
46+
True,
47+
False,
48+
),
49+
# MONEY -> NUMERIC with larger precision/scale should be allowed
50+
(
51+
col_kwargs("money", numeric_precision=19, numeric_scale=4),
52+
col_kwargs("numeric", numeric_precision=20, numeric_scale=4),
53+
True,
54+
False,
55+
),
56+
# SMALLMONEY -> NUMERIC with larger precision/scale should be allowed
57+
(
58+
col_kwargs("smallmoney", numeric_precision=10, numeric_scale=4),
59+
col_kwargs("numeric", numeric_precision=20, numeric_scale=4),
60+
True,
61+
False,
62+
),
63+
# Equal precision/scale between MONEY and NUMERIC is considered an
64+
# expansion when the dtype changes (money -> numeric) under the
65+
# feature flag
66+
(
67+
col_kwargs("money", numeric_precision=19, numeric_scale=4),
68+
col_kwargs("numeric", numeric_precision=19, numeric_scale=4),
69+
True,
70+
False,
71+
),
72+
# NUMERIC -> MONEY that would shrink precision should not be allowed
73+
(
74+
col_kwargs("numeric", numeric_precision=20, numeric_scale=4),
75+
col_kwargs("money", numeric_precision=19, numeric_scale=4),
76+
False,
77+
False,
78+
),
4179
],
4280
)
4381
def test_can_expand_parametrized(src_kwargs, tgt_kwargs, expect_with_flag, expect_without_flag):

0 commit comments

Comments
 (0)