Skip to content

Commit ba5ad75

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

File tree

2 files changed

+18
-100
lines changed

2 files changed

+18
-100
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: 18 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
from typing import Any, List, Optional, Dict, Union, Iterable, Tuple
1+
from typing import Any, List, Optional, Dict, Union
22

33
import databricks.sqlalchemy._ddl as dialect_ddl_impl
44
import databricks.sqlalchemy._types as dialect_type_impl
55
from databricks import sql
6-
from databricks.sqlalchemy._information_schema import tables
76
from databricks.sqlalchemy._parse import (
87
_describe_table_extended_result_to_dict_list,
98
_match_table_not_found_string,
@@ -15,16 +14,15 @@
1514
)
1615

1716
import sqlalchemy
18-
from sqlalchemy import DDL, event, select, bindparam, exc
17+
from sqlalchemy import DDL, event
1918
from sqlalchemy.engine import Connection, Engine, default, reflection
2019
from sqlalchemy.engine.interfaces import (
2120
ReflectedForeignKeyConstraint,
2221
ReflectedPrimaryKeyConstraint,
2322
ReflectedColumn,
2423
ReflectedTableComment,
25-
TableKey,
2624
)
27-
from sqlalchemy.engine.reflection import ReflectionDefaults, ObjectKind, ObjectScope
25+
from sqlalchemy.engine.reflection import ReflectionDefaults
2826
from sqlalchemy.exc import DatabaseError, SQLAlchemyError
2927

3028
try:
@@ -376,86 +374,29 @@ def get_schema_names(self, connection, **kw):
376374
schema_list = [row[0] for row in result]
377375
return schema_list
378376

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-
377+
@reflection.cache
440378
def get_table_comment(
441379
self,
442380
connection: Connection,
443381
table_name: str,
444382
schema: Optional[str] = None,
445383
**kw: Any,
446384
) -> ReflectedTableComment:
447-
data = self.get_multi_table_comment(
448-
connection,
449-
schema,
450-
[table_name],
451-
**kw,
385+
result = self._describe_table_extended(
386+
connection=connection,
387+
table_name=table_name,
388+
schema_name=schema,
389+
)
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()
452399
)
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
459400

460401

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

0 commit comments

Comments
 (0)