yirutang commented on code in PR #26794: URL: https://github.com/apache/beam/pull/26794#discussion_r1199496151
########## sdks/java/io/google-cloud-platform/src/main/java/org/apache/beam/sdk/io/gcp/bigquery/TableSchemaUpdateUtils.java: ########## @@ -0,0 +1,92 @@ +/* + * 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.beam.sdk.io.gcp.bigquery; + +import com.google.cloud.bigquery.storage.v1.TableFieldSchema; +import com.google.cloud.bigquery.storage.v1.TableSchema; +import java.util.List; +import java.util.Map; +import java.util.Optional; +import java.util.Set; +import java.util.stream.Collectors; +import javax.annotation.Nullable; +import org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.Lists; +import org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.Sets; + +/** Helper utilities for handling schema-update responses. */ +public class TableSchemaUpdateUtils { + /* + Given an original schema and an updated schema, return a schema that should be used to process future records. + This function returns: + - If the new schema is not compatible (e.g. missing fields), then it will return Optional.empty(). + - The returned schema will always contain the old schema as a prefix. This ensures that if any of the old + fields are reordered in the new schema, we maintain the old order. + */ + public static Optional<TableSchema> getUpdatedSchema( + TableSchema oldSchema, TableSchema newSchema) { + Optional<List<TableFieldSchema>> updatedFields = + getUpdatedSchema(oldSchema.getFieldsList(), newSchema.getFieldsList()); + return updatedFields.map( + tableFieldSchemas -> TableSchema.newBuilder().addAllFields(tableFieldSchemas).build()); + } + + private static Optional<List<TableFieldSchema>> getUpdatedSchema( + @Nullable List<TableFieldSchema> oldSchema, @Nullable List<TableFieldSchema> newSchema) { + if (newSchema == null) { + return Optional.empty(); + } + if (oldSchema == null) { + return Optional.of(newSchema); + } + + Map<String, TableFieldSchema> newSchemaMap = + newSchema.stream().collect(Collectors.toMap(TableFieldSchema::getName, x -> x)); + Set<String> fieldNamesPopulated = Sets.newHashSet(); + List<TableFieldSchema> updatedSchema = Lists.newArrayList(); + for (TableFieldSchema tableFieldSchema : oldSchema) { + @Nullable TableFieldSchema newTableFieldSchema = newSchemaMap.get(tableFieldSchema.getName()); + if (newTableFieldSchema == null) { + // We don't support deleting fields! + return Optional.empty(); + } + TableFieldSchema.Builder updatedTableFieldSchema = newTableFieldSchema.toBuilder(); + updatedTableFieldSchema.clearFields(); + if (tableFieldSchema.getType().equals(TableFieldSchema.Type.STRUCT)) { + Optional<List<TableFieldSchema>> updatedTableFields = + getUpdatedSchema(tableFieldSchema.getFieldsList(), newTableFieldSchema.getFieldsList()); + if (!updatedTableFields.isPresent()) { + return Optional.empty(); Review Comment: is this return too aggresive? -- 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]
