fix: dbt_cost_estimate reports what a model's runs cost on BigQuery, and supports BigQuery only
Issue for this PR
Refs #1839
Type of change
- Bug fix
- New feature
- Refactor / code improvement
- Documentation
What does this PR do?
dbt_cost_estimate reported bytes scanned, and its output was read as cost. This PR adds what a model's earlier runs actually cost, and narrows the tool to BigQuery.
Breaking change (needs a release note). Snowflake and Databricks support is removed, including the planner (EXPLAIN) scan estimate that shipped in 0.12.12. With the Claude plugin, the maxScanBytes gate no longer holds Snowflake or Databricks commands: they run with a "no estimate" line. Plugin PR https://github.com/AltimateAI/altimate-claude-plugin/pull/49 updates the plugin's text and merges after this one. #1839 also asked for Snowflake/Databricks history; that is out of scope, so this PR refers to it rather than closing it.
Default call (no history). Unchanged from before this PR: compile, then a BigQuery dry run, returning the same scan fields. It runs no INFORMATION_SCHEMA query and gives no dollar figure. The plugin's scan gate makes this call.
history: true reports what BigQuery billed for the model's runs in the last 30 days, from INFORMATION_SCHEMA.JOBS, or JOBS_BY_USER without bigquery.jobs.listAll. Runs are found by dbt's query comment ("node_id": "<unique_id>").
- What counts as a run. A run is one dbt invocation, grouped by the
dbt_invocation_idlabel that dbt-bigquery puts on every job. Jobs without the label fall back to: same user, each job starting within 60 s of the previous one ending, and a__dbt_tmpCTAS pairing with its final job in the same dataset. - Which target. Figures come from the current dbt target's runs only (
target_namein the query comment). Other targets' runs are listed separately. - On-demand pricing. Bytes billed × $/TiB (default 6.25).
- Reservation pricing. Slot-hours × $/slot-hour (default 0.06, Enterprise pay-as-you-go; Standard is 0.04). This is labelled "attributed slot time, not the bill" in the first line and in
cost.attribution. - Unknown, never $0. A run is unknown, not priced, when it has a job whose billed amount BigQuery hides, a BigQuery ML job, or a job still going. The same applies when the run may be cut by the window, by the row limit, or by a boundary read that failed.
- Own jobs only. When only the caller's jobs could be read, the first line says so (
cost.scope). - Pre-run estimate. The dry run is priced only on on-demand pricing, as a labelled estimate: max(dry-run bytes, 10 MiB) × $/TiB, with the reasons the bill can differ. Row-level security, external tables and cache are named.
- Python models. They run on Dataproc and never appear in
JOBS, so the tool says so and sends no history queries.
When cost can't be known, the output says why:
- no runs (naming the location read)
- missing permission (
bigquery.jobs.createwithlistAllorlist) - ad-hoc SQL
- an ephemeral model
Other warehouses get supported: false, with "cost estimates are only available on BigQuery".
Config. An optional cost block holds bigqueryUsdPerTib and bigquerySlotHourUsd. A configured rate always wins. A non-positive, non-finite or null value is ignored, and the tool names it.
Follow-ups: Snowflake/Databricks history (#1839).
How did you verify your code works?
bun run typecheckis clean.bun teston the dbt-cost, tool-cli and config tests passes.- A mutation pass broke each pricing, grouping, pairing, boundary and fallback rule in turn, and each break failed at least one test.
- The history SQL was checked against BigQuery's documented
INFORMATION_SCHEMA.JOBSschema and a mocked dbt adapter. - Not live-tested on BigQuery, because no BigQuery connection was available.
Cost side effects
The default call adds nothing: one dry run, as before.
With history: true the tool runs three billed INFORMATION_SCHEMA queries, after the dry run and under one 60-second deadline:
- a capacity check over 7 days of jobs
- the history query, which scans 30 days of the project's job
querytext - a boundary query covering the 96 h before the oldest run read
Two more run only when needed:
- an invocation read, for the jobs of the dbt invocations already read, going back up to 180 days (the retention of the
JOBSviews) - a children read, when a script matched
That makes five queries at most. Without bigquery.jobs.listAll, the capacity, history and children queries are retried on JOBS_BY_USER, so up to eight.
INFORMATION_SCHEMA is never cached. On on-demand pricing each query is billed at the bytes it reads, at least 10 MiB. Those bytes grow with how busy the project is, not with the model. The 500-row caps limit rows returned, not bytes billed. maximum_bytes_billed can't be set per query through dbt's run_query; a value set in the dbt profile applies.
Screenshots / recordings
N/A
Checklist
- I have tested my changes locally
- I have not included unrelated changes in this PR
- I checked for cost side effects (model calls, queries or cloud resources that grow with user data) and described them, or wrote "none"
🤖 Generated with Claude Code