|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package org.apache.fluss.lake.paimon.utils; |
| 19 | + |
| 20 | +import org.apache.fluss.exception.TableAlreadyExistException; |
| 21 | + |
| 22 | +import org.apache.paimon.CoreOptions; |
| 23 | +import org.apache.paimon.schema.Schema; |
| 24 | + |
| 25 | +import java.util.HashMap; |
| 26 | +import java.util.Map; |
| 27 | + |
| 28 | +/** Validator of Paimon table schema. */ |
| 29 | +public class PaimonSchemaValidation { |
| 30 | + |
| 31 | + public static void validatePaimonSchemaCapability(Schema existingSchema, Schema newSchema) { |
| 32 | + // check fields |
| 33 | + if (!existingSchema.fields().equals(newSchema.fields())) { |
| 34 | + throw new TableAlreadyExistException( |
| 35 | + String.format( |
| 36 | + "The fields of the existing Paimon table are not compatible with those of the new table to be created. " |
| 37 | + + "Existing fields: %s, new fields: %s.", |
| 38 | + existingSchema.fields(), newSchema.fields())); |
| 39 | + } |
| 40 | + |
| 41 | + // check pks |
| 42 | + if (!existingSchema.primaryKeys().equals(newSchema.primaryKeys())) { |
| 43 | + throw new TableAlreadyExistException( |
| 44 | + String.format( |
| 45 | + "The primary keys of the existing Paimon table are not compatible with those of the new table to be created. " |
| 46 | + + "Existing primary keys: %s, new primary keys: %s.", |
| 47 | + existingSchema.primaryKeys(), newSchema.primaryKeys())); |
| 48 | + } |
| 49 | + |
| 50 | + // check partition keys |
| 51 | + if (!existingSchema.partitionKeys().equals(newSchema.partitionKeys())) { |
| 52 | + throw new TableAlreadyExistException( |
| 53 | + String.format( |
| 54 | + "The partition keys of the existing Paimon table are not compatible with those of the new table to be created. " |
| 55 | + + "Existing partition keys: %s, new partition keys: %s.", |
| 56 | + existingSchema.partitionKeys(), newSchema.partitionKeys())); |
| 57 | + } |
| 58 | + |
| 59 | + // check options |
| 60 | + Map<String, String> existingOptions = new HashMap<>(existingSchema.options()); |
| 61 | + // `path` will be set automatically by Paimon, so we need to remove it here |
| 62 | + existingOptions.remove(CoreOptions.PATH.key()); |
| 63 | + if (!existingOptions.equals(newSchema.options())) { |
| 64 | + throw new TableAlreadyExistException( |
| 65 | + String.format( |
| 66 | + "The options of the existing Paimon table are not compatible with those of the new table to be created. " |
| 67 | + + "Existing options: %s, new options: %s.", |
| 68 | + existingOptions, newSchema.options())); |
| 69 | + } |
| 70 | + } |
| 71 | +} |
0 commit comments