-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy patheval_sql_optim.py
More file actions
403 lines (319 loc) · 14.5 KB
/
Copy patheval_sql_optim.py
File metadata and controls
403 lines (319 loc) · 14.5 KB
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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
"""
Evaluation Function for SQL Query Optimization Task
Evaluates the performance and correctness of SQL query transformations.
Measures execution time improvement while ensuring result correctness.
"""
import time
import hashlib
import sqlite3
from typing import Tuple, Optional, Callable, List, Any, Dict
from statistics import median
from tqdm import tqdm
try:
from .fake_database import get_connection, get_schema_info
from .test_queries import get_test_queries
except ImportError:
from fake_database import get_connection, get_schema_info
from test_queries import get_test_queries
# =============================================================================
# Configuration
# =============================================================================
# Number of warmup runs before timing (for both original and transformed)
WARMUP_RUNS = 1
# Number of timed runs for median calculation
TIMED_RUNS = 7
# Maximum time (seconds) for a single query execution
QUERY_TIMEOUT = 10.0
# Minimum speedup to get positive reward
MIN_SPEEDUP_THRESHOLD = 1.0
# Penalty for failed/incorrect queries (harsh penalty to discourage failures)
# A failed query contributes this negative value to total_speedup
FAILURE_PENALTY = -1.0
# =============================================================================
# Query Execution
# =============================================================================
def normalize_results(results: List[Tuple]) -> str:
"""
Normalize query results for comparison.
Sorts rows and converts to a canonical string representation.
"""
if not results:
return "EMPTY"
# Sort rows (convert each row to tuple for sorting)
sorted_results = sorted([tuple(str(x) for x in row) for row in results])
# Create hash of results for comparison
result_str = str(sorted_results)
return hashlib.md5(result_str.encode()).hexdigest()
def execute_query_with_timeout(conn: sqlite3.Connection, sql: str,
timeout: float = QUERY_TIMEOUT) -> Tuple[List[Tuple], float, Optional[str]]:
"""
Execute a query with timeout and return results, execution time, and error.
Args:
conn: Database connection
sql: SQL query to execute
timeout: Maximum execution time in seconds
Returns:
Tuple of (results, execution_time, error_message)
- results: List of result tuples (empty if error)
- execution_time: Time in seconds (-1 if error)
- error_message: Error string or None
"""
try:
# Set timeout (SQLite doesn't have native timeout, so we use progress handler)
start_time = time.time()
def progress_handler():
if time.time() - start_time > timeout:
return 1 # Non-zero to interrupt
return 0
conn.set_progress_handler(progress_handler, 100) # Check every 100 opcodes
cursor = conn.cursor()
t1 = time.time()
cursor.execute(sql)
results = cursor.fetchall()
t2 = time.time()
conn.set_progress_handler(None, 0)
return results, t2 - t1, None
except sqlite3.OperationalError as e:
if "interrupted" in str(e).lower():
return [], -1, f"Query timeout after {timeout}s"
return [], -1, f"SQL Error: {str(e)}"
except Exception as e:
return [], -1, f"Error: {str(e)}"
def measure_query_time(conn: sqlite3.Connection, sql: str,
warmup: int = WARMUP_RUNS,
runs: int = TIMED_RUNS) -> Tuple[float, Optional[str]]:
"""
Measure median execution time of a query.
Args:
conn: Database connection
sql: SQL query to execute
warmup: Number of warmup runs
runs: Number of timed runs
Returns:
Tuple of (median_time, error_message)
"""
# Warmup runs
for _ in range(warmup):
_, exec_time, error = execute_query_with_timeout(conn, sql)
if error:
return -1, error
# Timed runs
times = []
for _ in range(runs):
_, exec_time, error = execute_query_with_timeout(conn, sql)
if error:
return -1, error
times.append(exec_time)
return median(times), None
# =============================================================================
# Transformer Evaluation
# =============================================================================
def evaluate_transformer(transformer: Callable[[str], str]) -> Tuple[float, Dict[str, Any]]:
"""
Evaluate a query transformer function.
The transformer should take an SQL query string and return an optimized
SQL query string that produces identical results.
Args:
transformer: Function that takes SQL string and returns optimized SQL
Returns:
Tuple of (reward, details_dict)
- reward: Average speedup (original_time / transformed_time) across successful queries
- details_dict: Per-query results and statistics
"""
conn = get_connection(readonly=True)
test_queries = get_test_queries()
details = {
"queries": {},
"successful": 0,
"failed": 0,
"incorrect": 0,
"total_speedup": 0.0,
}
for query_id, original_sql, description, hint in tqdm(test_queries, desc="Evaluating", unit="query"):
query_result = {
"description": description,
"original_sql": original_sql.strip(),
"transformed_sql": None,
"original_time": None,
"transformed_time": None,
"speedup": None,
"reward": 0.0,
"status": "pending",
"error": None,
}
try:
# Get transformed query
transformed_sql = transformer(original_sql)
query_result["transformed_sql"] = transformed_sql.strip() if transformed_sql else None
if not transformed_sql or not transformed_sql.strip():
query_result["status"] = "failed"
query_result["error"] = "Transformer returned empty query"
details["failed"] += 1
details["queries"][query_id] = query_result
continue
# Warm up BOTH queries first to ensure fair comparison
# (SQLite caches query plans and data pages)
for _ in range(WARMUP_RUNS):
execute_query_with_timeout(conn, original_sql)
execute_query_with_timeout(conn, transformed_sql)
# Measure original query (multiple runs, take median)
original_times = []
original_results = None
for _ in range(TIMED_RUNS):
results, exec_time, error = execute_query_with_timeout(conn, original_sql)
if error:
query_result["status"] = "failed"
query_result["error"] = f"Original query error: {error}"
details["failed"] += 1
break
original_results = results
original_times.append(exec_time)
else:
original_time = median(original_times)
query_result["original_time"] = original_time
original_hash = normalize_results(original_results)
if query_result["status"] == "failed":
details["queries"][query_id] = query_result
continue
# Measure transformed query (multiple runs, take median)
transformed_times = []
transformed_results = None
for _ in range(TIMED_RUNS):
results, exec_time, error = execute_query_with_timeout(conn, transformed_sql)
if error:
query_result["status"] = "failed"
query_result["error"] = f"Transformed query error: {error}"
details["failed"] += 1
break
transformed_results = results
transformed_times.append(exec_time)
else:
transformed_time = median(transformed_times)
query_result["transformed_time"] = transformed_time
transformed_hash = normalize_results(transformed_results)
if query_result["status"] == "failed":
details["queries"][query_id] = query_result
continue
# Check correctness
if original_hash != transformed_hash:
query_result["status"] = "incorrect"
query_result["error"] = f"Results mismatch: original {len(original_results)} rows, transformed {len(transformed_results)} rows"
details["incorrect"] += 1
details["queries"][query_id] = query_result
continue
# Calculate speedup
if transformed_time > 0 and original_time > 0:
speedup = original_time / transformed_time
query_result["speedup"] = speedup
details["total_speedup"] += speedup
else:
query_result["speedup"] = 1.0
details["total_speedup"] += 1.0
query_result["status"] = "success"
details["successful"] += 1
except Exception as e:
query_result["status"] = "failed"
query_result["error"] = f"Exception: {str(e)}"
details["failed"] += 1
details["queries"][query_id] = query_result
conn.close()
# Add summary statistics
details["num_queries"] = len(test_queries)
if details["successful"] > 0:
details["avg_speedup"] = details["total_speedup"] / details["successful"]
else:
details["avg_speedup"] = 0.0
# Reward: average over ALL queries with harsh penalty for failures
# Failed/incorrect queries contribute FAILURE_PENALTY (negative) to hurt the score
num_failures = details["failed"] + details["incorrect"]
penalized_total = details["total_speedup"] + (num_failures * FAILURE_PENALTY)
total_reward = penalized_total / details["num_queries"] if details["num_queries"] > 0 else 0.0
total_reward = max(0.0, total_reward) # Clamp to non-negative
details["total_reward"] = total_reward
return total_reward, details
# =============================================================================
# Main Evaluation Entry Point
# =============================================================================
def get_reward(code: str,
verbose: bool = True) -> Tuple[float, Optional[str], str]:
"""
Evaluate a transformer from Python code.
The code must define a function called `transform_query` that takes
a SQL string and returns an optimized SQL string.
Args:
code: Python code string containing transform_query function
verbose: Whether to print progress
Returns:
Tuple of (reward, error_message, details_string)
- details_string: formatted string with per-query results
"""
try:
# Execute the code and extract the transform function
namespace = {}
exec(code, namespace)
# Get the transform function
if 'transform_query' not in namespace:
return 0.0, "Code must define 'transform_query' function", ""
transformer = namespace['transform_query']
# Verify it's callable
if not callable(transformer):
return 0.0, "'transform_query' must be a callable function", ""
if verbose:
print("Evaluating query transformer...")
reward, raw_details = evaluate_transformer(transformer)
# Build details string
lines = []
lines.append(f"Summary: {raw_details['successful']}/{raw_details['num_queries']} successful, "
f"{raw_details['failed']} failed, {raw_details['incorrect']} incorrect, "
f"avg_speedup={raw_details['avg_speedup']:.2f}x")
lines.append("")
lines.append("Per-query results:")
for query_id, query_result in raw_details.get("queries", {}).items():
success = query_result["status"] == "success"
speedup = query_result.get("speedup")
error = query_result.get("error")
original_sql = query_result.get("original_sql", "")
# Clean up the SQL for display (normalize whitespace)
sql_clean = " ".join(original_sql.split())
status_symbol = "✓" if success else "✗"
if speedup is not None:
lines.append(f" {status_symbol} {query_id}: speedup={speedup:.2f}x")
lines.append(f" SQL: {sql_clean}")
elif error:
lines.append(f" {status_symbol} {query_id}: error={error}")
lines.append(f" SQL: {sql_clean}")
else:
lines.append(f" {status_symbol} {query_id}")
lines.append(f" SQL: {sql_clean}")
details_string = "\n".join(lines)
if verbose:
print(f"\nResults:")
print(f" Successful: {raw_details['successful']}/{raw_details['num_queries']}")
print(f" Failed: {raw_details['failed']}")
print(f" Incorrect: {raw_details['incorrect']}")
print(f" Avg Speedup: {raw_details['avg_speedup']:.2f}x")
print(f" Total Reward: {reward:.2f}")
return reward, "", details_string
except Exception as e:
error_msg = f"Failed to evaluate transformer: {str(e)}"
if verbose:
print(error_msg)
return 0.0, error_msg, ""
# =============================================================================
# Test
# =============================================================================
if __name__ == "__main__":
import os
# Get the path to initial_code.py (baseline transformer)
script_dir = os.path.dirname(os.path.abspath(__file__))
#initial_code_path = os.path.join(script_dir, "initial_code.py")
initial_code_path = os.path.join(script_dir, "optimized_code.py")
print(f"Testing evaluation with baseline transformer: {initial_code_path}")
with open(initial_code_path) as f:
code = f.read()
reward, error, details = get_reward(code, verbose=True)
if error:
print(f"\nError: {error}")
else:
print("\n\nDetails:")
print(details)