Sub topic 2
Anatomy of a Parquet file
This chapter assumes you know what a table and schema are, can read basic Python, and understand the difference between row-oriented and column-oriented storage. It covers how one Parquet file is laid out on disk. Later chapters go deeper into each piece.
The four layers
A Parquet file is built from nested units. Each unit exists so that a reader can skip bytes it does not need.
- File: the whole thing, bracketed by the 4-byte magic marker
PAR1at the start and the end. - Row group: a horizontal slice of the table, such as rows 0 to 99,999.
- Column chunk: all the values of one column within one row group.
- Page: a small block inside a column chunk. Pages are the unit of encoding and compression.
After the last row group comes the footer. It holds the schema and the location of every row group and column chunk.
Why this shape
The table is split into row groups first, and each row group is then stored column by column. This gives two ways to skip data:
- A query that needs two columns reads only those two column chunks in each row group.
- A query with a filter can skip whole row groups whose statistics rule out a match.
Keeping rows together in a group means one row group can be written or read by one worker, and a reader never needs to hold the whole file in memory.
Writing a file to examine
The example below writes one million rows as 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",
)The row_group_size argument sets the maximum rows per row group. With two columns and ten row groups, the file holds 20 column chunks.
How a reader opens the file
The footer is at the end, so a reader does not scan from the start. It follows these steps:
- Read the last 8 bytes. They contain the footer length (4 bytes) and the closing
PAR1marker. - Read the footer, using that length.
- Use the footer to find the byte offset of each column chunk it needs.
- Read only those byte ranges.
This works well on object stores such as Amazon S3, where a reader can request a byte range instead of downloading the whole object.
Common errors
| Mistake | Effect |
|---|---|
| Assuming rows are stored contiguously | Row-by-row access is slow, because one row's values sit in many separate chunks. |
| Writing many tiny files | Each file has its own footer, so open and metadata costs add up. |
| Setting a very small row group size | The footer grows, and each column chunk is too small to compress and encode well. |
| Appending to an existing file | Not possible. The footer is written last, so a Parquet file is written once and then closed. |
Exercise
A file has 8 row groups and 5 columns. How many column chunks does it contain, and where does a reader look first to find them?
Show answer
It has 8 × 5 = 40 column chunks. The reader first reads the end of the file, where the footer length and the footer itself locate every chunk.