σμimplied vol · KKmax(S−K,0)udβρᵢⱼfat tailsPnL · drawdownyield · TdSₜ = μSₜ dt + σSₜ dWₜC = S·N(d₁) − K·e⁻ʳᵀ·N(d₂)σₚ² = wᵀΣwd₁ = [ln(S/K) + (r + σ²⁄2)T] ⁄ σ√T∂V/∂t + ½σ²S²·∂²V/∂S² + rS·∂V/∂S − rV = 0Sharpe = (Rₚ − R_f) ⁄ σₚVaR₉₉ = μ − 2.33σρ = Cov(X,Y) ⁄ σₓσᵧE[Rₚ] = Σ wᵢ·E[Rᵢ]φ(x) = e^(−x²/2) ⁄ √2πE[Rᵢ] = R_f + βᵢ(E[Rₘ] − R_f)dXₜ = θ(μ − Xₜ)dt + σ dWₜσ²ₜ = ω + α·ε²ₜ₋₁ + β·σ²ₜ₋₁C − P = S − K·e⁻ʳᵀΔ = ∂V/∂S Γ = ∂²V/∂S²p = (e^(rΔt) − d) ⁄ (u − d)rₜ = ln(Pₜ ⁄ Pₜ₋₁)E[Xₜ₊₁ | ℱₜ] = Xₜf* = (bp − q) ⁄ bz = (x − μ) ⁄ σP(N=k) = λᵏe^(−λ) ⁄ k!Θ = −∂V/∂Tν = S·φ(d₁)·√Tmin wᵀΣw s.t. wᵀμ = μ*∫Σ∂∞λ
uantCraft AcademyRoadmapsSkills
Ctrl K
AO
+1.2k Learners
LoginJoin ↗
RoadmapsSkills
AO
+1.2k Learners
Login
  • English
  • Français
  • Español
  • العربية
  • Português
  • Deutsch
  • 日本語
  • 中文
σμimplied vol · KKmax(S−K,0)udβρᵢⱼfat tailsPnL · drawdownyield · TdSₜ = μSₜ dt + σSₜ dWₜC = S·N(d₁) − K·e⁻ʳᵀ·N(d₂)σₚ² = wᵀΣwd₁ = [ln(S/K) + (r + σ²⁄2)T] ⁄ σ√T∂V/∂t + ½σ²S²·∂²V/∂S² + rS·∂V/∂S − rV = 0Sharpe = (Rₚ − R_f) ⁄ σₚVaR₉₉ = μ − 2.33σρ = Cov(X,Y) ⁄ σₓσᵧE[Rₚ] = Σ wᵢ·E[Rᵢ]φ(x) = e^(−x²/2) ⁄ √2πE[Rᵢ] = R_f + βᵢ(E[Rₘ] − R_f)dXₜ = θ(μ − Xₜ)dt + σ dWₜσ²ₜ = ω + α·ε²ₜ₋₁ + β·σ²ₜ₋₁C − P = S − K·e⁻ʳᵀΔ = ∂V/∂S Γ = ∂²V/∂S²p = (e^(rΔt) − d) ⁄ (u − d)rₜ = ln(Pₜ ⁄ Pₜ₋₁)E[Xₜ₊₁ | ℱₜ] = Xₜf* = (bp − q) ⁄ bz = (x − μ) ⁄ σP(N=k) = λᵏe^(−λ) ⁄ k!Θ = −∂V/∂Tν = S·φ(d₁)·√Tmin wᵀΣw s.t. wᵀμ = μ*∫Σ∂∞λ
uantCraft AcademyRoadmapsSkills
Ctrl K
AO
+1.2k Learners
LoginJoin ↗
RoadmapsSkills
AO
+1.2k Learners
Login
  • English
  • Français
  • Español
  • العربية
  • Português
  • Deutsch
  • 日本語
  • 中文
Parquet skill›Module 3 · Keeping a lake healthy›Topic 3.4·~1 hr 20 minAdvanced

Capstone: lay out a price lake for fast queries

Skill positionModule 3 of 3 · Topic 4 of 4
3.4 Capstone: lay out a price lake for fast queries
—Lab score— / 100Total XP—Streak—
Lecture0% read

3 min readCode examples: 3

  1. 01The idea in plain words
  2. 02Build it for the question
  3. 03Ask, then prove how little it read
  4. 04Compare with a layout built for nothing
  5. 05What the tasks ask

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.

01

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.

DefinitionData lake layout

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.

02

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.

main.pyPython
# 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']}
03

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.

main.pyPython
# 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%)
04

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.

main.pyPython
# 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
Design from the query, then measure

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.

05

What the tasks ask

  1. 1
    `build_lake`For each symbol: filter, sort_by("date"), drop the symbol column, write symbol=NAME/part-0.parquet with row_group_size; count files and row groups.
  2. 2
    `query_range`ds.dataset(root, format="parquet", partitioning="hive"), then to_table(filter=...) with the symbol and both dates, symbol first, sorted by date.
  3. 3
    `scan_plan` and `lake_summary`Walk the folders and read only the footers: row-group date statistics for the plan; row, row-group and size totals for the summary.

Key takeaways

  • Start from the question Partition on what you filter by, sort on what you range over.
  • Layers add up Folders skip files, statistics skip row groups, columns skip bytes.
  • Prove it from the footers Counts from metadata are stable evidence. Next skill: Apache Arrow.
Quick check
The lake is partitioned by symbol and sorted by date. Which query does it serve badly?
Pick an answer.

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
Explorer · lab-9-1
▾ lab-9-1
▾ data
…
bash · in-browser
learner@quantcraft:~/lab-9-1$
Python not loaded yetPythonLn 1, Col 1Spaces: 4UTF-8

Reset this topic?

Everything in lab-9-1 goes back to the starter files and the terminal is cleared. Your current solution will be lost — this can't be undone.

Will be reset
  • main.py → starter code
  • Terminal history and test output
  • Unsubmitted changes
Will be kept
  • Best lab score
  • Your XP and streak
  • Lecture progress and notes