Storage is cheap and bandwidth is not. A price lake read from object storage over a network spends most of its time moving bytes, so smaller files are faster files, as long as unpacking them costs little. The compression codec is the dial that trades one against the other, and it costs one argument to turn.
The idea in plain words
Where we are. Topic 2 showed the layers of a file and the encodings inside a chunk. Compression is the last step applied to every page, after encoding. It does not change what a reader sees, only how many bytes it fetches and how much CPU it spends unpacking them.
The algorithm that compresses a page's bytes and decompresses them again. Parquet records the codec per column chunk, so files compressed differently can be read by the same reader with no configuration.
Measure, do not guess
The ranking depends on the data, so write the same table once per codec and compare the files. compare_codecs does exactly that and returns the sizes in the order you asked. Notice that a codec that wins on prices can lose on a column of repeated labels, and that the differences between real codecs are much smaller than the gap between any of them and none at all.
# One file per codec: the sizes in bytes (codecs this Python lacks are left out)sizes = compare_codecs(table, ".", codecs)for codec, size in sizes.items(): print(f"{codec:7} {size:>9,} bytes {size / sizes['none']:.0%} of none")
none 1,498,175 bytes 100% of none snappy 1,246,066 bytes 83% of none gzip 938,263 bytes 63% of none zstd 988,171 bytes 66% of none
# The codec is recorded in each file, so nothing has to be told to the readerfor codec in codecs: print(codec, "->", codec_of(codec + ".parquet"))print("smallest:", best_codec(table, ".", codecs))
none -> UNCOMPRESSED
snappy -> SNAPPY
gzip -> GZIP
zstd -> ZSTD
smallest: ('gzip', 938263)Snappy is the long-time default because it is cheap to unpack. Zstandard usually gives smaller files at a similar speed and lets you raise the level for data written once and read many times, like an archive of history. Choose by measuring on your own columns, and remember that a slower codec costs CPU on every read.
What the tasks ask
- 1`write_codec`
pq.write_table(table, path, compression=codec), then returnos.path.getsize(path). - 2`codec_of`Read
.compressionof the first column chunk of the first row group. - 3`compare_codecs` and `best_codec`Write
<codec>.parquetfor each codec and collect the sizes in order; then take the smallest (first wins a tie) as(name, bytes).
Lab · what's graded
4 tasks · 100 pts- Write with a codec:
write_codec25 pts - Read the codec from the footer:
codec_of25 pts - Compare codecs on one table:
compare_codecs25 pts - Pick the smallest:
best_codec25 pts