Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions python/deltalake/table.py
Original file line number Diff line number Diff line change
Expand Up @@ -528,6 +528,29 @@ def _backwards_enumerate(
history.append(commit)
return history

def count(self) -> int:
"""
Get the approximate row count based on file statistics added to the Delta table.

This requires that add actions have been added to the Delta table with
per-file statistics enabled. Because this is an optional field this
"count" will be less than or equal to the true row count of the table.
In order to get an exact number of rows a full table scan must happen

Returns:
The approximate number of rows for this specific table
"""
total_rows = 0

for value in self.get_add_actions().column("num_records").to_pylist():
# Add action file statistics are optional and so while most modern
# tables are _likely_ to have this information it is not
# guaranteed.
if value is not None:
total_rows += value

return total_rows

def vacuum(
self,
retention_hours: int | None = None,
Expand Down
6 changes: 6 additions & 0 deletions python/tests/test_table_read.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@ def test_read_simple_table_to_dict():
).read_all()["id"].to_pylist() == [5, 7, 9]


def test_table_count():
table_path = "../crates/test/tests/data/COVID-19_NYT"
dt = DeltaTable(table_path)
assert dt.count() == 1


class _SerializableException(BaseException):
pass

Expand Down
Loading