Skip to content

Commit 78a5ad0

Browse files
author
Jesse Whitehouse
committed
Move DTE parsing into _parse.py
Signed-off-by: Jesse Whitehouse <[email protected]>
1 parent 8f8fef0 commit 78a5ad0

File tree

2 files changed

+34
-10
lines changed

2 files changed

+34
-10
lines changed

src/databricks/sqlalchemy/_parse.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,20 @@ def match_dte_rows_by_value(dte_output: List[Dict[str, str]], match: str) -> Lis
251251
return output_rows
252252

253253

254+
def match_dte_rows_by_key(dte_output: List[Dict[str, str]], match: str) -> List[dict]:
255+
"""Return a list of dictionaries containing only the col_name:data_type pairs where the `col_name`
256+
value contains the match argument.
257+
"""
258+
259+
output_rows = []
260+
261+
for row_dict in dte_output:
262+
if match in row_dict["col_name"]:
263+
output_rows.append(row_dict)
264+
265+
return output_rows
266+
267+
254268
def get_fk_strings_from_dte_output(dte_output: List[Dict[str, str]]) -> List[dict]:
255269
"""If the DESCRIBE TABLE EXTENDED output contains foreign key constraints, return a list of dictionaries,
256270
one dictionary per defined constraint
@@ -275,6 +289,15 @@ def get_pk_strings_from_dte_output(
275289
return output
276290

277291

292+
def get_comment_from_dte_output(dte_output: List[Dict[str, str]]) -> Optional[str]:
293+
"""Returns the value of the first "Comment" col_name data in dte_output"""
294+
output = match_dte_rows_by_key(dte_output, "Comment")
295+
if not output:
296+
return None
297+
else:
298+
return output[0]["data_type"]
299+
300+
278301
# The keys of this dictionary are the values we expect to see in a
279302
# TGetColumnsRequest's .TYPE_NAME attribute.
280303
# These are enumerated in ttypes.py as class TTypeId.

src/databricks/sqlalchemy/base.py

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
build_pk_dict,
1111
get_fk_strings_from_dte_output,
1212
get_pk_strings_from_dte_output,
13+
get_comment_from_dte_output,
1314
parse_column_info_from_tgetcolumnsresponse,
1415
)
1516

@@ -387,16 +388,16 @@ def get_table_comment(
387388
table_name=table_name,
388389
schema_name=schema,
389390
)
390-
# Type ignore is because mypy knows that self._describe_table_extended *can*
391-
# return None (even though it never will since expect_result defaults to True)
392-
comment_row: Dict[str, str] = next(
393-
filter(lambda r: r["col_name"] == "Comment", result), None
394-
) # type: ignore
395-
return (
396-
{"text": comment_row["data_type"]}
397-
if comment_row
398-
else ReflectionDefaults.table_comment()
399-
)
391+
392+
if result is None:
393+
return ReflectionDefaults.table_comment()
394+
395+
comment = get_comment_from_dte_output(result)
396+
397+
if comment:
398+
return dict(text=comment)
399+
else:
400+
return ReflectionDefaults.table_comment()
400401

401402

402403
@event.listens_for(Engine, "do_connect")

0 commit comments

Comments
 (0)