Skip to content

Commit

Permalink
Support for comments on views for Delta Lake
Browse files Browse the repository at this point in the history
  • Loading branch information
marcinsbd committed May 8, 2023
1 parent 2a9b6e5 commit 769a927
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1173,6 +1173,12 @@ public void setColumnComment(ConnectorSession session, ConnectorTableHandle tabl
}
}

@Override
public void setViewComment(ConnectorSession session, SchemaTableName viewName, Optional<String> comment)
{
trinoViewHiveMetastore.updateViewComment(session, viewName, comment);
}

@Override
public void addColumn(ConnectorSession session, ConnectorTableHandle tableHandle, ColumnMetadata newColumnMetadata)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ protected boolean hasBehavior(TestingConnectorBehavior connectorBehavior)
case SUPPORTS_SET_COLUMN_TYPE:
return false;

case SUPPORTS_COMMENT_ON_VIEW:
case SUPPORTS_COMMENT_ON_VIEW_COLUMN:
return false;

case SUPPORTS_CREATE_MATERIALIZED_VIEW:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -601,11 +601,6 @@ private String getTableLocation(String tableName)
return (String) computeScalar("SELECT DISTINCT regexp_replace(\"$path\", '/[^/]*/[^/]*$', '') FROM " + tableName);
}

protected String getTableComment(String tableName)
{
return (String) computeScalar("SELECT comment FROM system.metadata.table_comments WHERE catalog_name = 'iceberg' AND schema_name = '" + getSession().getSchema().orElseThrow() + "' AND table_name = '" + tableName + "'");
}

protected String getColumnComment(String tableName, String columnName)
{
return (String) computeScalar("SELECT comment FROM information_schema.columns WHERE table_schema = '" + getSession().getSchema().orElseThrow() + "' AND table_name = '" + tableName + "' AND column_name = '" + columnName + "'");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,32 +142,6 @@ public void testRenameSchema()
.hasStackTraceContaining("renameNamespace is not supported for Iceberg Glue catalogs");
}

@Test
public void testCommentView()
{
// TODO: Consider moving to BaseConnectorSmokeTest
try (TestView view = new TestView(getQueryRunner()::execute, "test_comment_view", "SELECT * FROM region")) {
// comment set
assertUpdate("COMMENT ON VIEW " + view.getName() + " IS 'new comment'");
assertThat((String) computeScalar("SHOW CREATE VIEW " + view.getName())).contains("COMMENT 'new comment'");
assertThat(getTableComment(view.getName())).isEqualTo("new comment");

// comment updated
assertUpdate("COMMENT ON VIEW " + view.getName() + " IS 'updated comment'");
assertThat(getTableComment(view.getName())).isEqualTo("updated comment");

// comment set to empty
assertUpdate("COMMENT ON VIEW " + view.getName() + " IS ''");
assertThat(getTableComment(view.getName())).isEmpty();

// comment deleted
assertUpdate("COMMENT ON VIEW " + view.getName() + " IS 'a comment'");
assertThat(getTableComment(view.getName())).isEqualTo("a comment");
assertUpdate("COMMENT ON VIEW " + view.getName() + " IS NULL");
assertThat(getTableComment(view.getName())).isNull();
}
}

@Test
public void testCommentViewColumn()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import io.trino.Session;
import io.trino.spi.security.Identity;
import io.trino.testing.sql.TestTable;
import io.trino.testing.sql.TestView;
import io.trino.tpch.TpchTable;
import org.testng.SkipException;
import org.testng.annotations.Test;
Expand All @@ -25,6 +26,7 @@
import java.util.regex.Pattern;

import static io.trino.spi.connector.ConnectorMetadata.MODIFYING_ROWS_MESSAGE;
import static io.trino.testing.TestingConnectorBehavior.SUPPORTS_COMMENT_ON_VIEW;
import static io.trino.testing.TestingConnectorBehavior.SUPPORTS_CREATE_MATERIALIZED_VIEW;
import static io.trino.testing.TestingConnectorBehavior.SUPPORTS_CREATE_SCHEMA;
import static io.trino.testing.TestingConnectorBehavior.SUPPORTS_CREATE_TABLE;
Expand Down Expand Up @@ -556,4 +558,44 @@ public void testMaterializedView()

assertUpdate("DROP MATERIALIZED VIEW " + viewName);
}

@Test
public void testCommentView()
{
if (!hasBehavior(SUPPORTS_COMMENT_ON_VIEW)) {
if (hasBehavior(SUPPORTS_CREATE_VIEW)) {
try (TestView view = new TestView(getQueryRunner()::execute, "test_comment_view", "SELECT * FROM region")) {
assertQueryFails("COMMENT ON VIEW " + view.getName() + " IS 'new comment'", "This connector does not support setting view comments");
}
return;
}
throw new SkipException("Skipping as connector does not support CREATE VIEW");
}

try (TestView view = new TestView(getQueryRunner()::execute, "test_comment_view", "SELECT * FROM region")) {
// comment set
assertUpdate("COMMENT ON VIEW " + view.getName() + " IS 'new comment'");
assertThat((String) computeScalar("SHOW CREATE VIEW " + view.getName())).contains("COMMENT 'new comment'");
assertThat(getTableComment(view.getName())).isEqualTo("new comment");

// comment updated
assertUpdate("COMMENT ON VIEW " + view.getName() + " IS 'updated comment'");
assertThat(getTableComment(view.getName())).isEqualTo("updated comment");

// comment set to empty
assertUpdate("COMMENT ON VIEW " + view.getName() + " IS ''");
assertThat(getTableComment(view.getName())).isEmpty();

// comment deleted
assertUpdate("COMMENT ON VIEW " + view.getName() + " IS 'a comment'");
assertThat(getTableComment(view.getName())).isEqualTo("a comment");
assertUpdate("COMMENT ON VIEW " + view.getName() + " IS NULL");
assertThat(getTableComment(view.getName())).isNull();
}
}

protected String getTableComment(String tableName)
{
return (String) computeScalar(format("SELECT comment FROM system.metadata.table_comments WHERE catalog_name = '%s' AND schema_name = '%s' AND table_name = '%s'", getSession().getCatalog().orElseThrow(), getSession().getSchema().orElseThrow(), tableName));
}
}

0 comments on commit 769a927

Please sign in to comment.