Eight topics gave you separate tools: columns, row groups, codecs, pushdown, partitions, schema, statistics, compaction. A lake is where they meet. The way to design one is to start from the question that will be asked a million times, and lay the files out so that question touches almost nothing. Here the question is: the bars of one symbol between two dates.
The idea in plain words
Where we are. The last topic of the Parquet skill. It uses PyArrow only; the DuckDB skill showed the same lake queried with SQL, and the Arrow skill goes into the memory format underneath. The labs of both skills share one set of bars, so you can read the same result three ways.
The choices that decide how little a query must read: which columns define the folders, how rows are ordered inside files, how large the files and row groups are, and how the schema may change. A layout is good for the questions it was built for and poor for the others.
Build it for the question
build_lake does three things. It gives every symbol its own folder symbol=NAME, so a symbol filter opens one file. It sorts each file by date, so a date range is a narrow stretch of the file. It cuts each file into row groups, each with its own date statistics. The symbol column is left out of the files because the folder name already says it.
# Lay the five stocks out as one folder each, sorted by date, in row groups of 500 rowsfiles, groups = build_lake(table, "price_lake", 500)print(files, "files,", groups, "row groups")print(sorted(os.listdir("price_lake")))print(lake_summary("price_lake"))
5 files, 70 row groups
['symbol=AAPL', 'symbol=JNJ', 'symbol=JPM', 'symbol=KO', 'symbol=XOM']
{'files': 5, 'row_groups': 70, 'rows': 33493, 'bytes': 1497830, 'symbols': ['AAPL', 'JNJ', 'JPM', 'KO', 'XOM']}Ask, then prove how little it read
query_range passes the symbol and both dates as one filter to the dataset reader. scan_plan answers the question a skeptic asks: how much of the lake did that touch? It works from the footers: one file opened, and the row groups whose date range meets the query. For KO in 2020 that is one row group of seventy.
# KO in 2020: the rows, then what the query had to touchlow, high = date(2020, 1, 1), date(2020, 12, 31)ko = query_range("price_lake", "KO", low, high)print(ko.num_rows, "rows;", ko.column_names)opened, read, total = scan_plan("price_lake", "KO", low, high)print(f"files opened: {opened} | row groups read: {read} of {total} ({read / total:.1%})")
253 rows; ['symbol', 'date', 'open', 'high', 'low', 'close', 'volume'] files opened: 1 | row groups read: 1 of 70 (1.4%)
Compare with a layout built for nothing
A lake is not good in the abstract. Take the same rows, shuffle them and write one file: no folders, no order. The statistics still exist, but every row group now holds dates from all years, so no group can be ruled out and the same question reads every one. The data and the format are identical; only the layout differs.
# The same bars shuffled into a single file with no folders: how many row groups can the same question skip?order = sorted(range(table.num_rows), key=lambda i: (i * 7919) % table.num_rows)pq.write_table(table.take(pa.array(order)), "naive.parquet", row_group_size=500)meta = pq.ParquetFile("naive.parquet").metadataindex = meta.schema.to_arrow_schema().names.index("date")hits = 0for g in range(meta.num_row_groups): stats = meta.row_group(g).column(index).statistics if stats.min <= high and stats.max >= low: hits += 1print(f"shuffled single file: {hits} of {meta.num_row_groups} row groups must be read")print(f"designed lake : {read} of {total} row groups")
shuffled single file: 67 of 67 row groups must be read designed lake : 1 of 70 row groups
If a second question matters, say all symbols on one day, this layout is poor for it: it must open every folder. Ask which questions are frequent, partition and sort for those, and let the rest be slower. Check with scan_plan-style numbers from the footers, not with a stopwatch, so the answer does not depend on today's machine.
What the tasks ask
- 1`build_lake`For each symbol: filter,
sort_by("date"), drop thesymbolcolumn, writesymbol=NAME/part-0.parquetwithrow_group_size; count files and row groups. - 2`query_range`
ds.dataset(root, format="parquet", partitioning="hive"), thento_table(filter=...)with the symbol and both dates,symbolfirst, sorted by date. - 3`scan_plan` and `lake_summary`Walk the folders and read only the footers: row-group
datestatistics for the plan; row, row-group and size totals for the summary.
Lab · what's graded
4 tasks · 100 pts- Lay out the lake:
build_lake25 pts - Ask the question:
query_range25 pts - Prove how little it reads:
scan_plan25 pts - Audit the lake:
lake_summary25 pts