Sub topic 11
Arrow and Parquet: in-memory versus on-disk
This chapter assumes you can read pyarrow code and know what an Arrow table, schema and IPC file are from earlier chapters. It also assumes you know what a row group means in the abstract: a block of rows stored together.
Arrow and Parquet are often mentioned together and often confused. Both are columnar, both have schemas, and both are Apache projects. They solve different problems. Arrow is a layout for data in memory that code can compute on directly. Parquet is a file format built to be small on disk and cheap to scan. You usually use both: Parquet to store data, Arrow to hold it while a program works on it.
Two formats, two goals
| Question | Arrow (in memory) | Parquet (on disk) |
|---|---|---|
| Main goal | Fast access and cheap sharing between tools | Small files and fast selective scans |
| Value encoding | Plain, fixed layout, no compression | Dictionary, run-length, bit-packing, delta |
| Compression | None by default | Per page, such as snappy, zstd or gzip |
| Random access to a value | Index arithmetic on a buffer | Must decode a page first |
| Null representation | Validity bitmap | Definition levels |
| Can be memory-mapped and used directly | Yes, for IPC files | No, it must be decoded |
| Typical lifetime | Seconds to hours | Months to years |
The key difference is that Arrow values are already in the form the CPU wants. Parquet values are in the form that is cheapest to store. Reading Parquet therefore always involves work: decompress the pages, decode the encodings, and write the result into Arrow buffers.
How a Parquet file is organized
A Parquet file has three levels of structure.
- A file is split into row groups. Each row group holds a horizontal slice of the table, for example 100,000 rows.
- Within a row group, each column is stored as a column chunk. All values of one column in that slice sit together.
- Each column chunk is split into pages. A page is the unit of compression and encoding.
A footer at the end of the file holds the schema, the location of every column chunk, and statistics such as the minimum and maximum value of each column chunk. A reader opens the footer first. From it, the reader knows which byte ranges it needs.
Two scan optimizations follow from this layout:
- Column projection: if you ask for 2 of 50 columns, the reader fetches only those 2 column chunks.
- Row-group skipping: if a row group's maximum
idis 249 and you filter onid > 800, the reader skips that row group without reading its data.
Writing and inspecting a Parquet file
The code below builds a table, writes it as Parquet with four row groups, and inspects the footer.
import pyarrow as pa
import pyarrow.parquet as pq
table = pa.table({
"id": pa.array(range(1000), type=pa.int64()),
"city": pa.array(["Oslo", "Lima", "Pune", None] * 250),
"score": pa.array([i * 0.5 for i in range(1000)]),
})
pq.write_table(table, "cities.parquet", compression="zstd", row_group_size=250)
pf = pq.ParquetFile("cities.parquet")
print(pf.metadata.num_row_groups)
stats = pf.metadata.row_group(0).column(0).statistics
print(stats.min, stats.max)This prints 4, then 0 249. The footer already knows that the first row group holds ids 0 to 249. No data page was read to learn that.
Reading with projection and filters
pq.read_table accepts columns for projection and filters for predicates. The reader uses the footer statistics to skip row groups, then applies the filter to the rows it did read.
import pyarrow.parquet as pq
subset = pq.read_table(
"cities.parquet",
columns=["id", "city"],
filters=[("id", ">", 800)],
)
print(subset.num_rows)
print(subset.schema.names)This prints 199 and ['id', 'city']. Row groups covering ids 0 to 749 are skipped. The score column is never read.
Statistics help most when the data is sorted or clustered on the filter column. If every row group contains both small and large ids, each group's range overlaps the filter and nothing can be skipped.
What happens during a read
A Parquet read is a decode step, not a view of the file. The reader:
- Fetches the needed byte ranges, from local disk or from object storage.
- Decompresses each page.
- Decodes dictionary, run-length and bit-packed values into plain values.
- Converts definition levels into a validity bitmap.
- Writes the result into newly allocated Arrow buffers.
Because new buffers are allocated, a Parquet read is never zero-copy. This is the main contrast with the memory-mapped IPC file from the previous chapter. An IPC file stores Arrow buffers exactly as they appear in memory, so the OS can map the file and the table points straight into it. A Parquet file cannot be used that way, because its bytes are not in Arrow layout.
Type mapping and the stored Arrow schema
Parquet has its own type system. It has a few physical types (INT32, INT64, FLOAT, DOUBLE, BYTE_ARRAY and others) plus logical annotations such as STRING, TIMESTAMP and DECIMAL. Several Arrow types can map to the same Parquet type. For example, Arrow int8, int16 and int32 all fit into Parquet INT32 with an integer annotation, and a plain string and a dictionary-encoded string both become BYTE_ARRAY.
To avoid losing information, pyarrow writes the original Arrow schema into the file's key-value metadata under the key ARROW:schema. When pyarrow reads the file back, it uses that schema to restore details such as time zones and dictionary types. A different reader, such as a Java or Rust tool, may not use this hint and could return a slightly different type.
You can request dictionary-encoded strings explicitly:
import pyarrow.parquet as pq
t = pq.read_table("cities.parquet", read_dictionary=["city"])
print(t.schema.field("city").type)This prints dictionary<values=string, indices=int32, ordered=0>. For a column with few distinct values, such as city, this stores each string once and keeps small integer indices, which saves memory and speeds up comparisons.
Nulls and nested data
Arrow marks nulls with a validity bitmap: one bit per value. Parquet does not store a bit per value. It stores definition levels, small integers that say how deep into a nested structure a value is defined. For a flat nullable column, the level is just 0 (null) or 1 (present), and it is compressed well with run-length encoding. For nested lists and structs, Parquet also stores repetition levels to mark where each new list starts. Arrow uses offsets buffers and child arrays instead.
The reader translates between the two. This is why reading deeply nested Parquet data is slower than reading flat data: the levels must be reassembled into offsets and bitmaps.
Common errors and gotchas
- Treating Parquet as memory-mappable.
pa.memory_mapon a Parquet file does not give you a table. The file must be decoded withpq.read_table. - Tiny row groups. Row groups of a few hundred rows give poor compression and a large footer. Typical sizes are in the range of hundreds of thousands to millions of rows, or around 128 MB per group.
- Reading everything then filtering. Calling
read_tablewith no arguments and filtering in pandas loads all columns and all rows. Passcolumnsandfiltersso the reader can skip data. - Many small files. Thousands of small Parquet files multiply footer reads and open calls. This is costly on object storage, where each request has latency.
- Assuming the type survives. A column written by one tool may read back with a different type in another. Check the schema after a read, and store timestamps with an explicit unit and time zone.
- Memory blow-up on decode. A 1 GB compressed file can decode to several GB of Arrow buffers, especially for dictionary-encoded strings read as plain strings. Read in batches with
ParquetFile.iter_batchesif the result does not fit in memory.
For files too large for memory, read one batch at a time:
import pyarrow.parquet as pq
pf = pq.ParquetFile("cities.parquet")
total = 0
for batch in pf.iter_batches(batch_size=300, columns=["id"]):
total += batch.num_rows
print(total)This prints 1000. Only one batch of one column is decoded at a time.
Choosing between them
Use Arrow's IPC format when data moves between running processes, or when you want zero-copy reads of a local file. Use Parquet when data is stored for later, shared between teams, or kept in a data lake. In most pipelines, Parquet files are the storage layer, and Arrow is the shared in-memory layer that engines, pandas and compute functions read from after decoding. The next chapter shows how a dataset scan combines both: it uses Parquet footers to plan the read and returns Arrow tables.
Exercise
A table has 20 columns and is stored as one Parquet file with 100 row groups. A query needs the columns user_id and amount for rows where amount > 1000. Name two things the reader can skip, and explain why the result is still not a zero-copy view of the file.
Show answer
The reader can skip the 18 columns that are not requested (column projection), and it can skip any row group whose footer statistics show a maximum amount of 1000 or less (row-group skipping). The result is not zero-copy because the pages are compressed and encoded. The reader must decompress and decode them into newly allocated Arrow buffers, so the table's memory does not point into the file.
Exercise
You write a table with a dictionary-encoded string column using pyarrow, and a colleague reads it with another tool and sees a plain string column. Why can this happen, and how can you request dictionary output in pyarrow?
Show answer
Parquet stores the column as BYTEARRAY with a string annotation, and the dictionary Arrow type is only a hint kept in the ARROW:schema metadata that pyarrow reads. Another reader may ignore that hint. In pyarrow you can ask for dictionary output with `pq.readtable(path, readdictionary=["columnname"])`.