Sub topic 2
Parquet Essentials: Row Groups and Column Chunks
This chapter assumes you know basic Python, can read a table of rows and columns (as in pandas or SQL), and have used pyarrow to build a pa.table. It explains how a single Parquet file is organized inside. Later chapters build on this to explain why filters and column selection can skip data.
Why the layout matters
Parquet is a columnar file format. It stores the values of each column together, not row by row. Two consequences follow:
- A reader that needs two columns out of twenty can skip the other eighteen.
- Each file keeps statistics (such as minimum and maximum values) that let a reader skip data that cannot match a filter.
The layers inside a file
A Parquet file is split into nested pieces.
- A row group is a horizontal slice of the table: a block of consecutive rows.
- A column chunk is the part of one column that falls inside one row group.
- A page is a small unit inside a column chunk. Pages are compressed and encoded. You rarely work with them directly.
- The footer is metadata at the end of the file. It holds the schema, the location of every column chunk, and statistics for each chunk.
Inspecting a file
This example writes a small file with two row groups and then reads its metadata.
import pyarrow as pa
import pyarrow.parquet as pq
table = pa.table({
"year": [2023, 2023, 2024, 2024],
"amount": [10.5, 20.0, 7.25, 3.0],
})
pq.write_table(table, "small.parquet", row_group_size=2)
meta = pq.ParquetFile("small.parquet").metadata
print("row groups:", meta.num_row_groups)
print("rows:", meta.num_rows)
for i in range(meta.num_row_groups):
stats = meta.row_group(i).column(1).statistics
print(i, stats.min, stats.max)The row_group_size=2 argument limits each row group to two rows, so this file has two row groups. The loop prints the minimum and maximum of the amount column chunk in each group: 10.5 20.0 for group 0 and 3.0 7.25 for group 1.
How readers use this
Reading only the amount column means reading only the amount column chunks. This is called column projection. Filtering with amount > 15 lets the reader check the statistics first. Row group 1 has a maximum of 7.25, so it can be skipped without reading its data. This is called predicate pushdown.
Common mistakes
- Expecting statistics to help on unsorted data. If every row group spans a wide range of values, no group can be skipped.
- Writing very small row groups. Each one adds metadata and lowers compression efficiency.
- Writing one huge row group. A reader must then load far more data at once.
Exercise
A Parquet file has 10 row groups and 5 columns. How many column chunks does it contain, and why can a query that selects one column read only a fraction of the file?
Show answer
It contains 50 column chunks (10 row groups times 5 columns). Each column's values are stored in separate chunks, so a query for one column reads only its 10 chunks and skips the other 40.