in catalogs/catalog-hive/src/main/java/org/apache/gravitino/catalog/hive/HiveCatalogOperations.java [835:930]
public Table alterTable(NameIdentifier tableIdent, TableChange... changes)
throws NoSuchTableException, IllegalArgumentException {
NameIdentifier schemaIdent = NameIdentifier.of(tableIdent.namespace().levels());
try {
// TODO(@Minghuang): require a table lock to avoid race condition
HiveTable table = (HiveTable) loadTable(tableIdent);
org.apache.hadoop.hive.metastore.api.Table alteredHiveTable =
table.toHiveTable(propertiesMetadata.tablePropertiesMetadata());
validateColumnChangeForAlter(changes, alteredHiveTable);
for (TableChange change : changes) {
// Table change
if (change instanceof TableChange.RenameTable) {
doRenameTable(alteredHiveTable, (TableChange.RenameTable) change);
} else if (change instanceof TableChange.UpdateComment) {
doUpdateComment(alteredHiveTable, (TableChange.UpdateComment) change);
} else if (change instanceof TableChange.SetProperty) {
doSetProperty(alteredHiveTable, (TableChange.SetProperty) change);
} else if (change instanceof TableChange.RemoveProperty) {
doRemoveProperty(alteredHiveTable, (TableChange.RemoveProperty) change);
} else if (change instanceof TableChange.ColumnChange) {
// Column change
StorageDescriptor sd = alteredHiveTable.getSd();
List<FieldSchema> cols = sd.getCols();
if (change instanceof TableChange.AddColumn) {
TableChange.AddColumn addColumn = (TableChange.AddColumn) change;
doAddColumn(cols, addColumn);
} else if (change instanceof TableChange.DeleteColumn) {
doDeleteColumn(cols, (TableChange.DeleteColumn) change);
} else if (change instanceof TableChange.RenameColumn) {
doRenameColumn(cols, (TableChange.RenameColumn) change);
} else if (change instanceof TableChange.UpdateColumnComment) {
doUpdateColumnComment(cols, (TableChange.UpdateColumnComment) change);
} else if (change instanceof TableChange.UpdateColumnPosition) {
doUpdateColumnPosition(cols, (TableChange.UpdateColumnPosition) change);
} else if (change instanceof TableChange.UpdateColumnType) {
doUpdateColumnType(cols, (TableChange.UpdateColumnType) change);
} else if (change instanceof TableChange.UpdateColumnAutoIncrement) {
throw new IllegalArgumentException(
"Hive does not support altering column auto increment");
} else {
throw new IllegalArgumentException(
"Unsupported column change type: " + change.getClass().getSimpleName());
}
} else {
throw new IllegalArgumentException(
"Unsupported table change type: "
+ (change == null ? "null" : change.getClass().getSimpleName()));
}
}
clientPool.run(
c -> {
c.alter_table(schemaIdent.name(), tableIdent.name(), alteredHiveTable);
return null;
});
LOG.info("Altered Hive table {} in Hive Metastore", tableIdent.name());
return HiveTable.fromHiveTable(alteredHiveTable)
.withProxyPlugin(proxyPlugin)
.withClientPool(clientPool)
.build();
} catch (TException | InterruptedException e) {
if (e.getMessage() != null
&& e.getMessage().contains("types incompatible with the existing columns")) {
throw new IllegalArgumentException(
"Failed to alter Hive table ["
+ tableIdent.name()
+ "] in Hive metastore, "
+ "since Hive metastore will check the compatibility of column type between the old and new column positions, "
+ "please ensure that the type of the new column position is compatible with the old one, "
+ "otherwise the alter operation will fail in Hive metastore.",
e);
}
throw new RuntimeException(
"Failed to alter Hive table " + tableIdent.name() + " in Hive metastore", e);
} catch (IllegalArgumentException | NoSuchTableException e) {
throw e;
} catch (Exception e) {
throw new RuntimeException(e);
}
}