You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@superset.apache.org by ma...@apache.org on 2020/09/15 04:12:14 UTC

[incubator-superset] branch master updated: Feat: Adding table comment and columns comment for SQLLab (#10844)

This is an automated email from the ASF dual-hosted git repository.

maximebeauchemin pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-superset.git


The following commit(s) were added to refs/heads/master by this push:
     new 38edb69  Feat: Adding table comment and columns comment for SQLLab (#10844)
38edb69 is described below

commit 38edb69d95a79e8507db7cdff431565215bcbda0
Author: Yongjie Zhao <yo...@gmail.com>
AuthorDate: Tue Sep 15 12:11:48 2020 +0800

    Feat: Adding table comment and columns comment for SQLLab (#10844)
    
    * Adding table comment and columns comment for backend
    
    * fix mypy
    
    * Fix CI
    
    * adding wider catch
    
    * use logger
    
    * fix lint
---
 superset/databases/utils.py      |  3 +++
 superset/db_engine_specs/base.py | 24 ++++++++++++++++++++++++
 superset/models/core.py          |  5 +++++
 tests/databases/api_tests.py     |  1 +
 4 files changed, 33 insertions(+)

diff --git a/superset/databases/utils.py b/superset/databases/utils.py
index c28c3e8..4d47517 100644
--- a/superset/databases/utils.py
+++ b/superset/databases/utils.py
@@ -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(
@@ -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 {
@@ -97,4 +99,5 @@ def get_table_metadata(
         "primaryKey": primary_key,
         "foreignKeys": foreign_keys,
         "indexes": keys,
+        "comment": table_comment,
     }
diff --git a/superset/db_engine_specs/base.py b/superset/db_engine_specs/base.py
index 502cd6e..f924ade 100644
--- a/superset/db_engine_specs/base.py
+++ b/superset/db_engine_specs/base.py
@@ -656,6 +656,30 @@ class BaseEngineSpec:  # pylint: disable=too-many-public-methods
         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]
     ) -> List[Dict[str, Any]]:
diff --git a/superset/models/core.py b/superset/models/core.py
index 775a9f0..4379a20 100755
--- a/superset/models/core.py
+++ b/superset/models/core.py
@@ -611,6 +611,11 @@ class Database(
             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]]:
diff --git a/tests/databases/api_tests.py b/tests/databases/api_tests.py
index 9dafb87..0e965d7 100644
--- a/tests/databases/api_tests.py
+++ b/tests/databases/api_tests.py
@@ -500,6 +500,7 @@ class TestDatabaseApi(SupersetTestCase):
         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"))