From 46a29a175d515072a2766ba7d8936574420e3167 Mon Sep 17 00:00:00 2001 From: Vladimir Kozbin Date: Thu, 28 Jun 2018 22:10:02 +0700 Subject: [PATCH] Support supplying raw integers in DateTime columns to avoid costly timezone conversions --- src/columns/datetimecolumn.py | 7 ++++++- tests/columns/test_datetime.py | 25 +++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/src/columns/datetimecolumn.py b/src/columns/datetimecolumn.py index 0b0644f4..d24755dc 100644 --- a/src/columns/datetimecolumn.py +++ b/src/columns/datetimecolumn.py @@ -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): @@ -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: diff --git a/tests/columns/test_datetime.py b/tests/columns/test_datetime.py index ea5f5991..946c37df 100644 --- a/tests/columns/test_datetime.py +++ b/tests/columns/test_datetime.py @@ -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]