Skip to content

Commit

Permalink
Support table comment for Snowflake connector
Browse files Browse the repository at this point in the history
Allow create table with comment.
Also allow to set table comment for Snowflake tables.
  • Loading branch information
chenjian2664 committed Mar 31, 2024
1 parent 14393b2 commit be5b003
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
*/
package io.trino.plugin.snowflake;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.inject.Inject;
Expand All @@ -35,6 +36,7 @@
import io.trino.plugin.jdbc.ObjectWriteFunction;
import io.trino.plugin.jdbc.PredicatePushdownController;
import io.trino.plugin.jdbc.QueryBuilder;
import io.trino.plugin.jdbc.RemoteTableName;
import io.trino.plugin.jdbc.SliceWriteFunction;
import io.trino.plugin.jdbc.StandardColumnMappings;
import io.trino.plugin.jdbc.WriteMapping;
Expand All @@ -52,6 +54,7 @@
import io.trino.spi.connector.AggregateFunction;
import io.trino.spi.connector.ColumnHandle;
import io.trino.spi.connector.ConnectorSession;
import io.trino.spi.connector.ConnectorTableMetadata;
import io.trino.spi.type.CharType;
import io.trino.spi.type.Chars;
import io.trino.spi.type.DateTimeEncoding;
Expand Down Expand Up @@ -93,6 +96,7 @@
import java.util.function.BiFunction;

import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Strings.emptyToNull;
import static io.trino.plugin.jdbc.CaseSensitivity.CASE_INSENSITIVE;
import static io.trino.plugin.jdbc.CaseSensitivity.CASE_SENSITIVE;
import static io.trino.plugin.jdbc.JdbcErrorCode.JDBC_ERROR;
Expand All @@ -103,6 +107,8 @@
import static io.trino.spi.type.VarcharType.createUnboundedVarcharType;
import static io.trino.spi.type.VarcharType.createVarcharType;
import static java.lang.String.format;
import static java.lang.String.join;
import static java.util.Objects.requireNonNull;

public class SnowflakeClient
extends BaseJdbcClient
Expand Down Expand Up @@ -319,9 +325,32 @@ public boolean isTopNGuaranteed(ConnectorSession session)

@Override
public Optional<String> getTableComment(ResultSet resultSet)
throws SQLException
{
// Don't return a comment until the connector supports creating tables with comment
return Optional.empty();
// Empty remarks means that the table doesn't have a comment in Snowflake
return Optional.ofNullable(emptyToNull(resultSet.getString("REMARKS")));
}

@Override
protected List<String> createTableSqls(RemoteTableName remoteTableName, List<String> columns, ConnectorTableMetadata tableMetadata)
{
checkArgument(tableMetadata.getProperties().isEmpty(), "Unsupported table properties: %s", tableMetadata.getProperties());
return ImmutableList.of(format("CREATE TABLE %s (%s) COMMENT = %s", quoted(remoteTableName), join(", ", columns), snowflakeVarcharLiteral(tableMetadata.getComment().orElse(""))));
}

@Override
public void setTableComment(ConnectorSession session, JdbcTableHandle handle, Optional<String> comment)
{
String sql = "COMMENT ON TABLE %s IS %s".formatted(
quoted(handle.asPlainTable().getRemoteTableName()),
snowflakeVarcharLiteral(comment.orElse("")));
execute(session, sql);
}

private static String snowflakeVarcharLiteral(String value)
{
requireNonNull(value, "value is null");
return "'" + value.replace("'", "''").replace("\\", "\\\\") + "'";
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,7 @@ protected boolean hasBehavior(TestingConnectorBehavior connectorBehavior)
SUPPORTS_AGGREGATION_PUSHDOWN_VARIANCE,
SUPPORTS_ARRAY,
SUPPORTS_COMMENT_ON_COLUMN,
SUPPORTS_COMMENT_ON_TABLE,
SUPPORTS_CREATE_TABLE_WITH_COLUMN_COMMENT,
SUPPORTS_CREATE_TABLE_WITH_TABLE_COMMENT,
SUPPORTS_PREDICATE_PUSHDOWN_WITH_VARCHAR_EQUALITY,
SUPPORTS_PREDICATE_PUSHDOWN_WITH_VARCHAR_INEQUALITY,
SUPPORTS_ROW_TYPE,
Expand Down

0 comments on commit be5b003

Please sign in to comment.