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

Support altering table comments in Hive Glue catalogs #19073

Merged
merged 1 commit into from
Nov 6, 2023
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@
import static io.trino.plugin.hive.HiveErrorCode.HIVE_FILESYSTEM_ERROR;
import static io.trino.plugin.hive.HiveErrorCode.HIVE_INVALID_METADATA;
import static io.trino.plugin.hive.HiveErrorCode.HIVE_METASTORE_ERROR;
import static io.trino.plugin.hive.HiveMetadata.TABLE_COMMENT;
import static io.trino.plugin.hive.TableType.MANAGED_TABLE;
import static io.trino.plugin.hive.TableType.VIRTUAL_VIEW;
import static io.trino.plugin.hive.metastore.MetastoreUtil.makePartitionName;
Expand Down Expand Up @@ -715,7 +716,11 @@ private TableInput convertGlueTableToTableInput(com.amazonaws.services.glue.mode
@Override
public void commentTable(String databaseName, String tableName, Optional<String> comment)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a test in TestHiveGlueMetastore to ensure test coverage for this new functionality.

{
throw new TrinoException(NOT_SUPPORTED, "Table comment is not yet supported by Glue service");
Table oldTable = getExistingTable(databaseName, tableName);
Table newTable = Table.builder(oldTable)
.setParameter(TABLE_COMMENT, comment)
.build();
replaceTable(databaseName, tableName, newTable, null);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,12 @@
import io.trino.spi.function.LanguageFunction;

import java.util.List;
import java.util.Map;
import java.util.Optional;

import static com.google.common.collect.ImmutableList.toImmutableList;
import static com.google.common.collect.ImmutableMap.toImmutableMap;
import static io.trino.plugin.hive.HiveMetadata.TABLE_COMMENT;
import static io.trino.plugin.hive.metastore.thrift.ThriftMetastoreUtil.metastoreFunctionName;
import static io.trino.plugin.hive.metastore.thrift.ThriftMetastoreUtil.toResourceUris;
import static io.trino.plugin.hive.metastore.thrift.ThriftMetastoreUtil.updateStatisticsParameters;
Expand All @@ -61,15 +64,21 @@ public static DatabaseInput convertDatabase(Database database)

public static TableInput convertTable(Table table)
{
Optional<String> comment = Optional.ofNullable(table.getParameters().get(TABLE_COMMENT));
Map<String, String> tableParameters = table.getParameters().entrySet().stream()
.filter(entry -> !entry.getKey().equals(TABLE_COMMENT))
.collect(toImmutableMap(Map.Entry::getKey, Map.Entry::getValue));

TableInput input = new TableInput();
input.setName(table.getTableName());
input.setOwner(table.getOwner().orElse(null));
input.setTableType(table.getTableType());
input.setStorageDescriptor(convertStorage(table.getStorage(), table.getDataColumns()));
input.setPartitionKeys(table.getPartitionColumns().stream().map(GlueInputConverter::convertColumn).collect(toImmutableList()));
input.setParameters(table.getParameters());
input.setParameters(tableParameters);
table.getViewOriginalText().ifPresent(input::setViewOriginalText);
table.getViewExpandedText().ifPresent(input::setViewExpandedText);
comment.ifPresent(input::setDescription);
alexjo2144 marked this conversation as resolved.
Show resolved Hide resolved
return input;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
import static com.google.common.base.Strings.nullToEmpty;
import static io.trino.plugin.hive.HiveErrorCode.HIVE_INVALID_METADATA;
import static io.trino.plugin.hive.HiveErrorCode.HIVE_UNSUPPORTED_FORMAT;
import static io.trino.plugin.hive.HiveMetadata.TABLE_COMMENT;
import static io.trino.plugin.hive.HiveType.HIVE_INT;
import static io.trino.plugin.hive.TableType.EXTERNAL_TABLE;
import static io.trino.plugin.hive.ViewReaderUtil.isTrinoMaterializedView;
Expand Down Expand Up @@ -126,7 +127,16 @@ public static Table convertTable(com.amazonaws.services.glue.model.Table glueTab
SchemaTableName table = new SchemaTableName(dbName, glueTable.getName());

String tableType = getTableType(glueTable);
Map<String, String> tableParameters = ImmutableMap.copyOf(getTableParameters(glueTable));

ImmutableMap.Builder<String, String> parameters = ImmutableMap.builder();
Optional<String> description = Optional.ofNullable(glueTable.getDescription());
description.ifPresent(comment -> parameters.put(TABLE_COMMENT, comment));
getTableParameters(glueTable).entrySet().stream()
// If the description was set we may have two "comment"s, prefer the description field
.filter(entry -> description.isEmpty() || !entry.getKey().equals(TABLE_COMMENT))
.forEach(parameters::put);
Map<String, String> tableParameters = parameters.buildOrThrow();

Table.Builder tableBuilder = Table.builder()
.setDatabaseName(table.getSchemaName())
.setTableName(table.getTableName())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1421,6 +1421,29 @@ public void testGlueObjectsWithoutStorageDescriptor()
}
}

@Test
public void testAlterTableComment()
throws Exception
{
SchemaTableName tableName = temporaryTable("test_alter_table_comment");
doCreateEmptyTable(tableName, ORC, ImmutableList.of(new ColumnMetadata("name", BIGINT)), ImmutableList.of());
try {
assertThat(metastore.getTable(tableName.getSchemaName(), tableName.getTableName()).orElseThrow().getParameters()).doesNotContainKey(TABLE_COMMENT);
metastore.commentTable(tableName.getSchemaName(), tableName.getTableName(), Optional.of("a table comment"));
Map<String, String> tableParameters = metastore.getTable(tableName.getSchemaName(), tableName.getTableName()).orElseThrow().getParameters();
assertThat(tableParameters.get(TABLE_COMMENT)).isEqualTo("a table comment");

metastore.commentTable(tableName.getSchemaName(), tableName.getTableName(), Optional.empty());
tableParameters = metastore.getTable(tableName.getSchemaName(), tableName.getTableName()).orElseThrow().getParameters();
assertThat(tableParameters.get(TABLE_COMMENT)).isNull();
}
finally {
glueClient.deleteTable(new DeleteTableRequest()
.withDatabaseName(tableName.getSchemaName())
.withName(tableName.getTableName()));
}
}

@Test
public void testAlterColumnComment()
throws Exception
Expand Down
Loading