Skip to content

Commit cc00d0e

Browse files
committed
feat: #108 Queryset.iterator use clickhouse_driver.Client.execute_iter
1 parent 52ced95 commit cc00d0e

File tree

4 files changed

+14
-3
lines changed

4 files changed

+14
-3
lines changed

clickhouse_backend/backend/base.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,8 @@ def __init__(self, settings_dict, alias=DEFAULT_DB_ALIAS):
163163
self.migration_cluster = self.settings_dict["OPTIONS"].pop(
164164
"migration_cluster", None
165165
)
166+
# https://clickhouse-driver.readthedocs.io/en/latest/quickstart.html#streaming-results
167+
self.max_block_size = self.settings_dict["OPTIONS"].pop("max_block_size", 65409)
166168
if not self.settings_dict["NAME"]:
167169
self.settings_dict["NAME"] = "default"
168170

@@ -224,6 +226,12 @@ def init_connection_state(self):
224226
def create_cursor(self, name=None):
225227
return self.connection.cursor()
226228

229+
@async_unsafe
230+
def chunked_cursor(self):
231+
cursor = self._cursor()
232+
cursor.cursor.set_stream_results(True, self.max_block_size)
233+
return cursor
234+
227235
def _savepoint(self, sid):
228236
pass
229237

clickhouse_backend/driver/connection.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,9 +116,9 @@ class Connection(dbapi_connection.Connection):
116116
"""Connection class with support for connection pool."""
117117

118118
def __init__(self, *args, **kwargs):
119-
super().__init__(*args, **kwargs)
120119
kwargs.setdefault("connections_min", 10)
121120
kwargs.setdefault("connections_max", 100)
121+
super().__init__(*args, **kwargs)
122122
self.pool = ClickhousePool(
123123
dsn=self.dsn,
124124
host=self.host,

clickhouse_backend/driver/pool.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ def __init__(self, **kwargs):
1717
self.dsn = kwargs.pop("dsn", None)
1818
self.connections_min = kwargs.pop("connections_min", 10)
1919
self.connections_max = kwargs.pop("connections_max", 20)
20-
21-
self.connection_args = {"host": kwargs.pop("host", "localhost"), **kwargs}
20+
kwargs.setdefault("host", "localhost")
21+
self.connection_args = kwargs
2222
self.closed = False
2323
self._pool = []
2424
self._used = {}

docs/Configurations.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,13 @@ Migration table will be created on this cluster with [Distributed Engine](https:
4040
- `connections_min` is maximum number of connections can be kept in connection pool, default 10. Set this value to 0 will disable connection pool.
4141
- `connections_max` is maximum number of connections can be used, default 100. In fact, `connections_max` is maximum numbers of queries one can execute concurrently.
4242
Because [source code of DBAPI Connection](https://github.com/mymarilyn/clickhouse-driver/blob/0.2.5/clickhouse_driver/dbapi/connection.py#L46) shows that every cursor creates a new connection.
43+
- `max_block_size` is used for [streaming results](https://clickhouse-driver.readthedocs.io/en/latest/quickstart.html#streaming-results).
4344
- `dsn` provide connection url, for example `clickhouse://localhost/test?param1=value1&...`. If dsn is provided, all other connection parameters are ignored.
4445
- All other [clickhouse_driver.connection.Connection](https://clickhouse-driver.readthedocs.io/en/latest/api.html#connection) parameters.
4546
- `settings` can contain [clickhouse_driver.Client](https://clickhouse-driver.readthedocs.io/en/latest/api.html?highlight=client#clickhouse_driver.Client) settings and [clickhouse settings](https://clickhouse.com/docs/en/operations/settings/settings).
4647

48+
> *Changed in version 1.3.2:* Add `max_block_size`, refer [#108](https://github.com/jayvynl/django-clickhouse-backend/issues/108).
49+
4750
Valid `TEST` keys:
4851

4952
- `managed`: whether create(`True`) test database or not(`False`), default `True`.

0 commit comments

Comments
 (0)