File size: 10,075 Bytes
778d47d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import time
import threading
from pathlib import Path
from typing import Optional, List, Dict, Any

import sqlite3
import pandas as pd
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel, Field


# -----------------------------
# Config (no memory watchdogs here)
# -----------------------------
SPIDER_DB_ROOT = os.getenv("SPIDER_DB_ROOT", "/app/data/spider/database")
BIRD_TRAIN_DB_ROOT = os.getenv("BIRD_TRAIN_DB_ROOT", "/app/data/bird/train/train_databases")
BIRD_DEV_DB_ROOT = os.getenv("BIRD_DEV_DB_ROOT", "/app/data/bird/dev/dev_databases")
SPIDER2_DB_ROOT = os.getenv("SPIDER2_DB_ROOT", "/home/datht/Spider2/spider2-lite/resource/databases/spider2-localdb")
MAX_CONCURRENT = int(os.getenv("MAX_CONCURRENT", "100"))
DEFAULT_TIMEOUT_MS = int(os.getenv("DEFAULT_TIMEOUT_MS", "120000"))
MAX_ROWS = int(os.getenv("MAX_ROWS", "10000"))


# -----------------------------
# FastAPI app
# -----------------------------
app = FastAPI(title="SQLite No-Lock SQL Executor")
gate = threading.Semaphore(MAX_CONCURRENT)


# -----------------------------
# Models
# -----------------------------
class ExecuteRequest(BaseModel):
    dataset_name: str = Field(..., description="Dataset name: 'spider', 'bird', or 'spider2'")
    db_id: str = Field(..., description="Database ID (folder name)")
    sql: str = Field(..., description="Single SQL statement.")
    mode: str = Field(
        "sandbox_rollback",
        description="read_only | sandbox_rollback",
        pattern="^(read_only|sandbox_rollback)$"
    )
    timeout_ms: int = Field(DEFAULT_TIMEOUT_MS, ge=1, le=120_000)
    max_rows: int = Field(MAX_ROWS, ge=1, le=50_000)


class ExecuteResponse(BaseModel):
    ok: bool
    statement_type: str
    rows: Optional[List[Dict[str, Any]]] = None
    row_count: Optional[int] = None
    pandas_result: Optional[str] = None
    notice: Optional[str] = None
    error: Optional[str] = None
    timed_out: bool = False


# -----------------------------
# Helpers
# -----------------------------
def reverse_map_db_name(mapped_db_name: str) -> str:
    """Reverse map database names from mapped names back to original names for SQLite files."""
    reverse_mappings = {
        "SQLITE_SAKILA": "sqlite-sakila",
        "DB_IMDB": "Db-IMDB",
    }
    return reverse_mappings.get(mapped_db_name, mapped_db_name)


def db_path_safe(dataset_name: str, db_id: str) -> Path:
    if dataset_name not in ["spider", "bird", "spider2"]:
        raise HTTPException(400, detail="dataset_name must be 'spider', 'bird', or 'spider2'")

    if dataset_name == "spider":
        root = Path(SPIDER_DB_ROOT)
        db_folder = root / db_id
        db_file = db_folder / f"{db_id}.sqlite"
    elif dataset_name == "spider2":
        root = Path(SPIDER2_DB_ROOT)
        # For spider2, databases are directly in the root directory (not in subdirectories)
        # Try mapped name first, then original db_id
        original_db_name = reverse_map_db_name(db_id)
        db_file = root / f"{original_db_name}.sqlite"
        if not db_file.exists():
            db_file = root / f"{db_id}.sqlite"
    else:  # bird
        train_root = Path(BIRD_TRAIN_DB_ROOT)
        dev_root = Path(BIRD_DEV_DB_ROOT)
        if (train_root / db_id / f"{db_id}.sqlite").exists():
            root = train_root
        elif (dev_root / db_id / f"{db_id}.sqlite").exists():
            root = dev_root
        else:
            raise HTTPException(404, detail=f"Database not found: {dataset_name}/{db_id}")
        db_folder = root / db_id
        db_file = db_folder / f"{db_id}.sqlite"

    if not db_file.exists():
        raise HTTPException(404, detail=f"Database not found: {dataset_name}/{db_id}")

    if not db_file.resolve().is_relative_to(root.resolve()):
        raise HTTPException(400, detail="Invalid database path.")

    return db_file.resolve()


def classify(sql: str) -> str:
    parts = sql.strip().split(None, 1)
    return parts[0].upper() if parts else "UNKNOWN"


def is_select_like(sql: str) -> bool:
    s = sql.lstrip().lower()
    return s.startswith("select") or s.startswith("with") or s.startswith("explain")


