Sub topic 6
Encodings: dictionary, run-length, and delta
This chapter assumes you know what a Parquet column chunk and page are, and that you can run basic PyArrow code. Encodings are the first size reduction Parquet applies to a page. Compression codecs run afterward on the encoded bytes.
Encoding versus compression
An encoding changes how values are represented, using what the writer knows about the column's type and contents. A compression codec is a general-purpose byte compressor and knows nothing about values. Parquet applies them in order: values are encoded, then the encoded page is compressed. A good encoding often makes the later compression step cheaper and more effective.
Dictionary encoding
Dictionary encoding stores each distinct value once, in a dictionary page. The data pages then hold small integer indexes into that dictionary. It works well when a column has few distinct values compared with its row count, such as a country code or a status field.
In the category column from the earlier example, one million rows contain only a, b, c, and d. The dictionary holds four entries, and each row stores an index from 0 to 3. Two bits are enough for each index.
Writers fall back to plain encoding when the dictionary grows too large. In that case the column chunk contains dictionary-encoded pages followed by plain pages.
Run-length and bit-packed encoding
Run-length encoding (RLE) replaces a run of repeated values with the value and a repeat count. Ten thousand zeros in a row become "10000 × 0".
Parquet combines RLE with bit packing in a hybrid format. Long runs are stored as run-length groups. Short or varied stretches are packed into the minimum number of bits per value. Dictionary indexes use this hybrid, and so do the definition and repetition levels for nullable and nested columns.
Sorted or clustered data benefits most. A column whose equal values sit next to each other forms long runs. The same values in random order form short runs and compress poorly.
Delta encoding
Delta encoding stores the first value, then the difference between each value and the one before it. It suits integers that grow steadily, such as timestamps or sequence numbers.
| Original values | Stored as |
|---|---|
| 1000, 1001, 1002, 1004 | 1000, then +1, +1, +2 |
The differences are small and similar, so they pack into few bits. The id column in the first example (0 to 999,999) is a good fit. Parquet also defines delta encodings for byte arrays: DELTA_LENGTH_BYTE_ARRAY stores string lengths with delta encoding, and DELTA_BYTE_ARRAY stores a shared prefix length plus a suffix for each string.
Seeing which encodings were used
Each column chunk records its encodings in the footer. You can read them with the metadata API from the inspection example.
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", compression="zstd")
rg = pq.ParquetFile("events.parquet").metadata.row_group(0)
for i in range(rg.num_columns):
col = rg.column(i)
print(col.path_in_schema, col.encodings)Exact output depends on the PyArrow version. Expect the dictionary-encoded category column to list a dictionary encoding along with RLE or RLE_DICTIONARY. By default PyArrow enables dictionary encoding for all columns, so id may start dictionary-encoded and fall back to plain pages once its dictionary grows too large.
Controlling encodings
write_table accepts use_dictionary, which can be a boolean or a list of column names. Setting use_dictionary=False for a high-cardinality column avoids wasted dictionary work. The column_encoding argument lets you request an encoding such as DELTA_BINARY_PACKED per column. It requires use_dictionary=False.
pq.write_table(
table,
"events_delta.parquet",
use_dictionary=False,
column_encoding={"id": "DELTA_BINARY_PACKED"},
)Exercise
A column holds 50 million rows of a 2-letter country code with about 200 distinct values. Which encoding does a Parquet writer usually choose, and what does each data page store?
Show answer
Dictionary encoding. The dictionary page holds the roughly 200 distinct codes once. Each data page stores small integer indexes into that dictionary, packed with the RLE and bit-packing hybrid, instead of the two-letter strings.
Exercise
Why does sorting a table by a low-cardinality column before writing often shrink the file?
Show answer
Equal values end up next to each other, so the RLE part of the hybrid encoding produces long runs that take very little space. In random order the runs are short and the savings shrink.