mirror of
https://github.com/lancedb/lancedb.git
synced 2026-12-03 07:45:17 +00:00
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:
@@ -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,
|
||||
*,
|
||||
|
||||
@@ -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(
|
||||
|
||||
Reference in New Issue
Block a user