Skip to content
Open
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
20 changes: 20 additions & 0 deletions src/databricks/sqlalchemy/_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,10 @@ def process_result_value(self, value: Union[None, datetime], dialect):
return None
return value.replace(tzinfo=None)

@property
def python_type(self) -> type:
return datetime


class TIMESTAMP(sqlalchemy.types.TypeDecorator):
"""Represents values comprising values of fields year, month, day, hour, minute, and second,
Expand Down Expand Up @@ -191,6 +195,10 @@ def process_result_value(self, value: Union[None, datetime], dialect):
return value.replace(tzinfo=timezone.utc)
return value

@property
def python_type(self) -> type:
return datetime

def process_bind_param(
self, value: Union[datetime, None], dialect
) -> Optional[datetime]:
Expand Down Expand Up @@ -279,6 +287,10 @@ def process_result_value(

return self._parse_result_string(value)

@property
def python_type(self) -> type:
return time


class DatabricksStringType(sqlalchemy.types.TypeDecorator):
"""We have to implement our own String() type because SQLAlchemy's default implementation
Expand All @@ -290,6 +302,10 @@ class DatabricksStringType(sqlalchemy.types.TypeDecorator):
cache_ok = True
pe = ParamEscaper()

@property
def python_type(self) -> type:
return str

def process_literal_param(self, value, dialect) -> str:
"""SQLAlchemy's default string escaping for backslashes doesn't work for databricks. The logic here
implements the same logic as our legacy inline escaping logic.
Expand Down Expand Up @@ -406,6 +422,10 @@ class TINYINT(sqlalchemy.types.TypeDecorator):
impl = sqlalchemy.types.SmallInteger
cache_ok = True

@property
def python_type(self) -> type:
return int


@compiles(TINYINT, "databricks")
def compile_tinyint(type_, compiler, **kw):
Expand Down
34 changes: 34 additions & 0 deletions tests/test_local/test_types.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import datetime
import enum
from uuid import UUID

Expand All @@ -13,6 +14,7 @@
TIMESTAMP,
TIMESTAMP_NTZ,
)
from databricks.sqlalchemy._parse import GET_COLUMNS_TYPE_MAP


class DatabricksDataType(enum.Enum):
Expand Down Expand Up @@ -366,3 +368,35 @@ def test_as_uuid_false_round_trip_normalizes_hex_input(self):
assert bind(self.HYPHENATED) == self.HYPHENATED
assert result(self.HYPHENATED) == self.HYPHENATED
assert result(self.HEX) == self.HYPHENATED


class TestPythonType:
"""TypeDecorator does not take python_type from its impl, so each of our
TypeDecorators has to declare it. Without that, SQLAlchemy 2.0 raises
NotImplementedError and 2.1 returns ``object``.
"""

dialect = DatabricksDialect()

@pytest.mark.parametrize(
"type_, expected",
[
(TIMESTAMP(), datetime.datetime),
(TIMESTAMP_NTZ(), datetime.datetime),
(TINYINT(), int),
# The dialect swaps these in for Time() and String() via colspecs.
(sqlalchemy.Time().dialect_impl(dialect), datetime.time),
(sqlalchemy.String().dialect_impl(dialect), str),
],
)
def test_python_type(self, type_, expected):
assert type_.python_type is expected

@pytest.mark.parametrize(
"type_name",
sorted(name for name in GET_COLUMNS_TYPE_MAP if name != "variant"),
)
def test_reflected_types_report_python_type(self, type_name):
"""Every type that column reflection can return, except VARIANT, maps
to a concrete Python type."""
assert GET_COLUMNS_TYPE_MAP[type_name]().python_type is not object