Skip to content

Commit 5e82562

Browse files
committed
Use per-table DTE to get the table comments
Signed-off-by: Christophe Bornet <[email protected]>
1 parent cfdf3f3 commit 5e82562

File tree

2 files changed

+16
-96
lines changed

2 files changed

+16
-96
lines changed

src/databricks/sqlalchemy/_information_schema.py

Lines changed: 0 additions & 23 deletions
This file was deleted.

src/databricks/sqlalchemy/base.py

Lines changed: 16 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1+
import time
12
from typing import Any, List, Optional, Dict, Union, Iterable, Tuple
23

34
import databricks.sqlalchemy._ddl as dialect_ddl_impl
45
import databricks.sqlalchemy._types as dialect_type_impl
56
from databricks import sql
6-
from databricks.sqlalchemy._information_schema import tables
77
from databricks.sqlalchemy._parse import (
88
_describe_table_extended_result_to_dict_list,
99
_match_table_not_found_string,
@@ -376,86 +376,29 @@ def get_schema_names(self, connection, **kw):
376376
schema_list = [row[0] for row in result]
377377
return schema_list
378378

379-
def get_multi_table_comment(
380-
self,
381-
connection,
382-
schema=None,
383-
filter_names=None,
384-
scope=ObjectScope.ANY,
385-
kind=ObjectKind.ANY,
386-
**kw,
387-
) -> Iterable[Tuple[TableKey, ReflectedTableComment]]:
388-
result = []
389-
_schema = schema or self.schema
390-
if ObjectScope.DEFAULT in scope:
391-
query = (
392-
select(tables.c.table_name, tables.c.comment)
393-
.select_from(tables)
394-
.where(
395-
tables.c.table_catalog == self.catalog,
396-
tables.c.table_schema == _schema,
397-
)
398-
)
399-
400-
if ObjectKind.ANY not in kind:
401-
where_in = set()
402-
if ObjectKind.TABLE in kind:
403-
where_in.update(
404-
["BASE TABLE", "MANAGED", "EXTERNAL", "STREAMING_TABLE"]
405-
)
406-
if ObjectKind.VIEW in kind:
407-
where_in.update(["VIEW"])
408-
if ObjectKind.MATERIALIZED_VIEW in kind:
409-
where_in.update(["MATERIALIZED_VIEW"])
410-
query = query.where(tables.c.table_type.in_(where_in))
411-
412-
if filter_names:
413-
query = query.where(tables.c.table_name.in_(bindparam("filter_names")))
414-
result = connection.execute(
415-
query, {"filter_names": [f.lower() for f in filter_names]}
416-
)
417-
else:
418-
result = connection.execute(query)
419-
420-
if ObjectScope.TEMPORARY in scope and ObjectKind.VIEW in kind:
421-
result = list(result)
422-
temp_views = self.get_view_names(connection, schema, only_temp=True)
423-
if filter_names:
424-
temp_views = set(temp_views).intersection(
425-
[f.lower() for f in filter_names]
426-
)
427-
result.extend(zip(temp_views, [None] * len(temp_views)))
428-
429-
# TODO: figure out how to return sqlalchemy.interfaces in a way that mypy respects
430-
return (
431-
(
432-
(schema, table),
433-
{"text": comment}
434-
if comment is not None
435-
else ReflectionDefaults.table_comment(),
436-
)
437-
for table, comment in result
438-
) # type: ignore
439-
379+
@reflection.cache
440380
def get_table_comment(
441381
self,
442382
connection: Connection,
443383
table_name: str,
444384
schema: Optional[str] = None,
445385
**kw: Any,
446386
) -> ReflectedTableComment:
447-
data = self.get_multi_table_comment(
448-
connection,
449-
schema,
450-
[table_name],
451-
**kw,
387+
result = self._describe_table_extended(
388+
connection=connection,
389+
table_name=table_name,
390+
schema_name=schema,
391+
)
392+
# Type ignore is because mypy knows that self._describe_table_extended *can*
393+
# return None (even though it never will since expect_result defaults to True)
394+
comment_row: Dict[str, str] = next(
395+
filter(lambda r: r["col_name"] == "Comment", result), None
396+
) # type: ignore
397+
return (
398+
{"text": comment_row["data_type"]}
399+
if comment_row
400+
else ReflectionDefaults.table_comment()
452401
)
453-
try:
454-
return dict(data)[(schema, table_name.lower())]
455-
except KeyError:
456-
raise exc.NoSuchTableError(
457-
f"{schema}.{table_name}" if schema else table_name
458-
) from None
459402

460403

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

0 commit comments

Comments
 (0)