-
Notifications
You must be signed in to change notification settings - Fork 356
Fixed the limit bug and added test for count() method and documentation for count() #2423
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 1 commit
ca777d6
d8f9411
ecf72d1
782bea5
8b59b81
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 | ||
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). |
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: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 Maybe we can also add some more fixtures to get FileScanTasks for empty files and large ones There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: We should call this variable |
||
|
||
# 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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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 😊