the-other-tim-brown commented on code in PR #632: URL: https://github.com/apache/incubator-xtable/pull/632#discussion_r1935948274
########## xtable-aws/src/main/java/org/apache/xtable/glue/table/IcebergGlueCatalogTableBuilder.java: ########## @@ -0,0 +1,121 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.xtable.glue.table; + +import static org.apache.iceberg.BaseMetastoreTableOperations.METADATA_LOCATION_PROP; +import static org.apache.iceberg.BaseMetastoreTableOperations.PREVIOUS_METADATA_LOCATION_PROP; +import static org.apache.iceberg.BaseMetastoreTableOperations.TABLE_TYPE_PROP; +import static org.apache.xtable.catalog.CatalogUtils.castToHierarchicalTableIdentifier; + +import java.util.HashMap; +import java.util.Map; + +import org.apache.hadoop.conf.Configuration; + +import org.apache.iceberg.BaseTable; +import org.apache.iceberg.hadoop.HadoopTables; + +import com.google.common.annotations.VisibleForTesting; + +import org.apache.xtable.catalog.CatalogTableBuilder; +import org.apache.xtable.glue.GlueCatalogSyncClient; +import org.apache.xtable.glue.GlueSchemaExtractor; +import org.apache.xtable.model.InternalTable; +import org.apache.xtable.model.catalog.CatalogTableIdentifier; +import org.apache.xtable.model.catalog.HierarchicalTableIdentifier; +import org.apache.xtable.model.storage.TableFormat; + +import software.amazon.awssdk.services.glue.model.StorageDescriptor; +import software.amazon.awssdk.services.glue.model.Table; +import software.amazon.awssdk.services.glue.model.TableInput; + +/** Iceberg specific table operations for Glue catalog sync */ +public class IcebergGlueCatalogTableBuilder implements CatalogTableBuilder<TableInput, Table> { + + private final GlueSchemaExtractor schemaExtractor; + private final HadoopTables hadoopTables; + private static final String tableFormat = TableFormat.ICEBERG; + + public IcebergGlueCatalogTableBuilder(Configuration configuration) { + this.schemaExtractor = GlueSchemaExtractor.getInstance(); + this.hadoopTables = new HadoopTables(configuration); + } + + @VisibleForTesting + IcebergGlueCatalogTableBuilder(GlueSchemaExtractor schemaExtractor, HadoopTables hadoopTables) { + this.schemaExtractor = schemaExtractor; + this.hadoopTables = hadoopTables; + } + + @Override + public TableInput getCreateTableRequest( + InternalTable table, CatalogTableIdentifier tableIdentifier) { + HierarchicalTableIdentifier tblIdentifier = castToHierarchicalTableIdentifier(tableIdentifier); + BaseTable fsTable = loadTableFromFs(table.getBasePath()); + return TableInput.builder() + .name(tblIdentifier.getTableName()) + .tableType(GlueCatalogSyncClient.GLUE_EXTERNAL_TABLE_TYPE) + .parameters(getTableParameters(fsTable)) + .storageDescriptor( + StorageDescriptor.builder() + .location(table.getBasePath()) + .columns(schemaExtractor.toColumns(tableFormat, table.getReadSchema())) + .build()) + .build(); + } + + @Override + public TableInput getUpdateTableRequest( + InternalTable table, Table catalogTable, CatalogTableIdentifier tableIdentifier) { + HierarchicalTableIdentifier tblIdentifier = castToHierarchicalTableIdentifier(tableIdentifier); + BaseTable icebergTable = loadTableFromFs(table.getBasePath()); + Map<String, String> parameters = new HashMap<>(catalogTable.parameters()); + parameters.put(PREVIOUS_METADATA_LOCATION_PROP, parameters.get(METADATA_LOCATION_PROP)); + parameters.put(METADATA_LOCATION_PROP, getMetadataFileLocation(icebergTable)); + parameters.putAll(icebergTable.properties()); + + return TableInput.builder() + .name(tblIdentifier.getTableName()) + .tableType(GlueCatalogSyncClient.GLUE_EXTERNAL_TABLE_TYPE) + .parameters(parameters) + .storageDescriptor( + StorageDescriptor.builder() + .location(table.getBasePath()) + .columns( + schemaExtractor.toColumns(tableFormat, table.getReadSchema(), catalogTable)) + .build()) + .build(); + } + + @VisibleForTesting + Map<String, String> getTableParameters(BaseTable icebergTable) { + Map<String, String> parameters = new HashMap<>(icebergTable.properties()); + parameters.put(TABLE_TYPE_PROP, tableFormat); + parameters.put(METADATA_LOCATION_PROP, getMetadataFileLocation(icebergTable)); + return parameters; + } + + private BaseTable loadTableFromFs(String tableBasePath) { + return (BaseTable) hadoopTables.load(tableBasePath); Review Comment: we are assuming that the table will not be in an existing catalog, is that safe to assume? ########## xtable-aws/src/main/java/org/apache/xtable/glue/GlueCatalogConversionSource.java: ########## @@ -0,0 +1,100 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.xtable.glue; + +import static org.apache.xtable.catalog.CatalogUtils.castToHierarchicalTableIdentifier; + +import java.util.Properties; + +import org.apache.hadoop.conf.Configuration; + +import com.google.common.annotations.VisibleForTesting; + +import org.apache.xtable.catalog.TableFormatUtils; +import org.apache.xtable.conversion.ExternalCatalogConfig; +import org.apache.xtable.conversion.SourceTable; +import org.apache.xtable.exception.CatalogSyncException; +import org.apache.xtable.model.catalog.CatalogTableIdentifier; +import org.apache.xtable.model.catalog.HierarchicalTableIdentifier; +import org.apache.xtable.model.storage.CatalogType; +import org.apache.xtable.spi.extractor.CatalogConversionSource; + +import software.amazon.awssdk.services.glue.GlueClient; +import software.amazon.awssdk.services.glue.model.GetTableRequest; +import software.amazon.awssdk.services.glue.model.GetTableResponse; +import software.amazon.awssdk.services.glue.model.GlueException; +import software.amazon.awssdk.services.glue.model.Table; + +public class GlueCatalogConversionSource implements CatalogConversionSource { + private final GlueClient glueClient; + private final GlueCatalogConfig glueCatalogConfig; + + public GlueCatalogConversionSource( + ExternalCatalogConfig catalogConfig, Configuration configuration) { + this.glueCatalogConfig = GlueCatalogConfig.of(catalogConfig.getCatalogProperties()); + this.glueClient = new DefaultGlueClientFactory(glueCatalogConfig).getGlueClient(); + } + + @VisibleForTesting + public GlueCatalogConversionSource(GlueCatalogConfig glueCatalogConfig, GlueClient glueClient) { Review Comment: make this package private? ########## xtable-api/src/main/java/org/apache/xtable/conversion/ExternalCatalogConfig.java: ########## @@ -61,5 +61,5 @@ public class ExternalCatalogConfig { /** * The properties for this catalog, used for providing any custom behaviour during catalog sync */ - @NonNull @Builder.Default Map<String, String> catalogProperties = Collections.emptyMap(); Review Comment: Can we keep this as NonNull? ########## xtable-aws/src/main/java/org/apache/xtable/glue/GlueCatalogConfig.java: ########## @@ -0,0 +1,90 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.xtable.glue; + +import java.util.Collections; +import java.util.Map; +import java.util.stream.Collectors; + +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; +import lombok.ToString; + +import com.fasterxml.jackson.annotation.JsonProperty; +import com.fasterxml.jackson.databind.DeserializationFeature; +import com.fasterxml.jackson.databind.ObjectMapper; + +/** Configurations for setting up Glue client and running Glue catalog operations */ +@Getter +@EqualsAndHashCode +@ToString +public class GlueCatalogConfig { + + private static final ObjectMapper OBJECT_MAPPER = + new ObjectMapper().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); + + public static final String CLIENT_CREDENTIAL_PROVIDER_PREFIX = + "externalCatalog.glue.credentials.provider."; + + @JsonProperty("externalCatalog.glue.catalogId") + private String catalogId; + + @JsonProperty("externalCatalog.glue.region") + private String region; + + @JsonProperty("externalCatalog.glue.credentialsProviderClass") + private String clientCredentialsProviderClass; Review Comment: can these be final? ########## xtable-aws/src/main/java/org/apache/xtable/glue/GlueSchemaExtractor.java: ########## @@ -0,0 +1,216 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.xtable.glue; + +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Locale; +import java.util.Map; +import java.util.Set; +import java.util.stream.Collectors; + +import lombok.AccessLevel; +import lombok.NoArgsConstructor; + +import org.apache.commons.lang3.StringUtils; + +import org.apache.hudi.common.util.VisibleForTesting; + +import com.google.common.collect.ImmutableMap; +import com.google.common.collect.Lists; +import com.google.common.collect.Sets; + +import org.apache.xtable.exception.NotSupportedException; +import org.apache.xtable.exception.SchemaExtractorException; +import org.apache.xtable.model.schema.InternalField; +import org.apache.xtable.model.schema.InternalSchema; + +import software.amazon.awssdk.services.glue.model.Column; +import software.amazon.awssdk.services.glue.model.Table; + +@NoArgsConstructor(access = AccessLevel.PRIVATE) +public class GlueSchemaExtractor { + private static final GlueSchemaExtractor INSTANCE = new GlueSchemaExtractor(); + private static final String FIELD_ID = "field.id"; + private static final String FIELD_OPTIONAL = "field.optional"; + private static final String FIELD_CURRENT = "field.current"; + + public static GlueSchemaExtractor getInstance() { + return INSTANCE; + } + + /** + * Extract column list from OneTable schema + * + * @param tableFormat tableFormat to handle format specific type conversion + * @param tableSchema OneTable schema + * @return glue table column list + */ + public List<Column> toColumns(String tableFormat, InternalSchema tableSchema) { + return toColumns(tableFormat, tableSchema, null); + } + + public List<Column> toColumns( + String tableFormat, InternalSchema tableSchema, Table existingTable) { + List<Column> columns = Lists.newArrayList(); + Set<String> addedNames = Sets.newHashSet(); + for (InternalField field : tableSchema.getFields()) { + if (!addedNames.contains(field.getName())) { + int fieldId = field.getFieldId() != null ? field.getFieldId() : -1; + Column.Builder builder = + Column.builder() + .name(field.getName()) + .type(toTypeString(field.getSchema(), tableFormat)) + .parameters( + ImmutableMap.of( + getColumnProperty(tableFormat, FIELD_ID), + Integer.toString(fieldId), + getColumnProperty(tableFormat, FIELD_OPTIONAL), + Boolean.toString(field.getSchema().isNullable()), + getColumnProperty(tableFormat, FIELD_CURRENT), + "true")); + + if (!StringUtils.isEmpty(field.getSchema().getComment())) { + builder.comment(field.getSchema().getComment()); + } + columns.add(builder.build()); + addedNames.add(field.getName()); + } + } + + // if there are columns in existing glueTable that are not part of tableSchema, + // include them by setting "field.current" property to false + List<Column> existingColumns = + existingTable != null && existingTable.storageDescriptor() != null + ? existingTable.storageDescriptor().columns() + : Collections.emptyList(); + for (Column column : existingColumns) { + if (!addedNames.contains(column.name())) { + Map<String, String> columnParams = new HashMap<>(); + if (column.hasParameters()) { + columnParams.putAll(column.parameters()); + } + columnParams.put(getColumnProperty(tableFormat, FIELD_CURRENT), "false"); + column = column.toBuilder().parameters(columnParams).build(); + columns.add(column); + addedNames.add(column.name()); + } + } + return columns; + } + + /** + * Get glue compatible column type from Onetable field schema Review Comment: Do a find and replace for all references to the old project name -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
