feat(python): add count_rows with filter option (#801)

Closes #795
This commit is contained in:
Chang She
2024-01-09 19:33:03 -08:00
committed by Andrew Miracle
parent f43c06d9ce
commit fcfb4587bb
2 changed files with 23 additions and 1 deletions

View File

@@ -647,8 +647,19 @@ class LanceTable(Table):
self._dataset.restore()
self._reset_dataset()
def count_rows(self, filter: Optional[str] = None) -> int:
"""
Count the number of rows in the table.
Parameters
----------
filter: str, optional
A SQL where clause to filter the rows to count.
"""
return self._dataset.count_rows(filter)
def __len__(self):
return self._dataset.count_rows()
return self.count_rows()
def __repr__(self) -> str:
return f"LanceTable({self.name})"

View File

@@ -597,3 +597,14 @@ def test_compact_cleanup(db):
with pytest.raises(Exception, match="Version 3 no longer exists"):
table.checkout(3)
def test_count_rows(db):
table = LanceTable.create(
db,
"my_table",
data=[{"text": "foo", "id": 0}, {"text": "bar", "id": 1}],
)
assert len(table) == 2
assert table.count_rows() == 2
assert table.count_rows(filter="text='bar'") == 1