To avoid reading data you need a cheap way to say "it is not in there". The footer holds two. Minimum and maximum statistics rule out a whole row group when a value falls outside its range. A bloom filter rules out a value that is inside the range but was never stored. Neither can prove a value is present, only that it is absent.
The idea in plain words
Where we are. Topic 4 used min and max to skip row groups for a range filter. This topic reads those statistics directly, shows where they stop being useful (a single value that happens to fall inside a range), and builds the bloom filter that covers that gap. The next topic deals with the cost of having too many files, which all of this assumes you have avoided.
A bit array that answers "is this value in the set?" with either "definitely not" or "probably". Each added value sets a few bits chosen by hashing; a value is reported present only if all of its bits are set. It never forgets a member, and occasionally reports a stranger.
Min and max, straight from the footer
row_group_stats reads minimum, maximum, null count and row count for a column in every row group, all from the footer. For a date column in a file sorted by date, the ranges are narrow and almost do not overlap, so looking up a date lands in one or two groups. But note the word "might": a date inside a group's range is not necessarily in the group.
# The first three row groups of a file sorted by date: (min, max, nulls, rows)for stats in row_group_stats("by_date.parquet", "date")[:3]: print(stats)
(datetime.date(2000, 1, 3), datetime.date(2003, 3, 13), 0, 4000) (datetime.date(2003, 3, 13), datetime.date(2006, 5, 16), 0, 4000) (datetime.date(2006, 5, 16), datetime.date(2009, 7, 21), 0, 4000)
# A Saturday sits inside a group's range but is not in the file: statistics can only say "maybe"saturday = date(2020, 1, 4)print("row groups that might hold it:", groups_with_value("by_date.parquet", "date", saturday))print("rows really dated that day:", sum(1 for d in table["date"].to_pylist() if d == saturday))
row groups that might hold it: [6] rows really dated that day: 0
A bloom filter for what statistics cannot rule out
An order ID or a trade ID is spread over its whole range in every row group, so min and max exclude nothing. A bloom filter per row group stores a fingerprint of the IDs it holds. bloom_build sets k bits per value; bloom_contains checks them. The size of the bit array decides how often a stranger is let in: the more bits per value, the fewer false positives, at the cost of a bigger footer.
# 1,000 order IDs in a filter; then 1,000 IDs that were never added, at three filter sizesmembers = [f"order-{i}" for i in range(1000)]strangers = [f"other-{i}" for i in range(1000)]for nbits in (2_000, 10_000, 40_000): bits = bloom_build(members, nbits, 4) missed = sum(not bloom_contains(bits, nbits, 4, m) for m in members) false_positives = sum(bloom_contains(bits, nbits, 4, s) for s in strangers) print(f"{nbits:>6} bits: members missed {missed}, strangers let in {false_positives} of 1000")
2000 bits: members missed 0, strangers let in 546 of 1000 10000 bits: members missed 0, strangers let in 19 of 1000 40000 bits: members missed 0, strangers let in 0 of 1000
Sorted columns like date already skip well from statistics, and a bloom filter adds only weight. Unsorted high-cardinality columns that you look up by equality, such as order IDs, trade IDs or account numbers, are where filters pay off. They help = and IN lookups, never range conditions.
What the tasks ask
- 1`row_group_stats` and `groups_with_value`Find the column chunk by
path_in_schema; readstatistics.min,.max,.null_count. Keep a group whenmin <= value <= max, or when it has no statistics. - 2`bloom_build` and `bloom_contains`For each value,
bits |= 1 << positionfor everybit_positions(value, nbits, k); to query, returnFalseas soon as one of those bits is 0.
Lab · what's graded
4 tasks · 100 pts- Read the statistics:
row_group_stats25 pts - A firm no or a maybe:
groups_with_value25 pts - Build a bloom filter:
bloom_build25 pts - Ask the filter:
bloom_contains25 pts