Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
9a7e1ea
WIP implement synchronous streaming
stepansergeevitch Apr 10, 2025
b31630b
improve error handling, docs, etc.
stepansergeevitch Apr 10, 2025
8c2a51b
more docstring improvements
stepansergeevitch Apr 10, 2025
5491287
add tests for base row set classes
stepansergeevitch Apr 10, 2025
d9f785e
update streaming test
stepansergeevitch Apr 10, 2025
1be1fb9
add unit tests for row set implementations
stepansergeevitch Apr 10, 2025
a4634dc
extend tests
stepansergeevitch Apr 10, 2025
48d48be
add async streaming rowset
stepansergeevitch Apr 11, 2025
c54e8a8
fix unit tests
stepansergeevitch Apr 11, 2025
575dd45
added async in memory row set tests
stepansergeevitch Apr 11, 2025
e32c8a2
add more tests for row sets
stepansergeevitch Apr 11, 2025
11a93c4
add async streaming unit tests
stepansergeevitch Apr 11, 2025
7a05975
add execute_stream to cursors
stepansergeevitch Apr 11, 2025
5f3f83b
improve json lines parsing
stepansergeevitch Apr 14, 2025
5945cf5
add new types parsing
stepansergeevitch Apr 14, 2025
0cd0e1e
add cursor streaming test
stepansergeevitch Apr 14, 2025
39aa2d5
extend cursor tests
stepansergeevitch Apr 14, 2025
01339a7
disable streaming for v1
stepansergeevitch Apr 14, 2025
561b627
add streaming integration tests
stepansergeevitch Apr 14, 2025
7a29f08
fix response streaming
stepansergeevitch Apr 14, 2025
a20dc79
uncomment needed code
stepansergeevitch Apr 15, 2025
e54c55c
fix unit tests
stepansergeevitch Apr 15, 2025
c103cbf
raise error for v1 streaming
stepansergeevitch Apr 15, 2025
cae4334
fix type annotation
stepansergeevitch Apr 15, 2025
b180683
improve error handling
stepansergeevitch Apr 15, 2025
48de18d
fix error handling
stepansergeevitch Apr 15, 2025
8d0a48f
add documentation section
stepansergeevitch Apr 15, 2025
4d1c2b9
fix import errors
stepansergeevitch Apr 15, 2025
acdbf76
fix status code fetching from response body
stepansergeevitch Apr 15, 2025
fd704bb
fix streaming test
stepansergeevitch Apr 15, 2025
aaedc1e
mark tests as slow
stepansergeevitch Apr 17, 2025
b283cdd
address some comments
stepansergeevitch Apr 17, 2025
7bc1f92
convert context manager to a generator
stepansergeevitch Apr 17, 2025
83d5d51
improve documentation
stepansergeevitch Apr 17, 2025
29e5997
add jupyter examples
stepansergeevitch Apr 17, 2025
502e86a
simplify row set initialization
stepansergeevitch Apr 18, 2025
a5bbb5f
extend integration tests
stepansergeevitch Apr 18, 2025
5d5a312
fix unit tests
stepansergeevitch Apr 18, 2025
194b5ac
update pre-commit action
stepansergeevitch Apr 18, 2025
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
2 changes: 1 addition & 1 deletion .github/workflows/code-check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,4 @@ jobs:
pip install ".[dev]"

- name: Run pre-commit checks
uses: pre-commit/action@v2.0.3
uses: pre-commit/action@v3.0.1
31 changes: 31 additions & 0 deletions docsrc/Connecting_and_queries.rst
Original file line number Diff line number Diff line change
Expand Up @@ -672,6 +672,37 @@ will send a cancel request to the server and the query will be stopped.
print(successful) # False



Streaming query results
==============================

By default, the driver will fetch all the results at once and store them in memory.
This does not always fit the needs of the application, especially when the result set is large.
In this case, you can use the `execute_stream` cursor method to fetch results in chunks.

.. note::
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.

.. note::
If you enable result streaming, the query execution might finish successfully, but the actual error might be returned while iterating the rows.

Synchronous example:
::

with connection.cursor() as cursor:
cursor.execute_stream("SELECT * FROM my_huge_table")
for row in cursor:
# Process the row
print(row)

Asynchronous example:
::
async with async_connection.cursor() as cursor:
await cursor.execute_stream("SELECT * FROM my_huge_table")
async for row in cursor:
# Process the row
print(row)

Thread safety
==============================

Expand Down
56 changes: 55 additions & 1 deletion examples/dbapi.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@
"id": "02e5db2f",
"metadata": {},
"source": [
"### Error handling\n",
"## Error handling\n",
"If one query fails during the execution, all remaining queries are canceled.\n",
"However, you still can fetch results for successful queries"
]
Expand All @@ -219,6 +219,34 @@
"cursor.fetchall()"
]
},
{
"cell_type": "raw",
"id": "9789285b0362e8a6",
"metadata": {
"collapsed": false
},
"source": [
"## Query result streaming\n",
"\n",
"Streaming is useful for large result sets, when you want to process rows one by one without loading all of them into memory."
]
},
{
"cell_type": "code",
"id": "e96d2bda533b250d",
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"cursor.execute_stream(\"select * from generate_series(1, 1000000)\")\n",
"for row in cursor:\n",
" print(row)\n",
" if row[0] > 10:\n",
" break\n",
"# Remaining rows will not be fetched"
]
},
{
"cell_type": "markdown",
"id": "b1cd4ff2",
Expand Down Expand Up @@ -377,6 +405,32 @@
" pass\n",
"async_conn.closed"
]
},
{
"cell_type": "raw",
"id": "80a885228cbad698",
"metadata": {
"collapsed": false
},
"source": [
"## Query result streaming"
]
},
{
"cell_type": "code",
"id": "5eaaf1c35bac6fc6",
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"await cursor.execute_stream(\"select * from generate_series(1, 1000000)\")\n",
"async for row in cursor:\n",
" print(row)\n",
" if row[0] > 10:\n",
" break\n",
"# Remaining rows will not be fetched"
]
}
],
"metadata": {
Expand Down
1 change: 1 addition & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ dev =
devtools==0.7.0
mypy==1.*,<1.10.0
pre-commit==3.5.0
psutil==7.0.0
pyfakefs>=4.5.3,<=5.6.0
pytest==7.2.0
pytest-cov==3.0.0
Expand Down
Loading