Skip to content

Commit b1c78f8

Browse files
authored
Same timezone fix (#158)
* Don't apply timezone if server/client match * Tiny cleanup
1 parent d239bc9 commit b1c78f8

File tree

4 files changed

+15
-6
lines changed

4 files changed

+15
-6
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,13 @@ in a future release. Starting with 0.5.9 the driver now requests ClickHouse pro
1717
The secondary effect of the `send_progress` argument -- to set `wait_end_of_query=1` -- is now handled automatically based
1818
on whether the query is streaming or not.
1919

20+
## 0.5.18, 2023-03-30
21+
### Performance Improvement
22+
- The server timezone will not be applied (and Python datetime types will be timezone naive) if the client and server timezones match
23+
and the `get_client` apply_server_timezone parameter is True (the default). This improves performance where client and server
24+
have the same (non-UTC) timezone. To override this behavior and always apply a server timezone to the result, use `apply_server_timezone='always'`.
25+
This should fix https://github.com/ClickHouse/clickhouse-connect/issues/157
26+
2027

2128
## 0.5.17, 2023-03-26
2229
### Timezone Improvements

clickhouse_connect/VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
0.5.17
1+
0.5.18

clickhouse_connect/driver/client.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import io
22
import logging
3-
from datetime import tzinfo
3+
from datetime import tzinfo, datetime
44

55
import pytz
66

@@ -12,7 +12,7 @@
1212
from clickhouse_connect.common import version
1313
from clickhouse_connect.datatypes.registry import get_from_name
1414
from clickhouse_connect.datatypes.base import ClickHouseType
15-
from clickhouse_connect.driver.common import dict_copy, StreamContext, coerce_int
15+
from clickhouse_connect.driver.common import dict_copy, StreamContext, coerce_int, coerce_bool
1616
from clickhouse_connect.driver.constants import CH_VERSION_WITH_PROTOCOL, PROTOCOL_VERSION_WITH_LOW_CARD
1717
from clickhouse_connect.driver.exceptions import ProgrammingError
1818
from clickhouse_connect.driver.external import ExternalData
@@ -42,7 +42,7 @@ def __init__(self,
4242
uri: str,
4343
query_retries: int,
4444
server_host_name: Optional[str],
45-
apply_server_timezone: Optional[bool]):
45+
apply_server_timezone: Optional[Union[str, bool]]):
4646
"""
4747
Shared initialization of ClickHouse Connect client
4848
:param database: database name
@@ -52,14 +52,16 @@ def __init__(self,
5252
self.query_limit = coerce_int(query_limit)
5353
self.query_retries = coerce_int(query_retries)
5454
self.server_host_name = server_host_name
55-
self.apply_server_timezone = apply_server_timezone is True
5655
self.server_tz = pytz.UTC
5756
self.server_version, server_tz, self.database = \
5857
tuple(self.command('SELECT version(), timezone(), currentDatabase()', use_database=False))
5958
try:
6059
self.server_tz = pytz.timezone(server_tz)
6160
except UnknownTimeZoneError:
6261
logger.warning('Warning, server is using an unrecognized timezone %s, will use UTC default', server_tz)
62+
offsets_differ = datetime.now().astimezone().utcoffset() != datetime.now(tz=self.server_tz).utcoffset()
63+
self.apply_server_timezone = apply_server_timezone == 'always' or (
64+
coerce_bool(apply_server_timezone) and offsets_differ)
6365
readonly = 'readonly'
6466
if not self.min_version('19.17'):
6567
readonly = common.get_setting('readonly')

clickhouse_connect/driver/httpclient.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ def __init__(self,
6868
http_proxy: Optional[str] = None,
6969
https_proxy: Optional[str] = None,
7070
server_host_name: Optional[str] = None,
71-
apply_server_timezone: Optional[bool] = True):
71+
apply_server_timezone: Optional[Union[str, bool]] = True):
7272
"""
7373
Create an HTTP ClickHouse Connect client
7474
See clickhouse_connect.get_client for parameters

0 commit comments

Comments
 (0)