Manual / Guides

Data and features

A model is only as trustworthy as the data it trained on, and the most common silent failure is leakage: training on a value that was not yet known when the model would have made its prediction. Pipewright reads features so that this cannot happen, and so that every model can say exactly what it read.

The data contract

Every use case declares the columns its model and checks see, wherever they come from. Rows are checked against it before anything learns from them.

data:
  contract:
    columns:
      - { name: customer_id,   type: string,  required: true, max_null_fraction: 0 }
      - { name: tenure_months, type: integer, min: 0, max: 1000 }
      - { name: churned,       type: integer, allowed: [0, 1] }
    min_rows: 500
    unique: [customer_id]
  label: { column: churned, positive: 1 }
  split: { strategy: time, time_column: snapshot_date, test_fraction: 0.2 }
data_protection:
  personal_data_columns: [customer_id]      # never a feature; found by `pipewright data erase`

pipewright new-usecase --from-bq <project.dataset.table> --label <column> drafts the contract from a table. Since 1.4.2, validate warns when personal_data_columns is empty but a column such as customer_id or email usually identifies a person.

Three ways to give a use case its data

WayWhenIn the YAML
A prepared table or viewA small team, one use case, data nobody else reuses. Fine to start withThe run’s source_uri points at it
Feature tablesFeatures other use cases also need, or whenever correctness over time matters (it nearly always does)data.features_from.sources[].table
Feature groups (Feature Store)Features your organisation registers centrally, so they are named, owned and catalogueddata.features_from.sources[].feature_group

Recommendation: feature groups when your organisation registers features, feature tables otherwise; a prepared table only for experiments. The platform may require feature tables (features.required); validate then names model features that still come from elsewhere.

Features, point in time

data:
  features_from:
    entity: { columns: [customer_id], time_column: snapshot_date }   # the run's source: who, when, the label
    sources:
      - feature_group: customer_profile           # registered: table and columns from the registry
        features: [tenure_months]
      - table: bq://features_core.support_activity
        features: [num_support_tickets]
        # entity_columns: [cust_id]               # when the table names the entity column differently
        # time_column: valid_from                 # default feature_timestamp
        # prefix: support_                        # two sources with a feature of the same name
  • The run’s source is the entity table: who, as of when, and the label. Each row gets the newest value of each feature dated at or before its cut-off. A date cut-off is midnight UTC at the start of that day.
  • Scoring runs the same query, so training and scoring read features the same way. Give the scoring table the cut-off column as the scoring date: scores become reproducible.
  • One query, in BigQuery, as your runner with the environment’s key. A data.features.resolved event records the tables, features, query job and how many rows found a value.
  • Access is the platform’s decision: tenants.<t>.features.datasets lists the datasets a team may read; validate refuses anything else.

What a feature table looks like

RuleWhy
One or more entity columns (for example customer_id)How rows match the entity table
A timestamp column (feature_timestamp by default): when each value became trueThe cut-off is compared with it
One column per feature, typed as the contract saysA feature is a column
A new row each time a value changes; never update in placeUpdating destroys history, so old training rows would see today’s value
One row per entity per timestampTwo rows with one timestamp make the choice arbitrary
The timestamp is when the value was knownA balance computed on 3 March for February is known on 3 March

Checks

WhenWhat
validate (offline)The block against the contract; no feature twice without a prefix; no feature that is the label, an entity or the cut-off; tables in the tenant’s datasets; with features.required, every model feature from features_from; a landing trigger is refused (the source must be bq://)
doctor (online)Each table exists with the entity, time and feature columns and types the contract accepts; each group resolves through the registry
Every runThe same dataset rule; a group is resolved again, so a group its owners moved is followed

Mistakes to avoid

MistakeWhat happensInstead
A view that joins the "latest" featuresEvery training row sees today’s values: leakageName the feature tables in features_from
Feature rows updated in placeHistory is lostAppend a row per change
A timestamp of the period the value describesValues look known before they wereUse when the value became available
The label as a featurevalidate refuses itThe label comes from the entity table
Copying a feature’s SQL into your own tableTwo definitions drift apartRead the owner’s table or group
Scoring without a scoring dateScores cannot be reproducedPut it in the cut-off column

For the owners of a feature

Compute the feature, append a row per change, keep the timestamp honest, keep the schema stable (add columns; never rename or retype), and handle erasure in your tables (pipewright data erase covers only a use case’s own tables). Any tool works; two make it easy on BigQuery:

Dataformdbt
RunsManaged, inside Google Cloud; nothing to hostdbt Core needs a runner (Cloud Run jobs, Composer, CI); dbt Cloud is managed and paid
A row only when a value changesAn incremental table with your own MERGE logicSnapshots, out of the box
Best whenGoogle Cloud only, nothing extra to run, private environmentsThe data team already uses dbt

Either way: partition by feature_timestamp, cluster by the entity column, test that (entity, timestamp) is unique and never null, and backfill history with each past value stamped with when it was known, never today.

Registering feature groups

# feature_groups.yaml
customer_activity:
  table: pw_retail.customer_activity
  entity: [customer_id]
  description: Monthly purchase behaviour per customer
  features:
    recency_days: Days since the last purchase
    orders_12m: Orders in the last twelve months

pipewright features register --env dev --file feature_groups.yaml (--dry-run first), as the identity that owns the tables. It is safe to repeat: a group or feature that exists is left as it is, never changed or deleted.

The feature contract (1.4.1)

An approval pins the model; the feature contract pins the definitions of the features it reads. Values change; definitions do not, without a new contract version, a release and a retrain.

pipewright features contract --env test --usecase churn            # writes config/feature_contract.yaml
pipewright features contract --env test --usecase churn --check    # CI: exit 0 same, 1 different, 2 cannot check

Per source it records the kind (view, materialized view, a table built by a query, a scheduled query), the SHA-256 of its SQL and of its schema, and each feature’s type. Run it again after a change: a removed or retyped column is a major version, changed SQL or an added column minor, descriptions a patch.

  • Tables built by a query carry no SQL: give the source’s sql_file, or scheduled_query. Since 1.4.2 a table several jobs write (a backfill and a monthly append) lists sql_files; the check accepts any of them.
  • The fingerprint ignores whitespace, leading comments, the CREATE TABLE ... AS prefix and the source’s own project, so test and production compare equal.
  • Turning it on is the platform’s choice (policies.integrity.features.check: on): training then refuses a changed definition, approvals record the definitions, and deploy, scoring and the watch compare with what was approved. See Integrity.
Time travel does not work through a view

FOR SYSTEM_TIME AS OF on a view returns today’s values without an error, and a view cannot be snapshotted. To reproduce what a scoring run read, turn on record_reads (the view’s base tables and when) and snapshot_days.

Serving features online

The same tables serve online lookups: an endpoint takes entity IDs and reads the features itself. Bigtable (Feature Store) is the default; Cloud SQL and BigQuery with BI Engine cost far less at modest traffic; the Parquet export feeds a store of your own. See Online feature stores.