Search in sources :

Example 1 with UpdateSchema

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;
}
Also used : Types(org.apache.iceberg.types.Types) DirectSqlTable(com.netflix.metacat.connector.hive.sql.DirectSqlTable) Table(org.apache.iceberg.Table) UpdateSchema(org.apache.iceberg.UpdateSchema) QualifiedName(com.netflix.metacat.common.QualifiedName) UpdateSchema(org.apache.iceberg.UpdateSchema) Schema(org.apache.iceberg.Schema) MetacatBadRequestException(com.netflix.metacat.common.exception.MetacatBadRequestException) FieldInfo(com.netflix.metacat.common.server.connectors.model.FieldInfo)

Example 2 with UpdateSchema

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();
}
Also used : TableIdentifier(org.apache.iceberg.catalog.TableIdentifier) Table(org.apache.iceberg.Table) UpdateSchema(org.apache.iceberg.UpdateSchema) CommitFailedException(org.apache.iceberg.exceptions.CommitFailedException) Test(org.junit.Test)

Aggregations

Table (org.apache.iceberg.Table)2 UpdateSchema (org.apache.iceberg.UpdateSchema)2 QualifiedName (com.netflix.metacat.common.QualifiedName)1 MetacatBadRequestException (com.netflix.metacat.common.exception.MetacatBadRequestException)1 FieldInfo (com.netflix.metacat.common.server.connectors.model.FieldInfo)1 DirectSqlTable (com.netflix.metacat.connector.hive.sql.DirectSqlTable)1 Schema (org.apache.iceberg.Schema)1 TableIdentifier (org.apache.iceberg.catalog.TableIdentifier)1 CommitFailedException (org.apache.iceberg.exceptions.CommitFailedException)1 Types (org.apache.iceberg.types.Types)1 Test (org.junit.Test)1