The fastest byte to read is the one you never read. Column pruning drops the columns a query does not name; predicate pushdown drops the row groups a filter cannot match. Together they let a query over decades of prices touch a few percent of the file, and the footer is all it needs to decide.
The idea in plain words
Where we are. Topic 1 pruned columns, topic 2 introduced row groups and topic 3 compressed them. This topic puts the row group's statistics to work. Partitioned folders (next topic) apply the same idea one level up, skipping whole files instead of row groups.
Handing a filter down to the file reader so it can use the stored minimum and maximum of each row group to skip groups that cannot match, instead of reading everything and filtering afterwards.
Let the reader do it
filters=[("date", ">=", since)] on pq.read_table is the one-line version. The file below is sorted by date and cut into row groups of 2,000 rows, so each group covers a narrow slice of time and the recent ones are the only ones that can hold 2026. Filtering after the read (table.filter(...)) gives the same rows but fetches every group first.
# A file sorted by date, in row groups of 2,000 rows, then a filtered readby_date = table.sort_by("date")pq.write_table(by_date, "by_date.parquet", row_group_size=2000)print("row groups:", pq.ParquetFile("by_date.parquet").metadata.num_row_groups)print("rows since 2026-01-01:", read_since("by_date.parquet", date(2026, 1, 1)).num_rows)
row groups: 17 rows since 2026-01-01: 800
The same decision by hand
Doing it yourself shows what the reader does. For each row group, read the minimum and maximum of the column from the footer. If the maximum is below your range or the minimum above it, no row in that group can match and it is dropped. Otherwise it is kept, but it may still hold rows that do not match, so the rows inside must be filtered afterwards.
# Which row groups can hold 2020, how many rows that is, and the exact rows after reading only thoselow, high = date(2020, 1, 1), date(2020, 12, 31)print("groups to read:", groups_to_read("by_date.parquet", "date", low, high))print("rows scanned:", rows_scanned("by_date.parquet", "date", low, high), "of", by_date.num_rows)print("rows in 2020:", pushdown_table("by_date.parquet", "date", low, high).num_rows)
groups to read: [12, 13] rows scanned: 4000 of 33493 rows in 2020: 1265
It only works on sorted data
The statistics only help when row groups cover narrow ranges. The original table lists one stock after another, so every row group spans a different stock's whole history and every group's range includes 2020. Same data, same filter, far less to skip. Sorting before writing is the cheapest speed-up Parquet has.
# The same 2020 filter on a file written in the original order, one stock after anotherpq.write_table(table, "by_stock.parquet", row_group_size=2000)print("sorted by date :", rows_scanned("by_date.parquet", "date", low, high), "rows scanned")print("sorted by stock:", rows_scanned("by_stock.parquet", "date", low, high), "rows scanned of", table.num_rows)
sorted by date : 4000 rows scanned sorted by stock: 19493 rows scanned of 33493
A kept row group only means it might hold a match. The reader still has to look at the rows inside, which is why pushdown_table filters after reading the kept groups. Never use the statistics alone as the answer: a group whose range overlaps yours can be entirely outside it, like a gap between two dates.
What the tasks ask
- 1`read_since`
pq.read_table(path, filters=[("date", ">=", since)]); no.filterafterwards. - 2`groups_to_read` and `rows_scanned`Keep a group when
stats.min <= high and stats.max >= low(or it has no statistics); add up itsnum_rows. - 3`pushdown_table`
ParquetFile.read_row_groups(groups), then filter the rows withpc.and_(pc.greater_equal(...), pc.less_equal(...)).
Lab · what's graded
4 tasks · 100 pts- Let the reader filter:
read_since25 pts - Which groups can match:
groups_to_read25 pts - Count the rows still scanned:
rows_scanned25 pts - Read only those groups:
pushdown_table25 pts