Skip to content

sqlserver_column.py: Handle string dtype of nvarchar #606

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
29 changes: 29 additions & 0 deletions dbt/adapters/sqlserver/sqlserver_column.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from dbt.adapters.fabric import FabricColumn
from dbt_common.exceptions import DbtRuntimeError


class SQLServerColumn(FabricColumn):
Expand All @@ -20,3 +21,31 @@ def is_integer(self) -> bool:
"serial8",
"int",
]

def is_string(self) -> bool:
return self.dtype.lower() in [
"text",
"character varying",
"character",
"varchar",
"nvarchar",
]

def string_size(self) -> int:
if not self.is_string():
raise DbtRuntimeError("Called string_size() on non-string field!")

if self.dtype == "text" or self.char_size is None:
# char_size should never be None. Handle it reasonably just in case
return 256
elif self.dtype.lower() == "nvarchar":
# char_size is doubled for nvarchar
return int(self.char_size // 2)
else:
return int(self.char_size)

def string_type(self, size: int) -> str:
if self.dtype:
return f"{self.dtype}({size if size > 0 else '8000'})"
else:
return f"varchar({size if size > 0 else '8000'})"