Sub topic 7
Silver: clean, type, and conform
This chapter assumes you can read basic SQL and PySpark, know what a Delta table is, and have already landed raw data in a bronze table such as main.bronze.orders. Deduplication with MERGE gets a full treatment in the next chapter. Here you will see what the silver layer is for and how to build its core transformation.
What silver is for
Bronze keeps data exactly as it arrived. That makes it replayable, but not trustworthy. Dates may be strings, amounts may be text, keys may be missing, and the same order may appear several times.
Silver is the layer where you fix that. A silver table holds data that is:
- Cleaned: rows without a usable key are removed or flagged, and obvious junk is handled.
- Typed: each column has the correct data type.
- Conformed: names, units, and codes follow one consistent convention across sources.
- Deduplicated: one row per business entity, such as one row per
order_id.
Silver is not shaped for a single report. It is the reusable, trustworthy version of the data that gold tables and data scientists build on.
A silver transformation
The query below reads bronze and applies each cleaning step. It is the same statement used in the MERGE chapter, so focus on the USING subquery for now.
SELECT
order_id,
customer_id,
CAST(order_ts AS TIMESTAMP) AS order_ts,
CAST(amount AS DECIMAL(12,2)) AS amount,
status
FROM main.bronze.orders
WHERE order_id IS NOT NULL
QUALIFY ROW_NUMBER() OVER (PARTITION BY order_id ORDER BY _ingested_at DESC) = 1Each part does one job:
CAST(order_ts AS TIMESTAMP)turns a string into a real timestamp, so date functions and comparisons work.CAST(amount AS DECIMAL(12,2))uses a fixed-precision type. PreferDECIMALoverDOUBLEfor money, because floating-point types introduce rounding errors.WHERE order_id IS NOT NULLremoves rows that cannot be identified.QUALIFY ROW_NUMBER() ...ranks rows within eachorder_idby load time and keeps only the newest.
The bronze metadata column _ingested_at is what makes the last step possible. This is one reason bronze adds load metadata.
Conforming data
Conforming means making different sources agree. If one source sends status as COMPLETED and another as completed, a gold filter on status = 'completed' will miss rows. Normalize in silver:
SELECT
order_id,
LOWER(TRIM(status)) AS status,
UPPER(currency) AS currency
FROM main.bronze.ordersStandardize column names, time zones, units, and code values here, once, so no downstream consumer has to repeat the work.
Common errors
- Failed casts become NULL silently. With ANSI mode off,
CAST('abc' AS DECIMAL(12,2))returnsNULLinstead of an error. Check how many non-null bronze values become null in silver. Databricks also offersTRY_CASTto make this intent explicit. - Dropping bad rows with no record. Filtering out rows hides problems. The data quality chapter covers how to quarantine or count them instead.
- Skipping deduplication. Bronze is append-only, so reruns and late files produce repeats. Without a dedup step, gold totals will be inflated.
- Business logic in silver. Revenue by day belongs in gold. Keep silver general so many consumers can reuse it.
Exercise
A bronze table has amount as a string. After your silver step, the sum of amount is lower than expected and some silver amounts are NULL. What is the most likely cause, and how would you confirm it?
Show answer
Some bronze amount values could not be parsed (for example "N/A" or "1,200.50"), so the cast returned NULL. Confirm by querying bronze for rows where amount IS NOT NULL and TRY_CAST(amount AS DECIMAL(12,2)) IS NULL, then fix or quarantine those values.
Exercise
Why does the silver query order by _ingested_at DESC inside ROW_NUMBER()?
Show answer
It ranks the most recently loaded record first for each order_id. Keeping row number 1 keeps the latest version of each order.