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

Add Date32 Type #307

Merged
merged 7 commits into from
Apr 23, 2024
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
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())