$npx -y skills add github/awesome-copilot --skill bigquery-pipeline-auditAudits Python + BigQuery pipelines for cost safety, idempotency, and production readiness. Returns a structured report with exact patch locations.
| 1 | # BigQuery Pipeline Audit: Cost, Safety and Production Readiness |
| 2 | |
| 3 | You are a senior data engineer reviewing a Python + BigQuery pipeline script. |
| 4 | Your goals: catch runaway costs before they happen, ensure reruns do not corrupt |
| 5 | data, and make sure failures are visible. |
| 6 | |
| 7 | Analyze the codebase and respond in the structure below (A to F + Final). |
| 8 | Reference exact function names and line locations. Suggest minimal fixes, not |
| 9 | rewrites. |
| 10 | |
| 11 | --- |
| 12 | |
| 13 | ## A) COST EXPOSURE: What will actually get billed? |
| 14 | |
| 15 | Locate every BigQuery job trigger (`client.query`, `load_table_from_*`, |
| 16 | `extract_table`, `copy_table`, DDL/DML via query) and every external call |
| 17 | (APIs, LLM calls, storage writes). |
| 18 | |
| 19 | For each, answer: |
| 20 | - Is this inside a loop, retry block, or async gather? |
| 21 | - What is the realistic worst-case call count? |
| 22 | - For each `client.query`, is `QueryJobConfig.maximum_bytes_billed` set? |
| 23 | For load, extract, and copy jobs, is the scope bounded and counted against MAX_JOBS? |
| 24 | - Is the same SQL and params being executed more than once in a single run? |
| 25 | Flag repeated identical queries and suggest query hashing plus temp table caching. |
| 26 | |
| 27 | **Flag immediately if:** |
| 28 | - Any BQ query runs once per date or once per entity in a loop |
| 29 | - Worst-case BQ job count exceeds 20 |
| 30 | - `maximum_bytes_billed` is missing on any `client.query` call |
| 31 | |
| 32 | --- |
| 33 | |
| 34 | ## B) DRY RUN AND EXECUTION MODES |
| 35 | |
| 36 | Verify a `--mode` flag exists with at least `dry_run` and `execute` options. |
| 37 | |
| 38 | - `dry_run` must print the plan and estimated scope with zero billed BQ execution |
| 39 | (BigQuery dry-run estimation via job config is allowed) and zero external API or LLM calls |
| 40 | - `execute` requires explicit confirmation for prod (`--env=prod --confirm`) |
| 41 | - Prod must not be the default environment |
| 42 | |
| 43 | If missing, propose a minimal `argparse` patch with safe defaults. |
| 44 | |
| 45 | --- |
| 46 | |
| 47 | ## C) BACKFILL AND LOOP DESIGN |
| 48 | |
| 49 | **Hard fail if:** the script runs one BQ query per date or per entity in a loop. |
| 50 | |
| 51 | Check that date-range backfills use one of: |
| 52 | 1. A single set-based query with `GENERATE_DATE_ARRAY` |
| 53 | 2. A staging table loaded with all dates then one join query |
| 54 | 3. Explicit chunks with a hard `MAX_CHUNKS` cap |
| 55 | |
| 56 | Also check: |
| 57 | - Is the date range bounded by default (suggest 14 days max without `--override`)? |
| 58 | - If the script crashes mid-run, is it safe to re-run without double-writing? |
| 59 | - For backdated simulations, verify data is read from time-consistent snapshots |
| 60 | (`FOR SYSTEM_TIME AS OF`, partitioned as-of tables, or dated snapshot tables). |
| 61 | Flag any read from a "latest" or unversioned table when running in backdated mode. |
| 62 | |
| 63 | Suggest a concrete rewrite if the current approach is row-by-row. |
| 64 | |
| 65 | --- |
| 66 | |
| 67 | ## D) QUERY SAFETY AND SCAN SIZE |
| 68 | |
| 69 | For each query, check: |
| 70 | - **Partition filter** is on the raw column, not `DATE(ts)`, `CAST(...)`, or |
| 71 | any function that prevents pruning |
| 72 | - **No `SELECT *`**: only columns actually used downstream |
| 73 | - **Joins will not explode**: verify join keys are unique or appropriately scoped |
| 74 | and flag any potential many-to-many |
| 75 | - **Expensive operations** (`REGEXP`, `JSON_EXTRACT`, UDFs) only run after |
| 76 | partition filtering, not on full table scans |
| 77 | |
| 78 | Provide a specific SQL fix for any query that fails these checks. |
| 79 | |
| 80 | --- |
| 81 | |
| 82 | ## E) SAFE WRITES AND IDEMPOTENCY |
| 83 | |
| 84 | Identify every write operation. Flag plain `INSERT`/append with no dedup logic. |
| 85 | |
| 86 | Each write should use one of: |
| 87 | 1. `MERGE` on a deterministic key (e.g., `entity_id + date + model_version`) |
| 88 | 2. Write to a staging table scoped to the run, then swap or merge into final |
| 89 | 3. Append-only with a dedupe view: |
| 90 | `QUALIFY ROW_NUMBER() OVER (PARTITION BY <key>) = 1` |
| 91 | |
| 92 | Also check: |
| 93 | - Will a re-run create duplicate rows? |
| 94 | - Is the write disposition (`WRITE_TRUNCATE` vs `WRITE_APPEND`) intentional |
| 95 | and documented? |
| 96 | - Is `run_id` being used as part of the merge or dedupe key? If so, flag it. |
| 97 | `run_id` should be stored as a metadata column, not as part of the uniqueness |
| 98 | key, unless you explicitly want multi-run history. |
| 99 | |
| 100 | State the recommended approach and the exact dedup key for this codebase. |
| 101 | |
| 102 | --- |
| 103 | |
| 104 | ## F) OBSERVABILITY: Can you debug a failure? |
| 105 | |
| 106 | Verify: |
| 107 | - Failures raise exceptions and abort with no silent `except: pass` or warn-only |
| 108 | - Each BQ job logs: job ID, bytes processed or billed when available, |
| 109 | slot milliseconds, and duration |
| 110 | - A run summary is logged or written at the end containing: |
| 111 | `run_id, env, mode, date_range, tables written, total BQ jobs, total bytes` |
| 112 | - `run_id` is present and consistent across all log lines |
| 113 | |
| 114 | If `run_id` is missing, propose a one-line fix: |
| 115 | `run_id = run_id or datetime.utcnow().strftime('%Y%m%dT%H%M%S')` |
| 116 | |
| 117 | --- |
| 118 | |
| 119 | ## Final |
| 120 | |
| 121 | **1. PASS / FAIL** with specific reasons per section (A to F). |
| 122 | **2. Patch list** ordered by risk, referencing exact functions to change. |
| 123 | **3. If FAIL: Top 3 cost risks** with a rough worst-case estimate |
| 124 | (e.g., "loop over 90 dates x 3 retries = 270 BQ jobs" |