Sub topic 10
Unity Catalog: catalogs, schemas, and tables
This chapter assumes you know basic SQL, have read the Delta Lake chapter, and understand that a workspace attaches to compute. Unity Catalog is the governance layer that names and secures the data your compute reads.
The three-level namespace
Unity Catalog organizes data objects in three levels. A table is addressed as catalog.schema.table.
- A catalog is the top container. Teams often use one per environment or business domain.
- A schema (also called a database) groups related tables, views, and volumes inside a catalog.
- A table holds the rows. Views, volumes, and functions sit at this same level.
Above all of this sits the metastore. It is the top-level container for metadata, and one metastore is typically shared by many workspaces in a region. Because the metastore lives outside any single workspace, the same table name works from every workspace attached to it.
Creating a schema and a managed table
This example assumes you can create objects in the main catalog.
CREATE SCHEMA IF NOT EXISTS main.tutorial;
CREATE TABLE IF NOT EXISTS main.tutorial.orders (
order_id BIGINT,
customer_id BIGINT,
amount DECIMAL(10, 2),
order_ts TIMESTAMP
);
INSERT INTO main.tutorial.orders VALUES
(1, 101, 25.00, current_timestamp()),
(2, 102, 40.50, current_timestamp()),
(3, 101, 15.25, current_timestamp());Because no location is given, this is a managed table. Unity Catalog chooses the storage path and stores the data in Delta format. Dropping a managed table removes its data. An external table points at a location you control, and dropping it leaves the files in place.
Reading the table from a notebook
In a notebook attached to compute, the predefined spark session reads the same name.
from pyspark.sql import functions as F
df = spark.table("main.tutorial.orders")
totals = (
df.groupBy("customer_id")
.agg(F.sum("amount").alias("total_amount"))
)
display(totals)The SQL and Python paths return the same data because both resolve the name through the metastore.
Permissions
Access is granted with GRANT. Reading a table requires privileges on every level above it.
GRANT USE CATALOG ON CATALOG main TO `data-analysts`;
GRANT USE SCHEMA, SELECT ON SCHEMA main.tutorial TO `data-analysts`;Privileges granted on a schema are inherited by the tables in it, including tables created later.
Common errors
- Missing USE privileges. A user with
SELECTon a table still gets a permission error if they lackUSE CATALOGorUSE SCHEMAon the parents. - Relying on the default catalog. A short name like
ordersresolves against the session's current catalog and schema. Use the full three-level name in shared code, or set them explicitly withUSE CATALOGandUSE SCHEMA. - Mixing up managed and external. Dropping a managed table deletes its data. Check the table type before you drop anything.
Exercise
A user has SELECT on main.tutorial.orders but the query fails with a permission error. Name two privileges to check, and the objects they apply to.
Show answer
Check USE CATALOG on the catalog main and USE SCHEMA on the schema main.tutorial. Unity Catalog requires these on the parent objects in addition to SELECT on the table.