mirror of
https://github.com/lancedb/lancedb.git
synced 2026-11-26 12:33:59 +00:00
fix: add SQL query timeout and cancellation controls
Add per-query SQL deadlines, read timeouts, and drop cancellation controls for Rust and Python.
This commit is contained in:
@@ -306,7 +306,8 @@ call or return a polling continuation that the SDK follows automatically.
|
||||
This behavior is the same with and without parameters. Open the reader directly;
|
||||
a directly streamed query needs its reader to make progress. Dropping an unread
|
||||
handle or its reader stops direct streaming. Deferred execution continues until
|
||||
explicitly cancelled.
|
||||
explicitly cancelled by default; use `cancel_on_drop=True` to attempt cancellation
|
||||
when its unread handle or reader is dropped.
|
||||
|
||||
For example:
|
||||
|
||||
@@ -360,6 +361,26 @@ reader = db.execute_query(
|
||||
# await query.cancel()
|
||||
```
|
||||
|
||||
Set `timeout` to bound submission and result consumption together, including
|
||||
any time before opening a reader. Set `read_timeout` to limit each server read.
|
||||
Both accept `datetime.timedelta` and apply to one execution. An expired timeout
|
||||
or failed result read stops reading and attempts to cancel Flight execution.
|
||||
Cancellation on drop requires the submitting runtime to remain alive. Durable
|
||||
server-side jobs require explicit `db.cancel_job(query.job_id)`.
|
||||
|
||||
```python
|
||||
from datetime import timedelta
|
||||
|
||||
query = db.execute_query_async(
|
||||
"SELECT * FROM events",
|
||||
timeout=timedelta(seconds=30),
|
||||
read_timeout=timedelta(seconds=5),
|
||||
cancel_on_drop=True,
|
||||
)
|
||||
# To stop this execution explicitly:
|
||||
query.cancel()
|
||||
```
|
||||
|
||||
## Namespaces (Synchronous)
|
||||
|
||||
A namespace-backed connection resolves tables through a
|
||||
|
||||
@@ -247,6 +247,53 @@ cancellation. Background operations such as index builds return a
|
||||
[job](/basics/jobs) handle instead. Use the API for the returned handle to
|
||||
check status and retrieve results.
|
||||
|
||||
### Timeouts and cancellation {#query-timeouts}
|
||||
|
||||
Rust and Python SQL executions support per-query limits. Python's synchronous
|
||||
and asynchronous connections accept the same keyword arguments:
|
||||
|
||||
```python
|
||||
from datetime import timedelta
|
||||
|
||||
query = db.execute_query_async(
|
||||
"SELECT frame_id, caption FROM frames",
|
||||
timeout=timedelta(seconds=30),
|
||||
read_timeout=timedelta(seconds=5),
|
||||
cancel_on_drop=True,
|
||||
)
|
||||
# Cancel explicitly if these results are no longer needed.
|
||||
query.cancel()
|
||||
```
|
||||
|
||||
`timeout` starts at submission and covers result consumption, including time
|
||||
before opening a reader. `read_timeout` overrides the connection's limit for each
|
||||
server read. Expiry stops reads and attempts to cancel Flight execution. The
|
||||
execution deadline is also sent to the server on result-streaming RPCs.
|
||||
|
||||
By default, deferred queries can be detached. `cancel_on_drop=True` attempts
|
||||
cancellation when an unread handle or its reader is dropped; it requires the
|
||||
submitting runtime to remain alive. Directly streamed queries always stop on
|
||||
drop. With an asynchronous Python connection, await submission and
|
||||
`query.cancel()`.
|
||||
|
||||
Rust exposes the same controls on the submission builder:
|
||||
|
||||
```rust
|
||||
use std::time::Duration;
|
||||
|
||||
let query = db.execute_query_async("SELECT frame_id, caption FROM frames")
|
||||
.timeout(Duration::from_secs(30))
|
||||
.read_timeout(Duration::from_secs(5))
|
||||
.cancel_on_drop(true)
|
||||
.execute()
|
||||
.await?;
|
||||
query.cancel().await?;
|
||||
```
|
||||
|
||||
These options cancel Flight execution. A durable server-side job can outlive its
|
||||
Flight waiter; cancel it explicitly with `db.cancel_job(query.job_id)` in Python,
|
||||
or `Connection::cancel_job` in Rust.
|
||||
|
||||
## Query plans {#query-plans}
|
||||
|
||||
Use query plans to understand how LanceDB reads data, applies filters and uses
|
||||
|
||||
Reference in New Issue
Block a user