Download code/score.py from mvdelibas/geosql-traps: direct link, hf CLI and curl.
- Browser
- Download file 3.08 kB
-
https://huggingface.co/datasets/mvdelibas/geosql-traps/resolve/main/code/score.py
- Command line
-
hf download hf://datasets/mvdelibas/geosql-traps/code/score.py
-
curl -L -o score.py https://huggingface.co/datasets/mvdelibas/geosql-traps/resolve/main/code/score.py
3.08 kB
| """Run SQL against a locked read-only DuckDB and classify the outcome. Also validates the benchmark: gold SQL must pass, naive SQL must fail silently. | |
| Usage: python code/score.py validate | |
| """ | |
| from __future__ import annotations | |
| import json | |
| import re | |
| import sys | |
| import threading | |
| from pathlib import Path | |
| import duckdb | |
| ROOT = Path(__file__).resolve().parents[1] | |
| DB = ROOT / "data" / "geosql.duckdb" | |
| def connect(): | |
| c = duckdb.connect(str(DB), read_only=True) | |
| c.execute("LOAD spatial; SET geometry_always_xy=true; SET enable_external_access=false; SET lock_configuration=true;") | |
| return c | |
| def extract_sql(text: str) -> str | None: | |
| t = re.sub(r"<think>.*?</think>", "", text or "", flags=re.S) | |
| m = re.search(r"```(?:sql)?\s*(.*?)```", t, flags=re.S | re.I) | |
| s = (m.group(1) if m else t).strip().rstrip(";").strip() | |
| return s if re.match(r"(?is)^\s*(select|with)\b", s) else None | |
| def run(con, sql: str, timeout: float = 20.0): | |
| out: dict = {} | |
| def work(): | |
| try: | |
| out["rows"] = con.execute(sql).fetchall() | |
| except Exception as e: # any SQL failure is a result to record | |
| out["error"] = f"{type(e).__name__}: {str(e)[:200]}" | |
| th = threading.Thread(target=work, daemon=True) | |
| th.start() | |
| th.join(timeout) | |
| if th.is_alive(): | |
| con.interrupt() | |
| th.join(5) | |
| return None, "timeout" | |
| return out.get("rows"), out.get("error") | |
| def classify(q: dict, rows, error) -> tuple[str, object]: | |
| if error: | |
| return ("timeout" if error == "timeout" else "sql_error"), None | |
| if not rows or not rows[0] or rows[0][0] is None: | |
| return "wrong_silent", None | |
| v = rows[0][0] | |
| if q["kind"] == "text": | |
| return ("correct" if str(v).strip() == q["answer"] else "wrong_silent"), str(v) | |
| try: | |
| n = float(v) | |
| except (TypeError, ValueError): | |
| return "wrong_silent", str(v) | |
| if q["kind"] == "int": | |
| return ("correct" if n == q["answer"] else "wrong_silent"), n | |
| return ("correct" if abs(n - q["answer"]) <= q["tolerance"] * abs(q["answer"]) else "wrong_silent"), n | |
| def validate() -> None: | |
| con = connect() | |
| bad, naive_same, n = [], [], 0 | |
| for line in open(ROOT / "data" / "questions.jsonl", encoding="utf-8"): | |
| q = json.loads(line) | |
| n += 1 | |
| r, e = run(con, q["gold_sql"]) | |
| c, v = classify(q, r, e) | |
| if c != "correct": | |
| bad.append((q["id"], c, v, q["answer"], e)) | |
| if q["naive_sql"]: | |
| r, e = run(con, q["naive_sql"]) | |
| c, v = classify(q, r, e) | |
| if c == "correct": | |
| naive_same.append((q["id"], v, q["answer"])) | |
| elif c != "wrong_silent": | |
| bad.append((q["id"], "naive-" + c, v, q["answer"], e)) | |
| print("questions", n, "gold or naive problems", len(bad), "naive accidentally correct", len(naive_same)) | |
| for b in bad[:12]: | |
| print(b) | |
| print(naive_same[:12]) | |
| if __name__ == "__main__": | |
| validate() | |