Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Show column comment in JDBC connector #1840

Merged
merged 1 commit into from Nov 19, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -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;
Expand Down Expand Up @@ -254,11 +255,14 @@ public List<JdbcColumnHandle> 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<String> comment = Optional.ofNullable(emptyToNull(resultSet.getString("REMARKS")));
ebyhr marked this conversation as resolved.
Show resolved Hide resolved
columns.add(JdbcColumnHandle.builder()
.setColumnName(columnName)
.setJdbcTypeHandle(typeHandle)
.setColumnType(columnMapping.get().getType())
.setNullable(nullable)
.setComment(comment)
.build());
}
}
Expand Down
Expand Up @@ -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)");
ebyhr marked this conversation as resolved.
Show resolved Hide resolved

assertUpdate("DROP TABLE test_column_comment");
}

private void execute(String sql)
throws SQLException
{
Expand Down
Expand Up @@ -238,11 +238,13 @@ public List<JdbcColumnHandle> getColumns(ConnectorSession session, JdbcTableHand
// skip unsupported column types
if (columnMapping.isPresent()) {
boolean nullable = (resultSet.getInt("NULLABLE") != columnNoNulls);
Optional<String> comment = Optional.ofNullable(resultSet.getString("REMARKS"));
columns.add(JdbcColumnHandle.builder()
.setColumnName(columnName)
.setJdbcTypeHandle(typeHandle)
.setColumnType(columnMapping.get().getType())
.setNullable(nullable)
.setComment(comment)
.build());
}
}
Expand Down
Expand Up @@ -290,6 +290,21 @@ public void testInsertIntoNotNullColumn()
assertUpdate("DROP TABLE test_insert_not_null");
}

@Test
public void testColumnComment()
findepi marked this conversation as resolved.
Show resolved Hide resolved
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
{
Expand Down
Expand Up @@ -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
Expand Down