Kalletlamadhav/sql-optimization-env
๐๏ธ SQL Query Optimization โ RL Training Environment
Meta PyTorch OpenEnv Hackathon ร SST 2026 Author: Kalletla Madhav OpenEnv-compliant ยท SQLite ยท Indian Data Domains ยท Curriculum Learning
Overview
A containerised FastAPI service that exposes the OpenEnv standard API (/reset, /step, /state) for training LLM agents to optimise slow SQL queries.
The environment actually executes queries on a real SQLite database populated with synthetic Indian enterprise data (GST, PDS/Ration, IRCTC, MGNREGA) and grades agents on real wall-clock speedup โ no simulations, no fake timing.
Quick Start
# Clone and build
git clone https://huggingface.co/spaces/Kalletlamadhav/sql-optimization-env
cd sql-optimization-env
# Generate the benchmark database
python data/seed_database.py --rows 100000
# Start the server
uvicorn server.main:app --host 0.0.0.0 --port 7860
# Run the baseline inference script
export OPENAI_API_KEY=sk-...
export MODEL_NAME=gpt-4o-mini
python inference.pyOr with Docker:
docker build -t sql-opt-env .
docker run -p 7860:7860 -e OPENAI_API_KEY=sk-... sql-opt-envAPI Reference
POST /reset
Start a new episode. Optionally pin to a specific task.
{ "task_id": "gst_missing_index" }POST /step
Submit an optimised query.
{
"optimized_query": "SELECT invoice_id, invoice_date FROM gst_invoice_records WHERE gstin_supplier = '27AABCU9603R1ZX' AND invoice_date >= '2025-01-01'",
"identified_pattern": "MISSING_INDEX",
"explanation": "The query does a full table scan because gstin_supplier has no index.",
"index_statements": [
"CREATE INDEX idx_gst_supplier_date ON gst_invoice_records(gstin_supplier, invoice_date DESC)"
],
"schema_analysis": "Table has 100k rows; gstin_supplier is high-selectivity but unindexed."
}GET /state
Returns full environment state including curriculum level and episode history.
Observation Space
Action Space
Anti-pattern enum values: N_PLUS_ONE, CARTESIAN_PRODUCT, MISSING_INDEX, SELECT_STAR, LEADING_WILDCARD, IMPLICIT_CAST, UNBOUNDED_AGGREGATION, NONE
Reward Function
Penalties: syntax error (โ0.30), wrong results (โ0.20), slower query (โ0.10), timeout (โ0.05/s), reward hacking (โ0.40).
Tasks
Curriculum Learning
The environment automatically adjusts difficulty:
- Graduate up after 3 consecutive episodes with score โฅ 0.70
- Step down after 2 consecutive episodes with score < 0.40
- State persisted to
/tmp/curriculum_state.jsonacross restarts
Baseline Scores
Scores from gpt-4o-mini running inference.py (SEED=42):
Run `python inference.py` to reproduce.
Data Domains
All data is synthetic, seeded with SEED=42 for full reproducibility.
Project Structure
sql-optimization-env/
โโโ openenv.yaml
โโโ Dockerfile
โโโ inference.py
โโโ requirements.txt
โโโ server/
โ โโโ main.py
โ โโโ environment.py
โ โโโ models.py
โ โโโ reward.py
โ โโโ hack_detector.py
โ โโโ graders/
โโโ tasks/
โ โโโ easy/ (3 tasks)
โ โโโ medium/ (3 tasks)
โ โโโ hard/ (3 tasks)
โโโ data/
โ โโโ schemas/
โ โโโ fixtures/benchmark_seed42.db
โโโ curriculum/Validation
pip install openenv-core
openenv validateLicense
MIT โ built for Meta PyTorch OpenEnv Hackathon 2026.