# -----------------------------
# Core executors (run in threads)
# -----------------------------
def _run_read_only_thread(dbfile: Path, sql: str, timeout_ms: int, max_rows: int) -> ExecuteResponse:
    # Use immutable read-only URI to avoid locking the original file
    uri = f"file:{dbfile}?immutable=1&mode=ro"
    deadline_ts = time.time() + (timeout_ms / 1000.0)

    def progress_cb():
        # Abort when over deadline
        return 1 if time.time() >= deadline_ts else 0

    try:
        conn = sqlite3.connect(uri, uri=True, timeout=min(2.0, timeout_ms/1000.0), check_same_thread=False)
        try:
            conn.row_factory = sqlite3.Row
            # Enforce read-only behavior and lower contention
            conn.set_progress_handler(progress_cb, 1000)
            conn.execute("PRAGMA query_only = ON;")
            conn.execute("PRAGMA foreign_keys = ON;")
            conn.execute("PRAGMA case_sensitive_like = ON;")
            conn.execute(f"PRAGMA busy_timeout={int(timeout_ms)};")

            stmt_type = classify(sql)
            cur = conn.execute(sql)

            # Fetch everything at once; enforce max_rows by slicing
            rows: List[sqlite3.Row] = cur.fetchall()
            if max_rows:
                rows = rows[:max_rows]

            cols = [d[0] for d in cur.description] if cur.description else []
            cur.close()

            data = [ {c: r[idx] for idx, c in enumerate(cols)} for r in rows ] if cols else []

            if rows:
                pandas_result = pd.DataFrame(rows, columns=pd.Index([str(c) for c in cols])).to_string(index=False)
            else:
                pandas_result = "Empty result set"

            return ExecuteResponse(
                ok=True,
                statement_type=stmt_type,
                rows=data,
                row_count=len(data),
                pandas_result=pandas_result,
                notice="Read-only mode (immutable)."
            )
        finally:
            conn.close()
    except sqlite3.OperationalError as e:
        msg = str(e)
        timed_out = ("timeout" in msg) or ("interrupt" in msg) or ("exceeded" in msg)
        return ExecuteResponse(ok=False, statement_type=classify(sql), error=msg, timed_out=timed_out)
    except Exception as e:
        return ExecuteResponse(ok=False, statement_type=classify(sql), error=str(e))


def _run_sandbox_thread(dbfile: Path, sql: str, timeout_ms: int, max_rows: int) -> ExecuteResponse:
    # Execute SQL on a temp copy and rollback to avoid modifying source and to avoid locking it
    deadline_ts = time.time() + (timeout_ms / 1000.0)

    def progress_cb():
        return 1 if time.time() >= deadline_ts else 0

    try:
        conn = sqlite3.connect(dbfile.as_posix(), timeout=min(2.0, timeout_ms/1000.0), check_same_thread=False)
        try:
            conn.row_factory = sqlite3.Row
            # Lower lock contention on the temp copy
            conn.set_progress_handler(progress_cb, 1000)

            stmt_type = classify(sql)
            if is_select_like(sql):
                # No transaction for SELECT queries
                cur = conn.execute(sql)
                rows: List[sqlite3.Row] = cur.fetchall()
                if max_rows:
                    rows = rows[:max_rows]
                cols = [d[0] for d in cur.description] if cur.description else []
                cur.close()
                data = [ {c: r[idx] for idx, c in enumerate(cols)} for r in rows ] if cols else []
                row_count = len(data)
                pandas_result = pd.DataFrame(rows, columns=pd.Index([str(c) for c in cols])).to_string(index=False) if rows else "Empty result set"
                return ExecuteResponse(
                    ok=True,
                    statement_type=stmt_type,
                    rows=data,
                    row_count=row_count,
                    pandas_result=pandas_result,
                    notice="Executed SELECT without transaction."
                )
            else:
                return ExecuteResponse(
                    ok=True,
                    statement_type=stmt_type,
                    notice="Not allowed to execute non-SELECT queries"
                )
        finally:
            conn.close()
    except sqlite3.OperationalError as e:
        print(e)
        msg = str(e)
        timed_out = ("timeout" in msg) or ("interrupt" in msg) or ("exceeded" in msg)
        return ExecuteResponse(ok=False, statement_type=classify(sql), error=msg, timed_out=timed_out)
    except Exception as e:
        print(e)
        return ExecuteResponse(ok=False, statement_type=classify(sql), error=str(e))


# -----------------------------
# API
# -----------------------------
@app.post("/execute", response_model=ExecuteResponse)
def execute(req: ExecuteRequest):
    dbfile = db_path_safe(req.dataset_name, req.db_id)

    gate.acquire()
    try:
        if req.mode == "read_only":
            # Quick fail if clearly a write
            sql_upper = req.sql.strip().upper()
            if sql_upper.startswith(("INSERT", "UPDATE", "DELETE", "MERGE", "REPLACE", "TRUNCATE", "ALTER", "DROP", "CREATE", "VACUUM", "PRAGMA")):
                raise HTTPException(400, detail="Write/DDL detected; use mode='sandbox_rollback' to test safely.")
            return _run_read_only_thread(dbfile, req.sql, req.timeout_ms, req.max_rows)
        else:
            return _run_sandbox_thread(dbfile, req.sql, req.timeout_ms, req.max_rows)
    finally:
        gate.release()


# -----------------------------
# Health Endpoints
# -----------------------------
@app.get("/healthz/live")
async def liveness():
    return {"ok": True, "ts": time.time()}


@app.get("/healthz/ready")
async def readiness():
    try:
        return {"ok": True, "ts": time.time()}
    except Exception:
        raise HTTPException(503, "not ready")


# Run: uvicorn api_no_lock:app --host 0.0.0.0 --port 8001 --workers 8