Sub topic 17
Design and operating practices for feature tables
This chapter assumes you can create, merge, and look up features in Unity Catalog feature tables, and that you know why point-in-time lookups matter. It collects the habits that keep those tables reliable once several teams depend on them.
Design the table
- Pick one grain per table. A table keyed by
user_idholds one row per user. Put product features in a separate table keyed byproduct_id. Mixed grains make joins ambiguous. - Keep keys stable and non-null. Primary key columns must be
NOT NULL. Use identifiers that never get reused. - Add a time series column when values change. If a feature is an aggregate over time, such as order counts, store a timestamp in the key and use
timestamp_lookup_keyat training time. Otherwise future values leak into training rows. - Handle missing values once. Decide in the pipeline how nulls and unseen categories are filled, and document it. Do not re-implement that logic separately in training and serving code.
- Treat feature meaning as a contract. Do not change what an existing column means. Add a new column or a new table, and let consumers migrate.
Operate the table
Write refreshes with mode="merge" so a rerun updates the same rows instead of duplicating them. Schedule the job with Databricks Jobs and fail loudly when it breaks.
A stale table fails silently, because models keep scoring with old values. Add a freshness check to the end of the pipeline:
from pyspark.sql import functions as F
history = spark.sql("DESCRIBE HISTORY main.ml.user_features")
is_stale = (
history
.agg(F.max("timestamp").alias("last_commit"))
.select(F.col("last_commit") < F.current_timestamp() - F.expr("INTERVAL 1 DAY"))
.first()[0]
)
if is_stale:
raise RuntimeError("main.ml.user_features has had no commits in the last 24 hours")Pick the threshold from the feature's real freshness need. A 90-day aggregate can tolerate a day. A session feature cannot.
Govern the table
Grant read access to consumers and write access only to the pipeline's service principal. Consumers also need access to the parent catalog and schema.
GRANT USE CATALOG ON CATALOG main TO `data-scientists`;
GRANT USE SCHEMA ON SCHEMA main.ml TO `data-scientists`;
GRANT SELECT ON TABLE main.ml.user_features TO `data-scientists`;Fill in table and column descriptions when you create the table. Other teams find features through search and lineage, and a clear description is what tells them whether to reuse yours.
Common mistakes
| Mistake | Effect | Fix |
|---|---|---|
| `overwrite` on a shared table | Drops rows other models rely on | Use `merge` unless you intend a full rebuild |
| No time series column on time-dependent features | Label leakage, inflated offline metrics | Add the timestamp key and a timestamp lookup |
| Renaming or redefining a column | Logged models break or change behavior | Add a new column and retire the old one later |
| No freshness check | Stale predictions with no error | Alert on last commit time |
| Broad write permissions | Accidental edits to shared features | Restrict writes to the pipeline principal |
Exercise
A feature table holds order_count_90d per user and is refreshed nightly. A teammate proposes switching the refresh from merge to overwrite to simplify the job. Give one risk of this change and one check you would add to the pipeline.
Show answer
Overwrite replaces the whole table, so a partial or failed run can remove rows that other models and scoring jobs depend on, and users missing from the new DataFrame lose their features. Keep merge, or validate the row count against the previous version before overwriting. Add a freshness check on the last Delta commit time so a stalled job raises an error instead of leaving stale features.