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
+36 -1
View File
@@ -29,7 +29,7 @@ from lancedb.namespace import (
LanceNamespaceDBConnection,
_query_to_namespace_request,
)
from lancedb.query import FullTextSearchQuery, Query
from lancedb.query import ColumnOrdering, FullTextSearchQuery, Query
from lancedb.table import AsyncTable, LanceTable
@@ -1772,6 +1772,23 @@ class TestPushdownOperations:
assert request.maximum_nprobes == 0
assert request.to_dict()["maximum_nprobes"] == 0
def test_lance_table_order_by_is_not_pushed_down(self, tmp_path):
namespace_client = _NamespaceClient()
db = lancedb.connect(str(tmp_path / "db"))
table = db.create_table("hist", data=[{"id": 3}, {"id": 1}, {"id": 2}])
table._namespace_path = ["geneva"]
table._namespace_client = namespace_client
table._pushdown_operations = {"QueryTable"}
table._route_pushdown_to_rust = False
result = table._execute_query(
Query(limit=1, order_by=[ColumnOrdering(column_name="id")])
).read_all()
# Pushing this query down would drop order_by and return id 3.
assert result.column("id").to_pylist() == [1]
assert namespace_client.requests == []
@pytest.mark.asyncio
class TestAsyncPushdownOperations:
@@ -1834,6 +1851,24 @@ class TestAsyncPushdownOperations:
assert [request.k for request in namespace_client.requests] == [_MAX_QUERY_K]
assert all(r.k <= 2**31 - 1 for r in namespace_client.requests)
async def test_async_table_order_by_is_not_pushed_down(self, tmp_path):
namespace_client = _NamespaceClient()
db = await lancedb.connect_async(str(tmp_path / "db"))
table = await db.create_table("hist", data=[{"id": 3}, {"id": 1}, {"id": 2}])
table._namespace_path = ["geneva"]
table._namespace_client = namespace_client
table._pushdown_operations = {"QueryTable"}
table._route_pushdown_to_rust = False
result = await table._execute_query(
Query(limit=1, order_by=[ColumnOrdering(column_name="id")])
)
# Pushing this query down would drop order_by and return id 3.
batches = await result.read_all()
assert pa.Table.from_batches(batches).column("id").to_pylist() == [1]
assert namespace_client.requests == []
@pytest.mark.parametrize(
"query_kwargs",
[