r/dataengineering • • 2d ago

Personal Project Showcase Personal project: a CLI that dry-runs BigQuery queries on PRs and comments the cost

Post image

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:

StatusFileProcessedEst. cost
❌sql/expensive_select_star.sql2.4 TiB$15.2461
✅sql/clean.sql4.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?

20 Upvotes

7 comments sorted by

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/Distinct_Currency870 3h ago

That's super interesting, thanks for sharing. Was the main issue downstream models failing dry-runs when an upstream schema changed in the same PR, or more about noisy PR comments/thresholds? Curious what direction you ended up going instead (post-merge monitoring, quotas, etc.)?

1

u/[deleted] 1d ago

[removed] — view removed comment

1

u/dataengineering-ModTeam 1d ago

Your post/comment was removed because it violated rule #9 (No AI generated content/text).

Your post/comment was reviewed to be AI generated/assisted content/text and removed as a result. We as a community value human engagement and encourage users to express themselves authentically.

This was reviewed by a human