Arman Pouyaei Start a conversation
Lesson 38  ·  Nov 24, 2026
Data Engineering Fundamentals

Serving the gold layer

Stage 4: serve. Gold is the dataset you hand to the world — enriched, aggregated to the grain people actually ask about, and published in a format they can query in place. For this pipeline: county-hour temperature summaries, partitioned by date, written once and read by DuckDB straight from the lake. No database server required.

The code

import duckdb

con = duckdb.connect()
con.execute("INSTALL spatial; LOAD spatial;")

con.execute("""
    COPY (
        SELECT county,
               CAST(time AS DATE)      AS date,
               date_trunc('hour', time) AS hour,
               ROUND(AVG(temp_c), 2)    AS mean_temp_c,
               COUNT(*)                 AS n_obs
        FROM read_parquet('lake/silver/enriched/**/*.parquet',
                          hive_partitioning := true)
        GROUP BY county, date, hour
    ) TO 'lake/gold/county_temps'
    (FORMAT PARQUET, PARTITION_BY (date), OVERWRITE_OR_IGNORE)
""")

In practice

Model the question, not the source. Nobody's dashboard asks for raw observations; they ask 'how warm was Mercer County each hour?' Gold stores exactly that grain — county × hour — pre-aggregated, so a query reads thousands of rows instead of millions.

Publish files your tools already read. GeoParquet and Parquet in a lake are queryable in place by DuckDB, PostGIS loaders, Sedona, and every BI tool — the serving layer is a *location and a format*, not necessarily a server. Lesson 25's partitioning pays off here: a query for one day prunes every other day's files without opening them.

Republishing a day is safe. OVERWRITE_OR_IGNORE rewrites whole date partitions, which makes the publish step idempotent (Lesson 36 again): fix silver, republish the day, and gold is correct — no surgical row edits in the serving layer. Consumers read gold only. Never bronze, rarely silver.

Python bridge: This is Lesson 21's read/write pipeline with an audience: the write side is now a product, with a chosen grain, a format, and consumers who depend on both staying stable.

Further reading

Spatial Extension — DuckDB documentation (DuckDB's spatial extension docs: reading and writing spatial files (ST_Read, GeoParquet) and querying them in place)

Try it

Practice question: A dashboard only ever shows the last 7 days for one county. Which two earlier lessons explain why this query stays fast against gold — and what exactly makes it fast?

Show answer

Result: Lesson 25 (partitioning): the date= folders mean the query opens only 7 days of files and prunes the rest of history untouched; the county filter then selects within a small, pre-aggregated table. Lesson 14/24 (Parquet and GeoParquet): columnar storage reads only the columns the query names, and the ** glob plus hive_partitioning turns the folder layout into queryable columns. Underneath both: gold is small on purpose — the aggregation happened once at publish time, so no query ever scans raw observations. Gold's speed is designed in, not bought with a bigger server.

← All posts