|
| 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.catalog.Identifier; |
| 24 | +import org.apache.paimon.options.ConfigOption; |
| 25 | +import org.apache.paimon.schema.Schema; |
| 26 | +import org.apache.paimon.table.FileStoreTable; |
| 27 | + |
| 28 | +import java.lang.reflect.Field; |
| 29 | +import java.util.HashMap; |
| 30 | +import java.util.Map; |
| 31 | + |
| 32 | +import static org.apache.fluss.lake.paimon.utils.PaimonConversions.FLUSS_CONF_PREFIX; |
| 33 | + |
| 34 | +/** Utils to verify whether the existing Paimon table is compatible with the table to be created. */ |
| 35 | +public class PaimonTableValidation { |
| 36 | + |
| 37 | + private static final Map<String, ConfigOption<?>> PAIMON_CONFIGS = extractPaimonConfigs(); |
| 38 | + |
| 39 | + public static void validatePaimonSchemaCompatible( |
| 40 | + Identifier tablePath, Schema existingSchema, Schema newSchema) { |
| 41 | + // Adjust options for comparison |
| 42 | + Map<String, String> existingOptions = existingSchema.options(); |
| 43 | + Map<String, String> newOptions = newSchema.options(); |
| 44 | + |
| 45 | + // when enable datalake with an existing table, `table.datalake.enabled` will be `false` |
| 46 | + // in existing options, but `true` in new options. |
| 47 | + String datalakeConfigKey = |
| 48 | + FLUSS_CONF_PREFIX |
| 49 | + + org.apache.fluss.config.ConfigOptions.TABLE_DATALAKE_ENABLED.key(); |
| 50 | + if (Boolean.FALSE.toString().equalsIgnoreCase(existingOptions.get(datalakeConfigKey))) { |
| 51 | + existingOptions.remove(datalakeConfigKey); |
| 52 | + newOptions.remove(datalakeConfigKey); |
| 53 | + } |
| 54 | + |
| 55 | + // remove changeable options |
| 56 | + removeChangeableOptions(existingOptions); |
| 57 | + removeChangeableOptions(newOptions); |
| 58 | + |
| 59 | + // ignore the existing options that are not in new options |
| 60 | + existingOptions.entrySet().removeIf(entry -> !newOptions.containsKey(entry.getKey())); |
| 61 | + |
| 62 | + if (!existingSchema.equals(newSchema)) { |
| 63 | + throw new TableAlreadyExistException( |
| 64 | + String.format( |
| 65 | + "The table %s already exists in Paimon catalog, but the table schema is not compatible. " |
| 66 | + + "Existing schema: %s, new schema: %s. " |
| 67 | + + "Please first drop the table in Paimon catalog or use a new table name.", |
| 68 | + tablePath.getEscapedFullName(), existingSchema, newSchema)); |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + private static void removeChangeableOptions(Map<String, String> options) { |
| 73 | + options.entrySet() |
| 74 | + .removeIf( |
| 75 | + entry -> |
| 76 | + // currently we take all Paimon options and Fluss option as |
| 77 | + // unchangeable. |
| 78 | + !PAIMON_CONFIGS.containsKey(entry.getKey()) |
| 79 | + && !entry.getKey().startsWith(FLUSS_CONF_PREFIX)); |
| 80 | + } |
| 81 | + |
| 82 | + public static void checkTableIsEmpty(Identifier tablePath, FileStoreTable table) { |
| 83 | + if (table.latestSnapshot().isPresent()) { |
| 84 | + throw new TableAlreadyExistException( |
| 85 | + String.format( |
| 86 | + "The table %s already exists in Paimon catalog, and the table is not empty. " |
| 87 | + + "Please first drop the table in Paimon catalog or use a new table name.", |
| 88 | + tablePath.getEscapedFullName())); |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + private static Map<String, ConfigOption<?>> extractPaimonConfigs() { |
| 93 | + Map<String, ConfigOption<?>> options = new HashMap<>(); |
| 94 | + |
| 95 | + Field[] fields = CoreOptions.class.getFields(); |
| 96 | + for (Field field : fields) { |
| 97 | + if (!ConfigOption.class.isAssignableFrom(field.getType())) { |
| 98 | + continue; |
| 99 | + } |
| 100 | + |
| 101 | + try { |
| 102 | + ConfigOption<?> configOption = (ConfigOption<?>) field.get(null); |
| 103 | + options.put(configOption.key(), configOption); |
| 104 | + } catch (IllegalAccessException e) { |
| 105 | + throw new RuntimeException( |
| 106 | + "Unable to extract ConfigOption fields from CoreOptions class.", e); |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + return options; |
| 111 | + } |
| 112 | +} |
0 commit comments