Sub topic 6
What a Feature Table Is
This chapter assumes you can write PySpark DataFrame code, know what a Delta table is, and understand Unity Catalog's catalog.schema.table naming. By the end, you will know what separates a feature table from an ordinary table and how to create one.
The working model
A feature table in Unity Catalog is a Delta table with a primary key constraint. Each row describes one entity, such as a user or a product. Each non-key column is a feature, which is a computed value a model can use as input.
The primary key is what makes the table usable as a feature source. Later chapters use it as the lookup key when joining features onto labeled data, so every key must be unique and non-null.
Create one from a DataFrame
Start with a DataFrame that has one row per entity. This one aggregates orders into per-user features over the last 90 days.
from pyspark.sql import functions as F
orders = spark.table("main.sales.orders")
user_features_df = (
orders
.filter(F.col("order_ts") >= F.date_sub(F.current_date(), 90))
.groupBy("user_id")
.agg(
F.count("*").alias("order_count_90d"),
F.avg("amount").alias("avg_order_amount_90d"),
F.max("order_ts").alias("last_order_ts"),
)
)The groupBy("user_id") guarantees one row per user, which satisfies the primary key. Now register the DataFrame as a feature table.
from databricks.feature_engineering import FeatureEngineeringClient
fe = FeatureEngineeringClient()
fe.create_table(
name="main.ml.user_features",
primary_keys=["user_id"],
df=user_features_df,
description="Per-user order features over the last 90 days",
)create_table creates the Delta table, adds the primary key constraint, and writes the rows from df.
Create one with SQL
Because the feature table is a Delta table with a key, plain SQL works too.
CREATE TABLE main.ml.product_features (
product_id BIGINT NOT NULL,
category STRING,
avg_rating DOUBLE,
CONSTRAINT product_features_pk PRIMARY KEY (product_id)
);Key columns must be declared NOT NULL. Without that, the primary key constraint is rejected.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| Error when creating the table | Key column contains nulls | Filter or fill nulls in the key column before `create_table` |
| Merge or lookup returns unexpected rows | The DataFrame has several rows per key | Aggregate or deduplicate so each key appears once |
| Permission denied | Missing privileges on the catalog or schema | Ask for `USE CATALOG`, `USE SCHEMA`, and `CREATE TABLE` |
| Table not found | Name is not fully qualified | Use `catalog.schema.table` |
Check your understanding
Exercise
A DataFrame has columns userid, country, and signupts, and some userid values are null. What two things must be true of userid before you pass the DataFrame to createtable with primarykeys=["user_id"]?
Show answer
Every userid must be non-null, and each userid must appear only once. Remove or fill the null keys and deduplicate the rows, for example with dropDuplicates(["userid"]), before calling createtable.
Exercise
How does a feature table differ from an ordinary Delta table?
Show answer
It is a Delta table in Unity Catalog with a primary key constraint on non-null key columns. Other than the key, it is not structurally different, and the key is what lets the table be used for feature lookups.