File size: 1,927 Bytes
778d47d | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 | """Build compact-format schema sequence matching paper's HF SFT dataset:
table lists , columns = [
lists.list_id | primary key ; type: integer ; values: 1
...
]
foreign keys:
...
"""
import sys, os
sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..'))
from utils.db_utils import detect_special_char, add_quotation_mark
def get_compact_schema(schema, max_values=2):
out = "database schema:\n"
for table in schema["schema_items"]:
tname = table["table_name"]
if detect_special_char(tname): tname = add_quotation_mark(tname)
col_lines = []
for cname, ctype, ccomment, ccontent, pk, *_ in zip(
table["column_names"], table["column_types"], table["column_comments"],
table["column_contents"], table["pk_indicators"]
):
disp = add_quotation_mark(cname) if detect_special_char(cname) else cname
qualified = f"{tname}.{disp}"
parts = []
if pk: parts.append("primary key")
parts.append(f"type: {ctype}")
if ccomment: parts.append(f"meaning: {ccomment}")
if ccontent:
vals = " , ".join(str(v) for v in ccontent[:max_values])
parts.append(f"values: {vals}")
col_lines.append(f" {qualified} | " + " ; ".join(parts))
out += f"table {tname} , columns = [\n" + "\n".join(col_lines) + "\n]\n"
if schema.get("foreign_keys"):
out += "foreign keys:\n"
for fk in schema["foreign_keys"]:
fk = [add_quotation_mark(p) if detect_special_char(p) else p for p in fk]
out += f"{fk[0]}.{fk[1]} = {fk[2]}.{fk[3]}\n"
else:
out += "foreign keys: None\n"
return out.strip()
if __name__ == "__main__":
import json
dev = json.load(open('data/rebuttal_sft_bird_dev_text2sql.json'))
s = dev[0]
print(get_compact_schema(s['schema'])[:2000])
|