Data Engineering for AnalyticsVol. 21 · Analytics
0 / 0

Shelves / Data Engineering for Analytics

DE·07

Lakehouses and File Formats

How analytical data is stored when it lives in object storage rather than inside one database: Parquet's anatomy (row groups, column chunks, pages, encodings, statistics), choosing sort orders and partitions, the small-files problem, and table formats (Apache Iceberg, Delta Lake, Apache Hudi) that add transactions, snapshots, time travel, schema evolution and hidden partitioning on top of files, so several engines can share one copy of the data; with a lab that skips 48 of 50 row groups by sorting, measures the cost of 2,000 tiny files, and rolls back a bad overwrite in Iceberg.

17 topics · ~9 min read · 16-question quiz
Updated Suggest a correction

Start here: one copy of the data, many engines

A warehouse keeps data in its own internal format, readable only through it. A lakehouse keeps analytical tables as open files in object storage (S3, GCS, Azure Blob) with a table format on top, so Spark, Flink, DuckDB, Trino, Snowflake, BigQuery and Python can all read and write the same tables. It combines the cheap, open storage of a data lake with the transactions and table management of a warehouse.

Two layers make it work:

  • File format: almost always Apache Parquet: columnar, compressed, self-describing, with statistics that let readers skip data.
  • Table format: Apache Iceberg, Delta Lake or Apache Hudi: metadata that says which files make up the table at each version, enabling atomic commits, snapshots, time travel, schema evolution and partition management.

Measured

Experiment Result
query one week from 5 M orders, Parquet not sorted by time read 50 of 50 row groups; 103 ms
same, Parquet sorted by time read 2 of 50 row groups; 7 ms
the same data as 1 file vs 2,000 small files 29 ms vs 262 ms
a bad job overwrote an Iceberg table (wrong rows, wrong units) time travel read the previous snapshot; rollback restored it in one metadata operation

Physical layout decided a 14× difference; file count a 9× difference; and table snapshots turned a data disaster into a one-line fix.

How deep to go

Volume 17 defines six mastery levels. For lakehouse storage:

Level What you can do
L1 Explain Parquet and why table formats exist
L2 Write partitioned Parquet and create Iceberg or Delta tables
L3 Explain row groups, statistics, snapshots, manifests and commit protocols
L4 Diagnose slow scans (no pruning, small files, delete files) and fix them
L5 Design layouts, partitioning, maintenance and multi-engine access
L6 Operate it: compaction, snapshot expiry, catalog availability, cost

Parquet's anatomy

Text
file
 ├─ row group 1 (e.g. 100,000–1,000,000 rows)
 │    ├─ column chunk: order_id     → pages (dictionary page, data pages)  + statistics (min, max, nulls)
 │    ├─ column chunk: placed_at    → pages …
 │    └─ …
 ├─ row group 2 …
 └─ footer: schema, row-group and column-chunk metadata, statistics, offsets
  • Encodings per column chunk (dictionary, RLE, delta, bit-packing) then compression per page (zstd, Snappy).
  • Statistics per row group (min/max/null count) drive row-group skipping; optional page indexes skip pages within a row group; bloom filters help equality lookups on high-cardinality columns.
  • Readers read the footer first, decide which row groups and columns they need, then fetch only those byte ranges: in object storage, a few HTTP range requests.

The lab's column sizes show where bytes go: in the sorted file, the timestamp column was the largest (16 MB of 56 MB) and a 4-column query needed 46% of the bytes; a SELECT * needs 100%.

Layout: sorting and partitioning

  • Partition by a low-cardinality column used in most filters, usually date (day or month), so whole directories are skipped. Aim for partitions of hundreds of MB to a few GB, not thousands of tiny ones.
  • Sort (cluster) within partitions by the next most common filter or join key (kitchen, customer), so row-group statistics prune. Z-ordering or Hilbert curves interleave several columns when filters vary.
  • Sorting can change compression either way: the lab's file grew from 34 MB to 56 MB when sorted by time, because the generator's original order happened to compress other columns better. Measure.

The small-files problem

Streaming writes, frequent micro-batches and over-partitioned tables produce thousands of small files. Each costs metadata, opens and planning (the lab: 2,000 files made a query 9× slower). Fixes:

  • write fewer, larger files (target ~128 MB–1 GB) by buffering or repartitioning before writing;
  • compaction: periodically rewrite small files into large ones (Iceberg's rewrite_data_files, Delta's OPTIMIZE);
  • partition less finely.

Table formats

Capability How
atomic commits a writer creates new data files and new metadata, then swaps the table's current-metadata pointer in the catalog in one atomic operation; readers see the old or the new version, never a mix
snapshots and time travel each commit is a snapshot listing its files (via manifest lists and manifests); query AS OF a snapshot or timestamp
concurrent writers optimistic concurrency: if another commit happened, re-check and retry
schema evolution columns tracked by ID, so add, drop, rename and reorder don't rewrite data or confuse old files
partition evolution / hidden partitioning (Iceberg) partition transforms (day(ts), bucket(n, id)) applied automatically; change the scheme without rewriting history
row-level deletes and updates copy-on-write (rewrite affected files: costly writes, fast reads) or merge-on-read (write delete files: cheap writes, reads apply them until compaction)
maintenance compaction, snapshot expiry, orphan-file cleanup, manifest rewriting

