mirror of
https://github.com/lancedb/lancedb.git
synced 2026-11-26 12:33:59 +00:00
perf(python): add direct sync query execution (#5141)
## Summary Route ordinary local `table.search(...).to_arrow()` queries through private synchronous schema and eager-collection bindings instead of the Python background event loop. Plain scans, vector searches (including default vector-column inference), and native FTS in lazy Blob mode now complete with zero `LOOP.run` submissions. The synchronous path reuses the existing query lowering, core query execution, eager collector, Arrow ownership, projection validation, fragments, row ids, Blob finalization, reranking, and snapshot handling. It uses the generation-scoped Tokio runtime and `OutstandingGuard`, releases the GIL while waiting, checks CPython signals on the caller thread at a bounded interval, and aborts and drops collected state when a handler raises. Multi-threaded Tokio callback re-entry uses `block_in_place`; current-thread contexts return a Python error. Namespace, RemoteTable, Enterprise, streaming, take, and non-lazy Blob routing are unchanged. No public API is added. ## Benchmark Workload: 200,000 rows, 64 dimensions, BTREE on `id`, IVF_PQ on `vector`; 50 warmups + 500 calls, p50/p99 of three alternating runs. Build: `release-with-debug`. Baseline Python package from `03fc5404` ran against the same candidate extension as the candidate, so this isolates the Python event-loop route; a separately built matching baseline extension is a follow-up validation target. The host had competing Rust builds, so these numbers are preliminary, not isolated release throughput. | Workload | LOOP/schema per call before -> after | p50 before -> after | p99 before -> after | |---|---|---|---| | plain point lookup `to_arrow()` | 2 / 1 -> 0 / 1 | 2.32 / 3.26 / 2.64 ms -> 1.64 / 1.89 / 1.80 ms | 3.87 / 3.92 / 3.69 ms -> 2.42 / 2.63 / 2.62 ms | | indexed vector `to_arrow()` | 3 / 2 -> 0 / 2 | 5.20 / 5.13 / 3.79 ms -> 4.65 / 4.22 / 4.31 ms | 7.18 / 7.52 / 6.45 ms -> 6.29 / 6.01 / 6.43 ms | `to_batches()` remains on the existing streaming path. OSS-2868 1/2/4/8/16-thread scaling and fully matched build artifacts remain separate follow-up. ## Validation - `test_sync_query_direct.py`: plain/vector/native FTS with `LOOP.run` patched to fail, default vector inference, empty schema, timeout, single-worker progress-callback re-entry, and SIGINT/runtime reuse. - `test_query.py` with direct tests: 253 passed, 1 skipped. - `test_blob.py`: 266 passed, 1 skipped. - `test_fts.py` + `test_rerankers.py`: 163 passed, 13 skipped. - `test_table.py` + `test_namespace.py` + `test_sync_api_parity.py`: 649 passed, 17 skipped. - Remote query/blob/schema subset: 206 passed. - Runtime unit tests: 12 passed; `cargo check`, `cargo clippy`, `cargo fmt --check`, and Python ruff passed. The SIGINT case is marked slow and was run in focused validation; it is not part of the default suite. Refs OSS-2867 (non-closing).
This commit is contained in:
+40
-13
@@ -5,7 +5,7 @@ use std::sync::Arc;
|
||||
use std::time::Duration;
|
||||
|
||||
use crate::error::PythonErrorExt;
|
||||
use crate::runtime::future_into_py;
|
||||
use crate::runtime::{block_on_interruptible, future_into_py};
|
||||
use arrow::{
|
||||
datatypes::SchemaRef,
|
||||
pyarrow::{IntoPyArrow, ToPyArrow},
|
||||
@@ -89,18 +89,45 @@ where
|
||||
Q: ExecutableQuery + Send + 'static,
|
||||
{
|
||||
future_into_py(py, async move {
|
||||
let mut options = QueryExecutionOptions::default();
|
||||
// `None` means the caller wants one eager result, not the core's
|
||||
// default batch size. An explicit value is still honored.
|
||||
options.max_batch_length = max_batch_length.unwrap_or(0);
|
||||
options.timeout = timeout;
|
||||
let stream = query.execute_with_options(options).await.infer_error()?;
|
||||
let schema = stream.schema();
|
||||
let batches = stream.try_collect::<Vec<_>>().await.infer_error()?;
|
||||
let reader: Box<dyn ArrowRecordBatchReader + Send> = Box::new(RecordBatchIterator::new(
|
||||
batches.into_iter().map(Ok),
|
||||
schema,
|
||||
));
|
||||
let reader = collect_to_reader(query, max_batch_length, timeout).await?;
|
||||
Python::attach(|py| reader.into_pyarrow(py).map(|obj| obj.unbind()))
|
||||
})
|
||||
}
|
||||
|
||||
/// Synchronous counterpart of [`execute_to_reader`].
|
||||
///
|
||||
/// The returned reader owns the collected batches; the caller converts it to
|
||||
/// Python after reattaching to the interpreter. The wait releases the GIL and
|
||||
/// keeps Python signal handlers on the calling thread.
|
||||
pub fn execute_to_reader_sync<Q>(
|
||||
query: Q,
|
||||
max_batch_length: Option<u32>,
|
||||
timeout: Option<Duration>,
|
||||
) -> PyResult<Box<dyn ArrowRecordBatchReader + Send>>
|
||||
where
|
||||
Q: ExecutableQuery + Send + 'static,
|
||||
{
|
||||
block_on_interruptible(collect_to_reader(query, max_batch_length, timeout))
|
||||
}
|
||||
|
||||
async fn collect_to_reader<Q>(
|
||||
query: Q,
|
||||
max_batch_length: Option<u32>,
|
||||
timeout: Option<Duration>,
|
||||
) -> PyResult<Box<dyn ArrowRecordBatchReader + Send>>
|
||||
where
|
||||
Q: ExecutableQuery + Send + 'static,
|
||||
{
|
||||
let mut options = QueryExecutionOptions::default();
|
||||
// `None` means the caller wants one eager result, not the core's
|
||||
// default batch size. An explicit value is still honored.
|
||||
options.max_batch_length = max_batch_length.unwrap_or(0);
|
||||
options.timeout = timeout;
|
||||
let stream = query.execute_with_options(options).await.infer_error()?;
|
||||
let schema = stream.schema();
|
||||
let batches = stream.try_collect::<Vec<_>>().await.infer_error()?;
|
||||
Ok(Box::new(RecordBatchIterator::new(
|
||||
batches.into_iter().map(Ok),
|
||||
schema,
|
||||
)))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user