Sub topic 3
Row groups: the unit of horizontal partitioning
This chapter 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. Earlier chapters covered why Parquet is columnar and how a file is laid out. Here we focus on one level of that layout: the row group.
What a row group is
A Parquet file stores a table in a columnar way, but it does not store each column as one giant block. It first cuts the table into horizontal slices. Each slice is a row group: a set of consecutive rows. Inside each row group, the data is stored column by column.
This gives Parquet two levels of organization:
- Horizontally, rows are split into row groups.
- Vertically, each row group stores one column chunk per column.
A reader that wants a few columns and a few rows can then pick only the column chunks and row groups it needs.
Why files are split into row groups
Writers and readers both benefit from the split.
Bounded memory for writers. To write a column chunk, the writer must hold that column's values for the whole row group, so it can encode and compress them together. If a file were one huge slice, the writer would have to buffer the entire dataset. With row groups, it buffers one group, writes it, and starts the next.
Independent units of work for readers. Each row group can be read, decoded, and decompressed without touching the others. A query engine can hand different row groups to different threads or machines. This is why row groups are the natural unit of parallelism in Parquet readers.
A unit for skipping. The footer stores statistics per column chunk in each row group. If a filter cannot match anything in a row group, the reader skips that group without reading its data. A later chapter covers this in detail. The key point here is that skipping works at row group granularity, so how you size row groups affects how well it works.
Rows stay together. Row group boundaries always fall between rows. Within a row group, every column chunk holds the same number of values (for flat data), so the i-th value of each column belongs to the i-th row. This is how a reader reassembles rows from separate columns.
Writing row groups with PyArrow
The row_group_size argument of pq.write_table sets the maximum number of rows per row group. This example 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",
)Check the result by reading the footer:
import pyarrow.parquet as pq
pf = pq.ParquetFile("events.parquet")
md = pf.metadata
print(md.num_rows, md.num_row_groups)
for i in range(md.num_row_groups):
rg = md.row_group(i)
print(i, rg.num_rows, rg.total_byte_size)You should see 1,000,000 rows and 10 row groups, with each group holding 100,000 rows. The exact byte sizes depend on your PyArrow version.
Note that row_group_size is a maximum. If the table has 1,050,000 rows, you get ten full groups and a final group of 50,000 rows. That small last group is normal.
Row group size in rows versus bytes
Row group size is set in rows by the writer in PyArrow, but what matters to storage and I/O is bytes. The same row count produces very different byte sizes depending on the data:
- A table of 100,000 rows with three integer columns may produce a row group of a few megabytes.
- A table of 100,000 rows with wide string or binary columns may produce a row group of hundreds of megabytes.
Encoding and compression also change the on-disk size. A column of repeated values shrinks dramatically, while a column of random identifiers barely shrinks. So you cannot choose a row count in the abstract. Write a sample, inspect the resulting sizes, and adjust. The total_byte_size field of a row group reports the uncompressed size of its data, and each column chunk reports both compressed and uncompressed sizes.
Different writers use different defaults and different limits. Some limit by row count, some by estimated bytes, and some by both. Check the documentation of the library you use rather than assuming PyArrow's behavior applies everywhere.
Trade-offs of row group size
Row group size is a trade-off with no universal right answer.
| Property | Smaller row groups | Larger row groups |
|---|---|---|
| Writer memory | Lower | Higher |
| Parallelism | More units, finer-grained | Fewer units, may leave workers idle |
| Skipping precision | Finer, since statistics cover fewer rows | Coarser, each group's min/max range is wider |
| Metadata size | More entries in the footer | Fewer entries |
| Compression and encoding | Less data per chunk, so dictionaries and runs are less effective | More data per chunk, so better ratios |
| Sequential read efficiency | More, shorter reads | Fewer, longer reads |
The pattern is that small row groups favor selective queries and parallelism, while large ones favor scan throughput and compression. Extremely small groups are harmful: the footer grows, each column chunk carries fixed overhead, and a reader makes many small requests. This hurts most on object storage such as Amazon S3, where each request has latency. Extremely large groups can exceed the memory a reader has per task and make skipping ineffective.
Common practice is to aim for row groups from tens of megabytes up to a few hundred megabytes, with 128 MB to 1 GB often cited for distributed file systems. Treat these as starting points and measure against your workload. A later chapter returns to choosing size and codec together.
Row group order and skipping
Skipping only helps if row groups have narrow, non-overlapping value ranges for the columns you filter on. That depends on the order of the rows you write.
Suppose id increases with the row position, as in the example above. Row group 0 holds ids 0 to 99,999, row group 1 holds 100,000 to 199,999, and so on. A filter like id >= 950_000 can only match the last group. The other nine have a maximum below 950,000, so the reader skips them.
Now suppose the same ids were shuffled before writing. Every row group would then have a minimum near 0 and a maximum near 999,999. The statistics could not rule out any group, and the filter would read all ten.
So the data is only as skippable as its sort order. If you often filter on a column, sort or cluster the data by that column before writing. This is one of the cheapest and most effective optimizations available.
Run this to see the effect for yourself:
import random
import pyarrow as pa
import pyarrow.parquet as pq
ids = list(range(1_000_000))
shuffled = ids[:]
random.shuffle(shuffled)
for name, values in [("sorted", ids), ("shuffled", shuffled)]:
t = pa.table({"id": values})
pq.write_table(t, f"ids_{name}.parquet", row_group_size=100_000)
md = pq.ParquetFile(f"ids_{name}.parquet").metadata
print(name)
for i in range(md.num_row_groups):
stats = md.row_group(i).column(0).statistics
print(" group", i, stats.min, stats.max)The sorted file prints ten disjoint ranges. The shuffled file prints ten ranges that all span nearly the full interval.
Reading selected row groups
Because row groups are independent, you can read just some of them. ParquetFile.read_row_group reads one group, and read_row_groups reads a list.
import pyarrow.parquet as pq
pf = pq.ParquetFile("events.parquet")
first = pf.read_row_group(0, columns=["id"])
print(first.num_rows)
last_two = pf.read_row_groups([8, 9], columns=["id"])
print(last_two.num_rows)This is what higher-level readers do after they consult the statistics. When you pass filters to pq.read_table, PyArrow's dataset layer evaluates the filter against row group statistics and reads only groups that might match.
import pyarrow.parquet as pq
result = pq.read_table(
"events.parquet",
columns=["id"],
filters=[("id", ">=", 950_000)],
)
print(result.num_rows)This returns 50,000 rows and touches only the id chunk of the final row group, because the other groups have a maximum id below 950,000. Note that skipping happens at group level. Inside a group that survives, the rows are still filtered after decoding, unless finer-grained structures such as page indexes are available.
Common errors and gotchas
- Many tiny files with tiny row groups. Writing a file per small batch, for example from a streaming job, produces files with one small row group each. Reads become slow because of per-file and per-group overhead. Compact small files into larger ones.
- Assuming the row count is exact.
row_group_sizeis an upper bound. The last group is usually smaller, and some writers also flush groups early. - Writing one table at a time in a loop with
write_table. Each call towrite_tablewith a new path creates a new file. To append row groups to one file, usepq.ParquetWriterand callwrite_tableon the writer. Each call then produces at least one new row group. - Expecting skipping on unsorted data. If min/max ranges overlap across groups, the filter reads everything. Check statistics before blaming the reader.
- Ignoring byte size. A row count that works for narrow tables can produce huge row groups for wide ones.
The following sketch shows incremental writing with ParquetWriter:
import pyarrow as pa
import pyarrow.parquet as pq
schema = pa.schema([("id", pa.int64()), ("category", pa.string())])
with pq.ParquetWriter("batches.parquet", schema, compression="zstd") as writer:
for start in range(0, 300_000, 100_000):
batch = pa.table(
{
"id": list(range(start, start + 100_000)),
"category": ["a"] * 100_000,
},
schema=schema,
)
writer.write_table(batch)
print(pq.ParquetFile("batches.parquet").metadata.num_row_groups)This prints 3, one row group per write_table call, because each table is smaller than the default maximum group size.
Exercise
A file has 20 row groups, and you filter on a column ts. The min/max ranges of ts overlap heavily across all 20 groups because the data was written in random order. What happens to a filtered read, and what is the most direct fix?
Show answer
Statistics cannot rule out any row group, so the reader must read all 20. The most direct fix is to sort or cluster the data by ts before writing, so each row group covers a narrow, mostly non-overlapping range of ts and groups outside the filter can be skipped.
Exercise
You write a table of 1,050,000 rows with row_group_size=100_000. How many row groups does the file contain, and how many rows are in the last one?
Show answer
Eleven row groups. The first ten hold 100,000 rows each, and the last holds the remaining 50,000 rows.