Sub topic 7
Choosing primary keys and time series columns
This chapter assumes you can create a Unity Catalog table, know what a Delta table is, and have seen FeatureEngineeringClient.create_table. It covers how to pick the key columns that define a feature table's rows, and when to add a time series column.
What the primary key does
A feature table's primary key says which entity each row describes. It is the column set that a feature lookup joins on. If you look up features by user_id, then user_id must be the key.
Three rules apply:
- Key columns must be
NOT NULL. - The combination of key values should identify one row. The constraint is informational in Unity Catalog and is not enforced, so duplicates are possible. Duplicates make joins return more rows than the labeled data had.
- Keep the key at the grain you will look up. Per-user features keyed by
user_id, per-product features keyed byproduct_id.
CREATE TABLE main.ml.product_features (
product_id BIGINT NOT NULL,
category STRING,
avg_rating DOUBLE,
CONSTRAINT product_features_pk PRIMARY KEY (product_id)
);A composite key is fine when the entity needs two columns, for example store_id and product_id. The lookup must then supply both.
When you need a time series column
Some features change. A user's 90-day order count today is not the same as it was last month. If you keep only the latest value, a training set built from old events sees future information. That is label leakage.
A time series column keeps a history. You add a timestamp or date column to the primary key and declare it with timeseries_columns.
from databricks.feature_engineering import FeatureEngineeringClient
fe = FeatureEngineeringClient()
fe.create_table(
name="main.ml.user_activity_features",
primary_keys=["user_id", "feature_ts"],
timeseries_columns=["feature_ts"],
df=activity_features_df,
)Here user_id identifies the entity and feature_ts says when the values were valid. Each user can have many rows, one per timestamp.
Choosing between the two designs
| Question | Entity key only | Entity key plus time series column |
|---|---|---|
| Rows per entity | One | Many, one per timestamp |
| Update style | Merge overwrites the row | Merge adds new timestamped rows |
| Training lookups | Latest values | Values as of each event |
| Storage | Small | Grows with history |
Use a time series column when the label depends on when something happened, such as churn or fraud. Use a plain key for slowly changing or static attributes like a product's category.
Common errors
- Nullable key columns. Creating the table fails or the constraint is rejected. Filter or fill nulls before
create_table. - Timestamp left out of the key. The time series column must also be in
primary_keys. - Duplicate keys. Deduplicate the DataFrame before writing, or lookups multiply rows.
- Wrong grain. Keying daily features by
user_idalone collapses history into one row.
Exercise
You build per-user features that are recomputed weekly, and you train a model on events from the past year. Which key design do you choose, and why?
Show answer
Use primarykeys=["userid", "featurets"] with timeseriescolumns=["featurets"]. Keeping weekly snapshots lets each past event be joined to the feature values that existed at that time. A table keyed by userid alone would hold only the latest values and leak future information into older events.