Spaces:
Paused
Paused
Commit ·
e91a3e2
1
Parent(s): 1ed0f8d
Local Postgres defaults, longer query timeout, fix Q9 task SQL.
Browse filesUse 127.0.0.1:5433 in examples; default SQLSAGE_TIMEOUT_MS 120s for SF=1 TPC-H; remove obsolete compose version; correct Level 3 Q9 join order so reset() never picks invalid SQL.
Made-with: Cursor
- .env.example +3 -3
- MANUAL_NEXT_STEPS.md +2 -2
- README.md +20 -1
- docker-compose.yml +1 -3
- sqlsage/env.py +2 -1
- sqlsage/tasks/level3.py +4 -4
.env.example
CHANGED
|
@@ -1,7 +1,7 @@
|
|
| 1 |
-
POSTGRES_HOST=
|
| 2 |
-
POSTGRES_PORT=
|
| 3 |
POSTGRES_USER=postgres
|
| 4 |
POSTGRES_PASSWORD=sqlsage
|
| 5 |
POSTGRES_DB=sqlsage
|
| 6 |
|
| 7 |
-
SQLSAGE_TIMEOUT_MS=
|
|
|
|
| 1 |
+
POSTGRES_HOST=127.0.0.1
|
| 2 |
+
POSTGRES_PORT=5433
|
| 3 |
POSTGRES_USER=postgres
|
| 4 |
POSTGRES_PASSWORD=sqlsage
|
| 5 |
POSTGRES_DB=sqlsage
|
| 6 |
|
| 7 |
+
SQLSAGE_TIMEOUT_MS=120000
|
MANUAL_NEXT_STEPS.md
CHANGED
|
@@ -17,13 +17,13 @@ Run these steps manually on your machine (no training is auto-run here).
|
|
| 17 |
## 2) PostgreSQL setup
|
| 18 |
|
| 19 |
Option A (single command):
|
| 20 |
-
- `docker run --name sqlsage-pg -e POSTGRES_PASSWORD=sqlsage -e POSTGRES_DB=sqlsage -p
|
| 21 |
|
| 22 |
Option B (repo helper):
|
| 23 |
- `docker compose up -d`
|
| 24 |
|
| 25 |
Then verify:
|
| 26 |
-
- `python scripts/check_db_connection.py`
|
| 27 |
|
| 28 |
## 3) Load TPC-H data
|
| 29 |
|
|
|
|
| 17 |
## 2) PostgreSQL setup
|
| 18 |
|
| 19 |
Option A (single command):
|
| 20 |
+
- `docker run --name sqlsage-pg -e POSTGRES_PASSWORD=sqlsage -e POSTGRES_DB=sqlsage -p 5433:5432 -d postgres:16`
|
| 21 |
|
| 22 |
Option B (repo helper):
|
| 23 |
- `docker compose up -d`
|
| 24 |
|
| 25 |
Then verify:
|
| 26 |
+
- `POSTGRES_HOST=127.0.0.1 POSTGRES_PORT=5433 python scripts/check_db_connection.py`
|
| 27 |
|
| 28 |
## 3) Load TPC-H data
|
| 29 |
|
README.md
CHANGED
|
@@ -12,6 +12,25 @@ license: mit
|
|
| 12 |
|
| 13 |
SQLSage is an OpenEnv-compatible RL environment for SQL query optimization via PostgreSQL execution-plan reading.
|
| 14 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
## Endpoints
|
| 16 |
|
| 17 |
- `GET /health`
|
|
@@ -22,7 +41,7 @@ SQLSage is an OpenEnv-compatible RL environment for SQL query optimization via P
|
|
| 22 |
## Local Run
|
| 23 |
|
| 24 |
```bash
|
| 25 |
-
uvicorn sqlsage.app:app --reload --port 8000
|
| 26 |
```
|
| 27 |
|
| 28 |
## Validation
|
|
|
|
| 12 |
|
| 13 |
SQLSage is an OpenEnv-compatible RL environment for SQL query optimization via PostgreSQL execution-plan reading.
|
| 14 |
|
| 15 |
+
## Database Mode
|
| 16 |
+
|
| 17 |
+
This project is configured for **local PostgreSQL via Docker**.
|
| 18 |
+
|
| 19 |
+
Use:
|
| 20 |
+
|
| 21 |
+
- `POSTGRES_HOST=127.0.0.1`
|
| 22 |
+
- `POSTGRES_PORT=5433`
|
| 23 |
+
- `POSTGRES_USER=postgres`
|
| 24 |
+
- `POSTGRES_PASSWORD=sqlsage`
|
| 25 |
+
- `POSTGRES_DB=sqlsage`
|
| 26 |
+
- `SQLSAGE_TIMEOUT_MS=120000` (SF=1 TPC-H can exceed 5s)
|
| 27 |
+
|
| 28 |
+
Start DB:
|
| 29 |
+
|
| 30 |
+
```bash
|
| 31 |
+
docker compose up -d
|
| 32 |
+
```
|
| 33 |
+
|
| 34 |
## Endpoints
|
| 35 |
|
| 36 |
- `GET /health`
|
|
|
|
| 41 |
## Local Run
|
| 42 |
|
| 43 |
```bash
|
| 44 |
+
POSTGRES_HOST=127.0.0.1 POSTGRES_PORT=5433 uvicorn sqlsage.app:app --reload --port 8000
|
| 45 |
```
|
| 46 |
|
| 47 |
## Validation
|
docker-compose.yml
CHANGED
|
@@ -1,5 +1,3 @@
|
|
| 1 |
-
version: "3.9"
|
| 2 |
-
|
| 3 |
services:
|
| 4 |
postgres:
|
| 5 |
image: postgres:16
|
|
@@ -8,7 +6,7 @@ services:
|
|
| 8 |
POSTGRES_PASSWORD: sqlsage
|
| 9 |
POSTGRES_DB: sqlsage
|
| 10 |
ports:
|
| 11 |
-
- "
|
| 12 |
volumes:
|
| 13 |
- sqlsage_pg_data:/var/lib/postgresql/data
|
| 14 |
healthcheck:
|
|
|
|
|
|
|
|
|
|
| 1 |
services:
|
| 2 |
postgres:
|
| 3 |
image: postgres:16
|
|
|
|
| 6 |
POSTGRES_PASSWORD: sqlsage
|
| 7 |
POSTGRES_DB: sqlsage
|
| 8 |
ports:
|
| 9 |
+
- "5433:5432"
|
| 10 |
volumes:
|
| 11 |
- sqlsage_pg_data:/var/lib/postgresql/data
|
| 12 |
healthcheck:
|
sqlsage/env.py
CHANGED
|
@@ -59,7 +59,8 @@ class SQLSageEnv:
|
|
| 59 |
resolved_user = db_user or os.getenv("POSTGRES_USER", "postgres")
|
| 60 |
resolved_password = db_password or os.getenv("POSTGRES_PASSWORD", "sqlsage")
|
| 61 |
resolved_db = db_name or os.getenv("POSTGRES_DB", "sqlsage")
|
| 62 |
-
|
|
|
|
| 63 |
|
| 64 |
self.conn: connection = psycopg2.connect(
|
| 65 |
host=resolved_host,
|
|
|
|
| 59 |
resolved_user = db_user or os.getenv("POSTGRES_USER", "postgres")
|
| 60 |
resolved_password = db_password or os.getenv("POSTGRES_PASSWORD", "sqlsage")
|
| 61 |
resolved_db = db_name or os.getenv("POSTGRES_DB", "sqlsage")
|
| 62 |
+
# SF=1 TPC-H queries can exceed 5s on modest hardware; override via SQLSAGE_TIMEOUT_MS.
|
| 63 |
+
resolved_timeout_ms = int(timeout_ms or os.getenv("SQLSAGE_TIMEOUT_MS", "120000"))
|
| 64 |
|
| 65 |
self.conn: connection = psycopg2.connect(
|
| 66 |
host=resolved_host,
|
sqlsage/tasks/level3.py
CHANGED
|
@@ -114,10 +114,10 @@ FROM (
|
|
| 114 |
n_name AS nation,
|
| 115 |
EXTRACT(YEAR FROM o_orderdate) AS o_year,
|
| 116 |
l_extendedprice * (1 - l_discount) - ps_supplycost * l_quantity AS amount
|
| 117 |
-
FROM
|
| 118 |
-
JOIN
|
| 119 |
-
JOIN supplier ON s_suppkey =
|
| 120 |
-
JOIN
|
| 121 |
JOIN orders ON o_orderkey = l_orderkey
|
| 122 |
JOIN nation ON s_nationkey = n_nationkey
|
| 123 |
WHERE p_name LIKE '%green%'
|
|
|
|
| 114 |
n_name AS nation,
|
| 115 |
EXTRACT(YEAR FROM o_orderdate) AS o_year,
|
| 116 |
l_extendedprice * (1 - l_discount) - ps_supplycost * l_quantity AS amount
|
| 117 |
+
FROM lineitem
|
| 118 |
+
JOIN part ON p_partkey = l_partkey
|
| 119 |
+
JOIN supplier ON s_suppkey = l_suppkey
|
| 120 |
+
JOIN partsupp ON ps_partkey = l_partkey AND ps_suppkey = l_suppkey
|
| 121 |
JOIN orders ON o_orderkey = l_orderkey
|
| 122 |
JOIN nation ON s_nationkey = n_nationkey
|
| 123 |
WHERE p_name LIKE '%green%'
|