Metadata-Version: 2.1
Name: sqlprove
Version: 0.1.0
Summary: Proof-carrying SQL cost optimizer for BigQuery, Snowflake, and dbt using Z3 SMT and DuckDB verification
License: MIT
Project-URL: Homepage, https://github.com/MarklarSystems/sqlprove
Project-URL: Repository, https://github.com/MarklarSystems/sqlprove
Project-URL: Issues, https://github.com/MarklarSystems/sqlprove/issues
Keywords: sql,dbt,bigquery,snowflake,duckdb,z3,smt,formal-verification,query-optimization,finops
Classifier: Development Status :: 4 - Beta
Classifier: Environment :: Console
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Topic :: Database
Classifier: Topic :: Software Development :: Quality Assurance
Requires-Python: >=3.10
Description-Content-Type: text/markdown
Provides-Extra: dev
License-File: LICENSE

# sqlprove

Proof-carrying SQL cost optimizer for **BigQuery**, **Snowflake**, and **dbt** — 100% local, zero PII.

Instead of asking data teams to blindly trust LLM-rewritten SQL (which introduced **17.5%–34.4%** silent `NULL`, duplicate-row, and `LEFT JOIN` bugs in our 120-rewrite blind benchmark), `sqlprove` pairs rewrite discovery with **unbounded algebraic proofs**, a **Z3 SMT formal equivalence checker** (bag semantics + 3-valued `NULL` logic + dbt `schema.yml` / `manifest.json` constraints), **DuckDB counterexample confirmation**, and **warehouse-specific cost gating** backed by live BigQuery and Snowflake benchmarks.

## Live Cloud Benchmark Summary (`$0` Reproducible Validation)

Measured cold (zero cache hits) on live **Google BigQuery** (2M rows/table, 365 partitions, clustered) and **Snowflake** (6M rows/table, ~100 micro-partitions, `USE_CACHED_RESULT = FALSE`). Full breakdown: [`benchmarks/week2-cloud-validation.md`](https://github.com/MarklarSystems/sqlprove/blob/main/benchmarks/week2-cloud-validation.md).

| Formally Proven Rewrite Pattern | BigQuery On-Demand (`$/TB`) | BigQuery Editions (`slot_ms`) | Snowflake Scan (`MB`) | Snowflake Compute (`ms`) |
|---|---:|---:|---:|---:|
| Cross-view dead `LEFT JOIN` + nested `DISTINCT` (`A4`) | `-20.0%` | `-5.8%` | `-13.1%` | **-95.9%** (24.4×) |
| Redundant `DISTINCT` via dbt `unique`+`not_null` (`A3b`) | `-10.3%` | **-79.7%** | `0.0%` | **-60.7%** |
| Dead `INNER JOIN` via dbt `relationships` FK (`A2`) | `-4.3%` | **-65.6%** | `-13.1%` | **-31.6%** |
| `COALESCE(bool, FALSE)` / `NOT COALESCE` / `<> sentinel` (`A5`) | **-76.9% to -87.1%** | **-21% to -37.5%** | `0%` / `+4.1%` | **-10% to -31.3%** |
| `EXTRACT(YEAR/MONTH)` → `[lo, hi)` range (`A7`) | **-91.3%** | **-92.5%** | `-3.1%` | `-12.3%` |
| 2× repeated CTE `UNION ALL` → single pass (`A8`) | `0.0%` | **-61.2%** | `0.0%` | **-70.4%** (3.4×) |
| 4× disjoint `UNION ALL` `GROUP BY` → 1 scan (`A9`) | `0.0%` | **-74.8%** | **-75.0%** | **-62.7%** |
| Cross-col `OR` → disjoint `UNION ALL` w/ `IS NULL` (`A6`) | `0.0%` | **-75.6%** | `+16.5%` *(auto-blocked on Snowflake!)* | `+1.9%` |

## Quickstart

```bash
pip install sqlprove

# Built-in Z3 + DuckDB bug-catch demo
sqlprove demo

# Audit a compiled dbt project (`dbt compile` -> target/manifest.json) with view unfolding + warehouse gating
sqlprove audit /path/to/dbt_project --target snowflake
sqlprove audit /path/to/dbt_project --target bigquery-ondemand --format markdown
sqlprove audit /path/to/dbt_project --target bigquery-editions --show-all

# Rewrite & verify a single SQL query for a specific warehouse target
sqlprove rewrite --sql "SELECT ..." --schema schema.json --dbt-schema models/ --target bigquery-ondemand

# Prove equivalence (or synthesize a DuckDB-confirmed counterexample) for any two SQL queries
sqlprove prove --q1 "..." --q2 "..." --schema schema.json --dbt-schema models/
```

## Verification Tiers (Fail-Closed)

- **`ALGEBRAICALLY_PROVEN`** — Unbounded equivalence established by a verified schema-aware algebraic rule (`A0`–`A9`).
- **`BOUNDED_VERIFIED`** — Z3 proved `UNSAT` (no counterexample) for all symbolic databases up to `bound` rows per table, including arbitrary `NULL`s and duplicates.
- **`COUNTEREXAMPLE_FOUND`** — Z3 synthesized concrete table rows where the two queries return different multisets, independently confirmed in DuckDB.
- **`UNSUPPORTED` / `UNKNOWN`** — Fails closed; never emitted as a proven rewrite.

## Layout

```text
sqlprove/
  src/sqlprove/
    cli.py            # CLI: demo, audit, rewrite, prove, scan
    audit.py          # Compiled dbt manifest audit orchestrator (text/markdown/json)
    dbt_manifest.py   # target/manifest.json loader, constraint extractor & sound view unfolder
    targets.py        # Live-benchmarked warehouse cost gating (BQ On-Demand, BQ Editions, Snowflake, DuckDB)
    algebra.py        # Unbounded sound algebraic equivalence rules (A0-A9)
    prover.py         # Z3 SMT 3-valued NULL + bag-semantics equivalence checker
    rewrites.py       # Dialect-native candidate rewrites + CTE congruence verification
    schema.py         # Schema constraints & zero-dependency dbt schema.yml parser
    fuzzer.py         # DuckDB counterexample confirmer & randomized differential fuzzer
    corpus.py         # Raw dbt/SQL repository pattern scanner
    cost.py           # DuckDB EXPLAIN (ANALYZE, FORMAT JSON) profiler
  benchmarks/         # Corpus audit, blind LLM bug-catch, 1M-row scale, and $0 live BQ/SF scripts & results
  tests/              # 32 unit, soundness, manifest, view-unfolding & differential fuzz tests
```
