Skip to content

Commit

Permalink
Same timezone fix (#158)
Browse files Browse the repository at this point in the history
* Don't apply timezone if server/client match

* Tiny cleanup
  • Loading branch information
genzgd authored Mar 30, 2023
1 parent d239bc9 commit b1c78f8
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 6 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ in a future release. Starting with 0.5.9 the driver now requests ClickHouse pro
The secondary effect of the `send_progress` argument -- to set `wait_end_of_query=1` -- is now handled automatically based
on whether the query is streaming or not.

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


## 0.5.17, 2023-03-26
### Timezone Improvements
Expand Down
2 changes: 1 addition & 1 deletion clickhouse_connect/VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.5.17
0.5.18
10 changes: 6 additions & 4 deletions clickhouse_connect/driver/client.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import io
import logging
from datetime import tzinfo
from datetime import tzinfo, datetime

import pytz

Expand All @@ -12,7 +12,7 @@
from clickhouse_connect.common import version
from clickhouse_connect.datatypes.registry import get_from_name
from clickhouse_connect.datatypes.base import ClickHouseType
from clickhouse_connect.driver.common import dict_copy, StreamContext, coerce_int
from clickhouse_connect.driver.common import dict_copy, StreamContext, coerce_int, coerce_bool
from clickhouse_connect.driver.constants import CH_VERSION_WITH_PROTOCOL, PROTOCOL_VERSION_WITH_LOW_CARD
from clickhouse_connect.driver.exceptions import ProgrammingError
from clickhouse_connect.driver.external import ExternalData
Expand Down Expand Up @@ -42,7 +42,7 @@ def __init__(self,
uri: str,
query_retries: int,
server_host_name: Optional[str],
apply_server_timezone: Optional[bool]):
apply_server_timezone: Optional[Union[str, bool]]):
"""
Shared initialization of ClickHouse Connect client
:param database: database name
Expand All @@ -52,14 +52,16 @@ def __init__(self,
self.query_limit = coerce_int(query_limit)
self.query_retries = coerce_int(query_retries)
self.server_host_name = server_host_name
self.apply_server_timezone = apply_server_timezone is True
self.server_tz = pytz.UTC
self.server_version, server_tz, self.database = \
tuple(self.command('SELECT version(), timezone(), currentDatabase()', use_database=False))
try:
self.server_tz = pytz.timezone(server_tz)
except UnknownTimeZoneError:
logger.warning('Warning, server is using an unrecognized timezone %s, will use UTC default', server_tz)
offsets_differ = datetime.now().astimezone().utcoffset() != datetime.now(tz=self.server_tz).utcoffset()
self.apply_server_timezone = apply_server_timezone == 'always' or (
coerce_bool(apply_server_timezone) and offsets_differ)
readonly = 'readonly'
if not self.min_version('19.17'):
readonly = common.get_setting('readonly')
Expand Down
2 changes: 1 addition & 1 deletion clickhouse_connect/driver/httpclient.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def __init__(self,
http_proxy: Optional[str] = None,
https_proxy: Optional[str] = None,
server_host_name: Optional[str] = None,
apply_server_timezone: Optional[bool] = True):
apply_server_timezone: Optional[Union[str, bool]] = True):
"""
Create an HTTP ClickHouse Connect client
See clickhouse_connect.get_client for parameters
Expand Down

0 comments on commit b1c78f8

Please sign in to comment.