| """Fast selector v2 data builder: use stored is_*_correct labels.""" |
| import json, os, re, sys, random |
| from concurrent.futures import ThreadPoolExecutor, as_completed |
|
|
| os.environ["NO_PROXY"] = "localhost,127.0.0.1" |
| ROOT = "/home/datht/mats-sql-tist" |
| os.chdir(ROOT); sys.path.insert(0, ROOT) |
|
|
| from validator_data.validator import _execute_sql |
| from datasets import Dataset, DatasetDict |
|
|
| PROMPT_TEMPLATE = ( |
| "You are a SQL correctness judge.\n" |
| "Schema:\n{schema}\n\n" |
| "Question: {question}\n" |
| "External knowledge: {evidence}\n\n" |
| "Candidate SQL:\n{sql}\n\n" |
| "Execution result:\n{exec_result}\n\n" |
| "Is this SQL correct for the question? Answer YES or NO." |
| ) |
|
|
| SRC_PATHS = [ |
| "data/rollouts/bird_train_3stage_K4.jsonl", |
| "data/rollouts/scaleup_bird_train_2stage_K4.jsonl", |
| "data/rollouts/scaleup_bird_train_3stage_K4.jsonl", |
| "data/rollouts/iter2_bird_train_3stage_K8.jsonl", |
| ] |
|
|
| def safe_truncate(s, n=400): |
| s = str(s) if s is not None else "" |
| return s if len(s) <= n else s[:n] + "..." |
|
|
| def process_one(item): |
| s, sql, label = item |
| db_path = s["db_path"] |
| try: |
| p_resp, p_err = _execute_sql("./" + db_path, sql) |
| except Exception: |
| p_err = True; p_resp = "" |
| if p_err: |
| exec_str = f"Error: {str(p_resp)[:180]}" |
| else: |
| rows = str(p_resp)[:280] |
| exec_str = f"OK. Rows preview: {rows}" if rows.strip() and rows.strip() != "[]" else "OK. (no rows returned)" |
| prompt = PROMPT_TEMPLATE.format( |
| schema=safe_truncate(s.get("schema", ""), 3000), |
| question=s.get("question", ""), |
| evidence=s.get("evidence", "") or "None", |
| sql=safe_truncate(sql, 800), |
| exec_result=safe_truncate(exec_str, 300), |
| ) |
| return {"prompt": prompt, "completion": label, |
| "messages": {"prompt": prompt, "completion": label}, |
| "question": s.get("question", ""), "db_id": s.get("db_id", ""), |
| "label_int": 1 if label == "YES" else 0} |
|
|
| def main(): |
| rng = random.Random(42) |
| work = [] |
| seen = set() |
| for src in SRC_PATHS: |
| if not os.path.exists(src): continue |
| print(f"Loading {src}...", flush=True) |
| with open(src) as f: |
| for line in f: |
| s = json.loads(line) |
| q = s.get("question", "") |
| for t in s.get("trajectories", []): |
| sql = (t.get("fixed_sql") or t.get("planner_sql") or "").strip() |
| if not sql: continue |
| norm = re.sub(r"\s+", " ", sql).lower() |
| key = (q, norm) |
| if key in seen: continue |
| seen.add(key) |
| |
| if t.get("fixed_sql"): |
| label = "YES" if t.get("is_fixed_correct") else "NO" |
| else: |
| label = "YES" if t.get("is_planner_correct") else "NO" |
| work.append((s, sql, label)) |
| print(f"Work items: {len(work)}", flush=True) |
|
|
| pairs = [] |
| with ThreadPoolExecutor(max_workers=32) as exe: |
| futs = [exe.submit(process_one, it) for it in work] |
| n_done = 0 |
| for fut in as_completed(futs): |
| pairs.append(fut.result()) |
| n_done += 1 |
| if n_done % 500 == 0: |
| print(f" {n_done}/{len(work)}", flush=True) |
|
|
| rng.shuffle(pairs) |
| n_test = max(200, len(pairs) // 25) |
| test = pairs[:n_test]; train = pairs[n_test:] |
| yes_train = sum(1 for p in train if p["completion"] == "YES") |
| print(f"=== v2 selector data ===") |
| print(f" train: {len(train)} ({100*yes_train/max(len(train),1):.1f}% YES)") |
| print(f" test: {len(test)}") |
| out = "/home/datht/mats-sql-tist/data/sft_selector_classifier_v2_rows" |
| DatasetDict({"train": Dataset.from_list(train), "test": Dataset.from_list(test)}).save_to_disk(out) |
| print(f" Saved {out}", flush=True) |
|
|
| if __name__ == "__main__": |
| main() |
|
|