Statistics skip blocks inside a file, but the reader still has to open the file to read them. When a lake holds thousands of files, even opening them costs time. Partitioning moves the decision outside the files: the folder names say what each one holds, so a filter can discard whole files from a directory listing.
The idea in plain words
Where we are. Topic 4 skipped row groups with their statistics; this topic skips files with their folder names, the same idea one level up. The DuckDB skill's last topic already wrote a partitioned lake; here we look at how the layout is chosen and what it costs. The next topic is what happens when the columns of those files change over time.
Splitting a dataset into folders named column=value, one level per partitioning column. The values live in the folder names rather than in the files, and a reader turns them back into columns, so a filter on them can skip whole folders.
Write the folders, then filter on them
write_hive partitions the bars by symbol and then by year, so a file sits at a path like symbol=KO/year=2020/. Asking for the files of one symbol with fragments_for matches the filter against the folder names only, and returns just that symbol's files.
# Write the bars partitioned by symbol and year, and see the foldersleaves = write_hive(table, "lake", ["symbol", "year"])print(len(leaves), "folders, for example:")print(leaves[0], "|", leaves[-1])print(fragments_for("lake", "KO"), "of", len(leaves), "files are KO's")
135 folders, for example: symbol=AAPL/year=2000 | symbol=XOM/year=2026 27 of 135 files are KO's
# Read one symbol-year: the reader opens a single folder and rebuilds the partition columnsko = read_slice("lake", "KO", 2020)print(ko.num_rows, "rows;", ko.column_names)print("first:", ko["date"][0], "| last:", ko["date"][-1])
253 rows; ['symbol', 'date', 'close', 'volume', 'year'] first: 2020-01-02 | last: 2020-12-31
Choose partition columns with care
A good partition column is one you filter on, with a modest number of values. symbol and year fit: a handful of symbols times twenty-seven years. date does not: it would make a folder and a tiny file for every trading day, and the cost of listing and opening them would outweigh any saving. The number of distinct values is the number of folders you get.
# What partitioning by date would cost: one folder per distinct valuefor column in ("symbol", "year", "date"): print(f"{column:7} -> {len(set(table[column].to_pylist())):>5} distinct values = that many folders per level")
symbol -> 5 distinct values = that many folders per level year -> 27 distinct values = that many folders per level date -> 6699 distinct values = that many folders per level
Open one file from symbol=KO/year=2020/ on its own and symbol and year are missing: they exist only in the path. Read the dataset through a dataset API and they come back as columns. Copy a file out of its folder and the information goes with the folder, so move partitions as folders, never as loose files.
What the tasks ask
- 1`with_year`
table.append_column("year", pc.year(table["date"])). - 2`write_hive`
pq.write_to_dataset(table, root, partition_cols=...), then walkrootand collect the folders that hold files. - 3`fragments_for` and `read_slice`Open
ds.dataset(root, format="parquet", partitioning="hive"); countget_fragments(filter=...)and read withto_table(filter=...), where the filter is built withds.field(...).
date, one folder per trading day, with ten rows per file. What is the most likely problem?Lab · what's graded
4 tasks · 100 pts- Add the year column:
with_year25 pts - Write the folders:
write_hive25 pts - Count the files a filter opens:
fragments_for25 pts - Read one symbol-year:
read_slice25 pts