Skip to content

Commit

Permalink
Add Date32 Type (#307)
Browse files Browse the repository at this point in the history
* Add date32 type

* Add date32 type

* Fix

* Flake linting fix

* Set required server version to 21_9

* Lint

* Fix test
  • Loading branch information
BTheunissen authored Apr 23, 2024
1 parent 83c5b46 commit 7ad94b0
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 0 deletions.
1 change: 1 addition & 0 deletions clickhouse_sqlalchemy/drivers/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
'UInt16': types.UInt16,
'UInt8': types.UInt8,
'Date': types.Date,
'Date32': types.Date32,
'DateTime': types.DateTime,
'DateTime64': types.DateTime64,
'Float64': types.Float64,
Expand Down
3 changes: 3 additions & 0 deletions clickhouse_sqlalchemy/drivers/compilers/typecompiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ def visit_uint256(self, type_, **kw):
def visit_date(self, type_, **kw):
return 'Date'

def visit_date32(self, type_, **kw):
return 'Date32'

def visit_datetime(self, type_, **kw):
if type_.timezone:
return "DateTime('%s')" % type_.timezone
Expand Down
2 changes: 2 additions & 0 deletions clickhouse_sqlalchemy/types/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
'Float32',
'Float64',
'Date',
'Date32',
'DateTime',
'DateTime64',
'Enum',
Expand Down Expand Up @@ -60,6 +61,7 @@
from .common import Float32
from .common import Float64
from .common import Date
from .common import Date32
from .common import DateTime
from .common import DateTime64
from .common import Enum
Expand Down
4 changes: 4 additions & 0 deletions clickhouse_sqlalchemy/types/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,10 @@ class Date(types.Date, ClickHouseTypeEngine):
__visit_name__ = 'date'


class Date32(types.Date, ClickHouseTypeEngine):
__visit_name__ = 'date32'


class DateTime(types.DateTime, ClickHouseTypeEngine):
__visit_name__ = 'datetime'

Expand Down
46 changes: 46 additions & 0 deletions tests/types/test_date32.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import datetime

from sqlalchemy import Column
from sqlalchemy.sql.ddl import CreateTable

from clickhouse_sqlalchemy import types, engines, Table
from tests.testcase import BaseTestCase, CompilationTestCase
from tests.util import with_native_and_http_sessions


class Date32CompilationTestCase(CompilationTestCase):
required_server_version = (21, 9, 0)

def test_create_table(self):
table = Table(
'test', CompilationTestCase.metadata(),
Column('x', types.Date32, primary_key=True),
engines.Memory()
)

self.assertEqual(
self.compile(CreateTable(table)),
'CREATE TABLE test (x Date32) ENGINE = Memory'
)


@with_native_and_http_sessions
class Date32TestCase(BaseTestCase):
required_server_version = (21, 9, 0)

table = Table(
'test', BaseTestCase.metadata(),
Column('x', types.Date32, primary_key=True),
engines.Memory()
)

def test_select_insert(self):
# Use a date before epoch to validate dates before epoch can be stored.
date = datetime.date(1925, 1, 1)
with self.create_table(self.table):
self.session.execute(self.table.insert(), [{'x': date}])
result = self.session.execute(self.table.select()).scalar()
if isinstance(result, datetime.date):
self.assertEqual(result, date)
else:
self.assertEqual(result, date.isoformat())

0 comments on commit 7ad94b0

Please sign in to comment.