Skip to content

Commit f02df5f

Browse files
CogitoAndrew Ardill
authored andcommitted
sqlserver_column.py: Handle string dtype of nvarchar
nvarchar is not considered a string in the base adapter, nor in the fabric adapter. This leads to issues when dealing with nvarchar columns, such as in https://github.com/dbt-msft/dbt-sqlserver-utils/blob/master/macros/sql/union.sql In this commit we add nvarchar to the list of string dtypes, return the correct string_size for nvarchar (which is doubled by default), and use the correct dtype in string_type
1 parent bcf4ac9 commit f02df5f

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

dbt/adapters/sqlserver/sqlserver_column.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,31 @@ def is_integer(self) -> bool:
2020
"serial8",
2121
"int",
2222
]
23+
24+
def is_string(self) -> bool:
25+
return self.dtype.lower() in [
26+
"text",
27+
"character varying",
28+
"character",
29+
"varchar",
30+
"nvarchar",
31+
]
32+
33+
def string_size(self) -> int:
34+
if not self.is_string():
35+
raise DbtRuntimeError("Called string_size() on non-string field!")
36+
37+
if self.dtype == "text" or self.char_size is None:
38+
# char_size should never be None. Handle it reasonably just in case
39+
return 256
40+
elif self.dtype.lower() == "nvarchar":
41+
# char_size is doubled for nvarchar
42+
return int(self.char_size // 2)
43+
else:
44+
return int(self.char_size)
45+
46+
def string_type(self, size: int) -> str:
47+
if self.dtype:
48+
return f"{self.dtype}({size if size > 0 else '8000'})"
49+
else:
50+
return f"varchar({size if size > 0 else '8000'})"

0 commit comments

Comments
 (0)