use of org.apache.iceberg.UpdateSchema in project metacat by Netflix.
the class IcebergTableHandler method update.
/**
* Updates the iceberg schema if the provided tableInfo has updated field comments.
*
* @param tableInfo table information
* @return true if an update is done
*/
public boolean update(final TableInfo tableInfo) {
boolean result = false;
final List<FieldInfo> fields = tableInfo.getFields();
if (fields != null && !fields.isEmpty() && // This parameter is only sent during data change and not during schema change.
Strings.isNullOrEmpty(tableInfo.getMetadata().get(DirectSqlTable.PARAM_PREVIOUS_METADATA_LOCATION))) {
final QualifiedName tableName = tableInfo.getName();
final String tableMetadataLocation = HiveTableUtil.getIcebergTableMetadataLocation(tableInfo);
if (Strings.isNullOrEmpty(tableMetadataLocation)) {
final String message = String.format("No metadata location specified for table %s", tableName);
log.error(message);
throw new MetacatBadRequestException(message);
}
final IcebergMetastoreTables icebergMetastoreTables = new IcebergMetastoreTables(new IcebergTableOps(conf, tableMetadataLocation, connectorContext.getConfig(), icebergTableOpsProxy));
final Table table = icebergMetastoreTables.loadTable(HiveTableUtil.qualifiedNameToTableIdentifier(tableName));
final UpdateSchema updateSchema = table.updateSchema();
final Schema schema = table.schema();
for (FieldInfo field : fields) {
final Types.NestedField iField = schema.findField(field.getName());
if (iField != null && !Objects.equals(field.getComment(), iField.doc())) {
updateSchema.updateColumnDoc(field.getName(), field.getComment());
result = true;
}
}
if (result) {
updateSchema.commit();
final String newTableMetadataLocation = icebergMetastoreTables.getTableOps().currentMetadataLocation();
if (!tableMetadataLocation.equalsIgnoreCase(newTableMetadataLocation)) {
tableInfo.getMetadata().put(DirectSqlTable.PARAM_PREVIOUS_METADATA_LOCATION, tableMetadataLocation);
tableInfo.getMetadata().put(DirectSqlTable.PARAM_METADATA_LOCATION, newTableMetadataLocation);
}
}
}
return result;
}
use of org.apache.iceberg.UpdateSchema in project hive by apache.
the class TestHiveIcebergStorageHandlerNoScan method testAlterTableAddColumnsConcurrently.
@Test
public void testAlterTableAddColumnsConcurrently() throws Exception {
TableIdentifier identifier = TableIdentifier.of("default", "customers");
testTables.createTable(shell, identifier.name(), HiveIcebergStorageHandlerTestUtils.CUSTOMER_SCHEMA, SPEC, FileFormat.PARQUET, ImmutableList.of());
org.apache.iceberg.Table icebergTable = testTables.loadTable(identifier);
UpdateSchema updateSchema = icebergTable.updateSchema().addColumn("newfloatcol", Types.FloatType.get());
shell.executeStatement("ALTER TABLE default.customers ADD COLUMNS " + "(newintcol int, newstringcol string COMMENT 'Column with description')");
try {
updateSchema.commit();
Assert.fail();
} catch (CommitFailedException expectedException) {
// Should fail to commit the addition of newfloatcol as another commit went in from Hive side adding 2 other cols
}
// Same verification should be applied, as we expect newfloatcol NOT to be added to the schema
verifyAlterTableAddColumnsTests();
}
Aggregations