Skip to content

Commit 36850e7

Browse files
Jessesaishreeeee
authored andcommitted
SQLAlchemy 2: Add support for TINYINT (#265)
Closes #123 Signed-off-by: Jesse Whitehouse <jesse.whitehouse@databricks.com> Signed-off-by: Sai Shree Pradhan <saishree.pradhan@databricks.com>
1 parent ee23215 commit 36850e7

File tree

6 files changed

+68
-0
lines changed

6 files changed

+68
-0
lines changed

src/databricks/sqlalchemy/README.tests.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ We maintain three files of test cases that we import from the SQLAlchemy source
1717

1818
In some cases, only certain tests in class should be skipped with a `SkipReason` or `FutureFeature` justification. In those cases, we import the class into `_regression.py`, then import it from there into one or both of `_future.py` and `_unsupported.py`. If a class needs to be "touched" by regression, unsupported, and future, the class will be imported in that order. If an entire class should be skipped, then we do not import it into `_regression.py` at all.
1919

20+
We maintain `_extra.py` with test cases that depend on SQLAlchemy's reusable dialect test fixtures but which are specific to Databricks (e.g TinyIntegerTest).
21+
2022
## Running the reusable dialect tests
2123

2224
```

src/databricks/sqlalchemy/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,4 @@
11
from databricks.sqlalchemy.base import DatabricksDialect
2+
from databricks.sqlalchemy._types import TINYINT
3+
4+
__all__ = ["TINYINT"]

src/databricks/sqlalchemy/_types.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,3 +212,15 @@ def process(value):
212212
return "%s" % _step2
213213

214214
return process
215+
216+
217+
class TINYINT(sqlalchemy.types.TypeDecorator):
218+
"""Represents 1-byte signed integers
219+
220+
Acts like a sqlalchemy SmallInteger() in Python but writes to a TINYINT field in Databricks
221+
222+
https://docs.databricks.com/en/sql/language-manual/data-types/tinyint-type.html
223+
"""
224+
225+
impl = sqlalchemy.types.SmallInteger
226+
cache_ok = True
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
"""Additional tests authored by Databricks that use SQLAlchemy's test fixtures
2+
"""
3+
4+
from sqlalchemy.testing.suite.test_types import (
5+
_LiteralRoundTripFixture,
6+
fixtures,
7+
testing,
8+
eq_,
9+
select,
10+
Table,
11+
Column,
12+
config,
13+
)
14+
from databricks.sqlalchemy import TINYINT
15+
16+
17+
class TinyIntegerTest(_LiteralRoundTripFixture, fixtures.TestBase):
18+
__backend__ = True
19+
20+
def test_literal(self, literal_round_trip):
21+
literal_round_trip(TINYINT, [5], [5])
22+
23+
@testing.fixture
24+
def integer_round_trip(self, metadata, connection):
25+
def run(datatype, data):
26+
int_table = Table(
27+
"tiny_integer_table",
28+
metadata,
29+
Column(
30+
"id",
31+
TINYINT,
32+
primary_key=True,
33+
test_needs_autoincrement=False,
34+
),
35+
Column("tiny_integer_data", datatype),
36+
)
37+
38+
metadata.create_all(config.db)
39+
40+
connection.execute(int_table.insert(), {"id": 1, "integer_data": data})
41+
42+
row = connection.execute(select(int_table.c.integer_data)).first()
43+
44+
eq_(row, (data,))
45+
46+
assert isinstance(row[0], int)
47+
48+
return run

src/databricks/sqlalchemy/test/test_suite.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@
99
from databricks.sqlalchemy.test._regression import *
1010
from databricks.sqlalchemy.test._unsupported import *
1111
from databricks.sqlalchemy.test._future import *
12+
from databricks.sqlalchemy.test._extra import TinyIntegerTest

src/databricks/sqlalchemy/test_local/test_types.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import sqlalchemy
55

66
from databricks.sqlalchemy.base import DatabricksDialect
7+
from databricks.sqlalchemy._types import TINYINT
78

89

910
class DatabricksDataType(enum.Enum):
@@ -127,6 +128,7 @@ def test_numeric_renders_as_decimal_with_precision_and_scale(self):
127128
sqlalchemy.types.INT: DatabricksDataType.INT,
128129
sqlalchemy.types.SMALLINT: DatabricksDataType.SMALLINT,
129130
sqlalchemy.types.TIMESTAMP: DatabricksDataType.TIMESTAMP,
131+
TINYINT: DatabricksDataType.TINYINT,
130132
}
131133

132134

0 commit comments

Comments
 (0)