sqlprove 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- sqlprove/__init__.py +28 -0
- sqlprove/algebra.py +1302 -0
- sqlprove/audit.py +347 -0
- sqlprove/cli.py +281 -0
- sqlprove/corpus.py +448 -0
- sqlprove/cost.py +92 -0
- sqlprove/dbt_manifest.py +582 -0
- sqlprove/fuzzer.py +312 -0
- sqlprove/prover.py +1568 -0
- sqlprove/rewrites.py +819 -0
- sqlprove/schema.py +430 -0
- sqlprove/targets.py +482 -0
- sqlprove-0.1.0.dist-info/LICENSE +21 -0
- sqlprove-0.1.0.dist-info/METADATA +99 -0
- sqlprove-0.1.0.dist-info/RECORD +18 -0
- sqlprove-0.1.0.dist-info/WHEEL +5 -0
- sqlprove-0.1.0.dist-info/entry_points.txt +2 -0
- sqlprove-0.1.0.dist-info/top_level.txt +1 -0
sqlprove/targets.py
ADDED
|
@@ -0,0 +1,482 @@
|
|
|
1
|
+
"""Warehouse-specific cost gating backed by empirical live BigQuery & Snowflake benchmarks.
|
|
2
|
+
|
|
3
|
+
See benchmarks/week2-cloud-validation.md, benchmarks/results/week2_bigquery_live.json,
|
|
4
|
+
and benchmarks/results/week2_snowflake_live.json for measured numbers.
|
|
5
|
+
|
|
6
|
+
Key empirical finding:
|
|
7
|
+
BigQuery On-Demand, BigQuery Editions (slots), and Snowflake have different—and
|
|
8
|
+
sometimes opposite—planner blind spots. A rewrite that saves 75.6% compute on
|
|
9
|
+
BigQuery Editions (OR -> disjoint UNION ALL) regresses Snowflake bytes scanned
|
|
10
|
+
by +16.5%. Engine-aware cost gating blocks regressions per target warehouse.
|
|
11
|
+
"""
|
|
12
|
+
|
|
13
|
+
from __future__ import annotations
|
|
14
|
+
|
|
15
|
+
from dataclasses import dataclass
|
|
16
|
+
from enum import Enum
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
class WarehouseTarget(str, Enum):
|
|
20
|
+
ALL = "all"
|
|
21
|
+
BIGQUERY_ONDEMAND = "bigquery-ondemand"
|
|
22
|
+
BIGQUERY_EDITIONS = "bigquery-editions"
|
|
23
|
+
SNOWFLAKE = "snowflake"
|
|
24
|
+
DUCKDB = "duckdb"
|
|
25
|
+
|
|
26
|
+
@property
|
|
27
|
+
def default_dialect(self) -> str:
|
|
28
|
+
if self in (WarehouseTarget.BIGQUERY_ONDEMAND, WarehouseTarget.BIGQUERY_EDITIONS):
|
|
29
|
+
return "bigquery"
|
|
30
|
+
if self == WarehouseTarget.SNOWFLAKE:
|
|
31
|
+
return "snowflake"
|
|
32
|
+
return "duckdb"
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
class TargetDecision(str, Enum):
|
|
36
|
+
RECOMMENDED = "RECOMMENDED"
|
|
37
|
+
NEUTRAL = "NEUTRAL"
|
|
38
|
+
BLOCKED = "BLOCKED"
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
@dataclass(frozen=True)
|
|
42
|
+
class TargetRuleAdvice:
|
|
43
|
+
target: WarehouseTarget
|
|
44
|
+
rule_family: str
|
|
45
|
+
decision: TargetDecision
|
|
46
|
+
expected_impact: str
|
|
47
|
+
rationale: str
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
def normalize_rule_family(rule: str) -> str:
|
|
51
|
+
"""Strip CTE prefixes (`cte[name]:...`) and dynamic suffixes (`:alias`) from a rule label."""
|
|
52
|
+
r = rule.strip()
|
|
53
|
+
if r.startswith("cte[") and "]:" in r:
|
|
54
|
+
r = r.split("]:", 1)[1]
|
|
55
|
+
if ":" in r:
|
|
56
|
+
r = r.split(":", 1)[0]
|
|
57
|
+
return r
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
# Canonical policy table: rule_family -> target -> (decision, expected_impact, rationale)
|
|
61
|
+
# Every percentage below comes directly from benchmarks/results/week2_bigquery_live.json,
|
|
62
|
+
# benchmarks/results/week2_snowflake_live.json, and benchmarks/results/day6_scale_cost.json.
|
|
63
|
+
_POLICY: dict[str, dict[WarehouseTarget, tuple[TargetDecision, str, str]]] = {
|
|
64
|
+
"collapse_view_dead_left_join_and_nested_distinct": {
|
|
65
|
+
WarehouseTarget.SNOWFLAKE: (
|
|
66
|
+
TargetDecision.RECOMMENDED,
|
|
67
|
+
"-95.9% compute time, -13.1% bytes scanned (live Snowflake C01)",
|
|
68
|
+
"Snowflake does not collapse cross-view dead GROUP BY LEFT JOIN + nested DISTINCT.",
|
|
69
|
+
),
|
|
70
|
+
WarehouseTarget.BIGQUERY_ONDEMAND: (
|
|
71
|
+
TargetDecision.RECOMMENDED,
|
|
72
|
+
"-20.0% billed bytes (live BigQuery C01; higher on wider RHS views)",
|
|
73
|
+
"Eliminates upstream RHS table reference from BigQuery On-Demand billing.",
|
|
74
|
+
),
|
|
75
|
+
WarehouseTarget.BIGQUERY_EDITIONS: (
|
|
76
|
+
TargetDecision.NEUTRAL,
|
|
77
|
+
"-5.8% slot-ms (live BigQuery C01)",
|
|
78
|
+
"BigQuery already prunes most unused view columns natively.",
|
|
79
|
+
),
|
|
80
|
+
WarehouseTarget.DUCKDB: (
|
|
81
|
+
TargetDecision.RECOMMENDED,
|
|
82
|
+
"-71.0% wall time, -93.6% intermediate bytes (DuckDB 2.0.1)",
|
|
83
|
+
"Eliminates dead hash join and duplicate aggregation in DuckDB.",
|
|
84
|
+
),
|
|
85
|
+
},
|
|
86
|
+
"drop_redundant_distinct_on_unique_key": {
|
|
87
|
+
WarehouseTarget.BIGQUERY_EDITIONS: (
|
|
88
|
+
TargetDecision.RECOMMENDED,
|
|
89
|
+
"-79.7% slot-ms, -10.3% billed bytes (live BigQuery C02)",
|
|
90
|
+
"BigQuery does not enforce dbt schema.yml UNIQUE+NOT NULL keys; dropping DISTINCT removes full hash agg.",
|
|
91
|
+
),
|
|
92
|
+
WarehouseTarget.BIGQUERY_ONDEMAND: (
|
|
93
|
+
TargetDecision.RECOMMENDED,
|
|
94
|
+
"-10.3% billed bytes (live BigQuery C02; enables downstream column pruning)",
|
|
95
|
+
"Removing redundant DISTINCT lets BigQuery prune unused columns from On-Demand billing.",
|
|
96
|
+
),
|
|
97
|
+
WarehouseTarget.SNOWFLAKE: (
|
|
98
|
+
TargetDecision.RECOMMENDED,
|
|
99
|
+
"-60.7% compute time (live Snowflake C02)",
|
|
100
|
+
"Snowflake does not enforce dbt UNIQUE+NOT NULL tests; removes expensive distinct aggregation.",
|
|
101
|
+
),
|
|
102
|
+
WarehouseTarget.DUCKDB: (
|
|
103
|
+
TargetDecision.RECOMMENDED,
|
|
104
|
+
"-53.7% to -73.1% wall time, -71% to -77% intermediate bytes (DuckDB 2.0.1)",
|
|
105
|
+
"Removes full distinct hash table build.",
|
|
106
|
+
),
|
|
107
|
+
},
|
|
108
|
+
"drop_redundant_distinct_with_group_by": {
|
|
109
|
+
WarehouseTarget.BIGQUERY_EDITIONS: (
|
|
110
|
+
TargetDecision.RECOMMENDED,
|
|
111
|
+
"Removes duplicate hash aggregation stage over GROUP BY output",
|
|
112
|
+
"GROUP BY keys already guarantee row uniqueness.",
|
|
113
|
+
),
|
|
114
|
+
WarehouseTarget.BIGQUERY_ONDEMAND: (
|
|
115
|
+
TargetDecision.NEUTRAL,
|
|
116
|
+
"Minimal On-Demand byte impact; saves slot compute",
|
|
117
|
+
"Same columns are still scanned, though compute stage is avoided.",
|
|
118
|
+
),
|
|
119
|
+
WarehouseTarget.SNOWFLAKE: (
|
|
120
|
+
TargetDecision.RECOMMENDED,
|
|
121
|
+
"Removes redundant second distinct pass over GROUP BY output",
|
|
122
|
+
"Reduces Snowflake compute seconds billed.",
|
|
123
|
+
),
|
|
124
|
+
WarehouseTarget.DUCKDB: (
|
|
125
|
+
TargetDecision.RECOMMENDED,
|
|
126
|
+
"Removes redundant distinct operator",
|
|
127
|
+
"GROUP BY already produces unique groups.",
|
|
128
|
+
),
|
|
129
|
+
},
|
|
130
|
+
"eliminate_unused_inner_join_fk": {
|
|
131
|
+
WarehouseTarget.BIGQUERY_EDITIONS: (
|
|
132
|
+
TargetDecision.RECOMMENDED,
|
|
133
|
+
"-65.6% slot-ms (live BigQuery C03)",
|
|
134
|
+
"BigQuery does not enforce dbt relationships FKs; eliminates join build/probe.",
|
|
135
|
+
),
|
|
136
|
+
WarehouseTarget.BIGQUERY_ONDEMAND: (
|
|
137
|
+
TargetDecision.RECOMMENDED,
|
|
138
|
+
"-4.3%+ billed bytes (live BigQuery C03; scales linearly with RHS table size)",
|
|
139
|
+
"Eliminates RHS table scan from On-Demand billed bytes.",
|
|
140
|
+
),
|
|
141
|
+
WarehouseTarget.SNOWFLAKE: (
|
|
142
|
+
TargetDecision.RECOMMENDED,
|
|
143
|
+
"-31.6% compute time, -13.1% bytes scanned (live Snowflake C03)",
|
|
144
|
+
"Eliminates RHS micro-partition scan and hash join.",
|
|
145
|
+
),
|
|
146
|
+
WarehouseTarget.DUCKDB: (
|
|
147
|
+
TargetDecision.RECOMMENDED,
|
|
148
|
+
"-34.5% wall time, -75.2% intermediate bytes (DuckDB 2.0.1)",
|
|
149
|
+
"Eliminates dead FK inner join.",
|
|
150
|
+
),
|
|
151
|
+
},
|
|
152
|
+
"eliminate_unused_left_join": {
|
|
153
|
+
WarehouseTarget.BIGQUERY_EDITIONS: (
|
|
154
|
+
TargetDecision.RECOMMENDED,
|
|
155
|
+
"Eliminates dead LEFT JOIN hash build/probe (~40–65% slot-ms)",
|
|
156
|
+
"Warehouse cannot drop LEFT JOIN without enforced UNIQUE key from dbt schema.yml.",
|
|
157
|
+
),
|
|
158
|
+
WarehouseTarget.BIGQUERY_ONDEMAND: (
|
|
159
|
+
TargetDecision.RECOMMENDED,
|
|
160
|
+
"Eliminates RHS join key scan from On-Demand billed bytes",
|
|
161
|
+
"Avoids billing RHS table completely.",
|
|
162
|
+
),
|
|
163
|
+
WarehouseTarget.SNOWFLAKE: (
|
|
164
|
+
TargetDecision.RECOMMENDED,
|
|
165
|
+
"Eliminates RHS scan and hash join (~30%+ compute time)",
|
|
166
|
+
"Avoids scanning RHS micro-partitions and probing join table.",
|
|
167
|
+
),
|
|
168
|
+
WarehouseTarget.DUCKDB: (
|
|
169
|
+
TargetDecision.RECOMMENDED,
|
|
170
|
+
"Eliminates dead hash join and RHS scan",
|
|
171
|
+
"Unlocked by UNIQUE key or GROUP BY uniqueness on RHS.",
|
|
172
|
+
),
|
|
173
|
+
},
|
|
174
|
+
"unwrap_coalesce_bool_true": {
|
|
175
|
+
WarehouseTarget.BIGQUERY_ONDEMAND: (
|
|
176
|
+
TargetDecision.RECOMMENDED,
|
|
177
|
+
"-87.1% billed bytes (live BigQuery C04)",
|
|
178
|
+
"BigQuery fails to cluster-prune COALESCE(col, FALSE); unwrapping to col = TRUE restores pruning.",
|
|
179
|
+
),
|
|
180
|
+
WarehouseTarget.BIGQUERY_EDITIONS: (
|
|
181
|
+
TargetDecision.RECOMMENDED,
|
|
182
|
+
"-37.5% slot-ms, -87.3% bytes processed (live BigQuery C04)",
|
|
183
|
+
"Restores BigQuery clustering block pruning.",
|
|
184
|
+
),
|
|
185
|
+
WarehouseTarget.SNOWFLAKE: (
|
|
186
|
+
TargetDecision.RECOMMENDED,
|
|
187
|
+
"-31.3% compute time (live Snowflake C04)",
|
|
188
|
+
"Snowflake prunes bytes natively, but unwrapping COALESCE still cuts filter evaluation time.",
|
|
189
|
+
),
|
|
190
|
+
WarehouseTarget.DUCKDB: (
|
|
191
|
+
TargetDecision.RECOMMENDED,
|
|
192
|
+
"-98.0% Parquet row groups, -34.3% bytes read, -32.5% wall time (DuckDB 2.0.1)",
|
|
193
|
+
"Restores Parquet min/max zone-map pruning.",
|
|
194
|
+
),
|
|
195
|
+
},
|
|
196
|
+
"unwrap_not_coalesce_null_safe": {
|
|
197
|
+
WarehouseTarget.BIGQUERY_ONDEMAND: (
|
|
198
|
+
TargetDecision.RECOMMENDED,
|
|
199
|
+
"-78.6% billed bytes on clustered archive tables (live BigQuery C05)",
|
|
200
|
+
"BigQuery fails to cluster-prune NOT COALESCE(col, FALSE); explicit (col = FALSE OR col IS NULL) restores pruning.",
|
|
201
|
+
),
|
|
202
|
+
WarehouseTarget.BIGQUERY_EDITIONS: (
|
|
203
|
+
TargetDecision.RECOMMENDED,
|
|
204
|
+
"-21.0% slot-ms, -78.7% bytes processed (live BigQuery C05)",
|
|
205
|
+
"Restores BigQuery clustering pruning.",
|
|
206
|
+
),
|
|
207
|
+
WarehouseTarget.SNOWFLAKE: (
|
|
208
|
+
TargetDecision.NEUTRAL,
|
|
209
|
+
"+4.1% bytes scanned, -15.6% time (live Snowflake C05)",
|
|
210
|
+
"Snowflake already prunes NOT COALESCE(col, FALSE) natively; rewriting slightly increased scanned bytes.",
|
|
211
|
+
),
|
|
212
|
+
WarehouseTarget.DUCKDB: (
|
|
213
|
+
TargetDecision.RECOMMENDED,
|
|
214
|
+
"-78.6% row groups, -46.8% bytes read on archive tables (DuckDB 2.0.1)",
|
|
215
|
+
"Restores Parquet zone-map pruning on clustered boolean columns.",
|
|
216
|
+
),
|
|
217
|
+
},
|
|
218
|
+
"unwrap_not_coalesce_not_null": {
|
|
219
|
+
WarehouseTarget.BIGQUERY_ONDEMAND: (
|
|
220
|
+
TargetDecision.RECOMMENDED,
|
|
221
|
+
"-78%+ billed bytes on clustered tables (BigQuery A5)",
|
|
222
|
+
"Unwraps NOT COALESCE(col, FALSE) to col = FALSE under NOT NULL constraint.",
|
|
223
|
+
),
|
|
224
|
+
WarehouseTarget.BIGQUERY_EDITIONS: (
|
|
225
|
+
TargetDecision.RECOMMENDED,
|
|
226
|
+
"Restores BigQuery clustering pruning and cuts compute",
|
|
227
|
+
"Simpler equality predicate enables block pruning.",
|
|
228
|
+
),
|
|
229
|
+
WarehouseTarget.SNOWFLAKE: (
|
|
230
|
+
TargetDecision.RECOMMENDED,
|
|
231
|
+
"Simpler equality predicate `col = FALSE` under NOT NULL",
|
|
232
|
+
"Avoids NULL branch and COALESCE overhead.",
|
|
233
|
+
),
|
|
234
|
+
WarehouseTarget.DUCKDB: (
|
|
235
|
+
TargetDecision.RECOMMENDED,
|
|
236
|
+
"Restores Parquet min/max pruning",
|
|
237
|
+
"Direct equality enables zone-map skipping.",
|
|
238
|
+
),
|
|
239
|
+
},
|
|
240
|
+
"unwrap_coalesce_neq_sentinel": {
|
|
241
|
+
WarehouseTarget.BIGQUERY_ONDEMAND: (
|
|
242
|
+
TargetDecision.RECOMMENDED,
|
|
243
|
+
"-76.9% billed bytes (live BigQuery C06)",
|
|
244
|
+
"BigQuery fails to cluster-prune COALESCE(col, 'x') <> 'x'; unwrapping restores clustering pruning.",
|
|
245
|
+
),
|
|
246
|
+
WarehouseTarget.BIGQUERY_EDITIONS: (
|
|
247
|
+
TargetDecision.NEUTRAL,
|
|
248
|
+
"-77.1% bytes processed, noisy slot-ms on small scans (live BigQuery C06)",
|
|
249
|
+
"Helps large clustered scans; verify slot impact on high-slot queries.",
|
|
250
|
+
),
|
|
251
|
+
WarehouseTarget.SNOWFLAKE: (
|
|
252
|
+
TargetDecision.NEUTRAL,
|
|
253
|
+
"0.0% bytes scanned, -10.4% time (live Snowflake C06)",
|
|
254
|
+
"Snowflake prunes COALESCE <> sentinel natively.",
|
|
255
|
+
),
|
|
256
|
+
WarehouseTarget.DUCKDB: (
|
|
257
|
+
TargetDecision.RECOMMENDED,
|
|
258
|
+
"Restores Parquet string zone-map pruning",
|
|
259
|
+
"Direct inequality avoids COALESCE expression wrapper.",
|
|
260
|
+
),
|
|
261
|
+
},
|
|
262
|
+
"unwrap_coalesce_eq_null_safe": {
|
|
263
|
+
WarehouseTarget.BIGQUERY_ONDEMAND: (
|
|
264
|
+
TargetDecision.RECOMMENDED,
|
|
265
|
+
"Restores BigQuery clustering pruning on COALESCE(col, lit) = lit",
|
|
266
|
+
"Same mechanism as live BigQuery C05 (-78.6% billed bytes).",
|
|
267
|
+
),
|
|
268
|
+
WarehouseTarget.BIGQUERY_EDITIONS: (
|
|
269
|
+
TargetDecision.RECOMMENDED,
|
|
270
|
+
"Restores BigQuery clustering pruning",
|
|
271
|
+
"Enables block skipping on clustered columns.",
|
|
272
|
+
),
|
|
273
|
+
WarehouseTarget.SNOWFLAKE: (
|
|
274
|
+
TargetDecision.NEUTRAL,
|
|
275
|
+
"Snowflake generally prunes COALESCE = lit natively",
|
|
276
|
+
"No guaranteed micro-partition win on Snowflake.",
|
|
277
|
+
),
|
|
278
|
+
WarehouseTarget.DUCKDB: (
|
|
279
|
+
TargetDecision.RECOMMENDED,
|
|
280
|
+
"Restores Parquet zone-map pruning",
|
|
281
|
+
"Removes COALESCE wrapper from filter predicate.",
|
|
282
|
+
),
|
|
283
|
+
},
|
|
284
|
+
"unwrap_coalesce_eq_not_null": {
|
|
285
|
+
WarehouseTarget.BIGQUERY_ONDEMAND: (
|
|
286
|
+
TargetDecision.RECOMMENDED,
|
|
287
|
+
"Restores BigQuery clustering pruning (`col = lit` under NOT NULL)",
|
|
288
|
+
"Avoids opaque COALESCE call on clustered column.",
|
|
289
|
+
),
|
|
290
|
+
WarehouseTarget.BIGQUERY_EDITIONS: (
|
|
291
|
+
TargetDecision.RECOMMENDED,
|
|
292
|
+
"Restores BigQuery clustering pruning and reduces slot compute",
|
|
293
|
+
"Direct equality predicate.",
|
|
294
|
+
),
|
|
295
|
+
WarehouseTarget.SNOWFLAKE: (
|
|
296
|
+
TargetDecision.RECOMMENDED,
|
|
297
|
+
"Direct equality predicate under NOT NULL",
|
|
298
|
+
"Removes COALESCE function evaluation.",
|
|
299
|
+
),
|
|
300
|
+
WarehouseTarget.DUCKDB: (
|
|
301
|
+
TargetDecision.RECOMMENDED,
|
|
302
|
+
"Restores Parquet zone-map pruning",
|
|
303
|
+
"Direct equality predicate.",
|
|
304
|
+
),
|
|
305
|
+
},
|
|
306
|
+
"split_or_to_disjoint_union_all_null_safe": {
|
|
307
|
+
WarehouseTarget.BIGQUERY_EDITIONS: (
|
|
308
|
+
TargetDecision.RECOMMENDED,
|
|
309
|
+
"-75.6% slot-ms (live BigQuery C07)",
|
|
310
|
+
"Allows BigQuery to partition/cluster-prune each disjoint branch independently.",
|
|
311
|
+
),
|
|
312
|
+
WarehouseTarget.BIGQUERY_ONDEMAND: (
|
|
313
|
+
TargetDecision.NEUTRAL,
|
|
314
|
+
"0.0% On-Demand billed bytes (live BigQuery C07)",
|
|
315
|
+
"Branch 2 still touches full table columns on On-Demand billing (only Editions slots benefit).",
|
|
316
|
+
),
|
|
317
|
+
WarehouseTarget.SNOWFLAKE: (
|
|
318
|
+
TargetDecision.BLOCKED,
|
|
319
|
+
"+16.5% bytes scanned, +1.9% time REGRESSION (live Snowflake C07)",
|
|
320
|
+
"Blocked on Snowflake: splitting OR into UNION ALL caused a measured scan/time regression.",
|
|
321
|
+
),
|
|
322
|
+
WarehouseTarget.DUCKDB: (
|
|
323
|
+
TargetDecision.RECOMMENDED,
|
|
324
|
+
"-76.6% bytes read, -76.5% row groups, -61.5% CPU (DuckDB 2.0.1)",
|
|
325
|
+
"Enables Parquet row-group pruning on the timestamp branch.",
|
|
326
|
+
),
|
|
327
|
+
},
|
|
328
|
+
"split_or_to_disjoint_union_all_not_null": {
|
|
329
|
+
WarehouseTarget.BIGQUERY_EDITIONS: (
|
|
330
|
+
TargetDecision.RECOMMENDED,
|
|
331
|
+
"-75.6% slot-ms (live BigQuery C07, tighter NOT NULL guard)",
|
|
332
|
+
"Allows independent partition/cluster pruning per branch.",
|
|
333
|
+
),
|
|
334
|
+
WarehouseTarget.BIGQUERY_ONDEMAND: (
|
|
335
|
+
TargetDecision.NEUTRAL,
|
|
336
|
+
"0.0% On-Demand billed bytes (live BigQuery C07)",
|
|
337
|
+
"BigQuery On-Demand still bills full table across branch 2.",
|
|
338
|
+
),
|
|
339
|
+
WarehouseTarget.SNOWFLAKE: (
|
|
340
|
+
TargetDecision.BLOCKED,
|
|
341
|
+
"+16.5% bytes scanned REGRESSION (live Snowflake C07)",
|
|
342
|
+
"Blocked on Snowflake: splitting OR into UNION ALL regressed live Snowflake scan cost.",
|
|
343
|
+
),
|
|
344
|
+
WarehouseTarget.DUCKDB: (
|
|
345
|
+
TargetDecision.RECOMMENDED,
|
|
346
|
+
"-76.6% bytes read on partitioned Parquet (DuckDB 2.0.1)",
|
|
347
|
+
"Enables Parquet row-group pruning.",
|
|
348
|
+
),
|
|
349
|
+
},
|
|
350
|
+
"rewrite_date_extractor_to_range": {
|
|
351
|
+
WarehouseTarget.BIGQUERY_ONDEMAND: (
|
|
352
|
+
TargetDecision.RECOMMENDED,
|
|
353
|
+
"-91.3% billed bytes (live BigQuery C08)",
|
|
354
|
+
"BigQuery cannot partition-prune EXTRACT(YEAR/MONTH); half-open [lo, hi) restores full partition pruning.",
|
|
355
|
+
),
|
|
356
|
+
WarehouseTarget.BIGQUERY_EDITIONS: (
|
|
357
|
+
TargetDecision.RECOMMENDED,
|
|
358
|
+
"-92.5% slot-ms, -91.5% bytes processed (live BigQuery C08)",
|
|
359
|
+
"Restores BigQuery partition pruning and cuts slot compute by >90%.",
|
|
360
|
+
),
|
|
361
|
+
WarehouseTarget.SNOWFLAKE: (
|
|
362
|
+
TargetDecision.NEUTRAL,
|
|
363
|
+
"-3.1% bytes scanned, -12.3% time (live Snowflake C08)",
|
|
364
|
+
"Snowflake already micro-partition prunes EXTRACT(YEAR/MONTH) natively.",
|
|
365
|
+
),
|
|
366
|
+
WarehouseTarget.DUCKDB: (
|
|
367
|
+
TargetDecision.RECOMMENDED,
|
|
368
|
+
"-88.2% to -94.7% Parquet bytes read (DuckDB 2.0.1)",
|
|
369
|
+
"Restores Parquet timestamp zone-map pruning.",
|
|
370
|
+
),
|
|
371
|
+
},
|
|
372
|
+
"collapse_repeated_cte_union_all": {
|
|
373
|
+
WarehouseTarget.BIGQUERY_EDITIONS: (
|
|
374
|
+
TargetDecision.RECOMMENDED,
|
|
375
|
+
"-61.2% slot-ms (live BigQuery C09)",
|
|
376
|
+
"BigQuery inlines CTEs; collapsing 2x UNION ALL into single-pass CROSS JOIN UNNEST halves join/scan compute.",
|
|
377
|
+
),
|
|
378
|
+
WarehouseTarget.BIGQUERY_ONDEMAND: (
|
|
379
|
+
TargetDecision.NEUTRAL,
|
|
380
|
+
"0.0% On-Demand billed bytes (live BigQuery C09)",
|
|
381
|
+
"BigQuery On-Demand deduplicates per-column table billing across repeated scans (huge win on Editions only).",
|
|
382
|
+
),
|
|
383
|
+
WarehouseTarget.SNOWFLAKE: (
|
|
384
|
+
TargetDecision.RECOMMENDED,
|
|
385
|
+
"-70.4% compute time / 3.4x faster (live Snowflake C09)",
|
|
386
|
+
"Eliminates duplicate join/CTE execution overhead on Snowflake virtual warehouses.",
|
|
387
|
+
),
|
|
388
|
+
WarehouseTarget.DUCKDB: (
|
|
389
|
+
TargetDecision.NEUTRAL,
|
|
390
|
+
"+5.9% wall time (DuckDB materializes CTE in RAM)",
|
|
391
|
+
"Minor gain in DuckDB because CTEs are materialized in memory.",
|
|
392
|
+
),
|
|
393
|
+
},
|
|
394
|
+
"collapse_disjoint_union_all_group_by": {
|
|
395
|
+
WarehouseTarget.BIGQUERY_EDITIONS: (
|
|
396
|
+
TargetDecision.RECOMMENDED,
|
|
397
|
+
"-74.8% slot-ms (live BigQuery C10)",
|
|
398
|
+
"Replaces M full table scans + aggregations with 1 single pass.",
|
|
399
|
+
),
|
|
400
|
+
WarehouseTarget.BIGQUERY_ONDEMAND: (
|
|
401
|
+
TargetDecision.NEUTRAL,
|
|
402
|
+
"0.0% On-Demand billed bytes (live BigQuery C10)",
|
|
403
|
+
"BigQuery On-Demand deduplicates per-column billing across UNION ALL branches of the same table.",
|
|
404
|
+
),
|
|
405
|
+
WarehouseTarget.SNOWFLAKE: (
|
|
406
|
+
TargetDecision.RECOMMENDED,
|
|
407
|
+
"-75.0% bytes scanned, -62.7% compute time (live Snowflake C10)",
|
|
408
|
+
"Replaces M micro-partition table scans with 1 scan.",
|
|
409
|
+
),
|
|
410
|
+
WarehouseTarget.DUCKDB: (
|
|
411
|
+
TargetDecision.RECOMMENDED,
|
|
412
|
+
"-74.0% Parquet bytes read (DuckDB 2.0.1)",
|
|
413
|
+
"Replaces M Parquet scans with 1 scan.",
|
|
414
|
+
),
|
|
415
|
+
},
|
|
416
|
+
"collapse_multi_cte_filtered_agg": {
|
|
417
|
+
WarehouseTarget.BIGQUERY_EDITIONS: (
|
|
418
|
+
TargetDecision.RECOMMENDED,
|
|
419
|
+
"-64.8% slot-ms (live BigQuery C11)",
|
|
420
|
+
"Replaces M filtered CTE scans + hash joins with 1 conditional aggregation pass.",
|
|
421
|
+
),
|
|
422
|
+
WarehouseTarget.BIGQUERY_ONDEMAND: (
|
|
423
|
+
TargetDecision.NEUTRAL,
|
|
424
|
+
"0.0% On-Demand billed bytes (live BigQuery C11)",
|
|
425
|
+
"BigQuery On-Demand deduplicates per-column billing across CTE scans of the same table.",
|
|
426
|
+
),
|
|
427
|
+
WarehouseTarget.SNOWFLAKE: (
|
|
428
|
+
TargetDecision.RECOMMENDED,
|
|
429
|
+
"-66.7% bytes scanned (live Snowflake C11)",
|
|
430
|
+
"Cuts micro-partition I/O by 3x and eliminates intermediate joins.",
|
|
431
|
+
),
|
|
432
|
+
WarehouseTarget.DUCKDB: (
|
|
433
|
+
TargetDecision.RECOMMENDED,
|
|
434
|
+
"-66.7% Parquet bytes read (DuckDB 2.0.1)",
|
|
435
|
+
"Single-pass conditional aggregation.",
|
|
436
|
+
),
|
|
437
|
+
},
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
|
|
441
|
+
def evaluate_rule_for_target(rule: str, target: WarehouseTarget | str) -> TargetRuleAdvice:
|
|
442
|
+
"""Return empirically grounded cost advice (RECOMMENDED / NEUTRAL / BLOCKED) for `rule` on `target`."""
|
|
443
|
+
tgt = target if isinstance(target, WarehouseTarget) else WarehouseTarget(target)
|
|
444
|
+
family = normalize_rule_family(rule)
|
|
445
|
+
|
|
446
|
+
if tgt == WarehouseTarget.ALL:
|
|
447
|
+
# Summarize best empirical targets for informational display
|
|
448
|
+
per_t = _POLICY.get(family, {})
|
|
449
|
+
wins = [
|
|
450
|
+
f"{t.value}: {impact}"
|
|
451
|
+
for t, (dec, impact, _) in per_t.items()
|
|
452
|
+
if dec == TargetDecision.RECOMMENDED
|
|
453
|
+
]
|
|
454
|
+
blocks = [t.value for t, (dec, _, _) in per_t.items() if dec == TargetDecision.BLOCKED]
|
|
455
|
+
summary = "; ".join(wins[:2]) if wins else "No measured cloud benchmark entry"
|
|
456
|
+
if blocks:
|
|
457
|
+
summary = f"{summary} [BLOCKED on: {', '.join(blocks)}]"
|
|
458
|
+
return TargetRuleAdvice(
|
|
459
|
+
target=tgt,
|
|
460
|
+
rule_family=family,
|
|
461
|
+
decision=TargetDecision.RECOMMENDED if wins or not per_t else TargetDecision.NEUTRAL,
|
|
462
|
+
expected_impact=summary,
|
|
463
|
+
rationale="Unfiltered target='all' mode; specify --target bigquery-ondemand|bigquery-editions|snowflake for warehouse gating.",
|
|
464
|
+
)
|
|
465
|
+
|
|
466
|
+
family_map = _POLICY.get(family)
|
|
467
|
+
if family_map is None or tgt not in family_map:
|
|
468
|
+
return TargetRuleAdvice(
|
|
469
|
+
target=tgt,
|
|
470
|
+
rule_family=family,
|
|
471
|
+
decision=TargetDecision.NEUTRAL,
|
|
472
|
+
expected_impact="Unbenchmarked on this target",
|
|
473
|
+
rationale="Fail-safe: no live cloud benchmark entry for this rule on the selected target.",
|
|
474
|
+
)
|
|
475
|
+
decision, impact, rationale = family_map[tgt]
|
|
476
|
+
return TargetRuleAdvice(
|
|
477
|
+
target=tgt,
|
|
478
|
+
rule_family=family,
|
|
479
|
+
decision=decision,
|
|
480
|
+
expected_impact=impact,
|
|
481
|
+
rationale=rationale,
|
|
482
|
+
)
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 sqlprove contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
Metadata-Version: 2.1
|
|
2
|
+
Name: sqlprove
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Proof-carrying SQL cost optimizer for BigQuery, Snowflake, and dbt using Z3 SMT and DuckDB verification
|
|
5
|
+
License: MIT
|
|
6
|
+
Project-URL: Homepage, https://github.com/MarklarSystems/sqlprove
|
|
7
|
+
Project-URL: Repository, https://github.com/MarklarSystems/sqlprove
|
|
8
|
+
Project-URL: Issues, https://github.com/MarklarSystems/sqlprove/issues
|
|
9
|
+
Keywords: sql,dbt,bigquery,snowflake,duckdb,z3,smt,formal-verification,query-optimization,finops
|
|
10
|
+
Classifier: Development Status :: 4 - Beta
|
|
11
|
+
Classifier: Environment :: Console
|
|
12
|
+
Classifier: Intended Audience :: Developers
|
|
13
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
14
|
+
Classifier: Operating System :: OS Independent
|
|
15
|
+
Classifier: Programming Language :: Python :: 3
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
20
|
+
Classifier: Topic :: Database
|
|
21
|
+
Classifier: Topic :: Software Development :: Quality Assurance
|
|
22
|
+
Requires-Python: >=3.10
|
|
23
|
+
Description-Content-Type: text/markdown
|
|
24
|
+
License-File: LICENSE
|
|
25
|
+
Requires-Dist: duckdb >=1.1.0
|
|
26
|
+
Requires-Dist: sqlglot >=25.0.0
|
|
27
|
+
Requires-Dist: z3-solver >=4.13.0
|
|
28
|
+
Provides-Extra: dev
|
|
29
|
+
Requires-Dist: build >=1.0.0 ; extra == 'dev'
|
|
30
|
+
Requires-Dist: pytest >=8.0.0 ; extra == 'dev'
|
|
31
|
+
Requires-Dist: twine >=5.0.0 ; extra == 'dev'
|
|
32
|
+
|
|
33
|
+
# sqlprove
|
|
34
|
+
|
|
35
|
+
Proof-carrying SQL cost optimizer for **BigQuery**, **Snowflake**, and **dbt** — 100% local, zero PII.
|
|
36
|
+
|
|
37
|
+
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.
|
|
38
|
+
|
|
39
|
+
## Live Cloud Benchmark Summary (`$0` Reproducible Validation)
|
|
40
|
+
|
|
41
|
+
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).
|
|
42
|
+
|
|
43
|
+
| Formally Proven Rewrite Pattern | BigQuery On-Demand (`$/TB`) | BigQuery Editions (`slot_ms`) | Snowflake Scan (`MB`) | Snowflake Compute (`ms`) |
|
|
44
|
+
|---|---:|---:|---:|---:|
|
|
45
|
+
| Cross-view dead `LEFT JOIN` + nested `DISTINCT` (`A4`) | `-20.0%` | `-5.8%` | `-13.1%` | **-95.9%** (24.4×) |
|
|
46
|
+
| Redundant `DISTINCT` via dbt `unique`+`not_null` (`A3b`) | `-10.3%` | **-79.7%** | `0.0%` | **-60.7%** |
|
|
47
|
+
| Dead `INNER JOIN` via dbt `relationships` FK (`A2`) | `-4.3%` | **-65.6%** | `-13.1%` | **-31.6%** |
|
|
48
|
+
| `COALESCE(bool, FALSE)` / `NOT COALESCE` / `<> sentinel` (`A5`) | **-76.9% to -87.1%** | **-21% to -37.5%** | `0%` / `+4.1%` | **-10% to -31.3%** |
|
|
49
|
+
| `EXTRACT(YEAR/MONTH)` → `[lo, hi)` range (`A7`) | **-91.3%** | **-92.5%** | `-3.1%` | `-12.3%` |
|
|
50
|
+
| 2× repeated CTE `UNION ALL` → single pass (`A8`) | `0.0%` | **-61.2%** | `0.0%` | **-70.4%** (3.4×) |
|
|
51
|
+
| 4× disjoint `UNION ALL` `GROUP BY` → 1 scan (`A9`) | `0.0%` | **-74.8%** | **-75.0%** | **-62.7%** |
|
|
52
|
+
| Cross-col `OR` → disjoint `UNION ALL` w/ `IS NULL` (`A6`) | `0.0%` | **-75.6%** | `+16.5%` *(auto-blocked on Snowflake!)* | `+1.9%` |
|
|
53
|
+
|
|
54
|
+
## Quickstart
|
|
55
|
+
|
|
56
|
+
```bash
|
|
57
|
+
pip install sqlprove
|
|
58
|
+
|
|
59
|
+
# Built-in Z3 + DuckDB bug-catch demo
|
|
60
|
+
sqlprove demo
|
|
61
|
+
|
|
62
|
+
# Audit a compiled dbt project (`dbt compile` -> target/manifest.json) with view unfolding + warehouse gating
|
|
63
|
+
sqlprove audit /path/to/dbt_project --target snowflake
|
|
64
|
+
sqlprove audit /path/to/dbt_project --target bigquery-ondemand --format markdown
|
|
65
|
+
sqlprove audit /path/to/dbt_project --target bigquery-editions --show-all
|
|
66
|
+
|
|
67
|
+
# Rewrite & verify a single SQL query for a specific warehouse target
|
|
68
|
+
sqlprove rewrite --sql "SELECT ..." --schema schema.json --dbt-schema models/ --target bigquery-ondemand
|
|
69
|
+
|
|
70
|
+
# Prove equivalence (or synthesize a DuckDB-confirmed counterexample) for any two SQL queries
|
|
71
|
+
sqlprove prove --q1 "..." --q2 "..." --schema schema.json --dbt-schema models/
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
## Verification Tiers (Fail-Closed)
|
|
75
|
+
|
|
76
|
+
- **`ALGEBRAICALLY_PROVEN`** — Unbounded equivalence established by a verified schema-aware algebraic rule (`A0`–`A9`).
|
|
77
|
+
- **`BOUNDED_VERIFIED`** — Z3 proved `UNSAT` (no counterexample) for all symbolic databases up to `bound` rows per table, including arbitrary `NULL`s and duplicates.
|
|
78
|
+
- **`COUNTEREXAMPLE_FOUND`** — Z3 synthesized concrete table rows where the two queries return different multisets, independently confirmed in DuckDB.
|
|
79
|
+
- **`UNSUPPORTED` / `UNKNOWN`** — Fails closed; never emitted as a proven rewrite.
|
|
80
|
+
|
|
81
|
+
## Layout
|
|
82
|
+
|
|
83
|
+
```text
|
|
84
|
+
sqlprove/
|
|
85
|
+
src/sqlprove/
|
|
86
|
+
cli.py # CLI: demo, audit, rewrite, prove, scan
|
|
87
|
+
audit.py # Compiled dbt manifest audit orchestrator (text/markdown/json)
|
|
88
|
+
dbt_manifest.py # target/manifest.json loader, constraint extractor & sound view unfolder
|
|
89
|
+
targets.py # Live-benchmarked warehouse cost gating (BQ On-Demand, BQ Editions, Snowflake, DuckDB)
|
|
90
|
+
algebra.py # Unbounded sound algebraic equivalence rules (A0-A9)
|
|
91
|
+
prover.py # Z3 SMT 3-valued NULL + bag-semantics equivalence checker
|
|
92
|
+
rewrites.py # Dialect-native candidate rewrites + CTE congruence verification
|
|
93
|
+
schema.py # Schema constraints & zero-dependency dbt schema.yml parser
|
|
94
|
+
fuzzer.py # DuckDB counterexample confirmer & randomized differential fuzzer
|
|
95
|
+
corpus.py # Raw dbt/SQL repository pattern scanner
|
|
96
|
+
cost.py # DuckDB EXPLAIN (ANALYZE, FORMAT JSON) profiler
|
|
97
|
+
benchmarks/ # Corpus audit, blind LLM bug-catch, 1M-row scale, and $0 live BQ/SF scripts & results
|
|
98
|
+
tests/ # 32 unit, soundness, manifest, view-unfolding & differential fuzz tests
|
|
99
|
+
```
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
sqlprove/__init__.py,sha256=2T8R8QmzdL-TcO_Mq74CL2NZTXXQNkeMDJRiTy_NAos,867
|
|
2
|
+
sqlprove/algebra.py,sha256=Z-ok6alsHYU-uEKZQ3zT9sLEgyvcG6MvbhugpgUK4dE,60349
|
|
3
|
+
sqlprove/audit.py,sha256=-n7KjwkbqWPGKJsk6M0rzYrV02OqM_LnrdTQ1q5SsqU,14388
|
|
4
|
+
sqlprove/cli.py,sha256=XnthxAPP9e2v_d4vGeZrIkk9JnTakcaLK-mU4gL6Gz0,12067
|
|
5
|
+
sqlprove/corpus.py,sha256=R9KZ-XwqW6WnXnOzLsjHAKykJ9jSQ5cTvocAMixSwpg,17342
|
|
6
|
+
sqlprove/cost.py,sha256=HXFloVMzoPUgXOidNAs9WdX5SGHAJbNDSXdhZQmAZQI,3268
|
|
7
|
+
sqlprove/dbt_manifest.py,sha256=T9RATGeOnyItKAHgP2b0Pei7HXiA-7vSM3fN5XDrhgk,23359
|
|
8
|
+
sqlprove/fuzzer.py,sha256=0awCmNJsvsZs6hdyTe1EL7dnqtKvKcGQVW_OLIYf3IU,11161
|
|
9
|
+
sqlprove/prover.py,sha256=Rb8GNGvbktzWqnhgP3AOSnmBR-xiIg8E3j6JNEq77gw,72948
|
|
10
|
+
sqlprove/rewrites.py,sha256=i8egbpL-hnni4K2f_kQZpnWmBtdbSgCKeOY85iZRmkI,36319
|
|
11
|
+
sqlprove/schema.py,sha256=3cy6UN961zxZ0CcCrjDY3I8GPEqgiZ_ItPBQk30LNO4,16061
|
|
12
|
+
sqlprove/targets.py,sha256=dd4h0p7TISHZEzRQ6ylo3VzV-8X10IeldYk6hjTvjx0,20984
|
|
13
|
+
sqlprove-0.1.0.dist-info/LICENSE,sha256=wJCKIZ0OAgnZFCaxlUy82J7yxHua8IeDKQ1anaTSaH0,1078
|
|
14
|
+
sqlprove-0.1.0.dist-info/METADATA,sha256=msieiumeI1AfXIKBEWrwF7DT7MNpsgwTgtCBrR0afkE,6031
|
|
15
|
+
sqlprove-0.1.0.dist-info/WHEEL,sha256=oiQVh_5PnQM0E3gPdiz09WCNmwiHDMaGer_elqB3coM,92
|
|
16
|
+
sqlprove-0.1.0.dist-info/entry_points.txt,sha256=gEdozpRDoXk2wFTxyqkf3Oe2xPn52V2m45h6r4g7b_g,47
|
|
17
|
+
sqlprove-0.1.0.dist-info/top_level.txt,sha256=xq2IaX6-Daw-meBo_OD_HaHEZe2IZCyRCzf8Q6GbEoE,9
|
|
18
|
+
sqlprove-0.1.0.dist-info/RECORD,,
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
sqlprove
|