CoolFace
Apppublic

RonyForAI/Mirage_DB_RL

sourceHugging Faceupdated 6mo agoView on Hugging Face
0likes
tasks.cpython-310.pyc116 linesDownload Raw Back to __pycache__
1o

2A_�i`�@sdUdZddlmZddlZddlZddlmZmZddlm	Z	m3Z4eGdd�d��ZeGdd	�d	��ZeGd5d�d��Z
edd
dedddddd�edddddd�edddddd�gd�eddded d!d"ddd�ed#d$d%ddd�ed&d'd(ddd�gd�ed)d
d*edddddd�ed+d,d-ddd�ed.d/ddd0d�gd�gZd1ed2<ed3d
d4ed5d6d"dd%d�ed7d!ddd(d�eddddd"d�eddd8ddd�ed+d,d-ddd�gd�ed9d:d;ed<d=ddd>d�ed d?d%ddd�ed@dAd>ddBd�edCd!d"ddDd�edEdFdGddHd�gd�edIdJdKedLdMdddNd�ed#d6d(dd(d�edOddDdd"d�edPd6d"ddNd�edQdRdSddHd�gd�gZd1edT<edUd
dVeddWd(ddd�ed+dXd(dd"d�edd6ddd(d�eddYd(ddd�ed7d6ddd%d�ed5dZd"ddNd�ed[d\ddd]d�gd�ed^dJd_ed`dWd(dd(d�edadXd(dd8d�edOd?d(dd"d�edbdcddd>d�edPddd(dded�ed#dZd"ddd�edLd=ddd>d�gd�edfd:dged@dWd(dd"d�edhdXd(dded�ed d?d(ddd�edCdcdddNd�ed<did"dd>d�edjdkdddld�edmd\ddd-d�gd�gZd1edn<e
dodpdodqedr�e
dsdtdsduedr�e
dvdwdvdxedr�dy�Zdzed{<d�d�d��Zd�d�d��Zd�d�d�d��Zd�d�d��Z	d�d�d�d��ZdS)�u6tasks.py — Enterprise-grade query optimization task definitions for Mirage_RL.7 8Each task tier (easy / medium / hard) contains a pool of production-realistic9query scenarios sampled randomly per episode so agents cannot memorize solutions.10 11Domains covered:12  - E-commerce (OLTP):   orders, customers, products, inventory, suppliers13  - Analytics (OLAP):    events, sessions, campaigns, conversions, attribution14  - Financial (OLAP):    transactions, accounts, merchants, fraud signals, risk15 16Key design decisions:17  - Table cardinalities match realistic production scales (10K – 1B rows)18  - Cardinality estimation noise (log-normal) simulates the core real-world19    challenge: a planner's row estimates are always wrong. Noise sigma maps to:20      σ=0.04  excellent statistics (small, frequently vacuumed)21      σ=0.15  typical OLTP tables22      σ=0.25  typical analytics/OLAP tables23      σ=0.40  poor statistics (large tables, column correlations)24      σ=0.60  very poor (event tables, multi-column predicates, data skew)25�)�annotationsN)�	dataclass�field)�List�Optionalc@s:eZdZUdZded<ded<ded<ded<ded	<d26S)�	TableSpecz0Configuration for one table in a query scenario.�str�name�int�	true_rows�float�selectivity�	has_index�noise_sigmaN��__name__�27__module__�__qualname__�__doc__�__annotations__�rr�:D:\Meta Hackathon DBMS Optimizer\Mirage_RL\server\tasks.pyr#s28rc@s2eZdZUdZded<ded<ded<ded<dS)	�Scenarioz&A complete multi-table query scenario.rr	�domain�
query_context�List[TableSpec]�tablesNrrrrrr-s29rc@s6eZdZUded<ded<ded<ded<ded<dS)	�30TaskConfigr�task_idr	�31difficulty�description�List[Scenario]�	scenariosN)rrrrrrrrr6s32rZecommerce_catalog_lookupZ	ecommercez�SELECT p.name, c.label, s.region FROM products JOIN categories ON p.category_id=c.id JOIN suppliers ON p.supplier_id=s.id WHERE c.segment='Electronics' AND s.active=trueZproductsi �g{�G�z�?�g�������?)rr
rr�33categoriesi'g�������?g���Q��?Z	suppliersiP�g���Q��?)r	rrrZsaas_active_subscriptionsZsaasz�SELECT u.email, a.plan, s.renewal_date FROM users JOIN accounts ON u.account_id=a.id JOIN subscriptions ON a.id=s.account_id WHERE a.tier='enterprise' AND s.status='active'�usersi��g{�G�z�?Zaccountsi5g333333�?Z
subscriptionsi�Og�������?Zinventory_reorder_checkz�SELECT p.sku, w.location, i.quantity FROM products JOIN warehouses ON i.warehouse_id=w.id JOIN inventory ON p.id=i.product_id WHERE i.quantity < p.reorder_point AND w.region='US-WEST'Z34warehousesi�g�������?Z	inventoryi�%&g���Q��?r!�EASY_SCENARIOSZecommerce_order_fulfillmentaSELECT o.id, c.name, p.sku, cat.label, w.region FROM orders JOIN customers ON o.customer_id=c.id JOIN products ON o.product_id=p.id JOIN categories ON p.category_id=cat.id JOIN warehouses ON o.warehouse_id=w.id WHERE o.status='pending' AND o.created_at > NOW() - INTERVAL '7 days'Zordersi���Z	customers皙�����?Zmarketing_funnel_analytics�	analyticsz�SELECT s.id, u.segment, c.name, cv.revenue, ch.source FROM sessions JOIN users ON s.user_id=u.id JOIN campaigns ON s.campaign_id=c.id JOIN conversions ON s.id=cv.session_id JOIN channels ON c.channel_id=ch.id WHERE c.type='paid' AND cv.revenue > 0�sessionsi���g�?i@KLZ	campaignsi��g�Q���?Zconversionsg35ףp=36�?�channelsi�gffffff�?g{�G�z�?Zfinancial_transaction_summaryZ	financiala"SELECT t.amount, a.holder, m.name, rs.score, cur.symbol FROM transactions JOIN accounts ON t.account_id=a.id JOIN merchants ON t.merchant_id=m.id JOIN risk_scores ON a.id=rs.account_id JOIN currencies ON t.currency_code=cur.code WHERE t.created_at >= CURRENT_DATE - 30 AND a.status='active'Ztransactionsi��g�������?Z	merchantsZrisk_scoresZ37currencies��皙�����?�MEDIUM_SCENARIOSZecommerce_full_pipeline_audita�SELECT o.id, oi.qty, p.sku, cat.label, c.name, s.contact, w.region FROM orders JOIN order_items ON o.id=oi.order_id JOIN products ON oi.product_id=p.id JOIN categories ON p.category_id=cat.id JOIN customers ON o.customer_id=c.id JOIN suppliers ON p.supplier_id=s.id JOIN warehouses ON oi.warehouse_id=w.id WHERE o.created_at >= '2024-01-01' AND c.country='US' AND cat.segment='Electronics'i@Bi��-izi-1Zorder_itemsiZbgffffff�?Zfraud_detection_pipelineatSELECT t.id, a.holder, m.category, fl.label, rs.score, d.fingerprint, loc.country FROM transactions JOIN accounts ON t.account_id=a.id JOIN merchants ON t.merchant_id=m.id JOIN fraud_labels ON t.id=fl.transaction_id JOIN risk_scores ON a.id=rs.account_id JOIN devices ON t.device_id=d.id JOIN locations ON t.location_id=loc.id WHERE t.amount > 10000 AND fl.is_flagged=true�	locationsZfraud_labelsZdevicesi$�i�g333333�?Zuser_journey_attributionanSELECT e.event_type, s.duration, u.segment, c.name, cv.revenue, ab.variant, pv.url FROM events JOIN sessions ON e.session_id=s.id JOIN users ON s.user_id=u.id JOIN campaigns ON s.campaign_id=c.id JOIN conversions ON s.id=cv.session_id JOIN ab_tests ON u.id=ab.user_id JOIN page_views ON s.id=pv.session_id WHERE s.started_at >= CURRENT_DATE - 7 AND c.status='active'Zab_testsi����eventsiH���?Z38page_views�HARD_SCENARIOS�easyu'OLTP Join Optimizer — 3-Table Queriesz�Production OLTP queries joining 3 tables from e-commerce and SaaS schemas. All indexes are available. Statistics are accurate. Goal: select the optimal join order and strategy. Scoring penalises nested-loop joins and missed indexes.)rr	rr r"�mediumu=OLAP Join Optimizer — 5-Table Queries with Estimation Noiseu�Analytical queries joining 5 tables across e-commerce, marketing, and financial schemas. Some tables lack indexes. Cardinality estimates contain realistic noise (σ 0.05–0.25), requiring the agent to reason under uncertainty about true table sizes.�harduJComplex OLAP Join Optimizer — 7-Table Queries with High Estimation Noiseu5Enterprise analytical queries joining 7 tables with billion-row event tables, missing indexes, and high cardinality estimation noise (σ up to 0.60). Models real-world conditions: data skew, stale statistics, and column correlations that cause traditional planners to underestimate result sizes by 10–100×.)r2r3r4zdict[str, TaskConfig]�TASKSrr39rr�rng�
random.Random�returncCs4|dkr|S|�d|�}t|t�|��}td|�S)u�40    Simulate cardinality estimation error using log-normal noise.41 42    real planners: estimated_rows = true_rows × exp(N(0, σ²))43 44    σ=0.05 → ~5% error  (excellent statistics)45    σ=0.15 → ~15% error (typical OLTP)46    σ=0.25 → ~28% error (typical OLAP)47    σ=0.40 → ~49% error (stale statistics / large tables)48    σ=0.60 → ~82% error (data skew, column correlations)49    r�r#)�gaussr50�math�exp�max)rrr6Z51log_factorZ	estimatedrrr�apply_estimation_noise,s5253r>rr�tuple[float, float]cCsZdd�|D�}t|dd�d�}t|dd�dd�}dd
d�}||dd�}||dd�}||fS)u^54    Analytical worst/best cost bounds for a scenario, accounting for cascading sizes.55    Joining early inflates all subsequent step costs via an additive intermediate size penalty.56 57    Worst: worst ordering by size (largest first), nested-loop (2.0×), no index58    Best:  best ordering by size (smallest first), merge-sort (0.8×), use index59    cSsg|]60}|j|j|f�qSr�rr
��.0�trrr�61<listcomp>Lsz'compute_cost_bounds.<locals>.<listcomp>cS�|dS�Nrr��xrrr�<lambda>M�z%compute_cost_bounds.<locals>.<lambda>)�keycSrErFrrGrrrrINrJT)rK�reverse�is_worst�boolr8rcSszd}d}|D]4\}}|r|j|jd|}n|jr|jdn|j}||jd|}||7}|dkr6|}q||9}q|S)Nr9�@r0r,)rr
r)Z62order_listrM�63total_cost�running_sizeZoutput_sizerCZ	step_cost�	best_baserrr�
simulate_costPs64z*compute_cost_bounds.<locals>.simulate_cost)rMFN)rMrNr8r)�sorted)r�outputsZ65best_orderZworst_orderrS�worst�bestrrr�compute_cost_boundsCs	66rX��?�tablerQcCsRtd|d�}|j|jd|}|jr|jdn|j}||jd|}||fS)zGWorst/best cost bounds for a single table step, given additive penalty.r9rYrOr0r,)r=rr
r)rZrQ�penaltyrVrRrWrrr�compute_step_cost_boundsjs67r\�chosen_order�	List[int]cs�t|���dkr68dSdd�|D���fdd�|D�}t���tt���}d��fd69d�}||�}||�}|dkr:dSd||}ttd
td|���S)u70    Score the join ordering quality using log-normalised rank mismatch.71 72    Optimal order: join tables with smallest (true_rows × selectivity) first,73    because this minimises intermediate result sizes throughout the sequence.74    Joining a large-output table early compounds the cost of every subsequent75    join — this is the core of real query optimisation difficulty.76 77    Method:78      1. Compute each table's output size = true_rows × selectivity79      2. For each position k, compare the log-output of the chosen k-th table80         against the log-output of the k-th optimal table (sorted ascending)81      3. Normalise mismatch against the worst possible ordering (descending)82 83    Returns 1.0 for a perfectly sorted order, 0.0 for reverse optimal.84    r#rYcSsg|]}|j|j�qSrr@rArrrrD�sz)compute_order_quality.<locals>.<listcomp>csg|]}�|�qSrr)rB�i)rUrrrD�s�
order_outputs�List[float]r8rcst��fdd�t��D��S)Nc3s0�|]}tt��|�t��|��VqdS�N)�absr;�log1p)rB�k)�optimal_outputsr`rr�	<genexpr>�s85��86�z>compute_order_quality.<locals>.log_mismatch.<locals>.<genexpr>)�sum�range�r`)�nrfrjr�log_mismatch�s�z+compute_order_quality.<locals>.log_mismatchg��&�.>r9N)r`rar8r)�lenrT�list�reversedrr=�min)r]rZchosen_outputsZ
worst_outputsrlZactual_mismatchZmax_mismatch�scorer)rkrfrUr�compute_order_qualityysrr�87final_cost�Optional[List[int]]cCs�t|�\}}||kr
d}nttdtd||||���}|dur/t|�t|�kr/t||�}n|}d|d|}ttdtd|���S)u+88    Grade a completed episode.  Combined score = 60% method + 40% order.89 90    method_score: how well the agent chose join algorithms and index usage.91      = (worst_cost - actual_cost) / (worst_cost - best_cost)92      worst = all nested-loop, no indexes93      best  = all merge-sort, use index when available94 95    order_score: how well the agent sequenced the joins.96      Optimal = smallest-output tables first (minimises intermediate blowup).97      Computed via log-normalised rank mismatch vs optimal ordering.98      Only applied when all tables have been joined (full episode).99 100    Combined = 0.60 × method_score + 0.40 × order_score101 102    This ensures:103      - A model that always picks merge_sort but joins biggest tables first104        scores ~0.60 instead of ~1.00 on hard tasks.105      - A model with correct ordering but mediocre method choices still106        receives partial credit from the order component.107      - Difficulty genuinely increases: easy (3 tables, 6 orderings) vs108        hard (7 tables, 5040 orderings) differentiates planning depth.109    rYr9Ng333333�?r')rXrr=rprmrr)rrsr]rVrWZmethod_scoreZorder_score�combinedrrr�grade�s rv)rr110rrr6r7r8r111)rrr8r?)rY)rZrrQrr8r?)r]r^rrr8rrb)rrrsrr]rtr8r)r�112__future__rr;�random�dataclassesrr�typingrrrrrr&rr-r1r5r>rXr\rrrvrrrr�<module>s	�������2�������>��113�����O����114,115'1161