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.
This commit is contained in:
Divyam Talwar
2026-11-30 20:12:27 +08:00
committed by GitHub
parent 70ab010a47
commit fd745ac4f3
2 changed files with 20 additions and 0 deletions
+10
View File
@@ -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,
*,
+10
View File
@@ -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(