File size: 7,637 Bytes
50bf486
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
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.",
        },
    },
}