Manual / Guides

BigQuery ML

A use case can train and score in BigQuery instead of in containers. It has a bqml section and no pipeline code: Pipewright provides the training and scoring pipelines and runs them on your environment’s orchestrator like any other. The data and the model stay in BigQuery; gates, approvals, monitoring, retrain decisions and evidence work as for every use case. pipewright new-usecase --from bqml starts one, and Tutorial 2 walks through segments and a forecast.

spec:
  bqml:
    model_type: logistic_reg            # boosted_tree_classifier, random_forest_classifier; for regression
                                        # linear_reg, boosted_tree_regressor, random_forest_regressor
    options: { l2_reg: 0.1 }            # CREATE MODEL options (Pipewright sets label, split, key, registry)
    prepare: sql/prepare.sql            # optional: SQL over {rows}, the run's rows
    transform: sql/transform.sql        # optional: the TRANSFORM clause's expressions
  inference:
    output_table: scores                # run_id, model_version_id, scored_at, the score, any input column

How a run trains

  1. The run’s rows (the entity table with its features at each row’s cut-off, the label window when data.label_source is set, then prepare.sql) are written to a table with a held-out test flag from data.split. The data contract is checked in SQL.
  2. CREATE MODEL sees the training rows only. Each run trains a new BigQuery model, so versions never overwrite each other.
  3. The new model and the champion score the same test rows; Pipewright computes its own metrics in SQL, defined as in the container path, per slice too.
  4. Only a model that passes the gates is registered in Model Registry (ALTER MODEL ... SET OPTIONS (vertex_ai_model_id)) as a new version of model.name. Approval and deployment work as for any model.

Model types

Taskmodel_typeMeasured with
binary, multiclasslogistic_reg, boosted_tree_classifier, random_forest_classifierPipewright’s metrics in SQL (auc, f1 and so on)
regressionlinear_reg, boosted_tree_regressor, random_forest_regressorrmse, mae, mape, r2
clusteringkmeansML.EVALUATE: davies_bouldin_index, mean_squared_distance; since 1.4.2 also min_segment_share
decompositionpcatotal_explained_variance_ratio
forecastarima_plus, or bqml.forecast.engine: ai_forecastmae, rmse, mape on a held-out latest period
any, trained elsewherebqml.import (TensorFlow SavedModel or ONNX)As its task, on the run’s test rows
Gate k-means on segment size too

Davies-Bouldin alone can pass lopsided segments: a few extreme customers form a tight, distant segment and the score looks good. Gate on both, for example { metric: min_segment_share, min: 0.02 }.

Forecasts

data.label.task: forecast with a bqml.forecast section: the horizon, id_columns for one series per store or product, and the confidence level. arima_plus holds out the latest period, forecasts it and measures the forecast against what happened, then refits on the whole series before registration; scoring writes the next horizon points with their prediction interval.

bqml.forecast.engine: ai_forecast uses AI.FORECAST with Google’s TimesFM model: nothing is trained, and the version records the settings it was measured with. It sends the series to a Google foundation model, so it is refused unless the platform sets policies.bqml.ai_forecast: true. A forecast is not served online.

Models trained elsewhere

bqml.import names TensorFlow SavedModel or ONNX files on gs:// and which output holds the score, label or prediction. The model is imported, evaluated on the run’s test rows, gated and registered like a trained one; the files’ checksums are recorded with the run.

Serving and scoring

  • Batch scoring: ML.PREDICT with the default version into inference.output_table, then feature and prediction drift and performance against observed outcomes into monitoring.output_table; what fires opens a retrain case as for any use case.
  • Online: deploy-model serves a BigQuery ML version on an Agent Platform endpoint, after checking the endpoint’s scores against ML.PREDICT on the run’s test rows.
  • Preparation: TRANSFORM is applied by ML.PREDICT and by an endpoint, so callers send raw values. prepare.sql is pinned by the release and run again when scoring; an endpoint runs no SQL, so online callers send its columns themselves.

Promotion

A BigQuery ML model is promoted by retraining in the next environment (model.promotion: retrain). A logistic_reg or linear_reg model may instead be promoted by its files (model.promotion: model): exported, copied, imported and registered in the next environment only when it scores the run’s test rows as the source does. Such a model can join the closed retrain loop. A version promoted by files with bqml.transform is not served online in the destination; move the expressions to prepare.sql.

Cost

Every job has a byte limit: bqml.max_bytes_billed, default 100 GiB, or the platform’s policies.bqml.max_bytes_billed. Tree models bill far more bytes than linear ones and take minutes even on small data; BigQuery lists them at a lower rate per TiB. The platform can limit the allowed types with policies.bqml.allowed_model_types.

Removing models

Drop the models before deleting a dataset

A registered BigQuery ML model is removed with DROP MODEL, which also removes its Model Registry version. Model Registry refuses to delete it itself, and deleting the dataset that holds it leaves the registry entry behind with no way to remove it.

Not yet

Hyperparameter tuning, and XGBoost imports.