Sub topic 10
Predicate pushdown and row group skipping
This chapter assumes you know what a row group, a column chunk, and the footer are, and that each column chunk stores min, max, and null count statistics. It also assumes you can read basic PyArrow code.
What predicate pushdown means
A predicate is a filter condition, such as id >= 950_000. Predicate pushdown means handing that condition to the file reader, so it can avoid reading data that cannot match. Without pushdown, the reader loads every row and a later step discards the non-matching ones. With pushdown, the reader consults the footer first and skips whole row groups before it reads any data pages.
Two savings combine here:
- Column projection reads only the columns you ask for.
- Row group skipping ignores row groups whose statistics prove no row can match.
How skipping works
The reader fetches the footer, which holds the min and max of every column chunk in every row group. For each row group, it compares your predicate with those values.
- If the predicate cannot be true for any value between min and max, the row group is skipped.
- If it might be true, the row group is read and the rows are filtered.
The test is conservative. Statistics can prove that a row group has no match. They cannot prove that every row matches, so rows inside a surviving group are still checked.
A worked example
The file events.parquet holds one million rows in ten row groups of 100,000 rows. The id column is sorted, so each group covers a distinct range.
| Row group | min id | max id | Read for `id >= 950_000`? |
|---|---|---|---|
| 0 | 0 | 99,999 | No |
| 1 | 100,000 | 199,999 | No |
| 8 | 800,000 | 899,999 | No |
| 9 | 900,000 | 999,999 | Yes |
Only the last group has a max of at least 950,000, so only it is read. Groups 0 through 8 are skipped (the table shows a sample). The read below does this:
import pyarrow.parquet as pq
result = pq.read_table(
"events.parquet",
columns=["id"],
filters=[("id", ">=", 950_000)],
)
print(result.num_rows)The output is 50000. The reader opened one row group out of ten, and only for the id column.
Why sort order decides the benefit
Skipping works only if row groups have narrow, non-overlapping ranges. Here the sorted id column gives each group a tight range. Now consider category, which cycles through a, b, c, d. Every row group contains all four values, so min is a and max is d in every group. A filter such as category == "c" cannot skip anything, because every range overlaps the filter.
The same data can be fast or slow to filter depending on how it was written. If you often filter on a column, sort or cluster the data by that column before writing.
Common errors
- Filtering unsorted data. Wide, overlapping min/max ranges mean nothing is skipped. The result is still correct, only slower.
- Assuming the filter is exact. In some readers,
filtersonly prunes row groups and does not remove every non-matching row. PyArrow'sread_tablefilters rows too, but lower-level APIs may not. Check what your reader returns. - Missing statistics. If a writer did not record statistics, or a column has no min/max (some types, or all-null chunks), the reader must read the group.
- Wrapping the column in a function. A predicate like
abs(id) > 5often cannot be compared with min/max, so pushdown may not apply. Compare the raw column when you can.
Checking that skipping happened
Use the footer to predict what a filter will skip. This loop lists the groups whose max reaches 950,000:
import pyarrow.parquet as pq
md = pq.ParquetFile("events.parquet").metadata
for i in range(md.num_row_groups):
stats = md.row_group(i).column(0).statistics
keep = stats.max >= 950_000
print(i, stats.min, stats.max, "read" if keep else "skip")Exercise
A file has 20 row groups. The user_id column is random, so every row group has min near 1 and max near 1,000,000. You filter on user_id == 424242. How many row groups can min/max statistics skip, and what could you change when writing the file to improve this?
Show answer
None, because 424242 lies between the min and max of every row group, so each group might contain it. Sorting or clustering the data by user_id before writing would give each group a narrow range, so most groups could be skipped. A Bloom filter on user_id is another option for equality lookups.