Sub topic 9
Inspecting row groups and statistics with PyArrow
This chapter assumes you know what a row group, column chunk, and footer are, and that you can run Python with PyArrow installed. You will read a file's metadata without loading any data, and learn how to interpret what you see.
Why inspect metadata
Every Parquet file ends with a footer that describes the file. PyArrow can read the footer alone, so inspection is cheap even for very large files. Use it to confirm the row group count, check which codec was applied, compare compressed and uncompressed sizes, and see whether statistics exist for filtering.
The examples use the file written earlier: one million rows, ten row groups of 100,000 rows each, compressed with Zstandard.
import pyarrow as pa
import pyarrow.parquet as pq
table = pa.table({
"id": list(range(1_000_000)),
"category": ["a", "b", "c", "d"] * 250_000,
})
pq.write_table(
table,
"events.parquet",
row_group_size=100_000,
compression="zstd",
)Reading the file-level metadata
pq.ParquetFile opens the file and parses the footer. Its metadata attribute holds the file-level summary.
import pyarrow.parquet as pq
md = pq.ParquetFile("events.parquet").metadata
print(md.num_rows, md.num_row_groups)This prints 1000000 10. If the row group count is far from what you expected, the writer's row_group_size setting or the way data was batched is the first place to look.
Walking down to a column chunk
The metadata is a hierarchy: file, then row group, then column chunk. md.row_group(0) returns the first row group, and .column(0) returns the chunk for the first column.
import pyarrow.parquet as pq
md = pq.ParquetFile("events.parquet").metadata
col = md.row_group(0).column(0)
print(col.path_in_schema, col.compression)
print(col.total_compressed_size, col.total_uncompressed_size)
stats = col.statistics
print(stats.has_min_max, stats.min, stats.max, stats.null_count)For the id column in row group 0, you should see the path id, the codec ZSTD, and statistics with a minimum of 0, a maximum of 99,999, and a null count of 0.
The fields mean the following:
| Field | Meaning |
|---|---|
| `path_in_schema` | The column's name, or its dotted path for nested columns |
| `compression` | The codec used for this chunk |
| `total_compressed_size` | Bytes the chunk occupies on disk |
| `total_uncompressed_size` | Bytes after decompression, still encoded |
| `has_min_max` | Whether min and max were recorded |
| `null_count` | Number of null values in the chunk |
Dividing the uncompressed size by the compressed size gives the chunk's compression ratio.
Checking every row group
A single chunk tells you little. Loop over all row groups to see how statistics partition the data.
import pyarrow.parquet as pq
md = pq.ParquetFile("events.parquet").metadata
for i in range(md.num_row_groups):
rg = md.row_group(i)
s = rg.column(0).statistics
print(i, rg.num_rows, s.min, s.max)Because id increases through the file, the ranges are disjoint: 0 to 99,999, then 100,000 to 199,999, and so on. Disjoint ranges are what make skipping effective. If each row group's range covered the whole value space, no group could be ruled out.
Common pitfalls
- Statistics can be missing. Check
stats is not Noneandstats.has_min_maxbefore using the values. Some writers omit them, and some types have no usable ordering. - Column order is the schema order. Use
path_in_schemato confirm which column you are looking at rather than trusting an index. - Sizes describe the chunk, not the file. Sum over all row groups for a column's total footprint.
- Metadata describes what the writer recorded. It does not prove that the data matches, so treat it as a hint for skipping, not a validation step.
Exercise
A file has five row groups. For the column ts, the (min, max) pairs are (1, 50), (40, 90), (80, 130), (120, 170), (160, 210). A query filters on ts >= 125. Which row groups can a reader skip using statistics alone?
Show answer
Only the first three can be reasoned about: row group 0 (max 50), row group 1 (max 90), and row group 2 (max 130) must be examined. Groups 0 and 1 can be skipped because their max values are below 125. Group 2 has max 130, so it may contain matching rows and must be read. Groups 3 and 4 must be read as well. Two row groups are skipped.