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

Fix common settings bug #82

Merged
merged 1 commit into from
Nov 24, 2022
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
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,15 @@ other client methods, `get_client` now accepts an optional `settings` Dict[str,
to set ClickHouse server settings. The use of `**kwargs` for this purpose is deprecated and will be removed in a future
release.

## 0.4.5, 2022-11-24

### Bug Fixes
* Common settings were stored in an immutable named tuple and could not be changed. This is fixed.
* Fixed issue where the query_arrow method would not use the client database

## 0.4.4, 2022-11-22

### Bug fixes
### Bug Fixes
* Ignore all "transport settings" when validating settings. This should fix https://github.com/ClickHouse/clickhouse-connect/issues/80
for older ClickHouse versions

Expand Down
2 changes: 1 addition & 1 deletion clickhouse_connect/VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.4.4
0.4.5
6 changes: 4 additions & 2 deletions clickhouse_connect/common.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from typing import NamedTuple, Any, Sequence, Optional, Dict
from dataclasses import dataclass
from typing import Any, Sequence, Optional, Dict

import pkg_resources

Expand All @@ -9,7 +10,8 @@ def version():
return pkg_resources.get_distribution('clickhouse-connect').version


class CommonSetting(NamedTuple):
@dataclass
class CommonSetting():
name: str
options: Sequence[Any]
default: Any
Expand Down
1 change: 1 addition & 0 deletions clickhouse_connect/driver/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,7 @@ def query_arrow(self,
:return: PyArrow.Table
"""
settings = dict_copy(settings)
settings['database'] = self.database
if arrow_str_setting in self.server_settings and arrow_str_setting not in settings:
settings[arrow_str_setting] = '1' if use_strings else '0'
return to_arrow(self.raw_query(query, parameters, settings, 'Arrow'))
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 @@ -274,7 +274,7 @@ def command(self,
params.update(self._validate_settings(settings or {}))
method = 'POST' if payload else 'GET'
response = self._raw_request(payload, params, headers, method)
result = response.content.decode('utf8')[:-1].split('\t')
result = response.content.decode()[:-1].split('\t')
if len(result) == 1:
try:
return int(result[0])
Expand Down
32 changes: 17 additions & 15 deletions tests/integration_tests/test_arrow.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,29 @@
from typing import Callable

import pytest

from clickhouse_connect.driver import Client
from clickhouse_connect.driver.options import arrow


def test_arrow(test_client: Client, test_table_engine: str):
def test_arrow(test_client: Client, table_context: Callable):
if not arrow:
pytest.skip('PyArrow package not available')
arrow_table = test_client.query_arrow('SELECT database, name, total_rows as total_rows FROM system.tables',
use_strings=False)
arrow_schema = arrow_table.schema
assert arrow_schema.field(0).name == 'database'
assert arrow_schema.field(1).type.id == 14
assert arrow_schema.field(2).type.bit_width == 64
assert arrow_table.num_rows > 20
assert len(arrow_table.columns) == 3
with table_context('test_arrow_insert', ['animal String', 'legs Int64']):
n_legs = arrow.array([2, 4, 5, 100])
animals = arrow.array(['Flamingo', 'Horse', 'Brittle stars', 'Centipede'])
names = ['legs', 'animal']
insert_table = arrow.Table.from_arrays([n_legs, animals], names=names)
test_client.insert_arrow('test_arrow_insert', insert_table)
result_table = test_client.query_arrow('SELECT * FROM test_arrow_insert', use_strings=False)
arrow_schema = result_table.schema
assert arrow_schema.field(0).name == 'animal'
assert arrow_schema.field(0).type.id == 14
assert arrow_schema.field(1).type.bit_width == 64
# pylint: disable=no-member
assert arrow.compute.sum(result_table['legs']).as_py() == 111
assert len(result_table.columns) == 2

test_client.command('DROP TABLE IF EXISTS test_arrow_insert')
test_client.command('CREATE TABLE test_arrow_insert (database String, name String, total_rows Nullable(UInt64))' +
f'ENGINE {test_table_engine} ORDER BY (database, name)')
test_client.insert_arrow('test_arrow_insert', arrow_table)
sum_total_rows = test_client.command('SELECT sum(total_rows) from test_arrow_insert')
assert sum_total_rows > 5
arrow_table = test_client.query_arrow('SELECT number from system.numbers LIMIT 500',
settings={'max_block_size': 50})
arrow_schema = arrow_table.schema
Expand Down
7 changes: 7 additions & 0 deletions tests/unit_tests/test_driver/test_settings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from clickhouse_connect import common


def test_setting():
assert common.get_setting('autogenerate_session_id')
common.set_setting('autogenerate_session_id', False)
assert common.get_setting('autogenerate_session_id') is False