import json, math, itertools KIND="Constructed analytical model; not a measured benchmark" def fig(i,title,xlabel,ylabel,series,caption,assumptions): return dict(id=i,title=title,x_label=xlabel,y_label=ylabel,series=series,caption=caption,assumptions=assumptions,kind=KIND) def check(name,passed,cases,detail): assert passed, name return dict(name=name,passed=bool(passed),cases=cases,detail=detail) def costs(n): return {'N':2*n,'B':120+.8*n,'H':500+.2*n} def choose(n): # Stable, declared tie priority: N, then B, then H. values=costs(n) return min(('N','B','H'),key=values.get) def regret(p,n): return costs(n)[p]/min(costs(n).values()) def run(): ns=list(range(100,1001)); worst={p:max(regret(p,n) for n in ns) for p in 'NBH'}; robust=min(worst,key=worst.get) xs=[10,50,100,200,400,600,1000,2000,5000] est=[10,25,50,100,200,500,1000,2000,10000] tests=[check('Declared equal-cost tie priority',choose(100)=='N' and costs(100)['N']==costs(100)['B'],1,'N wins the exact 100-row N/B tie; the q-error example depends on this declared choice.'),check('Discrete minimax enumeration',robust=='B' and abs(worst['B']-920/700)<1e-12,2703,'All 901 cardinalities for each of three plans.'),check('Exact switching intervals',all(choose(n)==('N' if n<100 else 'B' if n<634 else 'H') for n in range(1,2001) if n!=100),1999,'Analytic pairwise intersections checked against enumeration; tie at 100 excluded.'),check('Equal q-error does not imply equal regret',max(100/1000,1000/100)==max(10000/1000,1000/10000) and regret(choose(100),1000)>regret(choose(10000),1000),1,'Underestimate and overestimate both have q-error 10.'),check('Correlated selectivity counterexample',sum(1 for i in range(1000) if i%10==0 and i%10==0)==100,1,'Two identical 10 percent predicates select 100 rows, not independence prediction 10.')] return dict(slug='query-plan-uncertainty',checks=tests,figures=[fig('cost-envelopes','Three constructed plan cost envelopes','Actual cardinality','Abstract work units',[dict(name=p,x=xs,y=[costs(n)[p] for n in xs]) for p in 'NBH'],'Costs are invented transparent affine functions, not PostgreSQL optimizer units.',['Single uncertain intermediate cardinality','All plans semantically equivalent','No spill or concurrency']),fig('error-regret','Estimate error through the decision boundary','Estimated cardinality (actual 1000)','Multiplicative realized regret',[dict(name='Point estimate plan',x=est,y=[regret(choose(n),1000) for n in est]),dict(name='Interval-robust plan B',x=est,y=[regret('B',1000)]*len(est))],'Robust plan minimizes worst regret on integer cardinalities 100 through 1000.',['Perfect cost functions assumed','Uncertainty set fixed in advance','No planning overhead'])],result_summary=dict(robust_plan=robust,worst_regret=worst,underestimate_regret=regret(choose(100),1000),overestimate_regret=regret(choose(10000),1000),robust_cost_at_1000=costs(1000)['B'],optimal_cost_at_1000=min(costs(1000).values()),correlated_actual=100,independence_estimate=10)) if __name__=="__main__": print(json.dumps(run(),indent=2))