Sub topic 3
Columnar formats: why Parquet and Arrow matter
This chapter assumes you can load a dataset with pandas, read a basic SQL GROUP BY, and know what a column data type is. It explains how data is laid out on disk and in memory. That layout explains many of the speed and memory differences between pandas, Polars, DuckDB, and Spark, which later chapters cover.
Row layout versus column layout
A table has rows and columns, but a file or a block of memory is a single line of bytes. The tool has to decide which values sit next to each other in that line.
- Row-oriented storage keeps all values of one row together:
1, "eu", 10, 1, "us", 20, .... CSV and most transactional databases work this way. It is good for reading or writing whole records. - Column-oriented (columnar) storage keeps all values of one column together:
1, 2, 3, ...then"eu", "us", "eu", ...then10, 20, 5, .... It is good for analytics, where a query touches a few columns across many rows.
Consider SELECT region, sum(revenue) FROM sales GROUP BY region on a table with 40 columns. A row layout forces the reader to scan past all 40 columns of every row. A columnar layout lets it read only the region and revenue columns and skip the other 38.
Columnar layout has two more benefits:
- Better compression. Values in one column share a type and often repeat. A
regioncolumn with a few distinct values compresses far better than a mix of strings and numbers. - Vectorized processing. Summing a contiguous block of numbers lets the CPU process many values per instruction. This is much faster than hopping between mixed-type fields.
Parquet: columnar storage on disk
Apache Parquet is a columnar file format designed for analytics. It is the common format for sharing tables between all four tools in this tutorial.
A Parquet file is organized in levels:
- A file contains one or more row groups, each holding a range of rows (often tens of thousands to millions).
- Within a row group, each column is stored as a separate column chunk.
- Each column chunk is compressed and encoded on its own, for example with dictionary encoding for repeated strings.
- A footer at the end of the file stores the schema and statistics for each column chunk, such as the minimum and maximum value.
These pieces enable three optimizations that engines use.
Column pruning
The engine reads only the column chunks a query needs. The pandas call pd.read_parquet("sales.parquet", columns=["region", "revenue"]) does this explicitly. Lazy engines such as Polars, DuckDB, and Spark work out the needed columns from the whole query, so you do not have to list them.
Predicate pushdown
A predicate is a filter condition such as revenue > 0. With predicate pushdown, the engine uses the min/max statistics in the footer to skip row groups that cannot match. If a row group's maximum revenue is below the threshold, the engine never reads it. Pushdown works best when the data is sorted or clustered on the filtered column, because then each row group covers a narrow range of values.
Partitioned datasets
Large datasets are often split across many files in directories, such as sales/year=2025/month=11/part-0.parquet. This is called partitioning. A filter on year lets the engine skip whole directories. Polars, DuckDB, and Spark can all read such layouts. Choose partition columns with a modest number of distinct values. Partitioning by a column with millions of values creates millions of tiny files, which are slow to list and open.
Here is the Polars query from the spine that shows the first two optimizations:
import polars as pl
lf = (
pl.scan_parquet("sales.parquet")
.filter(pl.col("revenue") > 0)
.select("region", "revenue")
)
print(lf.explain())The printed plan shows the scan reading only region and revenue, with the filter applied during the scan. Exact wording varies by Polars version, so look for the projection and the selection (filter) attached to the Parquet scan node rather than separate steps after it.
Parquet gotchas
- Immutable files. You cannot update a row in place. You write new files. Table formats such as Delta Lake and Apache Iceberg add transactions on top of Parquet, but they are outside this chapter.
- Many small files. Thousands of files of a few kilobytes each cost more in metadata and open calls than the data is worth. Aim for files in the range of tens to hundreds of megabytes.
- Row group size. Very small row groups reduce compression and pushdown efficiency. Very large ones raise memory use during reads and writes.
- Type mapping. Types such as timestamps with time zones, decimals, and nullable integers do not map identically across tools. Check the schema after a round trip.
- Compression choice. Snappy is fast and widely supported. Zstd usually gives smaller files at slightly higher CPU cost. Both are readable by all four tools.
Arrow: columnar layout in memory
Parquet is a storage format optimized for small files. Apache Arrow is a specification for how columnar data is laid out in memory. Arrow is not optimized for compactness. It is optimized so that many tools can use the same bytes without converting them.
Key properties:
- Each column is a contiguous buffer of values, plus a separate bitmap that marks nulls.
- Every language and library that follows the specification reads the same layout.
- Because the layout is shared, two tools in the same process can often pass data by sharing a pointer to the buffers instead of copying. This is called zero-copy exchange.
Polars uses an Arrow-compatible memory layout internally. DuckDB can produce and consume Arrow data directly. Spark uses Arrow to transfer batches between its JVM and Python processes in several APIs, such as toPandas() when enabled. pandas can store columns as Arrow-backed types (in pandas 2.x, with pyarrow installed), though its default storage for numbers is NumPy arrays.
Zero-copy is not guaranteed. A conversion copies when the source layout differs from Arrow's. Examples include pandas columns of Python object strings and NumPy columns containing NaN used as missing values, because Arrow represents nulls with a separate bitmap. After conversion, memory use can briefly double, since both copies exist.
Parquet and Arrow together
The two formats fit together. Reading Parquet into Arrow means decoding compressed column chunks into in-memory column buffers. Writing does the reverse. A typical flow is: Parquet on disk, Arrow in memory, and whichever tool's engine on top.
What this means for tool choice
The format does not decide the tool, but it changes how much each tool's strengths matter.
| Aspect | Effect of using Parquet and Arrow |
|---|---|
| Read cost | Column pruning and pushdown cut I/O for Polars, DuckDB, and Spark when the query is lazy or SQL. |
| pandas | Benefits from `columns=` and `filters=` arguments, but you must supply them yourself. |
| Memory | Smaller on-disk size does not mean smaller in-memory size. Decoded data can be several times larger than the file. |
| Interchange | Arrow lets you hand a result from DuckDB to Polars or pandas with little or no copying. |
| Portability | A Parquet file written by one tool can be read by all the others. |
The memory row matters when you estimate whether a dataset fits in RAM. A 2 GB compressed Parquet file with many repeated strings can expand to well over 10 GB once decoded. The later chapter on memory limits builds on this.
Common errors
- Reading a whole Parquet file into pandas and then selecting columns. Select columns during the read instead.
- Using
pl.read_parquetwherepl.scan_parquetwould let the optimizer prune and push down. - Treating a conversion as free. Check for copies when data is large, especially from pandas
objectcolumns. - Writing one huge file or millions of tiny files. Both hurt performance.
- Assuming the schema survives every round trip. Verify types, null handling, and time zones.
Exercise
A table has 60 columns and 500 million rows, stored as one Parquet file. A query computes sum(revenue) grouped by region for rows where year = 2025. Name three Parquet features that reduce the work, and say what extra step would help if the year filter still reads most of the file.
Show answer
Column pruning reads only region, revenue, and year instead of all 60 columns. Predicate pushdown uses min/max statistics in the footer to skip row groups whose year range excludes 2025. Compression and encoding reduce the bytes read from disk. If the filter still reads most of the file, the data is probably not clustered by year, so row groups all contain mixed years. Sorting the data by year before writing, or partitioning it into year=... directories, lets the engine skip whole row groups or directories.
Exercise
You convert a pandas DataFrame with a Python object string column to Arrow, then pass it to DuckDB. Why might memory use rise during this step even though Arrow supports zero-copy exchange?
Show answer
Zero-copy only applies when the source memory already matches the Arrow layout. A pandas object column holds Python string objects, which differ from Arrow's contiguous string buffers, so the conversion must copy the data. While both copies exist, memory use can briefly roughly double for that column.