Skip to content

Commit a1bb428

Browse files
feat: FIR-43722 add streaming to python sdk (#424)
1 parent 28a90ac commit a1bb428

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+5283
-203
lines changed

.github/workflows/code-check.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,4 @@ jobs:
3030
pip install ".[dev]"
3131
3232
- name: Run pre-commit checks
33-
uses: pre-commit/action@v2.0.3
33+
uses: pre-commit/action@v3.0.1

docsrc/Connecting_and_queries.rst

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -672,6 +672,37 @@ will send a cancel request to the server and the query will be stopped.
672672
print(successful) # False
673673

674674

675+
676+
Streaming query results
677+
==============================
678+
679+
By default, the driver will fetch all the results at once and store them in memory.
680+
This does not always fit the needs of the application, especially when the result set is large.
681+
In this case, you can use the `execute_stream` cursor method to fetch results in chunks.
682+
683+
.. note::
684+
The `execute_stream` method is not supported with :ref:`connecting_and_queries:Server-side asynchronous query execution`. It can only be used with regular queries.
685+
686+
.. note::
687+
If you enable result streaming, the query execution might finish successfully, but the actual error might be returned while iterating the rows.
688+
689+
Synchronous example:
690+
::
691+
692+
with connection.cursor() as cursor:
693+
cursor.execute_stream("SELECT * FROM my_huge_table")
694+
for row in cursor:
695+
# Process the row
696+
print(row)
697+
698+
Asynchronous example:
699+
::
700+
async with async_connection.cursor() as cursor:
701+
await cursor.execute_stream("SELECT * FROM my_huge_table")
702+
async for row in cursor:
703+
# Process the row
704+
print(row)
705+
675706
Thread safety
676707
==============================
677708

examples/dbapi.ipynb

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@
194194
"id": "02e5db2f",
195195
"metadata": {},
196196
"source": [
197-
"### Error handling\n",
197+
"## Error handling\n",
198198
"If one query fails during the execution, all remaining queries are canceled.\n",
199199
"However, you still can fetch results for successful queries"
200200
]
@@ -219,6 +219,34 @@
219219
"cursor.fetchall()"
220220
]
221221
},
222+
{
223+
"cell_type": "raw",
224+
"id": "9789285b0362e8a6",
225+
"metadata": {
226+
"collapsed": false
227+
},
228+
"source": [
229+
"## Query result streaming\n",
230+
"\n",
231+
"Streaming is useful for large result sets, when you want to process rows one by one without loading all of them into memory."
232+
]
233+
},
234+
{
235+
"cell_type": "code",
236+
"id": "e96d2bda533b250d",
237+
"metadata": {
238+
"collapsed": false
239+
},
240+
"outputs": [],
241+
"source": [
242+
"cursor.execute_stream(\"select * from generate_series(1, 1000000)\")\n",
243+
"for row in cursor:\n",
244+
" print(row)\n",
245+
" if row[0] > 10:\n",
246+
" break\n",
247+
"# Remaining rows will not be fetched"
248+
]
249+
},
222250
{
223251
"cell_type": "markdown",
224252
"id": "b1cd4ff2",
@@ -377,6 +405,32 @@
377405
" pass\n",
378406
"async_conn.closed"
379407
]
408+
},
409+
{
410+
"cell_type": "raw",
411+
"id": "80a885228cbad698",
412+
"metadata": {
413+
"collapsed": false
414+
},
415+
"source": [
416+
"## Query result streaming"
417+
]
418+
},
419+
{
420+
"cell_type": "code",
421+
"id": "5eaaf1c35bac6fc6",
422+
"metadata": {
423+
"collapsed": false
424+
},
425+
"outputs": [],
426+
"source": [
427+
"await cursor.execute_stream(\"select * from generate_series(1, 1000000)\")\n",
428+
"async for row in cursor:\n",
429+
" print(row)\n",
430+
" if row[0] > 10:\n",
431+
" break\n",
432+
"# Remaining rows will not be fetched"
433+
]
380434
}
381435
],
382436
"metadata": {

setup.cfg

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ dev =
5555
devtools==0.7.0
5656
mypy==1.*,<1.10.0
5757
pre-commit==3.5.0
58+
psutil==7.0.0
5859
pyfakefs>=4.5.3,<=5.6.0
5960
pytest==7.2.0
6061
pytest-cov==3.0.0

0 commit comments

Comments
 (0)