Databricks Tutorials

Modeling the Business with Unity Catalog: Keys, Descriptions and Metric Views on Databricks Free Edition

From Genie Code Prototype to Production Agent on Databricks — Episode 3 of 10 ·

In Episode 1 Genie Code built a medallion pipeline over our support tickets in workspace.support_copilot. In Episode 2 we turned 48 PDF attachments into a silver table joined to those tickets. The data looks clean. What it doesn't have yet is meaning: open gold_sla_daily in Catalog Explorer, hover over sla_breach_rate, and there's no description. First response or resolution? Per ticket or per day?

So this episode was meant to be simple: write the business down in Unity Catalog, where people, Genie and agents can all read it.

But before you declare a key, you have to check it's true. And ours wasn't.

Clip 1: gold tables with no descriptions and no keys.

Prerequisites

Step 1: A skill that checks before it declares

The skill lives at /Users/{username}/.assistant/skills/support-copilot-semantics/SKILL.md. Besides names and definitions it has one rule that turned out to matter most:

## Key checks (always before declaring a key)
For every planned PK: count(*) vs count(DISTINCT key) and NULL count. For every FK: orphan count.
Keys in Unity Catalog are informational and NOT enforced. If a check fails, stop and report:
fix the data in the pipeline first, never declare a key you know is false.
@support-copilot-semantics List the tables in workspace.support_copilot with their comment and primary key status. Then run the key checks from the skill for every planned primary and foreign key (rows vs distinct keys, NULLs, orphans). Don't change anything yet.

Genie Code reports no primary keys anywhere and almost no descriptions. Then the key checks:

Table Rows Distinct key
silver_tickets 5,016 5,016
silver_customers 817 800
gold_ticket_facts 5,114 5,016

Step 2: The bug in Episode 1

Seventeen customer IDs appear twice in the raw customer file. Same customer, same tier, just loaded twice. The silver_customers table Genie Code built in E1 cleans every column but never deduplicates. gold_ticket_facts then LEFT JOINs tickets to customers, and every ticket of those 17 customers comes out twice: 98 extra rows.

It doesn't stop there. gold_sla_daily sums to 5,114 tickets instead of 5,016, and gold_agent_performance is inflated too. Every metric built on gold in E1 counted some tickets twice.

Why didn't anything catch it? The pipeline ran green, and nothing in it said "one row per customer". Even a primary key wouldn't have helped: Unity Catalog primary and foreign keys are informational, not enforced. You can declare a primary key on a column full of duplicates and Databricks will accept it. In rehearsal we did exactly that, and the run completed. A key is a promise you make to every tool that reads the table, so you have to check it first.

This is E1's lesson again: green is not the same as correct. In E1 a green graph hid missing expectations. Here it hid a fan-out join.

We fix it where it belongs, in the pipeline:

@support-copilot-semantics silver_customers has duplicate customer_ids, and they double tickets in gold_ticket_facts. Fix it in the pipeline: deduplicate silver_customers as the skill says (latest row per customer_id). Show me the diff, then run the pipeline and show me gold_ticket_facts row count vs distinct ticket_id.
SELECT ... FROM (
  SELECT ...,
         row_number() OVER (PARTITION BY customer_id ORDER BY _ingested_at DESC, _source_file DESC) AS rn
  FROM bronze_customers
) WHERE rn = 1;

After the run: silver_customers is 800/800 and gold_ticket_facts has 5,016/5,016 rows, one per ticket. An incremental pipeline update was enough in this take.

Clip 3: the diff, the run, and gold back to one row per ticket.

What this means for E1. E1's numbers are slightly off for those 17 customers' tickets. E1's own fallback SQL (fallback_pipeline/silver.sql) does deduplicate, which is why we didn't notice when building it. After the fix, gold_ticket_facts drops from 5,114 to 5,016 rows (98 duplicated rows gone), and the daily and agent tables built on it are recomputed from one row per ticket.

Step 3: Descriptions and keys, in two phases

Now the keys are honest. These tables belong to the E1 pipeline (streaming tables and materialized views), so a one-off ALTER TABLE could be lost on the next refresh. Descriptions and keys go into the pipeline's own table definitions:

CREATE MATERIALIZED VIEW silver_customers(
  customer_id STRING NOT NULL COMMENT 'Customer id. One row per customer (latest row wins).',
  tier STRING COMMENT 'Free, Pro, Business or Enterprise; drives SLA targets.',
  -- ... every column, with its type and comment
  CONSTRAINT pk_silver_customers PRIMARY KEY (customer_id)
) COMMENT 'One row per customer.' AS SELECT ...;

