feat(python)!: preserve input order for take offsets (#5140)

## Summary

Add an opt-in `preserve_order` flag to sync and async `take_offsets`. It
exposes the existing Rust `TakeRestoreExec` ordering path and carries
the flag through `QueryRequest`, `PyQueryRequest`, and the Python
`Query` model.

## Behavior

- Default `preserve_order=False` keeps the existing incremental,
unspecified-order behavior.
- `preserve_order=True` returns found rows in input order, including
repeated offsets.
- Missing non-negative offsets keep filter semantics and are omitted;
negative offsets remain rejected by the existing `u64` contract.
- Rust take queries apply `limit`/`offset` after order and repetition
are restored. Python take builders and Query conversion do not expose
pagination.
- An explicit `order_by` conflicts with offset `preserve_order` and
raises clearly. `take_row_ids` keeps its existing implicit ordering and
explicit-sort behavior.
- `__getitems__` is unchanged: it keeps its Python re-alignment and
strict missing-offset (`ArrowIndexError`) behavior.

## Remote lookup

The remote lookup sends only the deduplicated `_rowoffset IN (...)`
filter and restores order/repetition client-side; there is no new wire
field or backend extension. Namespace take queries stay on local
execution, and RTE/LSM rejects the `_rowoffset` lookup loudly.

## Testing

- Old reviewed head reproduced the serialization regression:
`test_query_serialization_sync/async` failed because the Query model
exposed `False` for an unset field.
- Fixed by using `Optional[bool] = None` and mapping Rust `false` to
absent in `PyQueryRequest`; explicit `True` still survives the actual
round trip.
- `python/tests/test_query.py`: 253 passed, 1 skipped.
- Rust preserve-order tests: 3 passed; row-id/order-by tests: 27 passed.
- Remote ordered Blob bytes, remote permutation, and Blob mode focused
cases: 3 passed.
- `cargo fmt --check`, `ruff format/check`, `py_compile`, and `git diff
--check` passed.

## Migration

`lancedb::query::QueryRequest` is a public non-`#[non_exhaustive]`
struct. Downstream code that constructs it with an exhaustive struct
literal must add `preserve_order: false` or use `..Default::default()`.
The field is client-side metadata and is not sent over the remote
protocol.

Seekable Blob APIs, rollout, and ENT-4059 remain separate work.

Refs OSS-2881 (non-closing).
This commit is contained in:
Xuanwo
2026-11-21 04:35:13 +08:00
committed by GitHub
parent 1658c8d762
commit a3b6fecfd6
12 changed files with 465 additions and 44 deletions
+7
View File
@@ -357,6 +357,7 @@ pub struct PyQueryRequest {
pub offset: Option<usize>,
pub take_offsets: Option<Vec<u64>>,
pub take_row_ids: Option<Vec<u64>>,
pub preserve_order: Option<bool>,
pub filter: Option<PyQueryFilter>,
pub full_text_search: Option<PyLanceDB<FtsQuery>>,
pub select: PySelect,
@@ -390,6 +391,7 @@ impl From<AnyQuery> for PyQueryRequest {
offset: query_request.offset,
take_offsets: query_request.take_offsets,
take_row_ids: query_request.take_row_ids,
preserve_order: query_request.preserve_order.then_some(true),
filter: query_request.filter.map(PyQueryFilter),
full_text_search: query_request
.full_text_search
@@ -423,6 +425,7 @@ impl From<AnyQuery> for PyQueryRequest {
offset: vector_query.base.offset,
take_offsets: vector_query.base.take_offsets,
take_row_ids: vector_query.base.take_row_ids,
preserve_order: vector_query.base.preserve_order.then_some(true),
filter: vector_query.base.filter.map(PyQueryFilter),
full_text_search: None,
select_source_columns: PySelect::source_columns(&vector_query.base.select),
@@ -765,6 +768,10 @@ impl TakeQuery {
self.inner = self.inner.clone().with_row_id();
}
pub fn preserve_order(&mut self) {
self.inner = self.inner.clone().preserve_order();
}
pub fn use_rte(&mut self, enable: bool) {
self.inner = self.inner.clone().use_rte(enable);
}