#!/usr/bin/env python3 """Gold-label sanity checker for the GRAST-SL evaluation sets. For every instance of every evaluation CSV in this folder it verifies: 1. the gold column set is non-empty; 2. every gold column is present in the instance's schema list (../schemas/_schemas.json[db_id], exact case-insensitive match); 3. n_schema_columns == len(schema list) for the instance's database. The schema lists are flat `table.column` (Spider/BIRD) or grouped-node (`FAMILY_*.column`, Spider 2.0) lists — the same granularity the gold columns are written in, so membership is exact, no fuzzy matching. Usage: python check_gold.py # check all four sets python check_gold.py bird_dev # check one set Exits non-zero if any check fails. """ import csv, json, os, sys csv.field_size_limit(10_000_000) HERE = os.path.dirname(os.path.abspath(__file__)) SCHEMAS = os.path.join(HERE, "..", "schemas") SETS = { "spider_dev": ("spider_dev.csv", "spider_dev_schemas.json"), "bird_dev": ("bird_dev.csv", "bird_dev_schemas.json"), "spider2_lite_256": ("spider2_lite_256.csv", "spider2_lite_schemas.json"), "spider2_snow_256": ("spider2_snow_256.csv", "spider2_snow_schemas.json"), } def check(rows, schemas): bad = [] for r in rows: rid = r.get("instance_id") or r.get("question_id") db = r["db_id"] schema = schemas.get(db) if schema is None: bad.append((rid, db, "NO_SCHEMA_FOR_DB", "")) continue up = {s.upper() for s in schema} cols = [c for c in r["gold_columns"].split("; ") if c] if not cols: bad.append((rid, db, "EMPTY_GOLD", "")) continue miss = [c for c in cols if c.upper() not in up] if miss: bad.append((rid, db, "NOT_IN_SCHEMA", "|".join(miss))) if "n_schema_columns" in r and int(r["n_schema_columns"]) != len(schema): bad.append((rid, db, "N_SCHEMA_MISMATCH", f"csv={r['n_schema_columns']} len(schema)={len(schema)}")) return bad def main(): targets = sys.argv[1:] or list(SETS) failed = False for name in targets: csv_file, schema_file = SETS[name] rows = list(csv.DictReader(open(os.path.join(HERE, csv_file)))) schemas = json.load(open(os.path.join(SCHEMAS, schema_file))) bad = check(rows, schemas) n_cols = sum(len([c for c in r["gold_columns"].split("; ") if c]) for r in rows) status = "OK" if not bad else f"{len(bad)} PROBLEM(S)" print(f"{name}: {len(rows)} instances, {n_cols} gold columns -> {status}") for b in bad[:20]: print(" ", *b) failed |= bool(bad) sys.exit(1 if failed else 0) if __name__ == "__main__": main()