Skip to content

Commit 2a2b497

Browse files
committed
enh: parse table description and include in the schema
also renamed fields->columns in the schema JSON
1 parent ccd4f56 commit 2a2b497

2 files changed

Lines changed: 52 additions & 4 deletions

File tree

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ build-backend = "scikit_build_core.build"
1313

1414
[project]
1515
name = "idc-index-data"
16-
version = "22.1.4"
16+
version = "22.1.5"
1717
authors = [
1818
{ name = "Andrey Fedorov", email = "andrey.fedorov@gmail.com" },
1919
{ name = "Vamsi Thiriveedhi", email = "vthiriveedhi@mgh.harvard.edu" },

scripts/python/idc_index_data_manager.py

Lines changed: 51 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,51 @@ def __init__(self, project_id: str):
2222
self.client = bigquery.Client(project=project_id)
2323
logger.debug("IDCIndexDataManager initialized with project ID: %s", project_id)
2424

25+
@staticmethod
26+
def parse_table_description(sql_query: str) -> str:
27+
"""
28+
Parses the table description from SQL query comments.
29+
30+
The method looks for comments following the pattern:
31+
# table-description:
32+
# description text continues here
33+
# and can span multiple lines
34+
35+
Args:
36+
sql_query: The SQL query string containing comments
37+
38+
Returns:
39+
The table description as a string
40+
"""
41+
description_lines = []
42+
logger.debug("Parsing table description from SQL query comments")
43+
logger.debug(sql_query)
44+
lines = sql_query.split("\n")
45+
46+
for i, line in enumerate(lines):
47+
stripped = line.strip()
48+
if stripped == "# table-description:":
49+
# Collect description lines until we hit a non-comment line
50+
j = i + 1
51+
while j < len(lines):
52+
next_line = lines[j]
53+
next_stripped = next_line.strip()
54+
if next_stripped.startswith("#") and next_stripped != "#":
55+
# Remove the leading # and whitespace
56+
desc_text = next_stripped[1:].strip()
57+
if desc_text:
58+
description_lines.append(desc_text)
59+
j += 1
60+
elif next_stripped.startswith("#"):
61+
# Empty comment line, skip
62+
j += 1
63+
else:
64+
# Non-comment line, stop collecting
65+
break
66+
break
67+
68+
return " ".join(description_lines)
69+
2570
@staticmethod
2671
def parse_column_descriptions(sql_query: str) -> dict[str, str]:
2772
"""
@@ -232,24 +277,27 @@ def save_schema_to_json(
232277
logger.debug("Parsing column descriptions from SQL query comments")
233278
logger.debug(sql_query)
234279
if sql_query is not None:
280+
table_description = self.parse_table_description(sql_query)
281+
logger.debug("Parsed table description: %s", table_description)
235282
descriptions = self.parse_column_descriptions(sql_query)
236283

237284
# Convert BigQuery schema to JSON-serializable format
238285
schema_dict = {
239-
"fields": [
286+
"table_description": table_description,
287+
"columns": [
240288
{
241289
"name": field.name,
242290
"type": field.field_type,
243291
"mode": field.mode,
244292
"description": descriptions.get(field.name, ""),
245293
}
246294
for field in schema
247-
]
295+
],
248296
}
249297
else:
250298
# If no SQL query provided, save schema without descriptions
251299
schema_dict = {
252-
"fields": [
300+
"columns": [
253301
{
254302
"name": field.name,
255303
"type": field.field_type,

0 commit comments

Comments
 (0)