One lesson from rehearsal: when we added all keys at once, the run failed. The foreign key on silver_tickets pointed to silver_customers before its primary key existed. So we do it in two phases: parent primary keys first, run, then the foreign keys, run.

@support-copilot-semantics Now that the keys are clean, add table and column comments and the primary and foreign keys from the skill to the pipeline schema blocks ... Do it in the two phases from the skill: parent primary keys first and run, then the foreign keys and run.

The foreign key from tickets to customers still has orphans: tickets whose customer isn't in the customer file (73 non-null orphan customer_ids). That's allowed for an informational key, but we don't mark it RELY, because then the optimizer would trust it.

In this take Genie declared four primary keys (pk_silver_customers, pk_silver_agents, pk_silver_tickets, pk_gold_ticket_facts) and two foreign keys on silver_tickets (customer_id → silver_customers, agent_id → silver_agents), without RELY on the FKs. Keys and descriptions stayed after the pipeline runs.

Step 4: One metric view, three business metrics

A metric view is a Unity Catalog view defined in YAML: a source, dimensions and measures. You query measures with MEASURE() and group by any dimension. The aggregation is always computed at ticket grain.

One practical tip: create it in the SQL editor on a SQL warehouse. In rehearsal we started in a notebook on serverless compute, which rejected the $$ YAML body. Genie Code then improvised a REST API workaround that took over 20 minutes. So the skill now contains the exact DDL, and Genie only has to run it:

CREATE OR REPLACE VIEW workspace.support_copilot.mv_support_metrics
WITH METRICS LANGUAGE YAML AS $$
version: 1.1
source: workspace.support_copilot.gold_ticket_facts
dimensions:
  - name: created_month
    expr: DATE_TRUNC('MONTH', created_at)
  - name: tier
    expr: tier
  - name: priority
    expr: priority
measures:
  - name: first_response_time
    expr: percentile_approx(first_response_time, 0.5)
  - name: sla_breach_rate
    expr: AVG(CASE WHEN is_first_response_breached OR is_resolution_breached THEN 1 ELSE 0 END)
  - name: csat
    expr: AVG(csat_score)
$$;

(Full version in fallback/e03_4_metric_view.sql. Watch the SQL editor's auto-indent when you paste YAML.) We tag it domain = support, layer = semantic. These are plain tags; Free Edition has Governed Tags too, but we haven't defined any.

@support-copilot-semantics Create mv_support_metrics with the exact DDL from the skill and add its tags. Then answer with MEASURE(): which tier and priority had the highest sla_breach_rate in October 2026, and what was its first_response_time and ticket_count?

Answer: Free / urgent — sla_breach_rate 27.59% (8 of 29 tickets), median first_response_time 18.0 minutes. Next: Free/high 23.0%, Free/normal 19.1%, Free/low 17.9%; top non-Free is Pro/high at 16%. The metric view has 7 dimensions and 5 measures, tagged domain=support and layer=semantic.

Clip 5 (hero): the metric view answers the SLA question.

Step 5: Why not just average the daily table?

gold_sla_daily already has a sla_breach_rate per day, tier and priority (resolution target only). Averaging those rows gives a day with 1 ticket the same weight as a day with 30. To compare like with like, the metric view also has resolution_breach_rate with the same definition:

Enterprise, October 2026 Resolution breach rate
MEASURE(resolution_breach_rate) 4.05% (3 of 74 tickets)
AVG(gold_sla_daily.sla_breach_rate) 2.78% (averaged over 39 date × priority rows)

The plain AVG weights each day×priority row equally, so many small zero-breach rows outvote the few rows that carry the breaches; MEASURE() weights every ticket once and returns 4.05%.

What we learned

Free Edition notes

Next up: real data

Synthetic tickets got us a pipeline, document extraction, and now a semantic layer that we actually trust. From the next episode we move to real open data. We're building Charity Lens, a public app that compares charities across the UK, Canada, Australia, the US and Poland. In E4 we bring five national registers into one catalog, with licence and attribution as tags. And we'll need today's habits right away: charity registers have duplicates and orphans too, and every country defines "charity" differently. Comparing them honestly starts with keys you've checked and metrics you've defined once. "SLA breach rate" becomes "share of spend on programmes".


Sources: Unity Catalog metric views, PRIMARY KEY / FOREIGN KEY constraints, Lakeflow pipeline SQL reference, tags, Genie Code skills, Free Edition limitations.