Sub topic 10
Partition pruning: skipping files you do not need
This chapter assumes you can write and open a Hive-partitioned dataset with ds.write_dataset and ds.dataset, and that you know what a filter expression is. The examples use the sales dataset with year and country partitions.
What pruning is
Partition pruning means skipping files based on the directory names alone. Every fragment (one file) carries a partition_expression built from its path. For sales/year=2024/country=DE/part-0.parquet, that expression is (year == 2024) and (country == "DE").
When you pass a filter, PyArrow compares it with each fragment's expression. If the two cannot both be true, the fragment is dropped. The file is never opened, so no bytes are read from it.
Seeing which files survive
get_fragments accepts the same filter. It shows what pruning keeps without reading any data.
import pyarrow.dataset as ds
dataset = ds.dataset("sales", format="parquet", partitioning="hive")
flt = (ds.field("year") == 2024) & (ds.field("country") == "DE")
for fragment in dataset.get_fragments(filter=flt):
print(fragment.path)Only the year=2024/country=DE file is printed. The same filter passed to to_table or scanner prunes the same way.
Pruning and row group statistics
Pruning uses only partition columns. A filter on a regular column such as amount cannot remove files by path. PyArrow still opens each remaining file and reads its footer. There, Parquet stores minimum and maximum values per row group, and row groups that cannot match are skipped. This is predicate pushdown. It saves less than pruning, because every footer must be read first.
| Filter column | Skips whole files | Skips row groups |
|---|---|---|
| Partition column (`year`) | Yes, from the path | Not needed |
| Regular column (`amount`) | No | Yes, if statistics rule them out |
Common errors
- Opening without
partitioning="hive". Theyearandcountrycolumns do not exist, so a filter on them raises an error. Even if you read the files, nothing is pruned. - Wrong type in the filter. If the partition field is an integer,
ds.field("year") == "2024"compares an integer with a string and fails. Match the type declared in the partition schema. - Filtering after loading. Calling
to_table()and then filtering in pandas reads every file first. Pass the filter toto_tableor the scanner instead. - Filtering on a transformed value. Pruning works best on direct comparisons such as
==,<,>=andisin. A filter that wraps the column in a function may not be recognized as prunable.
Exercise
A dataset is partitioned by year and country. You filter with ds.field("amount") > 100. Does PyArrow skip any files by path? What can it still skip, and how?
Show answer
No. amount is not a partition column, so no fragment's partition expression contradicts the filter. PyArrow opens each file and can skip row groups whose minimum and maximum amount statistics rule out values above 100.