From fd745ac4f3d4af49c229bd34b8b161a38d12eee4 Mon Sep 17 00:00:00 2001 From: Divyam Talwar Date: Mon, 30 Nov 2026 17:42:27 +0530 Subject: [PATCH] fix(python): honor a zero limit in synchronous plain scans (#5262) Fixes #5261. ## Problem Calling `limit(0)` on a synchronous ordinary scan clears the limit and returns every matching row. The same plain scan in the async SDK returns zero rows. ## Changes - Pass zero through to the native query for synchronous plain scans. Keep explicit `None` as an unlimited scan. - Verify that an empty Arrow table and batch stream retain their requested projection. ## Validation - The new regression on `main` returned one row instead of an empty projected table. It passes with the patch. - Ran 18 query tests covering plain scans, projection, ordering, stream output and the existing vector no-limit case. - Also checked Arrow, batches and pandas against a real in-memory table using the `0.42.0b38` native wheel. Zero returns an empty `id` projection in all three. Async zero and synchronous `None` retain their existing meanings. - Ruff formatting and lint passed. AI-assisted implementation and testing. --- python/python/lancedb/query.py | 10 ++++++++++ python/python/tests/test_query.py | 10 ++++++++++ 2 files changed, 20 insertions(+) diff --git a/python/python/lancedb/query.py b/python/python/lancedb/query.py index 3a66c2ed7..5f4260706 100644 --- a/python/python/lancedb/query.py +++ b/python/python/lancedb/query.py @@ -3073,6 +3073,16 @@ class LanceFtsQueryBuilder(LanceQueryBuilder): class LanceEmptyQueryBuilder(LanceQueryBuilder): + def limit(self, limit: Union[int, None]) -> Self: + """Set the maximum rows for a plain scan. Zero returns no rows. + + Pass ``None`` to scan without a limit. + """ + if limit == 0: + self._limit = 0 + return self + return super().limit(limit) + def to_arrow( self, *, diff --git a/python/python/tests/test_query.py b/python/python/tests/test_query.py index ed8e0d24f..faba507e2 100644 --- a/python/python/tests/test_query.py +++ b/python/python/tests/test_query.py @@ -1676,6 +1676,16 @@ async def test_multivector_async(multivec_table_async: AsyncTable): await multivec_table_async.query().nearest_to([[1, 2], [1, 2, 3]]).to_arrow() +def test_plain_scan_limit_zero_preserves_projection_without_returning_rows(mem_db): + table = mem_db.create_table("plain_limit", [{"id": 1, "text": "one"}]) + query = table.search().select(["id"]) + empty = pa.table({"id": pa.array([], pa.int64())}) + + assert query.limit(0).to_arrow().equals(empty) + assert query.to_batches().read_all().equals(empty) + assert query.limit(None).to_list() == [{"id": 1}] + + def test_vector_query_with_no_limit(table): with pytest.raises(ValueError): LanceVectorQueryBuilder(table, [0, 0], "vector").limit(0).select(