| """ |
| Build BM25 (Lucene) content indexes for all datasets needed by MATS-TIST. |
| |
| Run from the mats-sql-tist/ root directory: |
| conda run -n mats python scripts/build_all_bm25_indexes.py |
| |
| CPU-only; no GPU required. |
| |
| Datasets covered: |
| - BIRD dev / train (data/bird/dev|train) |
| - Spider dev+test (data/spider) |
| - Spider-DK (3 new databases) |
| - Spider-Syn / spider-realistic (share Spider databases → symlinked) |
| - Dr.Spider NLQ_* / SQL_* (share original Spider databases → symlinked) |
| - Dr.Spider DB_* (post-perturbation databases, built fresh) |
| - Domain datasets (Bank_Financials, Aminer_Simplified) |
| """ |
|
|
| import os |
| import sys |
| import shutil |
| import json |
| from tqdm import tqdm |
|
|
| |
| sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) |
| from utils.db_utils import get_cursor_from_path, execute_sql_long_time_limitation |
|
|
|
|
| |
| |
| |
|
|
| def build_content_index(db_path: str, index_path: str) -> None: |
| """Build a per-column BM25 index for one SQLite database.""" |
| cursor = get_cursor_from_path(db_path) |
| results = execute_sql_long_time_limitation( |
| cursor, "SELECT name FROM sqlite_master WHERE type='table';" |
| ) |
| table_names = [r[0] for r in results] |
|
|
| for table_name in table_names: |
| table_name = table_name.lower() |
| if table_name == "sqlite_sequence": |
| continue |
| results = execute_sql_long_time_limitation( |
| cursor, f"SELECT name FROM PRAGMA_TABLE_INFO('{table_name}')" |
| ) |
| column_names = [r[0].lower() for r in results] |
|
|
| for column_name in column_names: |
| column_index_path = f"{index_path}/{table_name}-**-{column_name}" |
| if os.path.exists(column_index_path): |
| continue |
|
|
| all_column_contents = [] |
| try: |
| rows = execute_sql_long_time_limitation( |
| cursor, |
| f"SELECT DISTINCT `{column_name}` FROM `{table_name}` WHERE `{column_name}` IS NOT NULL;", |
| ) |
| for c_id, row in enumerate(rows): |
| content = str(row[0]).strip() |
| if 0 < len(content) <= 50: |
| all_column_contents.append( |
| { |
| "id": f"{table_name}-**-{column_name}-**-{c_id}".lower(), |
| "contents": content, |
| } |
| ) |
| except Exception as e: |
| print(f" [WARN] {table_name}.{column_name}: {e}") |
|
|
| os.makedirs("./data/temp_db_index", exist_ok=True) |
| with open("./data/temp_db_index/contents.json", "w") as f: |
| json.dump(all_column_contents, f, indent=2, ensure_ascii=True) |
|
|
| cmd = ( |
| "python -m pyserini.index.lucene " |
| "--collection JsonCollection " |
| "--input ./data/temp_db_index " |
| f'--index "{column_index_path}" ' |
| "--generator DefaultLuceneDocumentGenerator " |
| "--threads 16 --storePositions --storeDocvectors --storeRaw" |
| ) |
| ret = os.system(cmd) |
| if os.path.exists("./data/temp_db_index/contents.json"): |
| os.remove("./data/temp_db_index/contents.json") |
| if ret != 0: |
| print(f" [ERROR] indexing failed for {column_index_path}") |
|
|
|
|
| def index_dataset(db_root: str, index_root: str, label: str) -> None: |
| """Index all databases under db_root into index_root.""" |
| if not os.path.isdir(db_root): |
| print(f"[SKIP] {label}: db_root not found: {db_root}") |
| return |
|
|
| os.makedirs(index_root, exist_ok=True) |
| db_ids = [d for d in os.listdir(db_root) if os.path.isdir(os.path.join(db_root, d))] |
| print(f"\n{'='*60}") |
| print(f"[INDEX] {label} ({len(db_ids)} databases → {index_root})") |
| print(f"{'='*60}") |
|
|
| for db_id in tqdm(db_ids, desc=label): |
| sqlite_path = os.path.join(db_root, db_id, f"{db_id}.sqlite") |
| if not os.path.exists(sqlite_path): |
| |
| candidates = [ |
| f for f in os.listdir(os.path.join(db_root, db_id)) |
| if f.endswith(".sqlite") |
| ] |
| if candidates: |
| sqlite_path = os.path.join(db_root, db_id, candidates[0]) |
| else: |
| print(f" [SKIP] no .sqlite in {db_id}") |
| continue |
| build_content_index(sqlite_path, os.path.join(index_root, db_id)) |
|
|
|
|
| def symlink_index(src: str, dst: str, label: str) -> None: |
| """Create a symlink dst → src if dst does not already exist.""" |
| if os.path.exists(dst) or os.path.islink(dst): |
| print(f"[OK] {label} already present: {dst}") |
| return |
| if not os.path.isdir(src): |
| print(f"[WARN] {label}: source index not found: {src}") |
| return |
| os.makedirs(os.path.dirname(dst), exist_ok=True) |
| os.symlink(src, dst) |
| print(f"[LINK] {label}: {dst} → {src}") |
|
|
|
|
| |
| |
| |
|
|
| if __name__ == "__main__": |
| ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) |
| os.chdir(ROOT) |
| print(f"Working directory: {ROOT}") |
|
|
| DR = "./data/sft_data_collections/diagnostic-robustness-text-to-sql/data" |
|
|
| |
| |
| |
| index_dataset( |
| db_root="./data/bird/dev/dev_databases", |
| index_root="./data/bird/dev/db_contents_index", |
| label="BIRD dev", |
| ) |
| index_dataset( |
| db_root="./data/bird/train/train_databases", |
| index_root="./data/bird/train/db_contents_index", |
| label="BIRD train", |
| ) |
|
|
| |
| symlink_index( |
| src=os.path.abspath("./data/bird/dev/db_contents_index"), |
| dst="./data/sft_data_collections/bird/dev/db_contents_index", |
| label="sft bird/dev (symlink)", |
| ) |
| symlink_index( |
| src=os.path.abspath("./data/bird/train/db_contents_index"), |
| dst="./data/sft_data_collections/bird/train/db_contents_index", |
| label="sft bird/train (symlink)", |
| ) |
|
|
| |
| |
| |
| index_dataset( |
| db_root="./data/spider/database", |
| index_root="./data/spider/db_contents_index", |
| label="Spider (dev/test/train databases)", |
| ) |
|
|
| |
| symlink_index( |
| src=os.path.abspath("./data/spider/db_contents_index"), |
| dst="./data/sft_data_collections/Spider-Syn/db_contents_index", |
| label="Spider-Syn (symlink)", |
| ) |
| symlink_index( |
| src=os.path.abspath("./data/spider/db_contents_index"), |
| dst="./data/sft_data_collections/spider-realistic/db_contents_index", |
| label="spider-realistic (symlink)", |
| ) |
|
|
| |
| |
| |
| index_dataset( |
| db_root="./data/sft_data_collections/Spider-DK/database", |
| index_root="./data/sft_data_collections/Spider-DK/db_contents_index", |
| label="Spider-DK", |
| ) |
|
|
| |
| |
| |
| |
| for dr_name in [ |
| "NLQ_keyword_synonym", "NLQ_keyword_carrier", "NLQ_column_synonym", |
| "NLQ_column_carrier", "NLQ_column_attribute", "NLQ_column_value", |
| "NLQ_value_synonym", "NLQ_multitype", "NLQ_others", |
| "SQL_comparison", "SQL_sort_order", "SQL_NonDB_number", |
| "SQL_DB_text", "SQL_DB_number", |
| ]: |
| symlink_index( |
| src=os.path.abspath("./data/spider/db_contents_index"), |
| dst=f"{DR}/{dr_name}/db_contents_index", |
| label=f"Dr.Spider/{dr_name} (symlink)", |
| ) |
|
|
| |
| |
| |
| for dr_name in [ |
| "DB_schema_synonym", |
| "DB_schema_abbreviation", |
| "DB_DBcontent_equivalence", |
| ]: |
| index_dataset( |
| db_root=f"{DR}/{dr_name}/database_post_perturbation", |
| index_root=f"{DR}/{dr_name}/db_contents_index", |
| label=f"Dr.Spider/{dr_name}", |
| ) |
|
|
| |
| |
| |
| index_dataset( |
| db_root="./data/sft_data_collections/domain_datasets/databases", |
| index_root="./data/sft_data_collections/domain_datasets/db_contents_index", |
| label="Domain datasets", |
| ) |
|
|
| print("\n[DONE] All BM25 indexes built / symlinked.") |
|
|