Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
39 changes: 39 additions & 0 deletions mkdocs/docs/recipe-count.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
---
title: Count Recipe
---

# Counting Rows in an Iceberg Table

This recipe demonstrates how to use the `count()` function to efficiently count rows in an Iceberg table using PyIceberg.

## Basic Usage

To count all rows in a table:

```python
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It could be worth mentioning as a note that we could get the total count of a table from snapshot properties doing this:
table.current_snapshot().summary.additional_properties["total-records"]

so users can avoid doing a full table scan

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the comments, I will work on them 😊

from pyiceberg.catalog import load_catalog

catalog = load_catalog("default")
table = catalog.load_table("default.cities")

row_count = table.count()
print(f"Total rows in table: {row_count}")
```

## Count with a Filter

To count only rows matching a filter:

```python
from pyiceberg.expressions import EqualTo

count = table.scan(row_filter=EqualTo("city", "Amsterdam")).count()
print(f"Rows with city == 'Amsterdam': {count}")
```

## Notes
- The `count()` method works for both catalog and static tables.
- Filters can be applied using the `scan` API for more granular counts.
- Deleted records are excluded from the count.

For more details, see the [API documentation](api.md).
58 changes: 58 additions & 0 deletions tests/table/test_count.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import pytest
from unittest.mock import MagicMock, Mock, patch
from pyiceberg.table import DataScan
from pyiceberg.expressions import AlwaysTrue

class DummyFile:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we could write real data files and use that for testing wdyt?

Here are some fixtures we could use to get a FileScanTask with a file with some rows in it: example

Maybe we can also add some more fixtures to get FileScanTasks for empty files and large ones

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, it will be a good addition actually.

def __init__(self, record_count):
self.record_count = record_count

class DummyTask:
def __init__(self, record_count, residual=None, delete_files=None):
self.file = DummyFile(record_count)
self.residual = residual if residual is not None else AlwaysTrue()
self.delete_files = delete_files or []

def test_count_basic():
# Create a mock table with the necessary attributes
table = Mock(spec=DataScan)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: We should call this variable scan rather than table since we are mocking a DataScan object


# Mock the plan_files method to return our dummy task
task = DummyTask(42, residual=AlwaysTrue(), delete_files=[])
table.plan_files = MagicMock(return_value=[task])

# Import and call the actual count method
from pyiceberg.table import DataScan as ActualDataScan
table.count = ActualDataScan.count.__get__(table, ActualDataScan)

assert table.count() == 42

def test_count_empty():
# Create a mock table with the necessary attributes
table = Mock(spec=DataScan)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here to rename to scan


# Mock the plan_files method to return no tasks
table.plan_files = MagicMock(return_value=[])

# Import and call the actual count method
from pyiceberg.table import DataScan as ActualDataScan
table.count = ActualDataScan.count.__get__(table, ActualDataScan)

assert table.count() == 0

def test_count_large():
# Create a mock table with the necessary attributes
table = Mock(spec=DataScan)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and here


# Mock the plan_files method to return multiple tasks
tasks = [
DummyTask(500000, residual=AlwaysTrue(), delete_files=[]),
DummyTask(500000, residual=AlwaysTrue(), delete_files=[]),
]
table.plan_files = MagicMock(return_value=tasks)

# Import and call the actual count method
from pyiceberg.table import DataScan as ActualDataScan
table.count = ActualDataScan.count.__get__(table, ActualDataScan)

assert table.count() == 1000000