BigQuery-Native Engine¶
bigquery_engine
¶
BigQuery-native models — the SQL runner for ARIMA_PLUS / TimesFM.
The BigQuery runtime executes forecasting as SQL inside BigQuery — the opposite of the Spark
track's per-cell fan-out. ARIMA_PLUS with time_series_id_col trains **all series in one
CREATE MODEL statement;AI.FORECAST(TimesFM) forecasts every series in one call with no
training at all. Both land in the *same* three-tier registry as the Python models, so a native model
and a Spark model are directly comparable onv_model_leaderboard``.
This module is the executor: run resolves Settings, owns the run_registry header lifecycle
exactly like spark_explode.run, executes bigquery_sql's statements via bigquery.Client,
reads the fold forecasts back, computes the metric panel through the shared compute_metrics (no
formula drift), and writes all three cell tables via the registry's Storage Write API row-dict path.
The SQL it runs is rendered by bigquery_sql; the object names in that SQL come from
bigquery_names.
Alignment with the Spark track. The native models mean the same thing as the Python models in every table:
forecast_predictionsalways holds a true beyond-data forecast — the final model is fit on all history and forecasts the nextdata.horizonsteps, exactly like the Spark path's final-fit-then-forecast. It is never a scored within-history window.- Scored evaluation lives entirely in the backtest path, for both engines. When
backtest.enabledis on, a BQML fold loop (per fold:CREATE MODELonds <= cutoff+ML.FORECAST) mirrorsbacktest.make_folds's anchored-from-end geometry, writingbacktest_oofwith realfold_ids and a rolled-upforecast_metadatapanel (fold_id=NULL). When backtest is off, the engine writes afold_id=NULLmetadata row per(series, model)with a NaN metric panel — precise parity with the Python worker, which also emits an unscored metadata row when backtesting is off.
Transform. cfg.features.transform (e.g. log1p) is intentionally not applied here:
ARIMA_PLUS runs its own decomposition, and TimesFM is a pretrained foundation model.
Holidays are honored — the custom-holiday CTE is built from the same
holiday_frame the Python suite uses, so "holiday" is identical
across runtimes.
BqOutcome
dataclass
¶
The BigQuery engine's run summary — what main.run folds into the shared header.
status is COMPLETED (the engine raises on any SQL failure rather than returning FAILED, so a
returned outcome is always COMPLETED today; the field is explicit for symmetry with the Spark
roll-up and future partial-success handling). n_series is the distinct series count observed
in the source subset; models is the executed native subset (feeds bq_models).
extract_arima_diagnostics
¶
Extract per-series diagnostics from ML.ARIMA_EVALUATE and ML.ARIMA_COEFFICIENTS.
Populates non_seasonal_order ([p, d, q]), has_drift, aic, log_likelihood,
variance, seasonal_periods, has_holiday_effect, has_spikes_and_dips,
has_step_changes, ar_coefficients, ma_coefficients, intercept_or_drift, and
xreg_weights (for ARIMA_PLUS_XREG numeric and categorical regressors).
Source code in src/scale_forecasting/engines/bigquery_engine.py
595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 | |
extract_bqml_explanations
¶
Extract Tier 1 and Tier 2 feature attributions from an ML.EXPLAIN_FORECAST frame (pure).
- Tier 1 (
tier1_by_ts): per-series normalized mean absolute attribution acrosstrend,seasonal(sum ofseasonal_period_*),holiday_effect, and each exogenous column inxreg_cols(attribution_<col>). Stored inforecast_metadata.fit_diagnostics["feature_attributions"]. - Tier 2 (
tier2_by_ts_date): per-step JSON string{"baseline_score": float, "attributions": {...}}keyed by(ts_id, forecast_date_iso)forforecast_predictions.explanations.
Source code in src/scale_forecasting/engines/bigquery_engine.py
513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 | |
run
¶
Execute the BigQuery-native subset end-to-end, mirroring spark_explode.run.
Header lifecycle: resolve Settings, derive the config-pinned
run_id, ensure_tables → write_header (RUNNING), run the SQL, then update_header
with the aggregated status, wall-clock runtime_seconds, n_series, n_models, and the
bq_models array.
Two phases per run (see the module docstring):
- Final forecast (always). Each model's
build_setup_statementsfits on all history and INSERTs a true beyond-data forecast intoforecast_predictions— parity with Spark. - Scored evaluation (backtest only). When
backtest.enabled, a fold loop (fold_plan) trains one model per fold onds <= cutoff, reads each fold's forecast joined to actuals viabuild_eval_query, writesbacktest_oofwith realfold_ids, and rolls the per-fold panels up (viaworker.rollup_metrics) into afold_id=NULLforecast_metadatarow. When backtest is off, a single unscoredfold_id=NULLmetadata row per(series, model)(NaN panel) is written instead — parity with the Python worker.
manage_header=False is contributor mode: main.run owns the single shared
header, so the engine skips ensure_tables / write_header / update_header and only
runs SQL + writes the cell tables. settings may be passed to reuse the orchestrator's
already-resolved infra; None resolves it here (standalone default).
Idempotent: run_id is a pure function of the config and every write is append-only /
dedupe-on-read, so a re-run of the same config lands byte-identical rows.
job_id_prefix (the orchestrator's deterministic system_job_id for the native family)
prefixes every BigQuery job this run submits, so the family's jobs are console-resolvable under
that id — the reverse-trace hook. None (the standalone default) lets BigQuery auto-name it.
Source code in src/scale_forecasting/engines/bigquery_engine.py
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 | |