Skip to content

Commit

Permalink
Feat: Adding table comment and columns comment for SQLLab (#10844)
Browse files Browse the repository at this point in the history
* Adding table comment and columns comment for backend

* fix mypy

* Fix CI

* adding wider catch

* use logger

* fix lint
  • Loading branch information
zhaoyongjie authored Sep 15, 2020
1 parent 4502690 commit 38edb69
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 0 deletions.
3 changes: 3 additions & 0 deletions superset/databases/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ def get_table_metadata(
indexes = get_indexes_metadata(database, table_name, schema_name)
keys += foreign_keys + indexes
payload_columns: List[Dict[str, Any]] = []
table_comment = database.get_table_comment(table_name, schema_name)
for col in columns:
dtype = get_col_type(col)
payload_columns.append(
Expand All @@ -81,6 +82,7 @@ def get_table_metadata(
"type": dtype.split("(")[0] if "(" in dtype else dtype,
"longType": dtype,
"keys": [k for k in keys if col["name"] in k["column_names"]],
"comment": col.get("comment"),
}
)
return {
Expand All @@ -97,4 +99,5 @@ def get_table_metadata(
"primaryKey": primary_key,
"foreignKeys": foreign_keys,
"indexes": keys,
"comment": table_comment,
}
24 changes: 24 additions & 0 deletions superset/db_engine_specs/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -655,6 +655,30 @@ def get_view_names(
views = [re.sub(f"^{schema}\\.", "", view) for view in views]
return sorted(views)

@classmethod
def get_table_comment(
cls, inspector: Inspector, table_name: str, schema: Optional[str]
) -> Optional[str]:
"""
Get comment of table from a given schema and table
:param inspector: SqlAlchemy Inspector instance
:param table_name: Table name
:param schema: Schema name. If omitted, uses default schema for database
:return: comment of table
"""
comment = None
try:
comment = inspector.get_table_comment(table_name, schema)
comment = comment.get("text") if isinstance(comment, dict) else None
except NotImplementedError:
# It's expected that some dialects don't implement the comment method
pass
except Exception as ex: # pylint: disable=broad-except
logger.error("Unexpected error while fetching table comment")
logger.exception(ex)
return comment

@classmethod
def get_columns(
cls, inspector: Inspector, table_name: str, schema: Optional[str]
Expand Down
5 changes: 5 additions & 0 deletions superset/models/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -611,6 +611,11 @@ def get_table(self, table_name: str, schema: Optional[str] = None) -> Table:
autoload_with=self.get_sqla_engine(),
)

def get_table_comment(
self, table_name: str, schema: Optional[str] = None
) -> Optional[str]:
return self.db_engine_spec.get_table_comment(self.inspector, table_name, schema)

def get_columns(
self, table_name: str, schema: Optional[str] = None
) -> List[Dict[str, Any]]:
Expand Down
1 change: 1 addition & 0 deletions tests/databases/api_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -500,6 +500,7 @@ def test_get_table_metadata(self):
self.assertEqual(rv.status_code, 200)
response = json.loads(rv.data.decode("utf-8"))
self.assertEqual(response["name"], "birth_names")
self.assertIsNone(response["comment"])
self.assertTrue(len(response["columns"]) > 5)
self.assertTrue(response.get("selectStar").startswith("SELECT"))

Expand Down

0 comments on commit 38edb69

Please sign in to comment.