Sub topic 1
Why large data breaks eager pipelines
Prerequisites
This chapter assumes you know basic Python and have used DataFrames: rows, columns, filtering, and aggregation. You should recognize Polars expressions such as pl.col, filter, and group_by. You should also know that CSV and Parquet are common file formats for tabular data.
The problem: the file is bigger than your RAM
An eager pipeline runs each step as soon as you write it. In Polars, pl.read_csv is eager. It parses the whole file and builds a DataFrame in memory before your next line runs.
import polars as pl
# Eager: loads the whole file into memory before any work happens.
df = pl.read_csv("events.csv")
result = df.filter(pl.col("status") == "ok").select("user_id", "amount")The filter and the column selection come after the load. Polars has already read every row and every column by then, including the rows you discard and the columns you never use. If the file is larger than the memory you have, the read fails or the operating system kills the process.
Why memory use exceeds file size
A DataFrame in memory is often larger than its file on disk. Three effects explain this.
- Compression. Parquet files are compressed and encoded. A 2 GB Parquet file can expand to several times that size once decoded into columns.
- Text parsing. A CSV stores numbers as text. Polars converts them to typed columns, and strings need extra memory for offsets and values.
- Temporary copies. Operations such as joins, sorts, and group-bys can create intermediate data on top of the input.
So the question is not whether the file fits, but whether the file, its decoded form, and the working data all fit together. A rule of thumb is to leave a generous margin, and the margin needed depends on the query.
What failure looks like
Out-of-memory failures are not always a clean Python exception. You may see any of these:
| Symptom | Likely cause |
|---|---|
| `MemoryError` or an allocation error | The process asked for more memory than was available. |
| `Killed` in the terminal | The Linux out-of-memory killer ended the process. |
| The machine becomes very slow | The system is swapping memory to disk. |
| The notebook kernel restarts | The kernel process was killed for memory use. |
Swapping is the hardest to diagnose. The job may still finish, but far slower than expected, so a slow job on a large file can be a memory problem.
The idea behind the fix
The eager pipeline wastes memory because it loads first and decides what it needs later. The fix has two parts, and the rest of this tutorial covers them:
- Describe the whole query first, without running it. Polars can then see which rows and columns the query needs.
- Execute the query in chunks, so that only a slice of the data is in memory at a time.
The first part is a lazy query. The second is the streaming engine.
What streaming does not promise
Streaming lowers peak memory, but it does not make every query fit. Some operations must hold a lot of state, such as a group-by with many distinct keys, a large join, or a full sort. Also, if the final result is itself larger than memory, collecting it into a DataFrame still fails. Later chapters cover these limits and how to work around them.
Exercises
Exercise
A 3 GB CSV file loads with pl.read_csv on a machine with 16 GB of RAM, but the same job on a 4 GB machine gets Killed. Give two reasons why the memory needed can be larger than 3 GB.
Show answer
The CSV text is converted into typed columns, and strings need extra memory for offsets and values, so the in-memory form can be larger than the file. Operations such as joins, sorts, and group-bys can also create temporary copies on top of the loaded data. Compression in formats like Parquet adds a third reason when the input is not CSV.
Exercise
In the eager example, the filter keeps only rows where status is "ok". Why does that filter not reduce the peak memory of pl.read_csv?
Show answer
The read runs to completion first, so every row and column is already in memory when the filter runs. The filter can only shrink the data after the peak has been reached.