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

Support supplying raw integers in DateTime columns to avoid costly ti… #42

Merged
merged 1 commit into from
Jun 29, 2018
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: 6 additions & 1 deletion src/columns/datetimecolumn.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

class DateTimeColumn(FormatColumn):
ch_type = 'DateTime'
py_types = (datetime, )
py_types = (datetime, int)
format = 'I'

def __init__(self, timezone=None, **kwargs):
Expand All @@ -21,6 +21,11 @@ def after_read_item(self, value):
return dt.replace(tzinfo=None)

def before_write_item(self, value):
if isinstance(value, int):
# support supplying raw integers to avoid
# costly timezone conversions when using datetime
return value

if self.timezone:
# Set server's timezone for offset-naive datetime.
if value.tzinfo is None:
Expand Down
25 changes: 25 additions & 0 deletions tests/columns/test_datetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,31 @@ def test_use_client_timezone(self):
inserted = self.client.execute(query, settings=settings)
self.assertEqual(inserted, [(self.dt, ), (self.dt, )])

def test_insert_integers(self):
settings = {'use_client_time_zone': True}

with self.patch_env_tz('Europe/Moscow'):
with self.create_table('a DateTime'):
self.client.execute(
'INSERT INTO test (a) VALUES', [(1530211034, )],
settings=settings
)

query = 'SELECT toUInt32(a), a FROM test'
inserted = self.emit_cli(query, use_client_time_zone=1)
self.assertEqual(inserted, '1530211034\t2018-06-28 21:37:14\n')

def test_insert_integer_bounds(self):
with self.create_table('a DateTime'):
self.client.execute(
'INSERT INTO test (a) VALUES',
[(0, ), (1, ), (1500000000, ), (2**32-1, )]
)

query = 'SELECT toUInt32(a) FROM test ORDER BY a'
inserted = self.emit_cli(query)
self.assertEqual(inserted, '0\n1\n1500000000\n4294967295\n')

@require_server_version(1, 1, 54337)
def test_datetime_with_timezone_use_server_timezone(self):
server_tz_name = self.client.execute('SELECT timezone()')[0][0]
Expand Down