From 744c74c6fee51e8e1c9c784fbf6d791c66c01ff7 Mon Sep 17 00:00:00 2001 From: Yuya Ebihara Date: Fri, 8 Nov 2019 23:36:26 +0900 Subject: [PATCH] Show column comment in JDBC based connector --- .../prestosql/plugin/jdbc/BaseJdbcClient.java | 4 ++++ .../mysql/TestMySqlIntegrationSmokeTest.java | 13 ++++++++++++ .../plugin/postgresql/PostgreSqlClient.java | 2 ++ .../TestPostgreSqlIntegrationSmokeTest.java | 15 ++++++++++++++ .../TestSqlServerIntegrationSmokeTest.java | 20 +++++++++++++++++++ 5 files changed, 54 insertions(+) diff --git a/presto-base-jdbc/src/main/java/io/prestosql/plugin/jdbc/BaseJdbcClient.java b/presto-base-jdbc/src/main/java/io/prestosql/plugin/jdbc/BaseJdbcClient.java index e0d2dd8a1f5a5..434aac5a0c49b 100644 --- a/presto-base-jdbc/src/main/java/io/prestosql/plugin/jdbc/BaseJdbcClient.java +++ b/presto-base-jdbc/src/main/java/io/prestosql/plugin/jdbc/BaseJdbcClient.java @@ -56,6 +56,7 @@ import static com.google.common.base.MoreObjects.firstNonNull; import static com.google.common.base.Preconditions.checkArgument; +import static com.google.common.base.Strings.emptyToNull; import static com.google.common.base.Strings.isNullOrEmpty; import static com.google.common.base.Verify.verify; import static com.google.common.collect.ImmutableMap.toImmutableMap; @@ -254,11 +255,14 @@ public List getColumns(ConnectorSession session, JdbcTableHand // skip unsupported column types if (columnMapping.isPresent()) { boolean nullable = (resultSet.getInt("NULLABLE") != columnNoNulls); + // Note: some databases (e.g. SQL Server) do not return column remarks/comment here. + Optional comment = Optional.ofNullable(emptyToNull(resultSet.getString("REMARKS"))); columns.add(JdbcColumnHandle.builder() .setColumnName(columnName) .setJdbcTypeHandle(typeHandle) .setColumnType(columnMapping.get().getType()) .setNullable(nullable) + .setComment(comment) .build()); } } diff --git a/presto-mysql/src/test/java/io/prestosql/plugin/mysql/TestMySqlIntegrationSmokeTest.java b/presto-mysql/src/test/java/io/prestosql/plugin/mysql/TestMySqlIntegrationSmokeTest.java index 204d03a237268..c6b16847c407d 100644 --- a/presto-mysql/src/test/java/io/prestosql/plugin/mysql/TestMySqlIntegrationSmokeTest.java +++ b/presto-mysql/src/test/java/io/prestosql/plugin/mysql/TestMySqlIntegrationSmokeTest.java @@ -219,6 +219,19 @@ public void testInsertIntoNotNullColumn() assertUpdate("DROP TABLE test_insert_not_null"); } + @Test + public void testColumnComment() + throws Exception + { + execute("CREATE TABLE tpch.test_column_comment (col1 bigint COMMENT 'test comment', col2 bigint COMMENT '', col3 bigint)"); + + assertQuery( + "SELECT column_name, comment FROM information_schema.columns WHERE table_schema = 'tpch' AND table_name = 'test_column_comment'", + "VALUES ('col1', 'test comment'), ('col2', null), ('col3', null)"); + + assertUpdate("DROP TABLE test_column_comment"); + } + private void execute(String sql) throws SQLException { diff --git a/presto-postgresql/src/main/java/io/prestosql/plugin/postgresql/PostgreSqlClient.java b/presto-postgresql/src/main/java/io/prestosql/plugin/postgresql/PostgreSqlClient.java index c17aeba971231..b592a98802310 100644 --- a/presto-postgresql/src/main/java/io/prestosql/plugin/postgresql/PostgreSqlClient.java +++ b/presto-postgresql/src/main/java/io/prestosql/plugin/postgresql/PostgreSqlClient.java @@ -238,11 +238,13 @@ public List getColumns(ConnectorSession session, JdbcTableHand // skip unsupported column types if (columnMapping.isPresent()) { boolean nullable = (resultSet.getInt("NULLABLE") != columnNoNulls); + Optional comment = Optional.ofNullable(resultSet.getString("REMARKS")); columns.add(JdbcColumnHandle.builder() .setColumnName(columnName) .setJdbcTypeHandle(typeHandle) .setColumnType(columnMapping.get().getType()) .setNullable(nullable) + .setComment(comment) .build()); } } diff --git a/presto-postgresql/src/test/java/io/prestosql/plugin/postgresql/TestPostgreSqlIntegrationSmokeTest.java b/presto-postgresql/src/test/java/io/prestosql/plugin/postgresql/TestPostgreSqlIntegrationSmokeTest.java index f17a51a3674f0..3fca64f607cde 100644 --- a/presto-postgresql/src/test/java/io/prestosql/plugin/postgresql/TestPostgreSqlIntegrationSmokeTest.java +++ b/presto-postgresql/src/test/java/io/prestosql/plugin/postgresql/TestPostgreSqlIntegrationSmokeTest.java @@ -290,6 +290,21 @@ public void testInsertIntoNotNullColumn() assertUpdate("DROP TABLE test_insert_not_null"); } + @Test + public void testColumnComment() + throws Exception + { + try (AutoCloseable ignoreTable = withTable("tpch.test_column_comment", + "(col1 bigint, col2 bigint, col3 bigint)")) { + execute("COMMENT ON COLUMN tpch.test_column_comment.col1 IS 'test comment'"); + execute("COMMENT ON COLUMN tpch.test_column_comment.col2 IS ''"); // it will be NULL, PostgreSQL doesn't store empty comment + + assertQuery( + "SELECT column_name, comment FROM information_schema.columns WHERE table_schema = 'tpch' AND table_name = 'test_column_comment'", + "VALUES ('col1', 'test comment'), ('col2', null), ('col3', null)"); + } + } + private AutoCloseable withSchema(String schema) throws Exception { diff --git a/presto-sqlserver/src/test/java/io/prestosql/plugin/sqlserver/TestSqlServerIntegrationSmokeTest.java b/presto-sqlserver/src/test/java/io/prestosql/plugin/sqlserver/TestSqlServerIntegrationSmokeTest.java index 25ed7eed54aeb..02aad30bf65e4 100644 --- a/presto-sqlserver/src/test/java/io/prestosql/plugin/sqlserver/TestSqlServerIntegrationSmokeTest.java +++ b/presto-sqlserver/src/test/java/io/prestosql/plugin/sqlserver/TestSqlServerIntegrationSmokeTest.java @@ -74,6 +74,26 @@ public void testView() sqlServer.execute("DROP VIEW IF EXISTS test_view"); } + @Test + public void testColumnComment() + throws Exception + { + try (AutoCloseable ignoreTable = withTable("test_column_comment", + "(col1 bigint, col2 bigint, col3 bigint)")) { + sqlServer.execute("" + + "EXEC sp_addextendedproperty " + + " 'MS_Description', 'test comment', " + + " 'Schema', 'dbo', " + + " 'Table', 'test_column_comment', " + + " 'Column', 'col1'"); + + // SQL Server JDBC driver doesn't support REMARKS for column comment https://github.com/Microsoft/mssql-jdbc/issues/646 + assertQuery( + "SELECT column_name, comment FROM information_schema.columns WHERE table_schema = 'dbo' AND table_name = 'test_column_comment'", + "VALUES ('col1', null), ('col2', null), ('col3', null)"); + } + } + @Test public void testDecimalPredicatePushdown() throws Exception