Sub topic 5
Unity Catalog essentials for feature storage
This chapter assumes you can write basic PySpark, know what a Delta table is, and have run notebooks on Databricks. It covers the parts of Unity Catalog you need before creating feature tables: the naming model, the permissions that matter, and the table properties a feature table relies on.
Why Unity Catalog matters for features
A feature table in Unity Catalog is a Delta table with a primary key constraint. Because it is a regular governed table, it inherits the access control, auditing, and lineage that Unity Catalog applies to every table. You do not need a separate feature store system with its own permissions.
This has three practical effects:
- Each feature table has a three-level name, so teams can find and reference it unambiguously.
- Access is granted with the same
GRANTstatements used for any other table. - Lineage can link the source tables, the feature table, and the models that consume it.
The three-level namespace
Unity Catalog organizes data in three levels: catalog.schema.table. The examples in this tutorial use main.ml.user_features.
| Level | Example | Purpose |
|---|---|---|
| Catalog | `main` | Top-level container, often one per environment or business unit |
| Schema | `ml` | Groups related tables, models, and volumes |
| Table | `user_features` | The Delta table holding feature rows |
Registered models use the same naming, so main.ml.churn_model lives in the ml schema next to the feature tables it depends on. Keeping features and models in one schema makes permissions simpler to reason about.
In this layout, raw data lives in main.sales. Features and models live in main.ml. The separation lets you give data scientists write access to ml while keeping sales read-only for them.
Privileges you need
Unity Catalog checks privileges at each level. To use a table, a user needs access to the catalog and schema as well as the table itself.
| Privilege | Granted on | What it allows |
|---|---|---|
| `USE CATALOG` | Catalog | Resolve names inside the catalog |
| `USE SCHEMA` | Schema | Resolve names inside the schema |
| `SELECT` | Table | Read feature values |
| `MODIFY` | Table | Insert, update, delete rows (needed for `write_table`) |
| `CREATE TABLE` | Schema | Create new feature tables |
| `CREATE MODEL` | Schema | Register models in the schema |
A typical split gives a feature-producing pipeline CREATE TABLE and MODIFY, and gives consumers only SELECT.
-- Producers: create and update feature tables in main.ml
GRANT USE CATALOG ON CATALOG main TO `feature-producers`;
GRANT USE SCHEMA, CREATE TABLE ON SCHEMA main.ml TO `feature-producers`;
-- Consumers: read features only
GRANT USE CATALOG ON CATALOG main TO `ml-consumers`;
GRANT USE SCHEMA ON SCHEMA main.ml TO `ml-consumers`;
GRANT SELECT ON TABLE main.ml.user_features TO `ml-consumers`;The table creator owns the table and can modify it. If another pipeline later writes with fe.write_table, grant it MODIFY on that table explicitly.
What makes a table a feature table
A feature table has two requirements beyond being a Delta table:
- It has a primary key constraint.
- The primary key columns are
NOT NULL.
The following SQL creates a valid feature table without using the Python client:
CREATE TABLE main.ml.product_features (
product_id BIGINT NOT NULL,
category STRING,
avg_rating DOUBLE,
CONSTRAINT product_features_pk PRIMARY KEY (product_id)
);In Unity Catalog, a primary key is informational: it documents the key and lets the feature engineering client identify lookup columns, but Delta does not enforce uniqueness on write. Your pipeline is responsible for producing one row per key. Duplicate keys lead to ambiguous lookups and unpredictable joins, so deduplicate before writing.
A table created with FeatureEngineeringClient.create_table, as in the next chapters, gets the same constraint automatically. Both routes produce the same kind of object. You can also turn an existing Delta table into a feature table by making the key columns NOT NULL and adding a primary key constraint.
Checking what you have
Before building on a table, confirm its location, schema, and constraints.
spark.sql("DESCRIBE TABLE EXTENDED main.ml.product_features").show(truncate=False)
spark.sql("SHOW GRANTS ON TABLE main.ml.product_features").show(truncate=False)DESCRIBE TABLE EXTENDED lists columns, the primary key constraint, the owner, and the storage location. SHOW GRANTS lists who can do what. Running both is a quick way to diagnose a permission error.
Compute and Unity Catalog
Feature engineering with Unity Catalog needs compute that supports it. In practice:
- Use a recent Databricks Runtime for Machine Learning, or serverless compute, so the
databricks-feature-engineeringpackage is available. - Use compute in an access mode that Unity Catalog supports, such as standard (shared) or dedicated (single user). Legacy modes without Unity Catalog support cannot read these tables.
- Set the model registry to Unity Catalog with
mlflow.set_registry_uri("databricks-uc")before logging models, as shown in later chapters.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| `Catalog 'main' not found` or similar | No `USE CATALOG`, or wrong catalog name | Check the name and grant `USE CATALOG` |
| `PERMISSION_DENIED` on `write_table` | Missing `MODIFY` on the table | Grant `MODIFY` to the writing identity |
| Cannot create a feature table | Missing `CREATE TABLE` on the schema | Grant `CREATE TABLE` on the schema |
| Table is not recognized as a feature table | No primary key constraint, or nullable key columns | Add `NOT NULL` and a primary key constraint |
| Lookups return duplicate rows | Duplicate keys written to the table | Deduplicate the DataFrame before writing |
| Model scoring fails for a service principal | No `SELECT` on the feature table | Grant `SELECT` to the principal |
Design choices to settle early
Decide these before the first feature table exists, because renaming later breaks the model metadata that refers to table names.
- Catalog per environment or per team. Separate
dev,staging, andprodcatalogs make promotion explicit. A single catalog with many schemas is simpler but mixes environments. - Schema per domain. Grouping by domain, such as
ml_customersorml_products, keeps ownership clear. - Group-based grants. Grant privileges to groups, not individuals, so access changes with team membership.
- Stable names. Logged models store the feature table names they were trained with. Treat those names as part of an interface.
Exercise
A scheduled job running as a service principal calls fe.write_table(name="main.ml.user_features", df=df, mode="merge") and fails with PERMISSION_DENIED. The principal already has USE CATALOG on main and USE SCHEMA on main.ml. Which privilege is missing, and why is SELECT alone not enough?
Show answer
It is missing MODIFY on main.ml.user_features. A merge inserts and updates rows, and SELECT only allows reading. CREATE TABLE on the schema is needed only to create new tables, not to write to an existing one.
Exercise
Your team writes the same user_id twice into a feature table with a primary key constraint, and the write succeeds. Why, and what should you do about it?
Show answer
The primary key in Unity Catalog is informational and Delta does not enforce uniqueness. The pipeline must guarantee one row per key, for example by deduplicating or aggregating the DataFrame before calling write_table.