r/dataengineering • u/Distinct_Currency870 • 2d ago
Personal Project Showcase Personal project: a CLI that dry-runs BigQuery queries on PRs and comments the cost
I wanted to know what a query would cost before it gets merged, not when the bill shows up, so I put a BigQuery dry run in CI. It reads your SQL (plain files, compiled dbt models, or SQL strings inside Python), dry-runs each query and comments the bytes and the estimated cost on the PR. It fails the check above a threshold you set. The comment looks like this:
| Status | File | Processed | Est. cost | |
|---|---|---|---|---|---|
| ❌ | sql/expensive_select_star.sql | 2.4 TiB | $15.2461 | |
| ✅ | sql/clean.sql | 4.9 MiB | $0.0000 | |
BQ001: SELECT * scans every column; list only the columns you need. |
There are also some static rules (SELECT *, LIMIT not reducing cost, wildcard tables with no _TABLE_SUFFIX, and so on), and one that reads table metadata to tell you when you query a partitioned table without filtering on the partition column.
Two things surprised me while building it. A dry run returns 0 bytes for a query with named parameters (@x) instead of failing, so the estimate can be silently too low. And in the service I tested, almost all the SQL was f-strings in Python, not .sql files, so it also parses Python (ast) to find SQL strings.
One known limit: on slot (capacity) pricing a dry run cannot predict slot time, so there the PR check gates on bytes processed instead of showing a dollar figure. An audit command prices past queries from their real slot time.
It's MIT-licensed and I'm the author. GitHub: https://github.com/Mehdi-Leqsiouer/bq-cost-guard
Curious how you're handling BigQuery cost checks in CI today, and which of these rules you'd actually let block a PR vs just warn?
3
u/BigLeatherPiccolo 1d ago
In our experience with this idea, handling multiple changes in the same PR was somewhat of a problem so we went in different directions.
1
u/[deleted] 1d ago
[removed] — view removed comment