fix: keep nonempty order_by out of namespace query pushdown (#5122)

## Problem

With namespace `QueryTable` pushdown enabled, a query with a nonempty
`order_by` was sent to the namespace server even though the request
schema has no `order_by` field. With `limit(1)` this returned the wrong
top row instead of the requested ordering.

## Fix

Treat a nonempty `order_by` as requiring local execution in both the
Python and Rust namespace pushdown gates. `None` or an empty ordering
still uses pushdown. Local execution preserves branch, version and RTE
semantics; if local storage cannot be read, the query fails explicitly
rather than silently dropping the ordering.

## Validation

- Public Python sync `search().order_by(...).limit(1).to_arrow()` and
async `query().order_by(...).limit(1).to_arrow()` against a namespace
mock: returned id 1 with zero pushdown calls.
- `python/tests/test_namespace.py`: 83 passed.
- Rust namespace pushdown tests: 5 passed; `table::query::tests`: 21
passed.
- `cargo fmt`, `cargo clippy`, and Ruff clean.

Fixes
[OSS-3126](https://linear.app/lancedb/issue/OSS-3126/bug-namespace-querytable-pushdown-silently-drops-order-by).
This commit is contained in:
Xuanwo
2026-11-20 20:45:03 +08:00
committed by GitHub
parent f460c34bdd
commit ea4f1a38cf
5 changed files with 124 additions and 9 deletions
+7
View File
@@ -2138,6 +2138,9 @@ use std::collections::HashSet;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum NamespaceClientPushdownOperation {
/// Execute queries on the namespace server via `query_table()` instead of locally.
///
/// Queries with a nonempty `order_by` execute locally and require client access
/// to the table storage.
QueryTable,
/// Execute table creation on the namespace server via `create_table()`
/// instead of using `declare_table` + local write.
@@ -2253,6 +2256,10 @@ impl ConnectNamespaceBuilder {
/// - [`NamespaceClientPushdownOperation::QueryTable`]: Execute queries via `namespace.query_table()`
/// - [`NamespaceClientPushdownOperation::CreateTable`]: Execute table creation via `namespace.create_table()`
///
/// Some query options cannot be represented in the namespace request. A query
/// with a nonempty `order_by` executes locally and requires client access to
/// the table storage.
///
/// By default, no operations are pushed down (all executed locally).
pub fn pushdown_operation(mut self, operation: NamespaceClientPushdownOperation) -> Self {
self.pushdown_operations.insert(operation);
+66 -4
View File
@@ -120,10 +120,18 @@ async fn can_execute_namespace_query(table: &NativeTable, query: &AnyQuery) -> R
}
fn requires_local_namespace_execution(query: &AnyQuery) -> bool {
// The namespace QueryTable request cannot represent approx_mode, search_effort or use_rte.
// For use_rte that is worse than a tuning miss: RTE read routing lives only
// in `create_plan`, so a pushed-down query would return stale base-only data.
if query.base().use_rte.is_some() || query.base().has_take() {
// The namespace QueryTable request cannot represent approx_mode, search_effort,
// use_rte or order_by. For use_rte that is worse than a tuning miss: RTE read
// routing lives only in `create_plan`, so a pushed-down query would return
// stale base-only data. A dropped order_by would return the wrong top-K rows.
if query.base().use_rte.is_some()
|| query.base().has_take()
|| query
.base()
.order_by
.as_ref()
.is_some_and(|order_by| !order_by.is_empty())
{
return true;
}
if matches!(query, AnyQuery::Query(query) if query.fast_search && query.full_text_search.is_some())
@@ -1858,6 +1866,60 @@ mod tests {
assert_eq!(namespace_client.query_table_calls.load(Ordering::SeqCst), 0);
}
#[tokio::test]
async fn test_execute_query_order_by_with_namespace_pushdown_runs_locally() {
use crate::connect;
use crate::query::ColumnOrdering;
use crate::table::query::execute_query;
use arrow_array::{Int32Array, RecordBatch};
use arrow_schema::{DataType, Field, Schema};
let conn = connect("memory://").execute().await.unwrap();
let schema = Arc::new(Schema::new(vec![Field::new("id", DataType::Int32, false)]));
// Insertion order differs from ascending id, so a pushed-down query that
// drops order_by would return 3 instead of the correct top-1 row 1.
let batch =
RecordBatch::try_new(schema, vec![Arc::new(Int32Array::from(vec![3, 1, 2]))]).unwrap();
let table = conn
.create_table("test_order_by_namespace_fallback", vec![batch])
.execute()
.await
.unwrap();
let namespace_client = Arc::new(CountingNamespaceClient::default());
let mut native_table = table.as_native().unwrap().clone();
native_table.namespace_client = Some(namespace_client.clone());
native_table
.pushdown_operations
.insert(NamespaceClientPushdownOperation::QueryTable);
let query = AnyQuery::Query(QueryRequest {
limit: Some(1),
order_by: Some(vec![ColumnOrdering::asc_nulls_first("id".to_string())]),
..Default::default()
});
let stream = execute_query(&native_table, &query, QueryExecutionOptions::default())
.await
.unwrap();
let batches = stream.try_collect::<Vec<_>>().await.unwrap();
let ids: Vec<i32> = batches
.iter()
.flat_map(|batch| {
batch
.column(0)
.as_any()
.downcast_ref::<Int32Array>()
.unwrap()
.values()
.to_vec()
})
.collect();
assert_eq!(ids, vec![1]);
assert_eq!(namespace_client.query_table_calls.load(Ordering::SeqCst), 0);
}
#[tokio::test]
async fn test_query_snapshot_disables_namespace_pushdown() {
use crate::connect;