the-other-tim-brown commented on code in PR #632: URL: https://github.com/apache/incubator-xtable/pull/632#discussion_r1938686339
########## xtable-aws/src/main/java/org/apache/xtable/glue/GlueClientFactory.java: ########## @@ -0,0 +1,36 @@ +/* + * 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 software.amazon.awssdk.services.glue.GlueClient; + +/** + * Abstract factory for creating {@link GlueClient} instances configured with {@link Review Comment: Why do we need this abstract factory? Will there be other implementations? ########## xtable-api/src/main/java/org/apache/xtable/spi/extractor/CatalogConversionSource.java: ########## @@ -32,4 +35,7 @@ public interface CatalogConversionSource { /** Returns the {@link org.apache.xtable.model.storage.CatalogType} for the catalog conversion */ String getCatalogType(); + + /** Initializes the ConversionSource with provided configuration */ + void init(ExternalCatalogConfig catalogConfig, Configuration configuration); Review Comment: We'll need to call these new init methods after the service loader creates the instance ########## xtable-aws/src/main/java/org/apache/xtable/glue/GlueSchemaExtractor.java: ########## @@ -0,0 +1,225 @@ +/* + * 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 com.google.common.annotations.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 InternalTable schema + * + * @param tableFormat tableFormat to handle format specific type conversion + * @param tableSchema InternalTable 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())) { + columns.add(toColumn(field, tableFormat)); + 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; + } + + /** create Glue column from InternalField */ + @VisibleForTesting + protected Column toColumn(InternalField field, String tableFormat) { + 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")); + + String comment = field.getSchema().getComment(); + if (!StringUtils.isEmpty(comment)) { + // Glue has restriction on column comment to not exceed 255 chars + // https://docs.aws.amazon.com/glue/latest/webapi/API_Column.html + comment = (comment.length() > 255) ? comment.substring(0, 255) : comment; Review Comment: Should we log a warning that the comment is being truncated? -- 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]
