Datasets:
Download scripts/import_round2.py from YukiKaze1337/cn-sqlite-sft: direct link, hf CLI and curl.
- Browser
- Download file 5.58 kB
-
https://huggingface.co/datasets/YukiKaze1337/cn-sqlite-sft/resolve/main/scripts/import_round2.py
- Command line
-
hf download hf://datasets/YukiKaze1337/cn-sqlite-sft/scripts/import_round2.py
-
curl -L -o import_round2.py https://huggingface.co/datasets/YukiKaze1337/cn-sqlite-sft/resolve/main/scripts/import_round2.py
5.58 kB
| """把第二轮 DeepSeek 出的 6 个大库和 2025 道题并进来(cn-sqlite-round2.zip,照 deepseek_tasks.md 做的)。 | |
| python scripts/import_round2.py --src <解压出来的 cn-sqlite-full 目录> | |
| zip 是在旧版项目上做的,只取两样:schemas/hard2/*.sql 和 data/hard2_train.jsonl。 | |
| - property_mgmt、car_service 的主键是复数(communities_id),引用它的外键却是单数(community_id), | |
| car_service 里还同时有 lead_technician_id 和 lead_technicians_id。统一改成单数,建表语句和 SQL 一起改 | |
| - 改名前后结果必须一模一样:同一份数据建两个库,一个 ALTER TABLE RENAME COLUMN,老 SQL 跑老库、新 SQL 跑新库,逐条比 | |
| - 题目写到 data/round2/raw/<库名>.jsonl(db / question / sql / difficulty / type),还没筛, | |
| 不进训练;check_round2.py 筛完才写到 data/new/ | |
| """ | |
| import argparse | |
| import json | |
| import re | |
| import sqlite3 | |
| from collections import Counter | |
| from pathlib import Path | |
| ROOT = Path(__file__).resolve().parent.parent | |
| RENAME = { | |
| "property_mgmt": { | |
| "communities_id": "community_id", "buildings_id": "building_id", "units_id": "unit_id", | |
| "houses_id": "house_id", "owners_id": "owner_id", "owner_house_relations_id": "owner_house_relation_id", | |
| "tenants_id": "tenant_id", "lease_contracts_id": "lease_contract_id", "fee_bills_id": "fee_bill_id", | |
| "payments_id": "payment_id", "repair_orders_id": "repair_order_id", "repair_workers_id": "repair_worker_id", | |
| "repair_assignments_id": "repair_assignment_id", | |
| }, | |
| "car_service": { | |
| "stores_id": "store_id", "vehicle_models_id": "vehicle_model_id", "owners_id": "owner_id", | |
| "vehicles_id": "vehicle_id", "sales_orders_id": "sales_order_id", "technicians_id": "technician_id", | |
| "repair_orders_id": "repair_order_id", "repair_items_id": "repair_item_id", "parts_id": "part_id", | |
| "part_issues_id": "part_issue_id", "insurance_claims_id": "insurance_claim_id", | |
| "technician_assignments_id": "technician_assignment_id", "lead_technicians_id": "lead_technician_id", | |
| }, | |
| } | |
| LABEL = {"中等": "中", "每组前 N/并列": "每组前N/并列", "反连接": "反连接/存在性"} | |
| def rename(text: str, mapping: dict[str, str]) -> str: | |
| for old, new in mapping.items(): | |
| text = re.sub(rf"\b{old}\b", new, text) | |
| return text | |
| def rows_of(conn: sqlite3.Connection, sql: str): | |
| return conn.execute(sql).fetchall() | |
| def main() -> None: | |
| ap = argparse.ArgumentParser() | |
| ap.add_argument("--src", required=True, help="cn-sqlite-round2.zip 解压出来的 cn-sqlite-full 目录") | |
| args = ap.parse_args() | |
| src = Path(args.src) | |
| out_schema = ROOT / "schemas" / "hard2" | |
| out_schema.mkdir(parents=True, exist_ok=True) | |
| old_db, new_db = {}, {} | |
| for f in sorted((src / "schemas" / "hard2").glob("*.sql")): | |
| db, ddl = f.stem, f.read_text(encoding="utf-8") | |
| mapping = RENAME.get(db, {}) | |
| (out_schema / f.name).write_text(rename(ddl, mapping), encoding="utf-8") | |
| seed = (src / "seeds" / "hard2" / f.name).read_text(encoding="utf-8") | |
| old_db[db] = sqlite3.connect(":memory:") | |
| old_db[db].executescript(ddl + seed) | |
| new_db[db] = sqlite3.connect(":memory:") | |
| new_db[db].executescript(ddl + seed) | |
| for t, in new_db[db].execute("select name from sqlite_master where type='table'").fetchall(): | |
| for col in [c[1] for c in new_db[db].execute(f'pragma table_info("{t}")')]: | |
| if col in mapping: | |
| new_db[db].execute(f'ALTER TABLE "{t}" RENAME COLUMN "{col}" TO "{mapping[col]}"') | |
| # 改完的建表语句和 ALTER 出来的库得是同一个结构,不然下面的比对没意义 | |
| want = sqlite3.connect(":memory:") | |
| want.executescript(rename(ddl, mapping)) | |
| shape = lambda c: sorted((t, tuple(x[1] for x in c.execute(f'pragma table_info("{t}")'))) | |
| for t, in c.execute("select name from sqlite_master where type='table'")) | |
| assert shape(want) == shape(new_db[db]), db | |
| items, mismatch, broken = [], 0, 0 | |
| for line in (src / "data" / "hard2_train.jsonl").read_text(encoding="utf-8").splitlines(): | |
| r = json.loads(line) | |
| db = r["db"] | |
| user, sql = r["messages"][1]["content"], r["messages"][2]["content"].strip() | |
| question = user.rsplit("\n\n问题:", 1)[1].strip() | |
| new_sql = rename(sql, RENAME.get(db, {})) | |
| try: | |
| same = rows_of(old_db[db], sql) == rows_of(new_db[db], new_sql) | |
| except sqlite3.Error as exc: | |
| print("跑不通:", db, exc, new_sql[:80]) | |
| broken += 1 | |
| continue | |
| if not same: | |
| print("改名后结果变了:", db, question) | |
| mismatch += 1 | |
| continue | |
| items.append({"db": db, "question": question, "sql": new_sql, | |
| "difficulty": LABEL.get(r.get("difficulty"), r.get("difficulty")), | |
| "type": LABEL.get(r.get("type"), r.get("type"))}) | |
| by_db = Counter(it["db"] for it in items) | |
| (ROOT / "data" / "round2" / "raw").mkdir(parents=True, exist_ok=True) | |
| for db in by_db: | |
| out = ROOT / "data" / "round2" / "raw" / f"{db}.jsonl" | |
| out.write_text("".join(json.dumps(it, ensure_ascii=False) + "\n" for it in items if it["db"] == db), | |
| encoding="utf-8") | |
| print(f"导入 {len(items)} 条(跑不通 {broken},改名后结果变了 {mismatch})", dict(by_db)) | |
| if __name__ == "__main__": | |
| main() | |