|
1 |
| -from typing import Any, List, Optional, Dict, Union, Iterable, Tuple |
| 1 | +from typing import Any, List, Optional, Dict, Union |
2 | 2 |
|
3 | 3 | import databricks.sqlalchemy._ddl as dialect_ddl_impl
|
4 | 4 | import databricks.sqlalchemy._types as dialect_type_impl
|
5 | 5 | from databricks import sql
|
6 |
| -from databricks.sqlalchemy._information_schema import tables |
7 | 6 | from databricks.sqlalchemy._parse import (
|
8 | 7 | _describe_table_extended_result_to_dict_list,
|
9 | 8 | _match_table_not_found_string,
|
|
15 | 14 | )
|
16 | 15 |
|
17 | 16 | import sqlalchemy
|
18 |
| -from sqlalchemy import DDL, event, select, bindparam, exc |
| 17 | +from sqlalchemy import DDL, event, exc |
19 | 18 | from sqlalchemy.engine import Connection, Engine, default, reflection
|
20 | 19 | from sqlalchemy.engine.interfaces import (
|
21 | 20 | ReflectedForeignKeyConstraint,
|
22 | 21 | ReflectedPrimaryKeyConstraint,
|
23 | 22 | ReflectedColumn,
|
24 | 23 | ReflectedTableComment,
|
25 |
| - TableKey, |
26 | 24 | )
|
27 |
| -from sqlalchemy.engine.reflection import ReflectionDefaults, ObjectKind, ObjectScope |
| 25 | +from sqlalchemy.engine.reflection import ReflectionDefaults |
28 | 26 | from sqlalchemy.exc import DatabaseError, SQLAlchemyError
|
29 | 27 |
|
30 | 28 | try:
|
@@ -376,86 +374,29 @@ def get_schema_names(self, connection, **kw):
|
376 | 374 | schema_list = [row[0] for row in result]
|
377 | 375 | return schema_list
|
378 | 376 |
|
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 |
440 | 378 | def get_table_comment(
|
441 | 379 | self,
|
442 | 380 | connection: Connection,
|
443 | 381 | table_name: str,
|
444 | 382 | schema: Optional[str] = None,
|
445 | 383 | **kw: Any,
|
446 | 384 | ) -> 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() |
452 | 399 | )
|
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 |
459 | 400 |
|
460 | 401 |
|
461 | 402 | @event.listens_for(Engine, "do_connect")
|
|
0 commit comments