Spaces:
Runtime error
Runtime error
Commit ·
0ecd959
1
Parent(s): 7493570
Add missing schema.py
Browse files
schema.py
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import json
|
| 2 |
+
import logging
|
| 3 |
+
from collections import defaultdict
|
| 4 |
+
|
| 5 |
+
log = logging.getLogger(__name__)
|
| 6 |
+
|
| 7 |
+
# download tables.json from spider via hf_hub_download
|
| 8 |
+
# builds a db id to schema string dictionary
|
| 9 |
+
def load_spider_schemas():
|
| 10 |
+
try:
|
| 11 |
+
from huggingface_hub import hf_hub_download
|
| 12 |
+
path = hf_hub_download(
|
| 13 |
+
repo_id="xlangai/spider",
|
| 14 |
+
filename="tables.json",
|
| 15 |
+
repo_type="dataset",
|
| 16 |
+
)
|
| 17 |
+
# fallback if download fails
|
| 18 |
+
except Exception as e:
|
| 19 |
+
log.warning(f"Could not download tables.json: {e}. Schema-aware prompting disabled.")
|
| 20 |
+
return {}
|
| 21 |
+
|
| 22 |
+
with open(path) as f:
|
| 23 |
+
tables_data = json.load(f)
|
| 24 |
+
|
| 25 |
+
lookup = {}
|
| 26 |
+
for db in tables_data:
|
| 27 |
+
lookup[db["db_id"]] = _format_schema(
|
| 28 |
+
db["table_names_original"],
|
| 29 |
+
db["column_names_original"],
|
| 30 |
+
db.get("column_types", []),
|
| 31 |
+
db.get("foreign_keys", []),
|
| 32 |
+
)
|
| 33 |
+
log.info(f"Loaded schemas for {len(lookup)} databases")
|
| 34 |
+
return lookup
|
| 35 |
+
|
| 36 |
+
# convert tables, columns, and foreign keys into concise
|
| 37 |
+
# "t1(c1:type, c2:type), t2(c3:type); FK: t1.c2=t2.c3" format
|
| 38 |
+
def _format_schema(table_names, column_names_original, column_types, foreign_keys):
|
| 39 |
+
table_columns = defaultdict(list)
|
| 40 |
+
for col_idx, (table_idx, col_name) in enumerate(column_names_original):
|
| 41 |
+
if table_idx < 0:
|
| 42 |
+
continue
|
| 43 |
+
col_type = column_types[col_idx] if col_idx < len(column_types) else ""
|
| 44 |
+
if col_type:
|
| 45 |
+
table_columns[table_idx].append(f"{col_name}:{col_type}")
|
| 46 |
+
else:
|
| 47 |
+
table_columns[table_idx].append(col_name)
|
| 48 |
+
|
| 49 |
+
parts = []
|
| 50 |
+
for i, name in enumerate(table_names):
|
| 51 |
+
cols = ", ".join(table_columns.get(i, []))
|
| 52 |
+
parts.append(f"{name}({cols})")
|
| 53 |
+
tables_str = ", ".join(parts)
|
| 54 |
+
|
| 55 |
+
fk_parts = []
|
| 56 |
+
for src_idx, dst_idx in foreign_keys:
|
| 57 |
+
src_table_idx, src_col = column_names_original[src_idx]
|
| 58 |
+
dst_table_idx, dst_col = column_names_original[dst_idx]
|
| 59 |
+
if src_table_idx < 0 or dst_table_idx < 0:
|
| 60 |
+
continue
|
| 61 |
+
fk_parts.append(f"{table_names[src_table_idx]}.{src_col}={table_names[dst_table_idx]}.{dst_col}")
|
| 62 |
+
if not fk_parts:
|
| 63 |
+
return tables_str
|
| 64 |
+
return f"{tables_str}; FK: {', '.join(fk_parts)}"
|
| 65 |
+
|
| 66 |
+
#trims long schemas, preferring to cut at a complete FK entry or table boundary
|
| 67 |
+
def truncate_schema(schema_str, max_length):
|
| 68 |
+
if len(schema_str) <= max_length:
|
| 69 |
+
return schema_str
|
| 70 |
+
truncated = schema_str[:max_length]
|
| 71 |
+
fk_marker_idx = truncated.find("; FK:")
|
| 72 |
+
if fk_marker_idx > 0:
|
| 73 |
+
# In the FK section: cut at the last complete FK entry
|
| 74 |
+
last_comma = truncated.rfind(",")
|
| 75 |
+
if last_comma > fk_marker_idx:
|
| 76 |
+
return truncated[:last_comma]
|
| 77 |
+
last_close = truncated.rfind(")")
|
| 78 |
+
if last_close > 0:
|
| 79 |
+
return truncated[:last_close + 1]
|
| 80 |
+
return truncated
|