Files
lancedb/rust
Xuanwo c55b7c0390 feat!: add explicit scan order contract for local queries (#5138)
BREAKING CHANGE: `QueryRequest` now has a public `scan_in_order:
Option<bool>` field. Existing code that constructs `QueryRequest` with
an exhaustive struct literal must add `scan_in_order: None` to retain
the previous backend default, or use `..Default::default()` where
appropriate. Python defaults are unchanged: leaving `scan_in_order()`
uncalled keeps `None`.

## Problem

Plain Embedded LanceDB scans already use Lance's ordered scan on a fixed
snapshot, but the storage-order contract was undocumented and callers
had no way to require it. Explicit `order_by` sorts the whole result.
Ranked, Enterprise, RTE/LSM, legacy scalar-index and namespace-pushdown
paths cannot always preserve base manifest order.

## Behavior

- `scan_in_order(true)` requires manifest order without adding a sort:
fragments in manifest order, then rows in fragment order.
- `true` disables scalar-index filter planning. Legacy scalar-index
plans can materialize index rows in a different sequence, so filters are
evaluated over the manifest-ordered scan instead. Filter semantics are
unchanged, but the scan can read more data. Default filtered scans can
still use those plans and may not preserve manifest order.
- `scan_in_order(false)` permits the backend to scan unordered. It does
**not** guarantee a different order; an explicit nonempty `order_by`
still sorts.
- Leaving the option unset preserves the backend default. The documented
manifest-order default applies to an unfiltered base-only scan of a
pinned Embedded table version. RTE tables scan the LSM by default and
need `use_rte(false)` for the base-only guarantee. Enterprise and
namespace-pushed scans have no order guarantee.
- `true` conflicts with nonempty `order_by`, take queries,
vector/full-text/hybrid/reranked queries, RTE/LSM reads, and Enterprise
tables. These fail explicitly instead of silently dropping the
requirement. Take conflict validation happens at the shared
take-preparation boundary before offset or row-id selectors are
stripped.
- `with_fragments()` is rejected with `scan_in_order(true)` because
fragment objects cannot be validated against the pinned dataset version.
Use `fragment_ids()`, which resolves fragment ids from the manifest and
preserves manifest order. Both sync and async pandas scanner paths share
this rule.
- Namespace `QueryTable` pushdown executes `true` against Embedded
storage instead of dropping the order. The existing path preserves
projection, filtering, limits, version/branch selection, and RTE
routing; if Embedded storage cannot be read, the query fails.

Supported entry points are plain Rust `Table::query()`, Python sync
`table.search()` and async `table.query()`. The Python pandas scanner
fast path forwards the option and disables scalar-index planning for
explicit order. Existing takes and `with_fragments()` calls with `None`
or `false` are unchanged.

## Validation

- Rust: 6 `scan_in_order` tests, 32 take tests, 25 query-route tests.
- Python: `test_query.py` and `test_namespace.py` 352 passed, 1 skipped
against a matching extension.
- Scoped `pytest --doctest-modules python/lancedb/query.py -k
scan_in_order`: 2 passed. The Rust/Python examples create their own
tables and are executable.
- Regressions cover legacy 0.1 and stable BTREE tables with update and a
filter admitting every row, across pinned versions before and after
update; reversed `fragment_ids`; explicit `with_fragments()` rejection;
and fixed-version multi-fragment reads with deletions, one-row batches,
and limit/offset. No `SortExec` is added.
- `cargo clippy --features remote --tests --examples`; `cargo fmt
--check`; Ruff format/check; `git diff --check`.
- Local checks ran on this branch. CI on this PR evaluates the merge
with current `main`; no combined local binary validation is claimed
here.

Refs OSS-3109.
2026-11-21 05:20:16 +08:00
..