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(