Iceberg, Delta Lake and Hudi differ in details and ecosystems (Iceberg's catalog standard via the REST catalog is widely supported across engines; Delta is central to Databricks; Hudi focuses on incremental upserts), and interoperability layers increasingly let one engine read another's format. The concepts are the same.

Catalogs

The catalog maps table names to their current metadata location and performs the atomic pointer swap: Iceberg's REST catalog implementations (Polaris, Nessie, cloud services), AWS Glue, Hive Metastore, Unity Catalog. It's also where access control and discovery live (chapter 09). The lab used PyIceberg's SQL catalog on SQLite: fine for learning, not for production.

TiffinBox's lakehouse

Text
s3://tiffinbox-lake/
  bronze/   raw events and CDC as Iceberg tables, partitioned by day(ingested_at)
  silver/   cleaned, deduplicated (dbt or Spark), partitioned by day(placed_at), sorted by kitchen_id
  gold/     facts and dimensions for BI, built by dbt
catalog:   Iceberg REST catalog; engines: Spark (batch), Flink (streaming writes), DuckDB/Trino (ad hoc), the warehouse (BI)
maintenance: nightly compaction of yesterday's partitions; snapshots kept 7 days; orphan cleanup weekly

(Bronze, silver and gold are common names for raw, cleaned and modelled layers: chapter 02's raw, staging and marts.)

Common traps

  • Directories of Parquet as "tables", with readers seeing half-written data.
  • Over-partitioning (by hour, by customer) into tiny files.
  • No maintenance: snapshots and delete files accumulating forever.
  • Unsorted data that defeats statistics.
  • Treating time travel as a backup (snapshots expire; the catalog is a single point of failure).

Lab: pruning, small files and a rollback

Setup. DuckDB and PyArrow on the lab machine; chapter 01's 5 million orders. PyIceberg 0.12 with a local SQL (SQLite) catalog and a file-system warehouse for the table-format experiment.

Experiment 1: row-group skipping

The orders were written twice as zstd Parquet with row groups of 100,000 rows: once in a scrambled order, once sorted by placed_at. Query: revenue per kitchen for one week (1–8 March 2026). Row groups "needed" are those whose placed_at min/max overlap the week.

File Row groups needed Query time (median of 5)
unsorted 50 of 50 102.6 ms
sorted by time 2 of 50 7.4 ms

Compressed bytes per column in the sorted file (total 56.0 MB): placed_at 16.0 MB, customer_id 11.8, amount_paise 8.4, note 7.7, order_id 6.8, rider_id 3.3, kitchen_id 1.1, delivery_minutes 0.5, rating, status and payment_method 0.1 each, is_veg < 0.1. A query reading kitchen_id, placed_at, amount_paise, status reads 46% of the bytes.

Experiment 2: small files

The same 5 million rows written as 2,000 files (partitioned by order_id % 2000) versus one file; query: revenue per kitchen over all rows.

Layout Query time
1 file 29 ms
2,000 files 262 ms

Experiment 3: snapshots, time travel and rollback (Iceberg)

Three daily appends (1–3 October), then a bad job overwrote the table with only one day's data, in rupees instead of paise:

Step Rows Revenue (reading amounts as paise)
after 3 appends 41,094 —
after the bad overwrite 13,697 ₹32,804
time travel to the snapshot before the overwrite 41,094 ₹98,62,953
rollback to that snapshot 41,094 restored

Then add_column("channel"): old rows read channel as null with no data rewritten. The table's history showed 6 snapshot entries.

What it shows

  • Sort order turns statistics into a fast index: 48 of 50 row groups skipped, 14× faster. On object storage, skipping also saves network requests and money.
  • File count is a cost of its own: 2,000 files made a full scan 9× slower than one file holding the same rows.
  • Sorting changed compression: the time-sorted file was 56 MB versus 34 MB for the generator's original order. Physical layout choices affect size and speed together; measure both.
  • Snapshots make mistakes reversible: the bad overwrite was undone by pointing the table back at an earlier snapshot, without restoring files from backup. Keep snapshots long enough to notice mistakes, and remember they expire.

Try it yourself

  1. Partition the Iceberg table by day(placed_at) and sort by kitchen_id; run a one-day query and inspect which files are planned (table.scan(row_filter=…).plan_files()).
  2. Append 200 small batches, then compact them, and compare scan planning time before and after.
  3. Delete one kitchen's orders with merge-on-read and look at the delete files created.
  4. Read the same Iceberg table from DuckDB's Iceberg extension and from Spark.

Revision sheet: lakehouses and file formats

Key ideas

  • Lakehouse: open files in object storage + a table format + a catalog; many engines, one copy.
  • Parquet: row groups, column chunks, pages, encodings, footer statistics; readers fetch only needed byte ranges.
  • Partition by common low-cardinality filters (date); sort within for statistics; measure compression effects.
  • Small files: write larger, compact, partition less finely.
  • Table formats: atomic commits via catalog pointer swaps, snapshots, time travel, schema and partition evolution, row-level deletes (copy-on-write or merge-on-read), maintenance.
  • Bronze/silver/gold = raw/cleaned/modelled.

Numbers to remember

  • Lab: sorted file read 2 of 50 row groups (7 ms vs 103 ms); 2,000 files 262 ms vs 1 file 29 ms; rollback restored 41,094 rows.
  • Target file sizes ~128 MB–1 GB.

Glossary

  • Row group: a horizontal slice of a Parquet file with per-column chunks.
  • Manifest: Iceberg metadata listing data files and their statistics.
  • Snapshot: a committed version of a table.
  • Copy-on-write / merge-on-read: strategies for row-level changes.
  • Catalog: the service mapping table names to current metadata.