Spaces:
Sleeping
Sleeping
File size: 13,206 Bytes
210535c 429a3ac 210535c 429a3ac 210535c 429a3ac 210535c 429a3ac 210535c | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 | """
Task definitions and deterministic graders for the SQL Query Optimizer environment.
Each task returns a TaskDef with:
- id, name, difficulty
- query: the broken/unoptimised SQL the agent must fix
- schema_context: relevant DDL
- description: what the agent must accomplish
- grader(rewritten_query) -> GraderResult(score, breakdown, feedback)
"""
from __future__ import annotations
import re
import dataclasses
from typing import Callable, Dict, Optional
_MIN_SCORE_EPS = 0.001
_MAX_SCORE_EPS = 0.999
def _strict_open_score(value: float) -> float:
return round(min(max(float(value), _MIN_SCORE_EPS), _MAX_SCORE_EPS), 3)
@dataclasses.dataclass
class GraderResult:
score: float # 0.0 β 1.0
correctness: float = 0.0
performance: float = 0.0
style: float = 0.0
feedback: str = ""
@dataclasses.dataclass
class TaskDef:
id: int
name: str
difficulty: str # easy | medium | hard
description: str
query: str
schema_context: str
hint: Optional[str]
max_steps: int
grader: Callable[[str], GraderResult]
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# Helpers
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
def _normalise(sql: str) -> str:
"""Lower-case, collapse whitespace."""
return re.sub(r"\s+", " ", sql.lower().strip())
def _has(sql: str, *patterns: str) -> bool:
s = _normalise(sql)
return all(p in s for p in patterns)
def _missing(sql: str, *patterns: str) -> bool:
s = _normalise(sql)
return any(p not in s for p in patterns)
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# Task 1 β Easy: Fix a broken JOIN (missing ON clause / wrong join type)
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
_T1_SCHEMA = """
CREATE TABLE orders (
order_id INT PRIMARY KEY,
customer_id INT NOT NULL,
total DECIMAL(10,2),
created_at TIMESTAMP
);
CREATE TABLE customers (
customer_id INT PRIMARY KEY,
name VARCHAR(255),
email VARCHAR(255)
);
"""
_T1_QUERY = """
SELECT o.order_id, c.name, o.total
FROM orders o, customers c
WHERE o.total > 100;
"""
_T1_DESC = (
"The query uses an implicit cross-join (comma syntax) between `orders` and "
"`customers` but never links the two tables. Rewrite it with an explicit "
"INNER JOIN β¦ ON o.customer_id = c.customer_id, keeping the WHERE filter."
)
def _grade_task1(rewritten: str) -> GraderResult:
s = _normalise(rewritten)
fb: list[str] = []
correctness = 0.0
performance = 0.0
style = 0.0
# Correctness: must have explicit JOIN with the correct ON key
if "inner join" in s or ("join" in s and "cross join" not in s):
if "on" in s and "customer_id" in s:
correctness = 1.0
else:
correctness = 0.4
fb.append("JOIN present but ON clause with customer_id is missing.")
else:
fb.append("Still uses implicit cross-join or missing JOIN keyword.")
# Correctness: must still filter total > 100
if "total > 100" in s or "total>100" in s:
correctness = min(correctness + 0.0, correctness) # already captured
else:
correctness = max(correctness - 0.3, 0.0)
fb.append("WHERE o.total > 100 filter has been removed.")
# Performance: explicit join is better than implicit cross join
performance = 1.0 if correctness >= 0.8 else 0.3
# Style: uses table aliases
style = 0.5
if re.search(r"\bo\b", s) and re.search(r"\bc\b", s):
style = 1.0
elif "select *" not in s:
style = 0.7
score = round(correctness * 0.6 + performance * 0.25 + style * 0.15, 3)
feedback = " ".join(fb) if fb else "Correct! The JOIN is properly formed."
return GraderResult(
score=_strict_open_score(score),
correctness=correctness,
performance=performance,
style=style,
feedback=feedback,
)
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# Task 2 β Medium: Eliminate N+1 correlated subquery
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
_T2_SCHEMA = """
CREATE TABLE employees (
emp_id INT PRIMARY KEY,
name VARCHAR(255),
dept_id INT,
salary DECIMAL(10,2)
);
CREATE TABLE departments (
dept_id INT PRIMARY KEY,
dept_name VARCHAR(255),
budget DECIMAL(12,2)
);
"""
_T2_QUERY = """
SELECT e.name,
(SELECT d.dept_name
FROM departments d
WHERE d.dept_id = e.dept_id) AS dept_name
FROM employees e
WHERE e.salary > 50000;
"""
_T2_DESC = (
"The query uses a correlated scalar subquery in the SELECT list that fires "
"once per row (N+1 problem). Collapse it into a single LEFT JOIN β¦ ON "
"e.dept_id = d.dept_id, keeping the salary filter."
)
def _grade_task2(rewritten: str) -> GraderResult:
s = _normalise(rewritten)
fb: list[str] = []
correctness = 0.0
performance = 0.0
style = 0.0
# Correctness: correlated subquery in SELECT must be gone
has_correlated = bool(
re.search(r"select\s+.*\(\s*select", s)
or re.search(r"\(\s*select\b.*\bwhere\b.*=\s*e\.", s)
)
if has_correlated:
fb.append("Correlated subquery still present in SELECT list.")
correctness = 0.1
else:
correctness = 0.5
# Correctness: must join on dept_id
if "join" in s and "dept_id" in s and "on" in s:
correctness = min(correctness + 0.5, 1.0)
else:
fb.append("Missing JOIN departments ON dept_id.")
correctness = max(correctness - 0.1, 0.0)
# Correctness: salary filter preserved
if "salary" not in s or ("salary > 50000" not in s and "salary>50000" not in s):
correctness = max(correctness - 0.2, 0.0)
fb.append("salary > 50000 filter is missing or incorrect.")
# Performance: single pass vs N+1
performance = 1.0 if not has_correlated and "join" in s else 0.2
# Style: uses aliases, selects explicit columns
style = 0.5
if "select *" not in s:
style += 0.25
if re.search(r"\be\b|\bd\b", s):
style += 0.25
score = round(correctness * 0.55 + performance * 0.30 + style * 0.15, 3)
feedback = " ".join(fb) if fb else "Excellent! N+1 eliminated with a clean JOIN."
return GraderResult(
score=_strict_open_score(score),
correctness=correctness,
performance=performance,
style=style,
feedback=feedback,
)
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# Task 3 β Hard: Full optimisation (4 independent issues)
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
_T3_SCHEMA = """
CREATE TABLE products (
product_id INT PRIMARY KEY,
name VARCHAR(255),
category VARCHAR(100),
price DECIMAL(10,2),
stock INT
);
CREATE TABLE order_items (
item_id INT PRIMARY KEY,
order_id INT,
product_id INT,
quantity INT,
unit_price DECIMAL(10,2)
);
"""
_T3_QUERY = """
SELECT DISTINCT *
FROM products p
JOIN order_items oi ON p.product_id = oi.product_id
WHERE CAST(p.price AS VARCHAR) LIKE '1%'
AND p.category = 'Electronics'
ORDER BY p.name;
"""
_T3_DESC = (
"The query has four problems: "
"(1) DISTINCT is redundant because product_id is PK and the JOIN is 1-to-many β remove it. "
"(2) SELECT * should list only needed columns: p.name, p.category, p.price, oi.quantity, oi.unit_price. "
"(3) CAST(p.price AS VARCHAR) LIKE '1%' prevents index use β rewrite as p.price >= 100 AND p.price < 200. "
"(4) Add a comment hinting an index on (category, price) would help."
)
def _grade_task3(rewritten: str) -> GraderResult:
s = _normalise(rewritten)
fb: list[str] = []
sub_scores: Dict[str, float] = {}
# Sub-criterion 1: DISTINCT removed (0.25)
if "distinct" not in s:
sub_scores["no_distinct"] = 0.25
else:
sub_scores["no_distinct"] = 0.0
fb.append("DISTINCT still present β it's redundant here.")
# Sub-criterion 2: SELECT * replaced with explicit columns (0.25)
if "select *" not in s and all(
col in s for col in ("p.name", "p.price", "oi.quantity")
):
sub_scores["explicit_columns"] = 0.25
elif "select *" not in s:
sub_scores["explicit_columns"] = 0.15
fb.append("SELECT * removed but explicit column list is incomplete.")
else:
sub_scores["explicit_columns"] = 0.0
fb.append("SELECT * still used β list explicit columns.")
# Sub-criterion 3: CASTβ¦LIKE replaced with range predicate (0.25)
cast_gone = "cast(" not in s and "cast (" not in s
has_price_range = (
("price >= 100" in s or "price>=100" in s)
and ("price < 200" in s or "price<200" in s)
)
if cast_gone and has_price_range:
sub_scores["sargable"] = 0.25
elif cast_gone:
sub_scores["sargable"] = 0.12
fb.append("CAST removed but price range predicate (>= 100 AND < 200) is missing.")
else:
sub_scores["sargable"] = 0.0
fb.append("CAST(price AS VARCHAR) LIKE β¦ still present β non-sargable predicate.")
# Sub-criterion 4: index hint comment present (0.25)
raw = rewritten.lower()
if "index" in raw and ("category" in raw or "price" in raw):
sub_scores["index_hint"] = 0.25
else:
sub_scores["index_hint"] = 0.0
fb.append("Missing comment / hint about adding an index on (category, price).")
total = sum(sub_scores.values())
correctness = min(sub_scores["no_distinct"] + sub_scores["explicit_columns"], 0.5) * 2
performance = min(sub_scores["sargable"] + sub_scores["index_hint"], 0.5) * 2
style = 1.0 if "select *" not in s else 0.0
feedback = " ".join(fb) if fb else "Perfect optimisation across all four dimensions!"
return GraderResult(
score=_strict_open_score(total),
correctness=round(correctness, 3),
performance=round(performance, 3),
style=round(style, 3),
feedback=feedback,
)
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# Registry
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
TASKS: Dict[int, TaskDef] = {
1: TaskDef(
id=1,
name="fix-broken-join",
difficulty="easy",
description=_T1_DESC,
query=_T1_QUERY.strip(),
schema_context=_T1_SCHEMA.strip(),
hint="Replace the comma-separated FROM list with an explicit INNER JOIN β¦ ON.",
max_steps=3,
grader=_grade_task1,
),
2: TaskDef(
id=2,
name="eliminate-n-plus-one",
difficulty="medium",
description=_T2_DESC,
query=_T2_QUERY.strip(),
schema_context=_T2_SCHEMA.strip(),
hint="Move the subquery out of the SELECT list and into a LEFT JOIN.",
max_steps=4,
grader=_grade_task2,
),
3: TaskDef(
id=3,
name="full-optimization",
difficulty="hard",
description=_T3_DESC,
query=_T3_QUERY.strip(),
schema_context=_T3_SCHEMA.strip(),
hint=None,
max_steps=5,
grader=_grade_task3,
),
}
def get_task(task_id: int) -> TaskDef:
if task_id not in TASKS:
raise ValueError(f"Unknown task_id {task_id}. Valid: {list(TASKS.keys())}")
return TASKS[task_id]
|