Skip to content

Commit

Permalink
Support for asynchronous streaming results (#323)
Browse files Browse the repository at this point in the history
  • Loading branch information
Simon-Chenzw authored Aug 5, 2024
1 parent 1d6a6b3 commit b0fb8ba
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
9 changes: 8 additions & 1 deletion clickhouse_sqlalchemy/drivers/asynch/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,24 @@
from sqlalchemy.pool import AsyncAdaptedQueuePool

from .connector import AsyncAdapt_asynch_dbapi
from ..native.base import ClickHouseDialect_native
from ..native.base import ClickHouseDialect_native, ClickHouseExecutionContext

# Export connector version
VERSION = (0, 0, 1, None)


class ClickHouseAsynchExecutionContext(ClickHouseExecutionContext):
def create_server_side_cursor(self):
return self.create_default_cursor()


class ClickHouseDialect_asynch(ClickHouseDialect_native):
driver = 'asynch'
execution_ctx_cls = ClickHouseAsynchExecutionContext

is_async = True
supports_statement_cache = True
supports_server_side_cursors = True

@classmethod
def import_dbapi(cls):
Expand Down
13 changes: 13 additions & 0 deletions tests/drivers/asynch/test_cursor.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,16 @@ async def test_check_iter_cursor(self):
)

self.assertListEqual(list(rv), [(x,) for x in range(5)])

@run_async
async def test_execute_with_stream(self):
conn = await self.get_connection()
async with conn.stream(
text("SELECT * FROM system.numbers LIMIT 10")
) as result:
idx = 0
async for r in result:
self.assertEqual(r[0], idx)
idx += 1

self.assertEqual(idx, 10)

0 comments on commit b0fb8ba

Please sign in to comment.