Sub topic 2
Parquet essentials: row groups and column chunks
This chapter assumes you can write basic Python, know what a table with typed columns is, and have used pyarrow.Table objects. You don't need to have opened a Parquet file's internals before. Later chapters build partitioned datasets from many Parquet files, so you need to know what one file contains and what a reader can skip inside it.
Why the file layout matters
Parquet is a columnar file format. It stores the values of each column together, not row by row. That layout gives two benefits that the dataset API relies on:
- A reader can fetch only the columns a query needs.
- A reader can skip chunks of rows by checking small summaries before reading any data.
Partitioning skips whole files and directories. Parquet's internal structure lets a reader skip pieces inside a file that survived that first step.
The structure of a Parquet file
A Parquet file has four levels:
- The file holds one or more row groups.
- A row group is a horizontal slice of the table, a block of consecutive rows.
- Inside a row group, each column is stored as one column chunk.
- A column chunk is split into pages, the smallest unit that is compressed and encoded.
The file ends with a footer. The footer holds the schema, the location of every column chunk, and statistics for each chunk. The file begins and ends with the four bytes PAR1, which mark it as Parquet.
A reader opens a file by reading the end first. It fetches the footer, learns where everything is, and then reads only the byte ranges it needs. This matters on remote storage such as S3, where each read is a network request.
Looking at the metadata
You can inspect all of this with pyarrow.parquet. The following script writes 10,000 rows in four row groups of 2,500 rows and prints the statistics the footer holds for the id column.
import pyarrow as pa
import pyarrow.parquet as pq
table = pa.table({
"id": list(range(10_000)),
"amount": [float(i % 100) for i in range(10_000)],
"country": ["DE", "FR"] * 5_000,
})
pq.write_table(table, "demo.parquet", row_group_size=2_500)
pf = pq.ParquetFile("demo.parquet")
print("row groups:", pf.metadata.num_row_groups)
print("rows:", pf.metadata.num_rows)
for i in range(pf.metadata.num_row_groups):
rg = pf.metadata.row_group(i)
chunk = rg.column(0) # the "id" column chunk
stats = chunk.statistics
print(i, rg.num_rows, chunk.path_in_schema, stats.min, stats.max, chunk.compression)The output shows four row groups. The id ranges are 0 to 2499, 2500 to 4999, 5000 to 7499 and 7500 to 9999. The compression is SNAPPY, the default in PyArrow.
Statistics and row group skipping
Each column chunk stores a minimum, a maximum and a null count. These are the column statistics. Suppose a query asks for id >= 7500. The reader compares 7500 with the maximum of each row group. The first three groups have maxima below 7500, so the reader never reads their data. This is called predicate pushdown: the filter is applied at the storage layer, before values are decoded.
import pyarrow.parquet as pq
result = pq.read_table("demo.parquet", filters=[("id", ">=", 7_500)])
print(result.num_rows) # 2500Statistics help most when the data is sorted or clustered on the filtered column. Here id increases with row position, so each row group covers a narrow range and three of four groups are skipped. For amount, every row group contains values from 0 to 99. A filter such as amount > 98 can't rule out any row group, so nothing is skipped.
Reading less: columns and row groups
Column projection works because column chunks are separate. If you ask for one column, the reader fetches only that column's chunks.
import pyarrow.parquet as pq
pf = pq.ParquetFile("demo.parquet")
only_amount = pf.read(columns=["amount"]) # skips id and country chunks
second_group = pf.read_row_group(1) # reads one row group only
total_rows = 0
for batch in pf.iter_batches(batch_size=1_000, columns=["id"]):
total_rows += batch.num_rows
print(total_rows) # 10000iter_batches streams the file in record batches, so you can process a file larger than memory. The unit of I/O is still the row group, so a very large row group raises peak memory use even when you ask for small batches.
Choosing a row group size
The row group is the main tuning knob. It sets a trade-off between skipping precision and per-group overhead.
| Row group size | Effect on reading | Effect on writing and storage |
|---|---|---|
| Small (thousands of rows) | Statistics are fine-grained, so filters skip more | More metadata, worse compression, more small reads |
| Large (millions of rows) | Efficient sequential reads, fewer requests | Coarse statistics, more memory per group |
A common starting point is a few hundred thousand to a few million rows per group. The later chapter on file and row group sizing returns to this. The write_dataset options min_rows_per_group and max_rows_per_group control the same setting when you write a dataset.
Common errors
- Expecting statistics to skip rows in unsorted data. If the filtered column is spread randomly across row groups, every group's min and max span the full range. Sort or partition on the column you filter by most often.
- Writing one row group per tiny write. Appending small tables in a loop with separate files produces many tiny row groups and files. Each needs its own footer read, and compression suffers.
- Assuming the footer is free. The reader must read the footer of every file it opens. Thousands of small files mean thousands of footer reads. This is the cost behind the small-files problem covered later.
- Confusing row groups with batches. A record batch is an in-memory unit that you choose when scanning. A row group is an on-disk unit chosen at write time. A batch can span no more than the rows a reader decodes at once, but the reader must still load whole column chunks from a row group.
- Trusting missing statistics. Some writers omit statistics or truncate long string values. When statistics are absent, the reader reads the row group in full. Correctness is unaffected, but speed is.
How this connects to datasets
A pyarrow.dataset treats each Parquet file as a fragment and can break a fragment into row groups. When you filter a dataset, the scanner works at three levels. It first prunes directories and files using partition values in the path. It then prunes row groups using the footer statistics. Finally it reads only the projected column chunks. The next chapters cover the first level in detail. Parquet's own structure supplies the other two.
Exercise
A Parquet file holds 1,000,000 rows in 10 row groups, written from data sorted by a timestamp column. You read it with a filter timestamp >= T, where T falls inside the last row group, and select only one of five columns. Which parts of the file does the reader need to load, and why does the result change if the file was written from unsorted data?
Show answer
The reader reads the footer, then only the chunk for the selected column in the last row group. Because the data is sorted, each row group has a narrow min/max range, and the first nine groups have maxima below T, so they are skipped. The other four columns are skipped by projection. With unsorted data, every row group's min/max range would span nearly the whole timestamp range, so no group could be ruled out and the reader would load the selected column's chunk in all 10 row groups.