Skip to content

Matrix Builder: Add cost estimation and smart row-count prediction #39

@sfc-gh-obielov

Description

@sfc-gh-obielov

Triage note (2026-04-23): Part of the Matrix Builder epic alongside #35 (road-aware tessellation). Land #35 first. Without #35's road-aware filter toggle, this ticket's estimator would systematically over-predict hex counts (and therefore rows, credits, and duration) for any region with significant uninhabited area. The ESTIMATE_MATRIX_COST procedure must accept the road_filter flag as an input and return distinct estimates for filter ON vs OFF; calibrate both against historical runs.


Summary

Add a pre-flight cost estimation feature to the Matrix Builder that, given a user's configuration (region, resolution, routing profile, bounding box, etc.), predicts:

  1. Resulting matrix row count — smart calculation based on tessellation resolution and region geometry, before any compute runs.
  2. Expected Snowflake query cost — warehouse credits consumed for the SQL-side work (tessellation, matrix assembly, writes).
  3. Expected SPCS / ORS compute cost — compute-pool credits consumed by ORS services answering matrix calls.
  4. Expected wall-clock duration — based on historical runs of comparable shapes/resolutions.

The goal is to let users make an informed choice before kicking off a potentially expensive matrix build.

Motivation

Today users configure a region + H3 resolution + routing profile and click build with no visibility into:

  • How many origin/destination pairs this will produce (can easily be millions)
  • How many warehouse credits will be burned
  • How much SPCS compute the ORS services will consume
  • How long it will take

This leads to surprise bills, stuck long-running jobs, and unrealistic resolutions (e.g. res 9 over a metropolitan region).

Proposed Work

1. Smart row-count estimation

  • Compute the effective tessellation cell count for the chosen region + resolution without materializing the tessellation.
    • Use ST_AREA(region_geometry) divided by average H3 cell area at the chosen resolution, adjusted for coastline/boundary clipping.
    • Validate against actual counts from previously-built regions; store calibration factors per resolution.
  • Resulting matrix rows = cells * cells (or cells * cells - cells if diagonal excluded).
  • Surface both the cell count and the matrix row count in the UI before the user confirms.

2. Query cost estimation

  • Reuse historical QUERY_HISTORY credits-used data for prior matrix builds, keyed by (region_size_bucket, h3_resolution, profile).
  • Formula: estimated_credits = base_cost + per_cell_cost * cell_count + per_pair_cost * pair_count.
  • Fit coefficients from QUERY_HISTORY / SERVERLESS_TASK_HISTORY for matrix pipeline queries (filter by query_tag origin sf_sit-is-fleet).

3. SPCS / ORS compute cost estimation

  • ORS matrix calls scale roughly with pair_count and routing profile complexity (driving-car < cycling < foot-walking < hgv).
  • Derive per-profile throughput (pairs/sec) and compute-pool credit rate from historical SPCS runs.
  • Surface a separate "ORS compute" line item alongside warehouse credits.

4. UI integration

  • Add an "Estimate cost" button in the Matrix Builder config panel (ors_control_app).
  • Show a summary card:
    • Tessellation cells: 12,450
    • Matrix rows: ~155M
    • Warehouse credits: ~8.2
    • ORS compute credits: ~4.1
    • Estimated duration: 22 min
    • Confidence: medium (based on 14 prior runs)
  • Color-code thresholds (green / yellow / red) so risky configs are obvious.
  • Require explicit confirmation when the estimate exceeds a configurable threshold.

5. Backend

  • New stored procedure OPENROUTESERVICE_APP.CORE.ESTIMATE_MATRIX_COST(region, resolution, profile, bbox) returning a JSON cost breakdown.
  • Backing calibration table populated from QUERY_HISTORY + SPCS usage views; refreshed via a scheduled task.
  • Tag all objects per repo convention:
    • query_tag: {"origin":"sf_sit-is-fleet","name":"oss-matrix-cost-estimator","version":{"major":1,"minor":0},"attributes":{"is_quickstart":1,"source":"sql"}}
    • Object COMMENT with the same tag on every CREATE.

Acceptance Criteria

  • ESTIMATE_MATRIX_COST procedure returns cell count, row count, WH credits, SPCS credits, duration estimate, and confidence.
  • Predicted row count within +/-10% of actual for 3+ test regions across resolutions 7/8/9.
  • Predicted credit cost within +/-25% of actual for the same regions.
  • Matrix Builder UI shows the estimate card and blocks builds above a configurable threshold without explicit confirmation.
  • Calibration table auto-refreshes (scheduled task) from QUERY_HISTORY / SPCS usage.
  • All new objects carry the tracking tag (sf_sit-is-fleet, oss-matrix-cost-estimator).
  • Documented in the build-routing-solution (or a new matrix-builder) skill's SKILL.md and references.

Out of Scope

  • Real-time in-flight cost tracking (separate ticket).
  • Cross-account chargeback (separate ticket).

Metadata

Metadata

Labels

enhancementNew feature or request

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions