Sub topic 2
Anatomy of a Parquet file
This chapter walks through the physical structure of a Parquet file, from the first bytes to the footer, and shows how to see each part from Python. It assumes you know what a table and a schema are, can read basic Python, and understand the difference between row-oriented and column-oriented storage. Later chapters cover encodings, compression, and statistics in detail. Here the goal is a clear map of where everything lives.
The big picture
A Parquet file is a sequence of bytes with three regions: a magic number at the start, a body of data, and a footer at the end. The body is split horizontally into row groups. Each row group holds one column chunk per column. Each column chunk is a series of pages. The footer records where all of these are.
| Level | What it is | Typical size | Who uses it |
|---|---|---|---|
| File | One `.parquet` object with a header magic, body, and footer | MBs to GBs | Query engines, catalogs |
| Row group | A horizontal slice of rows, stored column by column | Tens to hundreds of MB | Skipping and parallelism |
| Column chunk | All values of one column within one row group | A fraction of a row group | Column projection |
| Page | The unit of encoding and compression inside a chunk | About 1 MB by default in PyArrow | Decoding, page-level skipping |
| Footer | Schema, row group and chunk locations, statistics | KBs to a few MB | Every reader, first |
The byte-level frame
Every Parquet file begins with the four ASCII bytes PAR1. The file ends with three things in a fixed order:
- The serialized footer, called
FileMetaData. - A 4-byte little-endian integer giving the footer's length in bytes.
- The four bytes
PAR1again.
Because the last 8 bytes have a fixed shape, a reader can find the footer without scanning the file. It seeks to the end, reads 8 bytes, checks the trailing magic, reads the length, and then reads that many bytes just before it.
You can verify this with plain Python. First write a file with ten row groups, as in the spine's example:
import struct
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",
)
with open("events.parquet", "rb") as f:
head = f.read(4)
f.seek(-8, 2) # 8 bytes before the end of the file
tail = f.read(8)
footer_len = struct.unpack("<I", tail[:4])[0]
print(head, tail[4:], footer_len)The output shows b'PAR1' b'PAR1' and a footer length in bytes. The exact number depends on your PyArrow version, but for this file it is small, usually under a few KB.
Row groups
A row group is a set of consecutive rows, stored together. In the file above, one million rows with row_group_size=100_000 produce ten row groups. Within each row group the data is columnar. All id values of the group are stored contiguously, followed by all category values.
Row groups matter for three reasons:
- Memory. A writer buffers one row group at a time, so row group size bounds writer memory.
- Parallelism. Different workers can read different row groups of one file independently.
- Skipping. Statistics are kept per row group and per column. A reader can drop a whole row group if its statistics rule out a match.
Row groups do not repeat the schema. The schema lives once, in the footer. Row group size is a tuning decision, which a later chapter covers.
Column chunks
Inside a row group, each column has exactly one column chunk. A reader that needs only id reads only the id chunks and never touches category bytes. This is column projection, and it follows directly from the layout.
A column chunk's metadata records its path in the schema, its codec, its encodings, its value count, its compressed and uncompressed sizes, the offset of its first page, and its statistics. Offsets are absolute positions in the file, so a reader can issue a single range read for a chunk.
Pages
A column chunk is divided into pages. The page is the unit at which encoding and compression are applied. Each page starts with a small page header, which is also Thrift-serialized. The header holds the page type, the uncompressed and compressed sizes, the value count, and the encoding.
There are three page types you will meet:
- Dictionary page. Present at most once per chunk, and first, when dictionary encoding is used. It lists the distinct values.
- Data page. Holds the encoded values, plus definition and repetition levels when the column is nullable or nested. There are two versions of the data page format, v1 and v2.
- Index page. Defined in the spec but rarely written. Ignore it for now.
PyArrow's data_page_size option (default about 1 MiB) sets the target size of a data page before compression. Smaller pages give finer-grained reads and skipping, with more per-page overhead. Larger pages compress slightly better.
The footer
The footer is a FileMetaData structure, serialized with Thrift's compact protocol. It contains:
- the format version,
- the schema,
- the total row count,
- one entry per row group, with one entry per column chunk inside it,
- optional key-value metadata, such as an embedded Arrow schema,
- a
created_bystring naming the writer library and version.
The footer is the only way to locate data. The body contains no table of contents of its own. This has practical consequences:
- A file without a footer is unreadable. If a writer crashes before closing the file, the footer is missing, and the pages already written cannot be located by standard readers.
- Writers must buffer metadata. Footer entries for every row group are held in memory until
close(). - Large footers cost time. A file with thousands of row groups and hundreds of columns can have a footer of several MB, and every reader must download and parse it before reading data.
- Object stores need range requests. On S3 or similar stores, engines usually fetch the final 8 bytes, or a speculative tail of 64 KB or more, then fetch the rest of the footer if needed. Then they fetch only the column chunks they need.
Walking the hierarchy with PyArrow
pq.ParquetFile(...).metadata parses the footer and exposes the structure as Python objects. The next script prints the file-level values, then compares the first two row groups, column by column. It uses the file written above.
import pyarrow.parquet as pq
md = pq.ParquetFile("events.parquet").metadata
print("rows:", md.num_rows)
print("row groups:", md.num_row_groups)
print("columns:", md.num_columns)
print("footer bytes:", md.serialized_size)
print("created by:", md.created_by)
for rg_index in range(2):
rg = md.row_group(rg_index)
print(f"\nrow group {rg_index}: {rg.num_rows} rows")
for col_index in range(rg.num_columns):
col = rg.column(col_index)
print(
" ",
col.path_in_schema,
col.compression,
"values:", col.num_values,
"has dict page:", col.has_dictionary_page,
"dict offset:", col.dictionary_page_offset,
"data offset:", col.data_page_offset,
"encodings:", col.encodings,
)Things to notice in the output:
md.serialized_sizematches the footer length you read from the last 8 bytes.- Each row group reports 100,000 rows, and each has one entry per column.
- The
categorycolumn has only four distinct values, so it will normally showhas dict page: True, and itsdict offsetcomes before itsdata offset. - The
data offsetfor row group 1 is larger than for row group 0. The chunks are laid out one after another in the file.
Exact encoding names and whether id also uses a dictionary depend on the PyArrow version and its writer defaults. Do not rely on a specific tuple of encodings.
Common mistakes and edge cases
- Assuming one file means one row group. Small files written by a single
write_tablecall may have one row group, but PyArrow splits larger tables according torow_group_size. Always checknum_row_groups. - Reading the schema from the first data page. The schema is in the footer. Reading the first bytes after
PAR1gives you a page header, not a schema. - Treating chunk order as meaningful. Column chunks within a row group follow the schema order in files written by common libraries, but readers must use the offsets from the footer and not assume adjacency.
- Ignoring many small files. Each file carries its own footer and magic bytes, and every reader pays an open-and-parse cost per file. Thousands of tiny files are slow even when the total data is small.
- Expecting corrupt-file recovery. A truncated file will fail at the footer check. The error usually mentions the magic bytes or the footer length.
- Encrypted files. Parquet modular encryption can encrypt the footer. Such files end with
PAREinstead ofPAR1, and a reader needs the key before it can see the schema.
What comes next
You now have the map: magic bytes, row groups, column chunks, pages, footer. The next chapters look inside each part. Encodings and compression act on pages. Statistics sit in the footer, one set per column chunk, and they are what make row group skipping possible.
Exercise
A reader opens a 2 GB Parquet file on an object store and needs only the id column for row groups whose statistics might match a filter. List, in order, which byte ranges it must fetch, and explain why a file with a truncated footer cannot be read.
Show answer
The reader first fetches the last 8 bytes (4-byte footer length plus the trailing PAR1), then the footer itself, using the length it just read. It parses the schema and row group metadata, uses the statistics to pick the row groups that might match, and then fetches only the id column chunk of each selected row group, using the offsets and sizes recorded in the footer. It does not need the category chunks or the skipped row groups. A file with a truncated footer cannot be read because the footer is the only place that records where the row groups, column chunks, and pages are and what the schema is. Without it the trailing magic and length check fail, and the pages in the body cannot be located by a standard reader.
Exercise
Write code that opens events.parquet, and for every row group prints the row group index, its row count, and the compressed size of the id column chunk. Which attribute of the column chunk metadata gives the compressed size?
Show answer
Use total_compressed_size on the column chunk metadata:
import pyarrow.parquet as pq
md = pq.ParquetFile("events.parquet").metadata
for i in range(md.numrowgroups):
rg = md.rowgroup(i)
print(i, rg.numrows, rg.column(0).totalcompressedsize)
Column 0 is id because it is the first field in the schema. The loop prints ten lines, one per row group, each with 100000 rows.