|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with 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, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +package org.apache.doris.connector.paimon; |
| 19 | + |
| 20 | +import org.apache.commons.lang3.StringUtils; |
| 21 | +import org.apache.paimon.CoreOptions; |
| 22 | +import org.apache.paimon.options.ConfigOption; |
| 23 | +import org.apache.paimon.options.FallbackKey; |
| 24 | +import org.apache.paimon.options.Options; |
| 25 | + |
| 26 | +import java.util.Collections; |
| 27 | +import java.util.HashMap; |
| 28 | +import java.util.LinkedHashMap; |
| 29 | +import java.util.Locale; |
| 30 | +import java.util.Map; |
| 31 | + |
| 32 | +/** |
| 33 | + * The {@code paimon.table-option.*} catalog-property namespace (upstream #65955): a catalog-level |
| 34 | + * default for any Paimon {@link CoreOptions} table option, e.g. |
| 35 | + * {@code "paimon.table-option.read.batch-size" = "4096"}. |
| 36 | + * |
| 37 | + * <p>Ports the fe-core {@code AbstractPaimonProperties} table-option members deleted by P5-T29 |
| 38 | + * ({@code TABLE_OPTION_PREFIX}, {@code extractTableOptions}, {@code validateTableOption}, |
| 39 | + * {@code getTableOptionsForCopy}, {@code SupportedTableOptions}) into the connector, which is the |
| 40 | + * only paimon path left. Kept in fe-connector-paimon (not fe-connector-metastore-paimon) for the |
| 41 | + * same reason {@link PaimonCatalogFactory} holds the ported {@code appendCatalogOptions}: every |
| 42 | + * consumer — validate at CREATE CATALOG, exclude from the catalog Options passthrough, apply on |
| 43 | + * table load — lives in this module, and no metastore flavor needs it. |
| 44 | + */ |
| 45 | +public final class PaimonTableOptions { |
| 46 | + |
| 47 | + /** The suffix after this prefix is passed to Paimon as a dynamic table option. */ |
| 48 | + public static final String TABLE_OPTION_PREFIX = "paimon.table-option."; |
| 49 | + |
| 50 | + /** BE/JNI-scanner knobs; consumed by {@link PaimonScanPlanProvider}, never a catalog Option. */ |
| 51 | + public static final String JNI_PROPERTY_PREFIX = "paimon.jni."; |
| 52 | + |
| 53 | + /** Canonical and fallback {@link CoreOptions} names which support direct lookup. */ |
| 54 | + private static final Map<String, ConfigOption<?>> SUPPORTED_TABLE_OPTIONS = buildSupportedTableOptions(); |
| 55 | + |
| 56 | + private PaimonTableOptions() { |
| 57 | + } |
| 58 | + |
| 59 | + public static boolean isTableOptionProperty(String key) { |
| 60 | + return key.toLowerCase(Locale.ROOT).startsWith(TABLE_OPTION_PREFIX); |
| 61 | + } |
| 62 | + |
| 63 | + public static boolean isJniProperty(String key) { |
| 64 | + return key.toLowerCase(Locale.ROOT).startsWith(JNI_PROPERTY_PREFIX); |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * Strips {@link #TABLE_OPTION_PREFIX} off every table-option property and validates each one |
| 69 | + * against the bundled Paimon version, so a typo or a bad value fails the CREATE/ALTER CATALOG |
| 70 | + * instead of surfacing later as a query error. |
| 71 | + * |
| 72 | + * @throws IllegalArgumentException on an empty, unknown, or unparseable option |
| 73 | + */ |
| 74 | + public static Map<String, String> extract(Map<String, String> props) { |
| 75 | + Map<String, String> tableOptions = new LinkedHashMap<>(); |
| 76 | + props.forEach((key, value) -> { |
| 77 | + if (isTableOptionProperty(key)) { |
| 78 | + String tableOptionKey = key.substring(TABLE_OPTION_PREFIX.length()); |
| 79 | + if (StringUtils.isBlank(tableOptionKey)) { |
| 80 | + throw new IllegalArgumentException( |
| 81 | + "Paimon table option name must not be empty after prefix " + TABLE_OPTION_PREFIX); |
| 82 | + } |
| 83 | + validate(tableOptionKey, value); |
| 84 | + tableOptions.put(tableOptionKey, value); |
| 85 | + } |
| 86 | + }); |
| 87 | + return Collections.unmodifiableMap(tableOptions); |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * Narrows {@code tableOptions} to those the Paimon table does not already set itself, i.e. the |
| 92 | + * catalog-level default only fills a gap and never overrides an explicit table property. |
| 93 | + * |
| 94 | + * <p>The comparison goes through the Paimon {@link ConfigOption}, so canonical and fallback keys |
| 95 | + * follow the same precedence rule. |
| 96 | + */ |
| 97 | + public static Map<String, String> forCopy(Map<String, String> tableOptions, |
| 98 | + Map<String, String> currentTableOptions) { |
| 99 | + if (tableOptions.isEmpty() || currentTableOptions.isEmpty()) { |
| 100 | + return tableOptions; |
| 101 | + } |
| 102 | + |
| 103 | + Options existingOptions = new Options(currentTableOptions); |
| 104 | + Map<String, String> optionsForCopy = new LinkedHashMap<>(); |
| 105 | + tableOptions.forEach((key, value) -> { |
| 106 | + ConfigOption<?> option = SUPPORTED_TABLE_OPTIONS.get(key); |
| 107 | + if (!existingOptions.contains(option)) { |
| 108 | + optionsForCopy.put(key, value); |
| 109 | + } |
| 110 | + }); |
| 111 | + return Collections.unmodifiableMap(optionsForCopy); |
| 112 | + } |
| 113 | + |
| 114 | + private static void validate(String key, String value) { |
| 115 | + ConfigOption<?> option = SUPPORTED_TABLE_OPTIONS.get(key); |
| 116 | + if (option == null) { |
| 117 | + throw new IllegalArgumentException("Unsupported Paimon table option '" + key |
| 118 | + + "' for the bundled Paimon version"); |
| 119 | + } |
| 120 | + |
| 121 | + try { |
| 122 | + new Options(Collections.singletonMap(key, value)).get(option); |
| 123 | + } catch (IllegalArgumentException e) { |
| 124 | + throw new IllegalArgumentException("Invalid value for Paimon table option '" + key + "': " |
| 125 | + + e.getMessage(), e); |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + private static Map<String, ConfigOption<?>> buildSupportedTableOptions() { |
| 130 | + Map<String, ConfigOption<?>> exactOptions = new HashMap<>(); |
| 131 | + for (ConfigOption<?> option : CoreOptions.getOptions()) { |
| 132 | + exactOptions.put(option.key(), option); |
| 133 | + for (FallbackKey fallbackKey : option.fallbackKeys()) { |
| 134 | + exactOptions.put(fallbackKey.getKey(), option); |
| 135 | + } |
| 136 | + } |
| 137 | + return Collections.unmodifiableMap(exactOptions); |
| 138 | + } |
| 139 | +} |
0 commit comments