|
1 | | -from dbt.adapters.fabric import FabricColumn |
| 1 | +from typing import Any, ClassVar, Dict |
2 | 2 |
|
| 3 | +from dbt.adapters.base import Column |
| 4 | +from dbt_common.exceptions import DbtRuntimeError |
| 5 | + |
| 6 | + |
| 7 | +class SQLServerColumn(Column): |
| 8 | + TYPE_LABELS: ClassVar[Dict[str, str]] = { |
| 9 | + "STRING": "VARCHAR(8000)", |
| 10 | + "VARCHAR": "VARCHAR(8000)", |
| 11 | + "CHAR": "CHAR(1)", |
| 12 | + "NCHAR": "NCHAR(1)", |
| 13 | + "NVARCHAR": "NVARCHAR(4000)", |
| 14 | + "TIMESTAMP": "DATETIME2(6)", |
| 15 | + "DATETIME2": "DATETIME2(6)", |
| 16 | + "DATETIME2(6)": "DATETIME2(6)", |
| 17 | + "DATE": "DATE", |
| 18 | + "TIME": "TIME(6)", |
| 19 | + "FLOAT": "FLOAT", |
| 20 | + "REAL": "REAL", |
| 21 | + "INT": "INT", |
| 22 | + "INTEGER": "INT", |
| 23 | + "BIGINT": "BIGINT", |
| 24 | + "SMALLINT": "SMALLINT", |
| 25 | + "TINYINT": "SMALLINT", |
| 26 | + "BIT": "BIT", |
| 27 | + "BOOLEAN": "BIT", |
| 28 | + "DECIMAL": "DECIMAL", |
| 29 | + "NUMERIC": "NUMERIC", |
| 30 | + "MONEY": "DECIMAL", |
| 31 | + "SMALLMONEY": "DECIMAL", |
| 32 | + "UNIQUEIDENTIFIER": "UNIQUEIDENTIFIER", |
| 33 | + "VARBINARY": "VARBINARY(MAX)", |
| 34 | + "BINARY": "BINARY(1)", |
| 35 | + } |
| 36 | + |
| 37 | + @classmethod |
| 38 | + def string_type(cls, size: int) -> str: |
| 39 | + """Class-level string_type used by SQLAdapter.expand_column_types. |
| 40 | +
|
| 41 | + Return a VARCHAR default for the SQLAdapter path; this keeps behaviour |
| 42 | + consistent with the rest of dbt where class-level string_type is |
| 43 | + generic and not instance-aware. |
| 44 | + """ |
| 45 | + return f"varchar({size if size > 0 else '8000'})" |
| 46 | + |
| 47 | + def string_type_instance(self, size: int) -> str: |
| 48 | + """ |
| 49 | + Instance-level string type selection that respects NVARCHAR/NCHAR. |
| 50 | + """ |
| 51 | + dtype = (self.dtype or "").lower() |
| 52 | + # n types use half the byte size for character count |
| 53 | + if dtype == "nvarchar": |
| 54 | + return f"nvarchar({size//2 if size > 0 else '4000'})" |
| 55 | + if dtype == "nchar": |
| 56 | + return f"nchar({size//2 if size > 1 else '1'})" |
| 57 | + # default to varchar/char behaviour |
| 58 | + return f"varchar({size if size > 0 else '8000'})" |
| 59 | + |
| 60 | + def literal(self, value: Any) -> str: |
| 61 | + return "cast('{}' as {})".format(value, self.data_type) |
| 62 | + |
| 63 | + @property |
| 64 | + def data_type(self) -> str: |
| 65 | + # Always enforce datetime2 precision |
| 66 | + if self.dtype.lower() == "datetime2": |
| 67 | + return "datetime2(6)" |
| 68 | + if self.is_string(): |
| 69 | + return self.string_type_instance(self.string_size()) |
| 70 | + elif self.is_numeric(): |
| 71 | + return self.numeric_type(self.dtype, self.numeric_precision, self.numeric_scale) |
| 72 | + else: |
| 73 | + return self.dtype |
| 74 | + |
| 75 | + def is_string(self) -> bool: |
| 76 | + return self.dtype.lower() in ["varchar", "char", "nvarchar", "nchar"] |
| 77 | + |
| 78 | + def is_number(self): |
| 79 | + return any([self.is_integer(), self.is_numeric(), self.is_float()]) |
| 80 | + |
| 81 | + def is_float(self): |
| 82 | + return self.dtype.lower() in ["float", "real"] |
3 | 83 |
|
4 | | -class SQLServerColumn(FabricColumn): |
5 | 84 | def is_integer(self) -> bool: |
6 | | - return self.dtype.lower() in [ |
7 | | - # real types |
8 | | - "smallint", |
9 | | - "integer", |
10 | | - "bigint", |
11 | | - "smallserial", |
12 | | - "serial", |
13 | | - "bigserial", |
14 | | - # aliases |
15 | | - "int2", |
16 | | - "int4", |
17 | | - "int8", |
18 | | - "serial2", |
19 | | - "serial4", |
20 | | - "serial8", |
21 | | - "int", |
22 | | - ] |
| 85 | + # Treat BIT as an integer-like type so it participates in integer |
| 86 | + # promotions (bit -> tinyint -> smallint -> int -> bigint). |
| 87 | + return self.dtype.lower() in ["int", "integer", "bigint", "smallint", "tinyint", "bit"] |
| 88 | + |
| 89 | + def is_numeric(self) -> bool: |
| 90 | + return self.dtype.lower() in ["numeric", "decimal", "money", "smallmoney"] |
| 91 | + |
| 92 | + def string_size(self) -> int: |
| 93 | + if not self.is_string(): |
| 94 | + raise DbtRuntimeError("Called string_size() on non-string field!") |
| 95 | + if self.char_size is None: |
| 96 | + return 8000 |
| 97 | + else: |
| 98 | + return int(self.char_size) |
| 99 | + |
| 100 | + def can_expand_to( |
| 101 | + self, other_column: Column, enable_safe_type_expansion: bool = False |
| 102 | + ) -> bool: |
| 103 | + # If both are strings, allow size-based expansion regardless of the |
| 104 | + # feature flag. Only allow family changes (VARCHAR -> NVARCHAR) when |
| 105 | + # `enable_safe_type_expansion` is set by the adapter. |
| 106 | + self_dtype = self.dtype.lower() |
| 107 | + other_dtype = other_column.dtype.lower() |
| 108 | + if self.is_string() and other_column.is_string(): |
| 109 | + self_size = self.string_size() |
| 110 | + other_size = other_column.string_size() |
| 111 | + |
| 112 | + if other_size > self_size and self_dtype == other_dtype: |
| 113 | + return True |
| 114 | + |
| 115 | + # Allow safe conversions across the CHAR/VARCHAR -> NCHAR/NVARCHAR family |
| 116 | + # only when the feature flag is enabled. Do NOT allow shrinking |
| 117 | + # conversions or NVARCHAR -> VARCHAR. |
| 118 | + if self_dtype in ("varchar", "char") and other_dtype in ("nvarchar", "nchar"): |
| 119 | + # allow when target has at least the same character capacity |
| 120 | + if other_size >= self_size and enable_safe_type_expansion: |
| 121 | + return True |
| 122 | + |
| 123 | + # If none of the string rules matched, we can't expand. |
| 124 | + return False |
| 125 | + |
| 126 | + # If we reach here, at least one side is not a string. Apply integer/ |
| 127 | + # numeric promotion logic only if the adapter has enabled type expansion. |
| 128 | + if not enable_safe_type_expansion or not self.is_number() or not other_column.is_number(): |
| 129 | + return False |
| 130 | + |
| 131 | + # Integer family promotions (tinyint -> smallint -> int -> bigint) |
| 132 | + int_family = ("bit", "tinyint", "smallint", "int", "bigint") |
| 133 | + if self_dtype in int_family and other_dtype in int_family: |
| 134 | + if int_family.index(other_dtype) > int_family.index(self_dtype): |
| 135 | + return True |
| 136 | + |
| 137 | + self_prec = int(self.numeric_precision or 0) |
| 138 | + other_prec = int(other_column.numeric_precision or 0) |
| 139 | + # Integer -> numeric/decimal is a safe widening (integers fit in numerics). |
| 140 | + if self.is_integer() and other_column.is_numeric() and other_prec > self_prec: |
| 141 | + return True |
| 142 | + |
| 143 | + # Numeric/Decimal promotions: allow when target precision >= source precision |
| 144 | + # and target scale >= source scale (so we don't lose fractional digits). |
| 145 | + if self.is_numeric() and other_column.is_numeric(): |
| 146 | + # Access precision/scale directly from columns. Fall back to 0 when missing. |
| 147 | + self_scale = int(self.numeric_scale or 0) |
| 148 | + other_scale = int(other_column.numeric_scale or 0) |
| 149 | + |
| 150 | + if other_prec >= self_prec and other_scale >= self_scale: |
| 151 | + if other_prec > self_prec or other_scale > self_scale: |
| 152 | + return True |
| 153 | + |
| 154 | + return False |
0 commit comments