Sub topic 1
Why columnar storage matters
Parquet is a columnar file format. Everything else in this tutorial, including row groups, encodings, compression, and statistics, follows from that one decision. This chapter explains what columnar storage is, what it gains and costs, and how to see the effect in a real file.
Prerequisites
This chapter assumes you have:
- Basic familiarity with tabular data and schemas (columns, types, rows).
- Working knowledge of Python, including installing packages with
pip. - Some awareness that row-oriented and column-oriented storage differ. You don't need to know how either works internally.
To run the code, install pyarrow and numpy.
Row-oriented and column-oriented layouts
A file is a sequence of bytes, so a table must be flattened into a line. There are two natural ways to do it.
- Row-oriented layout writes all values of row 1, then all values of row 2, and so on. CSV, JSON Lines, Avro, and most transactional databases store data this way.
- Column-oriented layout writes all values of column 1, then all values of column 2, and so on. Parquet and ORC store data this way.
For a table with columns id, amount, and category, the two layouts look like this:
Neither layout is better in general. Each makes some access patterns cheap and others expensive.
What a row layout is good at
A row layout works well when you touch whole records:
- Fetching one customer by key and reading every field.
- Appending a new record. You write one contiguous chunk at the end of the file.
- Updating a record in place.
These are transactional workloads (often called OLTP, online transaction processing). The data for one entity sits together, so one read or write serves one request.
What a column layout is good at
Analytical workloads (OLAP, online analytical processing) look different. A typical query reads a few columns across many rows:
SELECT category, SUM(amount)
FROM events
WHERE id >= 950000
GROUP BY category;This query needs three columns, and a real events table might have 50. In a row layout, the reader must pull every row from disk, including the 47 columns it will discard. In a column layout, it reads only the byte ranges for the columns it needs. This is called column projection. It cuts I/O in proportion to the share of columns skipped.
Columnar layout has two further benefits.
Similar values sit together
Values in one column share a type and often a range or pattern. A country column contains a few hundred distinct strings repeated millions of times. A timestamp column increases steadily. When such values are adjacent, simple encodings shrink them well, and a general-purpose compressor then finds more redundancy to remove. A row layout mixes integers, floats, and strings in one byte stream, which hides these patterns. Later chapters cover the specific encodings (dictionary, run-length, delta) and the codecs (Snappy, Zstandard, Gzip).
Processing is faster in bulk
Aggregating a column means looping over one array of one type. Modern CPUs handle this well. The data is contiguous in memory, so the CPU cache is used efficiently, and the compiler can use SIMD (single instruction, multiple data) instructions that process several values per step. Libraries such as Apache Arrow build on this, which is why Parquet pairs naturally with Arrow in memory.
What columnar storage costs
The trade-offs are real, and they affect how you use Parquet.
| Operation | Row layout | Column layout |
|---|---|---|
| Read a few columns of many rows | Reads everything | Reads only those columns |
| Read one whole record | One contiguous read | One read per column |
| Append one record | Cheap | Costly, since every column must be extended |
| Update one value | Possible in place | Rarely possible in place |
| Compression ratio | Moderate | Usually higher |
Two consequences matter in practice:
- Parquet files are written in batches and treated as immutable. You don't update a Parquet file in place. You write a new file, or use a table format layered on top (such as Delta Lake or Apache Iceberg) that manages many files.
- Reconstructing a row is not free. Retrieving all fields of one record means reading from every column's region of the file, and each read may touch a different disk page.
Parquet's answer: a hybrid layout
Pure column layout has a problem. If a file stores all 100 million values of column 1 before any of column 2, a reader that wants rows 5,000,000 to 5,000,100 of three columns must jump across huge distances. Writers also can't flush data until they have seen every row.
Parquet avoids both problems by splitting the table horizontally first. It cuts the rows into row groups, and inside each row group it stores each column contiguously as a column chunk. In short:
- Rows are divided into groups of, for example, 100,000 rows.
- Within each group, data is column-oriented.
- A footer at the end of the file records where every column chunk lives, along with statistics.
This gives the benefits of columns, because a reader fetches only the column chunks it needs. It also gives bounded memory use for writers and readers, because they work one row group at a time. The next chapters take this structure apart piece by piece.
Seeing the effect in a real file
The following script writes a table with four columns and then compares reading one column against reading all of them. Exact timings depend on your machine and disk cache, but the gap between the two reads should be visible.
import time
import numpy as np
import pyarrow as pa
import pyarrow.parquet as pq
n = 1_000_000
rng = np.random.default_rng(0)
table = pa.table({
"id": np.arange(n),
"amount": rng.random(n),
"category": pa.array(rng.choice(["a", "b", "c", "d"], n).tolist()),
"noise": rng.integers(0, 10**9, n),
})
pq.write_table(table, "wide.parquet", compression="zstd")
def timed_read(columns):
start = time.perf_counter()
result = pq.read_table("wide.parquet", columns=columns)
return result.num_rows, time.perf_counter() - start
rows, seconds = timed_read(["category"])
print(f"one column : {rows} rows in {seconds:.3f}s")
rows, seconds = timed_read(None)
print(f"all columns: {rows} rows in {seconds:.3f}s")Passing columns=None reads every column. Passing ["category"] asks the reader to fetch only the category column chunks. The category column has four distinct values, so it is cheap to decode and small on disk.
You can also see where the bytes went. The footer records the compressed size of each column chunk:
import pyarrow.parquet as pq
md = pq.ParquetFile("wide.parquet").metadata
row_group = md.row_group(0)
for i in range(md.num_columns):
chunk = row_group.column(i)
print(
f"{chunk.path_in_schema:10s}"
f" compressed={chunk.total_compressed_size:>10,}"
f" uncompressed={chunk.total_uncompressed_size:>10,}"
)You should see that category is tiny compared with noise. The noise column holds random integers, which have almost no pattern to exploit. The id column also compresses well because consecutive values are predictable. A query that touches category and id pays only for those two chunks. Because the file is columnar, the cost of a query follows the columns it uses, not the width of the table.
Common mistakes and edge cases
- Selecting every column out of habit.
SELECT *andread_tablewithoutcolumns=throw away the main advantage of the format. Name the columns you need. - Expecting a speedup on narrow tables. If a table has three columns and you read all three, projection saves nothing. Gains from compression and bulk processing still apply.
- Writing many tiny files. Each file has its own footer and overhead, and each costs a request on object storage. A few larger files usually beat thousands of small ones.
- Assuming smaller always means faster. Heavier compression saves disk and network but costs CPU to decode. A later chapter weighs this directly.
- Treating Parquet like a database. The format has no indexes in the database sense and no in-place updates. Its skipping features (statistics, page indexes, Bloom filters) are coarse tools that help most when data is laid out to suit the filter.
Key takeaways
- A column layout stores each column's values together, so queries read only the columns they use.
- Values within one column are similar, which makes encoding and compression more effective.
- Columnar storage is weaker for single-record reads and for updates, so Parquet files are written in batches and not modified in place.
- Parquet combines both ideas: rows are split into row groups, and each row group stores columns contiguously, with a footer that maps everything.
Exercise
A table has 40 columns and 500 million rows. A dashboard query filters on event_date and sums revenue. Explain why the same data in Parquet typically costs less I/O than in CSV, and name one workload where a row-oriented format would be the better choice.
Show answer
In CSV, the reader must scan every byte of every row, including the 38 columns the query never uses, because fields of one row are interleaved. In Parquet, column projection lets the reader fetch only the event_date and revenue column chunks, which is about 2 of 40 columns. Those columns are also stored with type-specific encodings and compression, so they are smaller on disk. A row-oriented format is the better choice for a transactional workload, such as looking up and updating one customer record by key hundreds of times per second, where each request needs every field of one row and a column layout would need a separate read per column.
Exercise
In the timing script above, you change timed_read(["category"]) to timed_read(["category", "id", "amount", "noise"]). What do you expect to happen to the run time, and why?
Show answer
The run time should rise to roughly that of reading all columns. Projection only saves work when columns are left out. Listing all four columns is equivalent to columns=None, so the reader fetches and decodes every column chunk, including the poorly compressible noise column.