Spaces:
Sleeping
Sleeping
Upload environment.py
Browse files- server/environment.py +38 -0
server/environment.py
CHANGED
|
@@ -214,8 +214,46 @@ class SQLQualityEnvironment:
|
|
| 214 |
Returns a result preview in the observation. Does NOT end the episode.
|
| 215 |
Awards small step_reward (0.05) for queries that target the right area.
|
| 216 |
"""
|
|
|
|
|
|
|
| 217 |
sql = (action.sql or "").strip()
|
| 218 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 219 |
# Split multiple statements by semicolon, but preserve semicolons in strings
|
| 220 |
statements = []
|
| 221 |
current = ""
|
|
|
|
| 214 |
Returns a result preview in the observation. Does NOT end the episode.
|
| 215 |
Awards small step_reward (0.05) for queries that target the right area.
|
| 216 |
"""
|
| 217 |
+
import re
|
| 218 |
+
|
| 219 |
sql = (action.sql or "").strip()
|
| 220 |
|
| 221 |
+
# Strip comments and leading whitespace to get the first keyword
|
| 222 |
+
sql_stripped = re.sub(r"--[^\n]*", "", sql) # remove single-line comments
|
| 223 |
+
sql_stripped = re.sub(r"/\*.*?\*/", "", sql_stripped, flags=re.DOTALL) # block comments
|
| 224 |
+
first_keyword = sql_stripped.strip().split()[0].upper() if sql_stripped.strip() else ""
|
| 225 |
+
|
| 226 |
+
ALLOWED_READ_KEYWORDS = {"SELECT", "WITH", "EXPLAIN", "PRAGMA"}
|
| 227 |
+
if first_keyword not in ALLOWED_READ_KEYWORDS:
|
| 228 |
+
rejection_feedback = (
|
| 229 |
+
f"QUERY action only accepts read-only SQL. "
|
| 230 |
+
f"You submitted a '{first_keyword}' statement. "
|
| 231 |
+
f"To modify data, use the FIX action (action_type: 'fix'). "
|
| 232 |
+
f"To find the answer, use action_type: 'submit'."
|
| 233 |
+
)
|
| 234 |
+
# Add to trajectory as a step but with zero reward and clear feedback
|
| 235 |
+
self._trajectory.append(
|
| 236 |
+
TrajectoryStep(
|
| 237 |
+
step_number=len(self._trajectory) + 1,
|
| 238 |
+
action_type=ActionType.QUERY,
|
| 239 |
+
sql_submitted=sql[:200],
|
| 240 |
+
result_preview=rejection_feedback,
|
| 241 |
+
step_reward=0.0,
|
| 242 |
+
)
|
| 243 |
+
)
|
| 244 |
+
self._step_count = getattr(self, "_step_count", 0) + 1
|
| 245 |
+
obs = self._build_observation(result_preview=rejection_feedback)
|
| 246 |
+
return StepResponse(
|
| 247 |
+
observation=obs,
|
| 248 |
+
reward=SQLReward(
|
| 249 |
+
score=self._cumulative_reward,
|
| 250 |
+
step_reward=0.0,
|
| 251 |
+
feedback=rejection_feedback,
|
| 252 |
+
),
|
| 253 |
+
done=False,
|
| 254 |
+
info={"action_type": "query", "error": "non_select_rejected", "step": len(self._trajectory)},
|
| 255 |
+
)
|
| 256 |
+
|
| 257 |
# Split multiple statements by semicolon, but preserve semicolons in strings
|
| 258 |
statements = []
|
| 259 |
current = ""
|