""" Task definitions for the SQL Debug Environment. Each task has: - task_id / name / difficulty — the obvious stuff - schema_description — what we tell the agent about the tables - broken_query — the bugged query the agent needs to fix - correct_query — what it should produce (used by the grader) - hint_schedule — step-based hints if the agent is stuck Adding a new task is just adding another dict to TASKS. """ from __future__ import annotations TASKS: dict[str, dict] = { # ─── Task 1: Easy — syntax errors ──────────────────────────────── # # Classic case of fat-fingered SQL keywords. SELEKT, WHER, ORDDER... # An LLM should spot these immediately, but it's a good warmup. "fix_syntax": { "task_id": "fix_syntax", "name": "Fix SQL Syntax Errors", "description": ( "The query below has several syntax errors — misspelled keywords, " "wrong punctuation, messed up clause ordering. Fix it so it runs " "and gives the right results." ), "difficulty": "easy", "schema_description": ( "Tables:\n" " employees(id INTEGER PK, name TEXT, department TEXT, salary REAL, hire_date TEXT)\n" " departments(id INTEGER PK, name TEXT, budget REAL, manager_id INTEGER)\n" "\n" "The 'department' column in employees stores the department name (not the id).\n" "Departments: Engineering, Marketing, Sales, Human Resources, Finance, Product." ), "broken_query": ( "SELEKT name, salary FORM employees " "WHER department = 'Engineering' " "ORDDER BY salary DESC" ), "correct_query": ( "SELECT name, salary FROM employees " "WHERE department = 'Engineering' " "ORDER BY salary DESC" ), "hint_schedule": { 3: "Look at the SQL keywords — some of them are misspelled.", 5: "The keywords should be SELECT, FROM, WHERE, ORDER BY. Just fix the typos.", }, }, # ─── Task 2: Medium — wrong JOIN key ───────────────────────────── # # This one's trickier because the query actually runs without errors. # It just joins on the wrong column (order_id instead of user_id), # so you get garbage results and no error message to help you. "fix_join": { "task_id": "fix_join", "name": "Fix Wrong JOIN Logic", "description": ( "This query runs fine — no errors at all — but the results are wrong. " "The JOIN condition is matching on the wrong column. Figure out which " "JOIN is broken and fix the key." ), "difficulty": "medium", "schema_description": ( "Tables:\n" " users(user_id INTEGER PK, name TEXT, email TEXT, region TEXT)\n" " orders(order_id INTEGER PK, user_id INTEGER FK→users, created_at TEXT, status TEXT)\n" " order_items(item_id INTEGER PK, order_id INTEGER FK→orders, product_id INTEGER, quantity INTEGER, price REAL)\n" "\n" "users → orders via users.user_id = orders.user_id\n" "orders → order_items via orders.order_id = order_items.order_id" ), "broken_query": ( "SELECT u.name, COUNT(o.order_id) as order_count, " "SUM(oi.price * oi.quantity) as total_spent\n" "FROM users u\n" "JOIN orders o ON u.user_id = o.order_id\n" # <- the bug "JOIN order_items oi ON oi.order_id = o.order_id\n" "GROUP BY u.user_id, u.name\n" "ORDER BY total_spent DESC" ), "correct_query": ( "SELECT u.name, COUNT(o.order_id) as order_count, " "SUM(oi.price * oi.quantity) as total_spent\n" "FROM users u\n" "JOIN orders o ON u.user_id = o.user_id\n" "JOIN order_items oi ON oi.order_id = o.order_id\n" "GROUP BY u.user_id, u.name\n" "ORDER BY total_spent DESC" ), "hint_schedule": { 3: "Look at the first JOIN condition. What column in orders should reference users?", 5: "The JOIN should be ON u.user_id = o.user_id — not o.order_id.", }, }, # ─── Task 3: Hard — correlated subquery optimization ───────────── # # Correct results, terrible performance. The query uses correlated # subqueries that run once per user row. On a big table this would # be devastating. The agent needs to refactor into JOINs or a CTE. "optimize_query": { "task_id": "optimize_query", "name": "Optimize Correlated Subquery", "description": ( "This query gives the right answer but it's doing it the slow way — " "correlated subqueries that run for every single row. Rewrite it " "using JOINs, CTEs, or window functions. Output must stay identical." ), "difficulty": "hard", "schema_description": ( "Tables:\n" " users(user_id INTEGER PK, name TEXT, email TEXT, region TEXT)\n" " orders(order_id INTEGER PK, user_id INTEGER FK→users, created_at TEXT, status TEXT)\n" " order_items(item_id INTEGER PK, order_id INTEGER FK→orders, product_id INTEGER FK→products, quantity INTEGER, price REAL)\n" " products(product_id INTEGER PK, name TEXT, category TEXT, unit_price REAL)\n" "\n" "Indexes on: orders(user_id), order_items(order_id).\n" "Goal: eliminate the correlated subqueries." ), "broken_query": ( "SELECT \n" " u.user_id,\n" " u.name,\n" " (SELECT COUNT(*) FROM orders o WHERE o.user_id = u.user_id) as order_count,\n" " (SELECT SUM(oi.price * oi.quantity) \n" " FROM order_items oi \n" " JOIN orders o ON oi.order_id = o.order_id \n" " WHERE o.user_id = u.user_id) as total_spent\n" "FROM users u\n" "WHERE (SELECT COUNT(*) FROM orders o WHERE o.user_id = u.user_id) > 0\n" "ORDER BY total_spent DESC" ), "correct_query": ( "WITH order_stats AS (\n" " SELECT \n" " o.user_id,\n" " COUNT(DISTINCT o.order_id) as order_count,\n" " SUM(oi.price * oi.quantity) as total_spent\n" " FROM orders o\n" " JOIN order_items oi ON oi.order_id = o.order_id\n" " GROUP BY o.user_id\n" ")\n" "SELECT u.user_id, u.name, os.order_count, os.total_spent\n" "FROM users u\n" "JOIN order_stats os ON os.user_id = u.user_id\n" "ORDER BY total_spent DESC" ), "hint_schedule": { 2: "Try using a CTE (WITH clause) to pre-compute the aggregates before joining.", 4: "Build a single CTE that groups orders + order_items by user_id first.", 6: "Something like: WITH order_stats AS (SELECT o.user_id, COUNT(...), SUM(...) FROM orders o JOIN order_items oi ... GROUP BY o.user_id) then JOIN users to that.", 7: "Careful! Joining orders to order_items creates multiple rows per order. Use COUNT(DISTINCT o.order_id) to avoid overcounting.", }, }, }