Skip to main content

bq-cost-guard

Know what a query will cost before it is merged. bq-cost-guard runs a BigQuery dry run on your SQL (raw files or compiled dbt models), flags costly anti-patterns, and comments the result on your pull request.

## BigQuery cost report

| Status | File | Processed | Est. cost |
|---|---|---|---|
| ❌ | `models/marts/events.sql` | 2.4 TiB | $15.2461 |
| ✅ | `models/marts/users.sql` | 4.9 MiB | $0.0000 |

**Total estimated cost: $15.2461**

### Warnings
- `models/marts/events.sql` **BQ001**: SELECT * scans every column; list only the columns you need.

Install

pip install bq-cost-guard   # or: uvx bq-cost-guard

Usage

bq-cost-guard check query.sql --project my-gcp-project
bq-cost-guard check ./dbt_project --git-diff origin/main --format markdown   # run `dbt compile` first
bq-cost-guard check ./sql --skip-estimate                                    # lint only, no GCP call
bq-cost-guard audit --project my-gcp-project --region eu --days 30           # past spend report

check options

Option Description
--format table|markdown|json Output format (default table)
--git-diff <base> Only analyze files changed vs. <base>
--project GCP project used for the dry run
--config Path to .bqcostguard.yaml
--skip-estimate Lint only

Exit codes: 0 ok, 1 a query exceeds fail_cost_usd, 2 an estimation failed (auth, syntax, no project). Dry runs are free and scan no data.

audit

Reads INFORMATION_SCHEMA.JOBS_BY_PROJECT and reports the top query groups (literals normalized), cost by user and lint rules triggered by the costliest queries. Options: --project, --region (default us), --days (default 30), --top (default 20), --format. Requires roles/bigquery.resourceViewer (bigquery.jobs.listAll); no table data is read. Costs are on-demand estimates from billed bytes (price_per_tib); the audit queries themselves bill a few MiB.

GitHub Action

name: bq-cost-guard
on: pull_request
permissions:
  contents: read
  id-token: write
  pull-requests: write
jobs:
  cost:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
        with: { fetch-depth: 0 }
      - uses: google-github-actions/auth@v2
        with:
          workload_identity_provider: ${{ vars.WIF_PROVIDER }}
          service_account: ${{ vars.WIF_SERVICE_ACCOUNT }}
      - uses: Mehdi-Leqsiouer/bq-cost-guard@v0.2.0
        with:
          path: .
          base: origin/${{ github.base_ref }}
          project: my-gcp-project

The service account needs roles/bigquery.jobUser and roles/bigquery.dataViewer on the queried datasets.

Configuration

Copy .bqcostguard.yaml.example to .bqcostguard.yaml:

price_per_tib: 6.25   # USD per TiB (on-demand)
warn_cost_usd: 1.0
fail_cost_usd: 10.0
ignore_rules: []      # e.g. [BQ002]

Rules

Rule Detects
BQ001 SELECT *
BQ002 ORDER BY without LIMIT in a subquery or CTE
BQ003 CROSS JOIN / implicit comma join without a WHERE
BQ004 Wildcard table (events_*) without a _TABLE_SUFFIX filter
BQ005 CTE referenced more than once that reads a table
BQ006 LIMIT without WHERE (does not reduce bytes billed)

Development

uv sync
uv run pytest
pre-commit install

MIT licensed.

Metadata

Release files for bq-cost-guard 0.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for bq-cost-guard 0.2.0
File Size Uploaded Provenance Actions
bq_cost_guard-0.2.0.tar.gz 72.3 kB Without provenance Details for bq_cost_guard-0.2.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for bq-cost-guard 0.2.0
File Interpreter ABI Platform Actions
bq_cost_guard-0.2.0-py3-none-any.whl Python 3 none any Details for bq_cost_guard-0.2.0-py3-none-any.whl

Total release size: 85.3 kB

Release files / bq_cost_guard-0.2.0.tar.gz

Download URL bq_cost_guard-0.2.0.tar.gz
Size 72.3 kB
Tags Source
SHA-256 checksum
How to use checksums
e5724ce3f8dcf93b9e751b8c1b69910dd54981b4c8db9c2edf9e40aad8b3059b
BLAKE2b-256 checksum
How to use checksums
222c8257cf5a105eaebe3a184c0549cdb879feff3657db9bf8ba4b796558c79d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.13.12 {"installer":{"name":"uv","version":"0.13.12","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"26.04","id":"resolute","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release files / bq_cost_guard-0.2.0-py3-none-any.whl

Download URL bq_cost_guard-0.2.0-py3-none-any.whl
Size 13.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
ac06a7926507c4a664e3fc164cdd4e968f086e3d90b0134e4dfd7281d79029f2
BLAKE2b-256 checksum
How to use checksums
59cc615e6bbf0fb680414f271fb10015de3dacec672dd819e065591a22c590b8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.13.12 {"installer":{"name":"uv","version":"0.13.12","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"26.04","id":"resolute","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Security for bq-cost-guard 0.2.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page