Gallery
One dataset, two layouts
120 million yellow-cab trips written as one file, as Hive year/month partitions, and with DuckDB 2.0's PARTITION_PATH and ORDER_BY, then measured by how much of each a query has to read.
Sorting, not partitioning, cut what a query reads
One recent month (August 2026)
| One file | 12.3 |
|---|---|
| Hive year/month | 12.5 |
| Archive/recent, sorted | 6.4 |
One archive year (2024, four columns)
| One file | 240.8 |
|---|---|
| Hive year/month | 248.0 |
| Archive/recent, sorted | 168.4 |
One evening (31 Dec 2025 from 6 pm)
| One file | 1.8 |
|---|---|
| Hive year/month | 2.5 |
| Archive/recent, sorted | 0.2 |
DuckDB 2.0 added two options to COPY … (PARTITION_BY …). PARTITION_PATH takes an expression that names each partition's folder, so a layout no longer has to be year=2024/month=1/. ORDER_BY sorts the rows before they are written. The third layout here uses both: one folder per past year under archive/, one per month of 2026 under recent/, each sorted by pickup time.
| One file | 1 | 1 | 972 | 1,700.8 |
| Hive: year/month | 32 | 32 | 1,012 | 1,749.0 |
| Archive by year, recent by month, sorted | 10 | 74 | 1,012 | 1,475.8 |
Sorting also made the data smaller: 1,475.8 MB against 1,700.8 MB for the single file, 13% less, because sorted timestamps compress better. Folders on their own did not cut what a query reads. For the 2024 query, the Hive layout reads 12 files and slightly more data than the single file, since each small file carries its own row-group edges. What the folders give is a way to find, copy or delete one month without reading the rest.
ORDER_BY did not produce one sorted file per partition. The writer split each archive year into many files, 26 for 2024, and sorted the rows within each file, so 26 of them overlap another file's time range. A query still skips most row groups inside each file, but it has to read every file's footer, and the number of files changed from run to run. A test that raised partitioned_write_flush_threshold, so that a year would be written in one batch, ran out of the container's temporary disk space.
Write the archive/recent layout with PARTITION_PATH and ORDER_BYgallery/g08-partition-layouts/pipeline.sqlShow SQL
-- Archive years get month bucket 0, so each year is one partition and one folder.
COPY (SELECT *, year(pickup_at) AS year,
CASE WHEN year(pickup_at) < 2026 THEN 0 ELSE month(pickup_at) END AS bucket
FROM a)
TO 'gallery/g08-partition-layouts/out/layouts/c_archive_recent'
(FORMAT parquet, COMPRESSION zstd, PARTITION_BY (year, bucket),
PARTITION_PATH CASE WHEN bucket = 0 THEN 'archive/' || year
ELSE 'recent/' || year || '/' || lpad(bucket::VARCHAR, 2, '0') END,
ORDER_BY (pickup_at));
-- A reader names the folder in the path; there are no year=/month= keys to filter on.
SELECT hour(pickup_at) AS hour, count(*) AS trips
FROM read_parquet('gallery/g08-partition-layouts/out/layouts/c_archive_recent/recent/2026/08/*.parquet')
GROUP BY ALL;Runs at build time in DuckDB 2.0; shown for reading.Open file
The pipeline downloads the 32 monthly files, drops 780 trips whose pickup falls outside their file's month, and writes the three layouts. It then reads every footer with parquet_metadata and counts, for each query, the row groups whose pickup range overlaps the query's window. It fails unless all three layouts hold the same rows and each query returns the same result on all three.
duckdb -bail :memory: -c ".read gallery/g08-partition-layouts/pipeline.sql"Full pipeline: gallery/g08-partition-layouts/pipeline.sql. Chapter 1 writes its own working copy of the trip files with PARTITION_BY; Chapter 14 covers keeping public data files versioned.