mirror of
https://github.com/lancedb/lancedb.git
synced 2026-11-26 12:33:59 +00:00
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:
@@ -509,7 +509,9 @@ class LanceNamespaceDBConnection(DBConnection):
|
||||
Supported values:
|
||||
|
||||
- "QueryTable": Execute queries on the namespace server via
|
||||
namespace.query_table() instead of locally.
|
||||
namespace.query_table() instead of locally. Queries with a
|
||||
nonempty ``order_by`` execute locally and require the client to
|
||||
access the table storage.
|
||||
- "CreateTable": Execute table creation on the namespace server via
|
||||
namespace.create_table() instead of using declare_table + local write.
|
||||
|
||||
@@ -1208,7 +1210,9 @@ class AsyncLanceNamespaceDBConnection:
|
||||
Supported values:
|
||||
|
||||
- "QueryTable": Execute queries on the namespace server via
|
||||
namespace.query_table() instead of locally.
|
||||
namespace.query_table() instead of locally. Queries with a
|
||||
nonempty ``order_by`` execute locally and require the client to
|
||||
access the table storage.
|
||||
- "CreateTable": Execute table creation on the namespace server via
|
||||
namespace.create_table() instead of using declare_table + local write.
|
||||
|
||||
@@ -1811,7 +1815,9 @@ def connect_namespace(
|
||||
Supported values:
|
||||
|
||||
- "QueryTable": Execute queries on the namespace server via
|
||||
namespace.query_table() instead of locally.
|
||||
namespace.query_table() instead of locally. Queries with a nonempty
|
||||
``order_by`` execute locally and require the client to access the
|
||||
table storage.
|
||||
- "CreateTable": Execute table creation on the namespace server via
|
||||
namespace.create_table() instead of using declare_table + local write.
|
||||
|
||||
@@ -1902,7 +1908,9 @@ def connect_namespace_async(
|
||||
Supported values:
|
||||
|
||||
- "QueryTable": Execute queries on the namespace server via
|
||||
namespace.query_table() instead of locally.
|
||||
namespace.query_table() instead of locally. Queries with a nonempty
|
||||
``order_by`` execute locally and require the client to access the
|
||||
table storage.
|
||||
- "CreateTable": Execute table creation on the namespace server via
|
||||
namespace.create_table() instead of using declare_table + local write.
|
||||
|
||||
|
||||
@@ -146,6 +146,9 @@ def _requires_local_namespace_execution(query: Query) -> bool:
|
||||
or query.take_row_ids is not None
|
||||
or query.search_effort is not None
|
||||
or query.approx_mode is not None
|
||||
# The namespace QueryTable request has no order_by field. Pushing a
|
||||
# nonempty ordering down would drop it and return the wrong top-K rows.
|
||||
or bool(query.order_by)
|
||||
or (
|
||||
query.full_text_query is not None
|
||||
and query.fast_search is True
|
||||
|
||||
@@ -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",
|
||||
[
|
||||
|
||||
Reference in New Issue
Block a user