Sub topic 8
Column statistics: min, max, and null count
This chapter assumes you know what a row group and a column chunk are, and that you can read basic PyArrow code. Column statistics are small summaries that Parquet stores for each column chunk. Readers use them to decide whether a chunk can contain rows they need, before they read any data.
What gets stored
For every column chunk, a writer can record:
- min: the smallest non-null value in the chunk.
- max: the largest non-null value in the chunk.
- null_count: how many values in the chunk are null.
Each column chunk has its own statistics. A file with 10 row groups and 2 columns holds 20 sets of statistics. They live in the footer, so a reader gets them without touching the data pages.
Statistics describe a chunk, not individual rows. A chunk with min 0 and max 99,999 tells you the range of values, but not which values appear.
Where they come from
The writer computes statistics while it encodes each chunk. PyArrow does this by default. You can turn it off with write_statistics=False in pq.write_table, which saves a little footer space and loses all skipping based on them.
import pyarrow as pa
import pyarrow.parquet as pq
table = pa.table({
"id": list(range(1_000_000)),
"score": [None if i % 10 == 0 else i % 500 for i in range(1_000_000)],
})
pq.write_table(table, "stats_demo.parquet", row_group_size=100_000)
md = pq.ParquetFile("stats_demo.parquet").metadata
for rg in range(md.num_row_groups):
s = md.row_group(rg).column(0).statistics
print(rg, s.min, s.max, s.null_count)Because id increases steadily, each row group covers a separate range: row group 0 has min 0 and max 99,999, row group 1 starts at 100,000, and so on. These ranges do not overlap.
How a reader uses them
Consider the filter id >= 950_000. The reader checks each row group's max for id:
- If
max < 950_000, no row in the group can match, so skip it. - Otherwise, read the group and filter its rows.
Here, only the last row group has a max of at least 950,000. The reader skips nine of ten.
What null_count adds
The null count answers questions that min and max cannot:
| Question | Statistic used |
|---|---|
| Is the chunk entirely null? | `null_count` equals the number of values |
| Does the chunk contain any nulls? | `null_count > 0` |
| Can `IS NOT NULL` skip this chunk? | Yes, if every value is null |
| Can `IS NULL` skip this chunk? | Yes, if `null_count == 0` |
Min and max ignore nulls. A chunk that is all null has no min or max, so check has_min_max before you read them.
Common errors
- Assuming statistics help every filter. They help when values are clustered. If
idwere shuffled, every row group would span nearly the full range, and nothing could be skipped. - Reading min and max as exact data facts for strings. Some writers truncate long string min and max values. A truncated min is still a valid lower bound, and a truncated max is still a valid upper bound, so skipping stays correct but is less precise.
- Trusting statistics from old writers. Early versions of some writers computed min and max for strings with the wrong sort order. Modern readers ignore the older fields for types where this was a problem.
- Forgetting that statistics may be absent. A file written with statistics off, or by a tool that omits them, simply gets no skipping. Check
has_min_maxrather than assuming.
Exercise
A file has four row groups with max values for ts of 100, 250, 400, and 400, and min values of 0, 90, 240, and 10. For the filter ts > 300, which row groups must be read, and why might the fourth be a poor sign for the data layout?
Show answer
Row groups 3 and 4 must be read, because their max values (400) exceed 300. Row groups 1 and 2 are skipped, since their max values are at most 250. The fourth has a range of 10 to 400, which overlaps the others heavily. That suggests the data is not well clustered by ts, so the range gives the reader little to prune with.
Exercise
In the stats_demo.parquet example, the score column has null values in every tenth row. Write code that prints null_count for score in each row group, and state what value you expect.
Show answer
Use md.row_group(rg).column(1).statistics.null_count in the loop over row groups. Each row group has 100,000 rows with every tenth null, so the expected null_count is 10,000 in each.