Skip to content
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

fix: correctly handle unbound varchar case instead of defaulting to varchar(256) #194

Merged
merged 2 commits into from
Feb 10, 2023
Merged
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
7 changes: 7 additions & 0 deletions .changes/unreleased/Fixes-20221209-114403.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
kind: Fixes
body: Handle unbound varchar data_type instead of defaulting to varchar(256)
time: 2022-12-09T11:44:03.689108-08:00
custom:
Author: kalvinnchau
Issue: "35"
PR: "194"
20 changes: 20 additions & 0 deletions dbt/adapters/trino/column.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
from dbt.adapters.base.column import Column
from dbt.exceptions import RuntimeException

# Taken from the MAX_LENGTH variable in
# https://github.com/trinodb/trino/blob/master/core/trino-spi/src/main/java/io/trino/spi/type/VarcharType.java
TRINO_VARCHAR_MAX_LENGTH = 2147483646


@dataclass
class TrinoColumn(Column):
Expand All @@ -15,10 +19,26 @@ class TrinoColumn(Column):
"INTEGER": "INT",
}

@property
def data_type(self):
# when varchar has no defined size, default to unbound varchar
# the super().data_type defaults to varchar(256)
if self.dtype.lower() == "varchar" and self.char_size is None:
return self.dtype

return super().data_type

@classmethod
def string_type(cls, size: int) -> str:
return "varchar({})".format(size)

def string_size(self) -> int:
# override the string_size function to handle the unbound varchar case
if self.dtype.lower() == "varchar" and self.char_size is None:
return TRINO_VARCHAR_MAX_LENGTH

return super().string_size()

@classmethod
def from_description(cls, name: str, raw_data_type: str) -> "Column":
# some of the Trino data types specify a type and not a precision
Expand Down
26 changes: 26 additions & 0 deletions tests/unit/test_adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from dbt.exceptions import DatabaseException, FailedToConnectException, RuntimeException

from dbt.adapters.trino import TrinoAdapter
from dbt.adapters.trino.column import TRINO_VARCHAR_MAX_LENGTH, TrinoColumn
from dbt.adapters.trino.connections import (
HttpScheme,
TrinoCertificateCredentials,
Expand Down Expand Up @@ -530,3 +531,28 @@ def test_convert_date_type(self):
expected = ["DATE", "DATE", "DATE"]
for col_idx, expect in enumerate(expected):
assert TrinoAdapter.convert_date_type(agate_table, col_idx) == expect


class TestTrinoColumn(unittest.TestCase):
def test_bound_varchar(self):
col = TrinoColumn.from_description("my_col", "VARCHAR(100)")
assert col.column == "my_col"
assert col.dtype == "VARCHAR"
assert col.char_size == 100
# bounded varchars get formatted to lowercase
assert col.data_type == "varchar(100)"
assert col.string_size() == 100
assert col.is_string() is True
assert col.is_number() is False
assert col.is_numeric() is False

def test_unbound_varchar(self):
col = TrinoColumn.from_description("my_col", "VARCHAR")
assert col.column == "my_col"
assert col.dtype == "VARCHAR"
assert col.char_size is None
assert col.data_type == "VARCHAR"
assert col.string_size() == TRINO_VARCHAR_MAX_LENGTH
assert col.is_string() is True
assert col.is_number() is False
assert col.is_numeric() is False