|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | +package org.apache.beam.sdk.io.gcp.bigquery; |
| 19 | + |
| 20 | +import com.google.auto.value.AutoValue; |
| 21 | +import com.google.cloud.bigquery.storage.v1.TableFieldSchema; |
| 22 | +import com.google.cloud.bigquery.storage.v1.TableSchema; |
| 23 | +import java.util.List; |
| 24 | +import java.util.Map; |
| 25 | +import java.util.Optional; |
| 26 | +import java.util.Set; |
| 27 | +import java.util.stream.Collectors; |
| 28 | +import javax.annotation.Nullable; |
| 29 | +import org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.Lists; |
| 30 | +import org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.Sets; |
| 31 | + |
| 32 | +/** Helper utilities for handling schema-update responses. */ |
| 33 | +public class TableSchemaUpdateUtils { |
| 34 | + /* |
| 35 | + Given an original schema and an updated schema, return a schema that should be used to process future records. |
| 36 | + This function returns: |
| 37 | + - If the new schema is not compatible (e.g. missing fields), then it will return Optional.empty(). |
| 38 | + - If the new schema is equivalent (i.e. equal modulo field ordering) to the old schema, then it will return |
| 39 | + Optional.empty(). |
| 40 | + - The returned schema will always contain the old schema as a prefix. This ensures that if any of the old |
| 41 | + fields are reordered in the new schema, we maintain the old order. |
| 42 | + */ |
| 43 | + public static Optional<TableSchema> getUpdatedSchema( |
| 44 | + TableSchema oldSchema, TableSchema newSchema) { |
| 45 | + Result updatedFields = getUpdatedSchema(oldSchema.getFieldsList(), newSchema.getFieldsList()); |
| 46 | + if (updatedFields.isEquivalent()) { |
| 47 | + return Optional.empty(); |
| 48 | + } else { |
| 49 | + return updatedFields |
| 50 | + .getFields() |
| 51 | + .map( |
| 52 | + tableFieldSchemas -> |
| 53 | + TableSchema.newBuilder().addAllFields(tableFieldSchemas).build()); |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + @AutoValue |
| 58 | + abstract static class Result { |
| 59 | + abstract Optional<List<TableFieldSchema>> getFields(); |
| 60 | + |
| 61 | + abstract boolean isEquivalent(); |
| 62 | + |
| 63 | + static Result of(List<TableFieldSchema> fields, boolean isEquivalent) { |
| 64 | + return new AutoValue_TableSchemaUpdateUtils_Result(Optional.of(fields), isEquivalent); |
| 65 | + } |
| 66 | + |
| 67 | + static Result empty() { |
| 68 | + return new AutoValue_TableSchemaUpdateUtils_Result(Optional.empty(), false); |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + private static Result getUpdatedSchema( |
| 73 | + @Nullable List<TableFieldSchema> oldSchema, @Nullable List<TableFieldSchema> newSchema) { |
| 74 | + if (newSchema == null) { |
| 75 | + return Result.empty(); |
| 76 | + } |
| 77 | + if (oldSchema == null) { |
| 78 | + return Result.of(newSchema, false); |
| 79 | + } |
| 80 | + |
| 81 | + Map<String, TableFieldSchema> newSchemaMap = |
| 82 | + newSchema.stream().collect(Collectors.toMap(TableFieldSchema::getName, x -> x)); |
| 83 | + Set<String> fieldNamesPopulated = Sets.newHashSet(); |
| 84 | + List<TableFieldSchema> updatedSchema = Lists.newArrayList(); |
| 85 | + boolean isEquivalent = oldSchema.size() == newSchema.size(); |
| 86 | + for (TableFieldSchema tableFieldSchema : oldSchema) { |
| 87 | + @Nullable TableFieldSchema newTableFieldSchema = newSchemaMap.get(tableFieldSchema.getName()); |
| 88 | + if (newTableFieldSchema == null) { |
| 89 | + // We don't support deleting fields! |
| 90 | + return Result.empty(); |
| 91 | + } |
| 92 | + TableFieldSchema.Builder updatedTableFieldSchema = newTableFieldSchema.toBuilder(); |
| 93 | + updatedTableFieldSchema.clearFields(); |
| 94 | + if (tableFieldSchema.getType().equals(TableFieldSchema.Type.STRUCT)) { |
| 95 | + Result updatedTableFields = |
| 96 | + getUpdatedSchema(tableFieldSchema.getFieldsList(), newTableFieldSchema.getFieldsList()); |
| 97 | + if (!updatedTableFields.getFields().isPresent()) { |
| 98 | + return updatedTableFields; |
| 99 | + } |
| 100 | + updatedTableFieldSchema.addAllFields(updatedTableFields.getFields().get()); |
| 101 | + isEquivalent = isEquivalent && updatedTableFields.isEquivalent(); |
| 102 | + isEquivalent = |
| 103 | + isEquivalent |
| 104 | + && tableFieldSchema |
| 105 | + .toBuilder() |
| 106 | + .clearFields() |
| 107 | + .build() |
| 108 | + .equals(newTableFieldSchema.toBuilder().clearFields().build()); |
| 109 | + } else { |
| 110 | + isEquivalent = isEquivalent && tableFieldSchema.equals(newTableFieldSchema); |
| 111 | + } |
| 112 | + updatedSchema.add(updatedTableFieldSchema.build()); |
| 113 | + fieldNamesPopulated.add(updatedTableFieldSchema.getName()); |
| 114 | + } |
| 115 | + |
| 116 | + // Add in new fields at the end of the schema. |
| 117 | + newSchema.stream() |
| 118 | + .filter(f -> !fieldNamesPopulated.contains(f.getName())) |
| 119 | + .forEach(updatedSchema::add); |
| 120 | + return Result.of(updatedSchema, isEquivalent); |
| 121 | + } |
| 122 | +} |
0 commit comments