This is an automated email from the ASF dual-hosted git repository.
JingsongLi pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/paimon.git
The following commit(s) were added to refs/heads/master by this push:
new 5a4aafa627 [core] Support entropy-inject-template external paths
(#10978)
5a4aafa627 is described below
commit 5a4aafa627c1f3ac410df055a999fdf8ced3bc58
Author: Liurnly <[email protected]>
AuthorDate: Tue Nov 24 12:03:51 2026 +0800
[core] Support entropy-inject-template external paths (#10978)
---
docs/generated/core_configuration.html | 2 +-
.../main/java/org/apache/paimon/CoreOptions.java | 7 +
.../fs/EntropyInjectExternalPathProvider.java | 4 +-
.../org/apache/paimon/fs/ExternalPathProvider.java | 3 +
.../TemplateEntropyInjectExternalPathProvider.java | 88 +++++
...plateEntropyInjectExternalPathProviderTest.java | 105 +++++
.../org/apache/paimon/catalog/AbstractCatalog.java | 15 +-
.../paimon/schema/FileSystemSchemaManager.java | 3 +
.../apache/paimon/schema/SchemaManagerUtils.java | 8 +
.../org/apache/paimon/schema/SchemaValidation.java | 41 ++
.../paimon/catalog/FileSystemCatalogTest.java | 46 +++
.../operation/EntropyInjectTemplateTest.java | 440 +++++++++++++++++++++
12 files changed, 758 insertions(+), 4 deletions(-)
diff --git a/docs/generated/core_configuration.html
b/docs/generated/core_configuration.html
index 2578dd8560..d5d6d9e6f1 100644
--- a/docs/generated/core_configuration.html
+++ b/docs/generated/core_configuration.html
@@ -654,7 +654,7 @@ under the License.
<td><h5>data-file.external-paths.strategy</h5></td>
<td style="word-wrap: break-word;">none</td>
<td><p>Enum</p></td>
- <td>The strategy of selecting an external path when writing
data.<br /><br />Possible values:<ul><li>"none": Do not choose any external
storage, data will still be written to the default warehouse
path.</li><li>"specific-fs": Select a specific file system as the external
path. Currently supported are S3 and OSS.</li><li>"round-robin": When writing a
new file, a path is chosen from data-file.external-paths in
turn.</li><li>"entropy-inject": When writing a new file, a path is c [...]
+ <td>The strategy of selecting an external path when writing
data.<br /><br />Possible values:<ul><li>"none": Do not choose any external
storage, data will still be written to the default warehouse
path.</li><li>"specific-fs": Select a specific file system as the external
path. Currently supported are S3 and OSS.</li><li>"round-robin": When writing a
new file, a path is chosen from data-file.external-paths in
turn.</li><li>"entropy-inject": When writing a new file, a path is c [...]
</tr>
<tr>
<td><h5>data-file.external-paths.weights</h5></td>
diff --git a/paimon-api/src/main/java/org/apache/paimon/CoreOptions.java
b/paimon-api/src/main/java/org/apache/paimon/CoreOptions.java
index aa511a20e1..faaeeb9b7e 100644
--- a/paimon-api/src/main/java/org/apache/paimon/CoreOptions.java
+++ b/paimon-api/src/main/java/org/apache/paimon/CoreOptions.java
@@ -6061,6 +6061,13 @@ public class CoreOptions implements Serializable {
"entropy-inject",
"When writing a new file, a path is chosen based on the hash
value of the file's content."),
+ ENTROPY_INJECT_TEMPLATE(
+ "entropy-inject-template",
+ "Uses a single path template in data-file.external-paths
containing a '{hash}' placeholder. "
+ + "The placeholder is replaced in place by hash
directories computed from the file name. "
+ + "Place '{hash}' before the database and table
directories to share object storage prefixes across tables. "
+ + "This strategy cannot be enabled or disabled on an
existing table."),
+
WEIGHTED(
"weight-robin",
"When writing a new file, a path is chosen based on configured
weights.");
diff --git
a/paimon-common/src/main/java/org/apache/paimon/fs/EntropyInjectExternalPathProvider.java
b/paimon-common/src/main/java/org/apache/paimon/fs/EntropyInjectExternalPathProvider.java
index da44099793..4cbf392f63 100644
---
a/paimon-common/src/main/java/org/apache/paimon/fs/EntropyInjectExternalPathProvider.java
+++
b/paimon-common/src/main/java/org/apache/paimon/fs/EntropyInjectExternalPathProvider.java
@@ -57,7 +57,7 @@ public class EntropyInjectExternalPathProvider implements
ExternalPathProvider {
return new Path(externalTablePaths.get(position),
filePathWithHashDirs);
}
- public String computeHash(String fileName) {
+ public static String computeHash(String fileName) {
HashCode hashCode = HASH_FUNC.hashString(fileName,
StandardCharsets.UTF_8);
String hashAsBinaryString = Integer.toBinaryString(hashCode.asInt() |
Integer.MIN_VALUE);
String hash =
@@ -71,7 +71,7 @@ public class EntropyInjectExternalPathProvider implements
ExternalPathProvider {
* @param hash 10011001100110011001
* @return 1001/1001/1001/10011001 with depth 3 and length 4
*/
- private String dirsFromHash(String hash) {
+ private static String dirsFromHash(String hash) {
StringBuilder hashWithDirs = new StringBuilder();
for (int i = 0; i < ENTROPY_DIR_DEPTH * ENTROPY_DIR_LENGTH; i +=
ENTROPY_DIR_LENGTH) {
diff --git
a/paimon-common/src/main/java/org/apache/paimon/fs/ExternalPathProvider.java
b/paimon-common/src/main/java/org/apache/paimon/fs/ExternalPathProvider.java
index 8ad617f8a7..efcb1515a6 100644
--- a/paimon-common/src/main/java/org/apache/paimon/fs/ExternalPathProvider.java
+++ b/paimon-common/src/main/java/org/apache/paimon/fs/ExternalPathProvider.java
@@ -40,6 +40,9 @@ public interface ExternalPathProvider extends Serializable {
case ENTROPY_INJECT:
return new EntropyInjectExternalPathProvider(
externalTablePaths, relativeBucketPath);
+ case ENTROPY_INJECT_TEMPLATE:
+ return new TemplateEntropyInjectExternalPathProvider(
+ externalTablePaths, relativeBucketPath);
case SPECIFIC_FS:
// specific fs can use round-robin with only one path
case ROUND_ROBIN:
diff --git
a/paimon-common/src/main/java/org/apache/paimon/fs/TemplateEntropyInjectExternalPathProvider.java
b/paimon-common/src/main/java/org/apache/paimon/fs/TemplateEntropyInjectExternalPathProvider.java
new file mode 100644
index 0000000000..6542c27594
--- /dev/null
+++
b/paimon-common/src/main/java/org/apache/paimon/fs/TemplateEntropyInjectExternalPathProvider.java
@@ -0,0 +1,88 @@
+/*
+ * 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.paimon.fs;
+
+import javax.annotation.concurrent.ThreadSafe;
+
+import java.util.List;
+
+import static
org.apache.paimon.fs.EntropyInjectExternalPathProvider.computeHash;
+import static org.apache.paimon.utils.Preconditions.checkArgument;
+
+/**
+ * Provider for external data paths which injects the hash directories into a
path template.
+ *
+ * <p>{@link EntropyInjectExternalPathProvider} appends the hash directories
after the table
+ * context, so the object keys of a table all share one table-specific prefix
and the storage
+ * partitions warmed up for that prefix cannot be shared with other tables.
This provider instead
+ * replaces the {@value #HASH_PLACEHOLDER} placeholder inside the configured
template, which lets
+ * the hash appear before the database and table directories:
+ *
+ * <pre>
+ * template: s3://bucket-a/{hash}/my_db.db/my_table
+ * expanded: s3://bucket-a/1001/1001/1001/10011001/my_db.db/my_table
+ * file:
s3://bucket-a/1001/1001/1001/10011001/my_db.db/my_table/dt=2026-11-02/bucket-0/data-xxx.parquet
+ * </pre>
+ *
+ * <p>Multiple databases and tables pointing at the same template root
therefore share the same hash
+ * prefixes.
+ *
+ * <p>Only a single template is accepted. The placeholder is replaced in place
with hash directories
+ * computed from the file name. No path rotation or mutable state is needed.
+ */
+@ThreadSafe
+public class TemplateEntropyInjectExternalPathProvider implements
ExternalPathProvider {
+
+ public static final String HASH_PLACEHOLDER = "{hash}";
+
+ private final Path externalTablePathTemplate;
+ private final Path relativeBucketPath;
+
+ public TemplateEntropyInjectExternalPathProvider(
+ List<Path> externalTablePathTemplates, Path relativeBucketPath) {
+ checkArgument(
+ externalTablePathTemplates.size() == 1,
+ "Only a single external path template is supported, but got
%s.",
+ externalTablePathTemplates);
+ Path template = externalTablePathTemplates.get(0);
+ checkTemplate(template);
+ this.externalTablePathTemplate = template;
+ this.relativeBucketPath = relativeBucketPath;
+ }
+
+ @Override
+ public Path getNextExternalDataPath(String fileName) {
+ Path externalTablePath = expand(externalTablePathTemplate, fileName);
+ return new Path(new Path(externalTablePath, relativeBucketPath),
fileName);
+ }
+
+ /** Validates that the normalized URI path contains the hash placeholder.
*/
+ public static void checkTemplate(Path template) {
+ String path = template.toUri().getPath();
+ checkArgument(
+ path != null && path.contains(HASH_PLACEHOLDER),
+ "External path '%s' must contain the '%s' placeholder.",
+ template,
+ HASH_PLACEHOLDER);
+ }
+
+ private static Path expand(Path template, String fileName) {
+ return new Path(template.toString().replace(HASH_PLACEHOLDER,
computeHash(fileName)));
+ }
+}
diff --git
a/paimon-common/src/test/java/org/apache/paimon/fs/TemplateEntropyInjectExternalPathProviderTest.java
b/paimon-common/src/test/java/org/apache/paimon/fs/TemplateEntropyInjectExternalPathProviderTest.java
new file mode 100644
index 0000000000..705a5b695f
--- /dev/null
+++
b/paimon-common/src/test/java/org/apache/paimon/fs/TemplateEntropyInjectExternalPathProviderTest.java
@@ -0,0 +1,105 @@
+/*
+ * 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.paimon.fs;
+
+import org.junit.jupiter.api.Test;
+
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.List;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+
+/** Tests for {@link TemplateEntropyInjectExternalPathProvider}. */
+public class TemplateEntropyInjectExternalPathProviderTest {
+
+ private static final Path RELATIVE_BUCKET_PATH = new
Path("dt=2026-11-02/bucket-0");
+
+ @Test
+ public void testHashIsInjectedBeforeTableContext() {
+ TemplateEntropyInjectExternalPathProvider provider =
+ new TemplateEntropyInjectExternalPathProvider(
+ Collections.singletonList(
+ new
Path("s3://bucket-a/{hash}/my_db.db/my_table")),
+ RELATIVE_BUCKET_PATH);
+
+ for (int i = 0; i < 20; i++) {
+ String fileName = "data-" + i + ".parquet";
+ String hashDirs =
EntropyInjectExternalPathProvider.computeHash(fileName);
+ assertThat(provider.getNextExternalDataPath(fileName))
+ .isEqualTo(
+ new Path(
+ "s3://bucket-a/"
+ + hashDirs
+ +
"/my_db.db/my_table/dt=2026-11-02/bucket-0/"
+ + fileName));
+ }
+ }
+
+ @Test
+ public void testTablesShareTheSamePrefix() {
+ String fileName = "data-0.parquet";
+ String hashDirs =
EntropyInjectExternalPathProvider.computeHash(fileName);
+
+ Path first =
+ new TemplateEntropyInjectExternalPathProvider(
+ Collections.singletonList(
+ new
Path("s3://bucket-a/{hash}/db1.db/table1")),
+ new Path("bucket-0"))
+ .getNextExternalDataPath(fileName);
+ Path second =
+ new TemplateEntropyInjectExternalPathProvider(
+ Collections.singletonList(
+ new
Path("s3://bucket-a/{hash}/db2.db/table2")),
+ new Path("bucket-0"))
+ .getNextExternalDataPath(fileName);
+
+ String sharedPrefix = "s3://bucket-a/" + hashDirs;
+ assertThat(first.toString()).startsWith(sharedPrefix +
"/db1.db/table1/");
+ assertThat(second.toString()).startsWith(sharedPrefix +
"/db2.db/table2/");
+ }
+
+ @Test
+ public void testMultipleTemplatesRejected() {
+ List<Path> templates =
+ Arrays.asList(
+ new Path("s3://bucket-a/{hash}/my_db.db/my_table"),
+ new Path("s3://bucket-b/{hash}/my_db.db/my_table"));
+
+ assertThatThrownBy(
+ () ->
+ new TemplateEntropyInjectExternalPathProvider(
+ templates, new Path("bucket-0")))
+ .isInstanceOf(IllegalArgumentException.class)
+ .hasMessageContaining("a single external path template");
+ }
+
+ @Test
+ public void testTemplateWithoutPlaceholder() {
+ assertThatThrownBy(
+ () ->
+ new TemplateEntropyInjectExternalPathProvider(
+ Collections.singletonList(
+ new
Path("s3://bucket-a/my_db.db/my_table")),
+ RELATIVE_BUCKET_PATH))
+ .isInstanceOf(IllegalArgumentException.class)
+ .hasMessageContaining("{hash}");
+ }
+}
diff --git
a/paimon-core/src/main/java/org/apache/paimon/catalog/AbstractCatalog.java
b/paimon-core/src/main/java/org/apache/paimon/catalog/AbstractCatalog.java
index f0e3064693..350f4a0ade 100644
--- a/paimon-core/src/main/java/org/apache/paimon/catalog/AbstractCatalog.java
+++ b/paimon-core/src/main/java/org/apache/paimon/catalog/AbstractCatalog.java
@@ -19,6 +19,7 @@
package org.apache.paimon.catalog;
import org.apache.paimon.CoreOptions;
+import org.apache.paimon.CoreOptions.ExternalPathStrategy;
import org.apache.paimon.PagedList;
import org.apache.paimon.Snapshot;
import org.apache.paimon.TableType;
@@ -38,6 +39,7 @@ import org.apache.paimon.schema.FileSystemSchemaManager;
import org.apache.paimon.schema.Schema;
import org.apache.paimon.schema.SchemaChange;
import org.apache.paimon.schema.SchemaManager;
+import org.apache.paimon.schema.SchemaValidation;
import org.apache.paimon.schema.TableSchema;
import org.apache.paimon.table.FileStoreTable;
import org.apache.paimon.table.FormatTable;
@@ -71,6 +73,7 @@ import java.util.Set;
import java.util.stream.Collectors;
import static org.apache.paimon.CoreOptions.DATA_FILE_EXTERNAL_PATHS;
+import static org.apache.paimon.CoreOptions.DATA_FILE_EXTERNAL_PATHS_STRATEGY;
import static org.apache.paimon.CoreOptions.GLOBAL_INDEX_EXTERNAL_PATH;
import static org.apache.paimon.CoreOptions.PATH;
import static org.apache.paimon.CoreOptions.TYPE;
@@ -415,9 +418,16 @@ public abstract class AbstractCatalog implements Catalog {
.flatMap(
schema -> {
Map<String, String> options =
schema.toSchema().options();
+ String strategy =
options.get(DATA_FILE_EXTERNAL_PATHS_STRATEGY.key());
+ boolean template =
+
ExternalPathStrategy.ENTROPY_INJECT_TEMPLATE
+ .toString()
+ .equalsIgnoreCase(strategy);
return Arrays.stream(
new String[] {
-
options.get(DATA_FILE_EXTERNAL_PATHS.key()),
+ template
+ ? null
+ :
options.get(DATA_FILE_EXTERNAL_PATHS.key()),
options.get(GLOBAL_INDEX_EXTERNAL_PATH.key())
});
})
@@ -560,6 +570,9 @@ public abstract class AbstractCatalog implements Catalog {
+ "). Drop and re-create the table instead.");
}
+ String strategy = DATA_FILE_EXTERNAL_PATHS_STRATEGY.key();
+ SchemaValidation.validateExternalPathStrategyChange(
+ existing.options().get(strategy),
newSchema.options().get(strategy));
replaceTableImpl(identifier, (FileStoreTable) existing, newSchema);
}
diff --git
a/paimon-core/src/main/java/org/apache/paimon/schema/FileSystemSchemaManager.java
b/paimon-core/src/main/java/org/apache/paimon/schema/FileSystemSchemaManager.java
index 8eb6ecec9b..85e5c62eeb 100644
---
a/paimon-core/src/main/java/org/apache/paimon/schema/FileSystemSchemaManager.java
+++
b/paimon-core/src/main/java/org/apache/paimon/schema/FileSystemSchemaManager.java
@@ -277,6 +277,9 @@ public class FileSystemSchemaManager implements
SchemaManager {
public boolean commit(TableSchema newSchema) throws Exception {
Optional<TableSchema> previous = latest();
if (previous.isPresent()) {
+ String strategy =
CoreOptions.DATA_FILE_EXTERNAL_PATHS_STRATEGY.key();
+ SchemaValidation.validateExternalPathStrategyChange(
+ previous.get().options().get(strategy),
newSchema.options().get(strategy));
SchemaValidation.validateGeneratedColumnEvolution(previous.get(),
newSchema);
}
SchemaValidation.validateTableSchema(newSchema);
diff --git
a/paimon-core/src/main/java/org/apache/paimon/schema/SchemaManagerUtils.java
b/paimon-core/src/main/java/org/apache/paimon/schema/SchemaManagerUtils.java
index e6021a2448..43fdb65f7f 100644
--- a/paimon-core/src/main/java/org/apache/paimon/schema/SchemaManagerUtils.java
+++ b/paimon-core/src/main/java/org/apache/paimon/schema/SchemaManagerUtils.java
@@ -74,6 +74,7 @@ import java.util.stream.Collectors;
import static org.apache.paimon.CoreOptions.AGG_FUNCTION;
import static org.apache.paimon.CoreOptions.BUCKET_KEY;
import static org.apache.paimon.CoreOptions.CLUSTERING_COLUMNS;
+import static org.apache.paimon.CoreOptions.DATA_FILE_EXTERNAL_PATHS_STRATEGY;
import static org.apache.paimon.CoreOptions.DELETION_VECTORS_ENABLED;
import static org.apache.paimon.CoreOptions.DELETION_VECTORS_MODIFIABLE;
import static org.apache.paimon.CoreOptions.DISTINCT;
@@ -468,6 +469,9 @@ final class SchemaManagerUtils {
newSchema.primaryKeys(),
newSchema.options(),
newSchema.comment());
+ String strategy = DATA_FILE_EXTERNAL_PATHS_STRATEGY.key();
+ SchemaValidation.validateExternalPathStrategyChange(
+ oldOptions.get(strategy),
newTableSchema.options().get(strategy));
SchemaValidation.validateGeneratedColumnEvolution(oldTableSchema,
newTableSchema);
SchemaValidation.validateTableSchema(newTableSchema);
return newTableSchema;
@@ -1065,6 +1069,10 @@ final class SchemaManagerUtils {
String.format("Change '%s' is not supported yet.", key));
}
+ if (DATA_FILE_EXTERNAL_PATHS_STRATEGY.key().equals(key)) {
+ SchemaValidation.validateExternalPathStrategyChange(oldValue,
newValue);
+ }
+
if (CoreOptions.DATA_EVOLUTION_NESTED_FIELD_ENABLED.key().equals(key))
{
boolean oldEnabled =
oldValue == null
diff --git
a/paimon-core/src/main/java/org/apache/paimon/schema/SchemaValidation.java
b/paimon-core/src/main/java/org/apache/paimon/schema/SchemaValidation.java
index 3416d66f9f..66ceb822f0 100644
--- a/paimon-core/src/main/java/org/apache/paimon/schema/SchemaValidation.java
+++ b/paimon-core/src/main/java/org/apache/paimon/schema/SchemaValidation.java
@@ -20,6 +20,7 @@ package org.apache.paimon.schema;
import org.apache.paimon.CoreOptions;
import org.apache.paimon.CoreOptions.ChangelogProducer;
+import org.apache.paimon.CoreOptions.ExternalPathStrategy;
import org.apache.paimon.CoreOptions.MapStorageLayout;
import org.apache.paimon.CoreOptions.MergeEngine;
import org.apache.paimon.TableType;
@@ -31,6 +32,8 @@ import org.apache.paimon.fileindex.FileIndexerFactory;
import org.apache.paimon.fileindex.FileIndexerFactoryUtils;
import org.apache.paimon.format.FileFormat;
import org.apache.paimon.format.csv.CsvOptions;
+import org.apache.paimon.fs.Path;
+import org.apache.paimon.fs.TemplateEntropyInjectExternalPathProvider;
import org.apache.paimon.iceberg.IcebergOptions;
import org.apache.paimon.mergetree.compact.PartialUpdateMergeFunction;
import org.apache.paimon.mergetree.compact.aggregate.AggregateMergeFunction;
@@ -445,6 +448,44 @@ public class SchemaValidation {
validatePkClusteringOverride(options);
validateManifestSort(schema, options);
+
+ validateExternalPathTemplate(options);
+ }
+
+ public static void validateExternalPathStrategyChange(
+ @Nullable String previous, @Nullable String next) {
+ String template =
ExternalPathStrategy.ENTROPY_INJECT_TEMPLATE.toString();
+ if (template.equalsIgnoreCase(previous) !=
template.equalsIgnoreCase(next)) {
+ throw new UnsupportedOperationException(
+ "Switching to or from " + template + " is not supported.");
+ }
+ }
+
+ private static void validateExternalPathTemplate(CoreOptions options) {
+ if (options.externalPathStrategy() !=
ExternalPathStrategy.ENTROPY_INJECT_TEMPLATE) {
+ return;
+ }
+
+ checkArgument(
+ !options.isChainTable(),
+ "Chain tables do not support the '%s' external path strategy.",
+ ExternalPathStrategy.ENTROPY_INJECT_TEMPLATE);
+ String externalPaths = options.dataFileExternalPaths();
+ checkArgument(
+ externalPaths != null && !externalPaths.trim().isEmpty(),
+ "'%s' requires a non-empty template when '%s' is '%s'.",
+ CoreOptions.DATA_FILE_EXTERNAL_PATHS.key(),
+ CoreOptions.DATA_FILE_EXTERNAL_PATHS_STRATEGY.key(),
+ ExternalPathStrategy.ENTROPY_INJECT_TEMPLATE);
+ String[] paths = externalPaths.split(",", -1);
+ checkArgument(
+ paths.length == 1,
+ "'%s' only accepts a single path when '%s' is '%s', but got
%s.",
+ CoreOptions.DATA_FILE_EXTERNAL_PATHS.key(),
+ CoreOptions.DATA_FILE_EXTERNAL_PATHS_STRATEGY.key(),
+ ExternalPathStrategy.ENTROPY_INJECT_TEMPLATE,
+ paths.length);
+ TemplateEntropyInjectExternalPathProvider.checkTemplate(new
Path(paths[0].trim()));
}
/**
diff --git
a/paimon-core/src/test/java/org/apache/paimon/catalog/FileSystemCatalogTest.java
b/paimon-core/src/test/java/org/apache/paimon/catalog/FileSystemCatalogTest.java
index 0e180938a1..6bd6f0bfe1 100644
---
a/paimon-core/src/test/java/org/apache/paimon/catalog/FileSystemCatalogTest.java
+++
b/paimon-core/src/test/java/org/apache/paimon/catalog/FileSystemCatalogTest.java
@@ -289,6 +289,52 @@ public class FileSystemCatalogTest extends CatalogTestBase
{
.isInstanceOf(IllegalArgumentException.class);
}
+ @Test
+ public void testDropTableWithMissingChainBranch() throws Exception {
+ catalog.createDatabase("chain_db", false);
+ Identifier identifier = Identifier.create("chain_db", "chained");
+ catalog.createTable(
+ identifier,
+ Schema.newBuilder()
+ .column("k", DataTypes.BIGINT())
+ .column("seq", DataTypes.BIGINT())
+ .column("dt", DataTypes.STRING())
+ .partitionKeys("dt")
+ .primaryKey("dt", "k")
+ .option(CoreOptions.BUCKET.key(), "1")
+ .option(CoreOptions.BUCKET_KEY.key(), "k")
+ .option(CoreOptions.SEQUENCE_FIELD.key(), "seq")
+ .option(CoreOptions.CHAIN_TABLE_ENABLED.key(), "true")
+ .option(CoreOptions.PARTITION_TIMESTAMP_PATTERN.key(),
"$dt")
+
.option(CoreOptions.PARTITION_TIMESTAMP_FORMATTER.key(), "yyyyMMdd")
+
.option(CoreOptions.CHAIN_TABLE_CHAIN_PARTITION_KEYS.key(), "dt")
+ .build(),
+ false);
+ FileStoreTable table = (FileStoreTable) catalog.getTable(identifier);
+ table.createBranch("snapshot");
+ table.createBranch("delta");
+ Path tablePath =
+ ((FileSystemCatalog) DelegateCatalog.rootCatalog(catalog))
+ .getTableLocation(identifier);
+ for (String branch : Arrays.asList(Identifier.DEFAULT_MAIN_BRANCH,
"snapshot", "delta")) {
+ new FileSystemSchemaManager(fileIO, tablePath, branch)
+ .commitChanges(
+ SchemaChange.setOption(
+
CoreOptions.SCAN_FALLBACK_SNAPSHOT_BRANCH.key(), "snapshot"),
+ SchemaChange.setOption(
+
CoreOptions.SCAN_FALLBACK_DELTA_BRANCH.key(), "delta"));
+ }
+ // the chain loads while both branches are there
+ assertThat(catalog.getTable(identifier)).isNotNull();
+
+ table.branchManager().dropBranch("snapshot");
+
+ catalog.dropTable(identifier, false);
+
+ assertThatThrownBy(() -> catalog.getTable(identifier))
+ .isInstanceOf(Catalog.TableNotExistException.class);
+ }
+
@ParameterizedTest
@ValueSource(booleans = {false, true})
public void testDropTableWithInvalidOptions(boolean cacheEnabled) throws
Exception {
diff --git
a/paimon-core/src/test/java/org/apache/paimon/operation/EntropyInjectTemplateTest.java
b/paimon-core/src/test/java/org/apache/paimon/operation/EntropyInjectTemplateTest.java
new file mode 100644
index 0000000000..55372df604
--- /dev/null
+++
b/paimon-core/src/test/java/org/apache/paimon/operation/EntropyInjectTemplateTest.java
@@ -0,0 +1,440 @@
+/*
+ * 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.paimon.operation;
+
+import org.apache.paimon.CoreOptions;
+import org.apache.paimon.CoreOptions.ExternalPathStrategy;
+import org.apache.paimon.catalog.Catalog;
+import org.apache.paimon.catalog.CatalogContext;
+import org.apache.paimon.catalog.CatalogFactory;
+import org.apache.paimon.catalog.Identifier;
+import org.apache.paimon.data.BinaryString;
+import org.apache.paimon.data.GenericRow;
+import org.apache.paimon.fs.Path;
+import org.apache.paimon.options.ExpireConfig;
+import org.apache.paimon.schema.Schema;
+import org.apache.paimon.schema.SchemaChange;
+import org.apache.paimon.table.FileStoreTable;
+import org.apache.paimon.table.sink.BatchTableCommit;
+import org.apache.paimon.table.sink.BatchTableWrite;
+import org.apache.paimon.table.sink.BatchWriteBuilder;
+import org.apache.paimon.types.DataTypes;
+
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.io.TempDir;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.ValueSource;
+
+import java.io.IOException;
+import java.nio.file.Files;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.Set;
+import java.util.stream.Collectors;
+import java.util.stream.Stream;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+
+/**
+ * Tests for tables using {@link
ExternalPathStrategy#ENTROPY_INJECT_TEMPLATE}, whose data files
+ * live in external paths shared with other tables.
+ */
+public class EntropyInjectTemplateTest {
+
+ private static final String DATABASE = "shared_db";
+
+ @TempDir java.nio.file.Path tempPath;
+
+ private Catalog catalog;
+ private java.nio.file.Path sharedRoot;
+
+ @BeforeEach
+ public void before() throws Exception {
+ catalog =
+ CatalogFactory.createCatalog(
+ CatalogContext.create(new Path("file://" + tempPath +
"/warehouse")));
+ catalog.createDatabase(DATABASE, true);
+ sharedRoot = tempPath.resolve("shared");
+ }
+
+ @Test
+ public void testTablesShareHashPrefixes() throws Exception {
+ FileStoreTable t1 = createTable("t1");
+ FileStoreTable t2 = createTable("t2");
+ write(t1, 1, "a");
+ write(t2, 2, "b");
+
+ // every data file is laid out as <hash
dirs>/<db>.db/<table>/bucket-0/<file>
+ for (String file : listSharedFiles()) {
+ assertThat(file)
+ .matches(
+ "[01]{4}/[01]{4}/[01]{4}/[01]{8}/"
+ + DATABASE
+ + "\\.db/t[12]/bucket-0/.+");
+ }
+ assertThat(sharedTableFiles("t1")).hasSize(1);
+ assertThat(sharedTableFiles("t2")).hasSize(1);
+ }
+
+ @Test
+ public void testExpireSnapshotsDeletesSharedFiles() throws Exception {
+ FileStoreTable table = createTable("t1");
+ write(table, 1, "a");
+ write(table, 2, "b");
+ write(table, 3, "c");
+ assertThat(sharedTableFiles("t1")).hasSize(3);
+
+ // the overwrite makes the three files unreferenced by the latest
snapshot
+ overwrite(table, 4, "d");
+ assertThat(sharedTableFiles("t1")).hasSize(4);
+
+ table.newExpireSnapshots()
+
.config(ExpireConfig.builder().snapshotRetainMax(1).snapshotRetainMin(1).build())
+ .expire();
+
+ // expiration locates the files by their full external path, the
overwritten ones are gone
+ assertThat(sharedTableFiles("t1")).hasSize(1);
+ }
+
+ /** The literal template path is excluded from recursive directory
cleanup. */
+ @Test
+ public void testDropTablePreservesTheLiteralPlaceholderDirectory() throws
Exception {
+ FileStoreTable t1 = createTable("t1");
+ FileStoreTable t2 = createTable("t2");
+ write(t1, 1, "a");
+ write(t2, 2, "b");
+ Set<String> t2Files = sharedTableFiles("t2");
+
+ java.nio.file.Path literal =
+ sharedRoot.resolve("{hash}").resolve(DATABASE +
".db").resolve("t1");
+ Files.createDirectories(literal);
+ Files.write(literal.resolve("legacy.parquet"), new byte[] {1});
+
+ Set<String> t1Files = sharedTableFiles("t1");
+ assertThat(t1Files).hasSize(2);
+
+ Identifier identifier = Identifier.create(DATABASE, "t1");
+ catalog.dropTable(identifier, false);
+
+ assertThatThrownBy(() -> catalog.getTable(identifier))
+ .isInstanceOf(Catalog.TableNotExistException.class);
+ assertThat(sharedTableFiles("t1")).isEqualTo(t1Files);
+ assertThat(sharedTableFiles("t2")).isEqualTo(t2Files);
+ }
+
+ @ParameterizedTest
+ @ValueSource(booleans = {false, true})
+ public void testCannotSwitchTemplateStrategy(boolean withData) throws
Exception {
+ String strategy = CoreOptions.DATA_FILE_EXTERNAL_PATHS_STRATEGY.key();
+ String paths = CoreOptions.DATA_FILE_EXTERNAL_PATHS.key();
+ String template =
ExternalPathStrategy.ENTROPY_INJECT_TEMPLATE.toString();
+ String roundRobin = ExternalPathStrategy.ROUND_ROBIN.toString();
+ String templatePath = "file://" + sharedRoot + "/{hash}/template";
+ String normalPath = "file://" + tempPath.resolve("normal");
+ Identifier templateId = Identifier.create(DATABASE, "template");
+ Identifier normalId = Identifier.create(DATABASE, "normal");
+ catalog.createTable(templateId, sharedSchema("template"), false);
+ Schema normalSchema =
+ schemaBuilder().option(paths, normalPath).option(strategy,
roundRobin).build();
+ catalog.createTable(normalId, normalSchema, false);
+ FileStoreTable templateTable = (FileStoreTable)
catalog.getTable(templateId);
+ FileStoreTable normalTable = (FileStoreTable)
catalog.getTable(normalId);
+ if (withData) {
+ write(templateTable, 1, "template");
+ write(normalTable, 2, "normal");
+ }
+
+ assertThatThrownBy(
+ () ->
+ catalog.alterTable(
+ normalId,
+ Arrays.asList(
+ SchemaChange.setOption(paths,
templatePath),
+
SchemaChange.setOption(strategy, template)),
+ false))
+ .isInstanceOf(UnsupportedOperationException.class)
+ .hasMessageContaining(template);
+ assertThatThrownBy(
+ () ->
+ catalog.alterTable(
+ templateId,
+ Arrays.asList(
+ SchemaChange.setOption(paths,
normalPath),
+
SchemaChange.setOption(strategy, roundRobin)),
+ false))
+ .isInstanceOf(UnsupportedOperationException.class)
+ .hasMessageContaining(template);
+ assertThatThrownBy(
+ () ->
+ catalog.alterTable(
+ templateId,
+ Collections.singletonList(
+
SchemaChange.removeOption(strategy)),
+ false))
+ .isInstanceOf(UnsupportedOperationException.class)
+ .hasMessageContaining(template);
+ assertThatThrownBy(() ->
normalTable.copy(Collections.singletonMap(strategy, template)))
+ .isInstanceOf(UnsupportedOperationException.class)
+ .hasMessageContaining(template);
+ assertThatThrownBy(() ->
templateTable.copy(Collections.singletonMap(strategy, roundRobin)))
+ .isInstanceOf(UnsupportedOperationException.class)
+ .hasMessageContaining(template);
+
+ assertThatThrownBy(() -> catalog.replaceTable(templateId,
normalSchema, false))
+ .isInstanceOf(UnsupportedOperationException.class)
+ .hasMessageContaining(template);
+ assertThatThrownBy(() -> catalog.replaceTable(normalId,
sharedSchema("normal"), false))
+ .isInstanceOf(UnsupportedOperationException.class)
+ .hasMessageContaining(template);
+ if (withData) {
+ assertThat(
+ ((FileStoreTable) catalog.getTable(templateId))
+ .snapshotManager()
+ .latestSnapshotId())
+ .isEqualTo(1L);
+ assertThat(
+ ((FileStoreTable) catalog.getTable(normalId))
+ .snapshotManager()
+ .latestSnapshotId())
+ .isEqualTo(1L);
+ }
+ }
+
+ @Test
+ public void testOrphanCleanupDoesNotReachExpandedTemplatePath() throws
Exception {
+ FileStoreTable table = createTable("t1");
+ write(table, 1, "a");
+
+ Set<String> files = sharedTableFiles("t1");
+ assertThat(files).hasSize(1);
+ java.nio.file.Path committedFile =
sharedRoot.resolve(files.iterator().next());
+ java.nio.file.Path orphan =
committedFile.resolveSibling("orphan.parquet");
+ Files.copy(committedFile, orphan);
+ long cutoff = Files.getLastModifiedTime(orphan).toMillis() + 1;
+
+ CleanOrphanFilesResult result = new LocalOrphanFilesClean(table,
cutoff).clean();
+ assertThat(result.getDeletedFilesPath()).isEmpty();
+ assertThat(orphan).exists();
+ }
+
+ @ParameterizedTest
+ @ValueSource(strings = {"/bad", "/{hash}/.."})
+ public void testExternalPathMustContainPlaceholder(String suffix) {
+ assertThatThrownBy(
+ () ->
+ catalog.createTable(
+ Identifier.create(DATABASE, "bad"),
+ schemaBuilder()
+ .option(
+
CoreOptions.DATA_FILE_EXTERNAL_PATHS.key(),
+ "file://" + sharedRoot
+ suffix)
+ .option(
+ CoreOptions
+
.DATA_FILE_EXTERNAL_PATHS_STRATEGY
+ .key(),
+
ExternalPathStrategy.ENTROPY_INJECT_TEMPLATE
+ .toString())
+ .build(),
+ false))
+ .rootCause()
+ .isInstanceOf(IllegalArgumentException.class)
+ .hasMessageContaining("{hash}");
+ }
+
+ @Test
+ public void testRoundRobinAcceptsLiteralPlaceholderDirectory() throws
Exception {
+ Identifier identifier = Identifier.create(DATABASE, "literal");
+ catalog.createTable(
+ identifier,
+ schemaBuilder()
+ .option(
+ CoreOptions.DATA_FILE_EXTERNAL_PATHS.key(),
+ "file://" + sharedRoot + "/{hash}/literal")
+ .option(
+
CoreOptions.DATA_FILE_EXTERNAL_PATHS_STRATEGY.key(),
+ ExternalPathStrategy.ROUND_ROBIN.toString())
+ .build(),
+ false);
+ FileStoreTable table = (FileStoreTable) catalog.getTable(identifier);
+ write(table, 1, "a");
+
+
assertThat(table.newReadBuilder().newScan().plan().splits()).isNotEmpty();
+
assertThat(Files.isDirectory(sharedRoot.resolve("{hash}").resolve("literal"))).isTrue();
+ }
+
+ @Test
+ public void testRejectsChainTableWithTemplateStrategy() throws Exception {
+ Identifier identifier = Identifier.create(DATABASE, "chain_template");
+ String chainEnabled = CoreOptions.CHAIN_TABLE_ENABLED.key();
+ Schema schema =
+ schemaBuilder()
+ .partitionKeys("name")
+ .primaryKey("name", "id")
+ .option(CoreOptions.BUCKET.key(), "1")
+ .option(CoreOptions.SEQUENCE_FIELD.key(), "id")
+ .option(CoreOptions.PARTITION_TIMESTAMP_PATTERN.key(),
"$name")
+
.option(CoreOptions.PARTITION_TIMESTAMP_FORMATTER.key(), "yyyy-MM-dd")
+ .option(
+ CoreOptions.DATA_FILE_EXTERNAL_PATHS.key(),
+ "file://"
+ + sharedRoot
+ + "/{hash}/"
+ + DATABASE
+ + ".db/chain_template")
+ .option(
+
CoreOptions.DATA_FILE_EXTERNAL_PATHS_STRATEGY.key(),
+
ExternalPathStrategy.ENTROPY_INJECT_TEMPLATE.toString())
+ .option(chainEnabled, "true")
+ .build();
+ String message = "Chain tables do not support the
'entropy-inject-template'";
+ assertThatThrownBy(() -> catalog.createTable(identifier, schema,
false))
+ .rootCause()
+ .hasMessageContaining(message);
+
+ schema.options().remove(chainEnabled);
+ catalog.createTable(identifier, schema, false);
+ FileStoreTable table = (FileStoreTable) catalog.getTable(identifier);
+ write(table, 1, "2026-11-19");
+ assertThat(sharedTableFiles("chain_template")).hasSize(1);
+ assertThatThrownBy(
+ () ->
+ catalog.alterTable(
+ identifier,
+ Collections.singletonList(
+
SchemaChange.setOption(chainEnabled, "true")),
+ false))
+ .isInstanceOf(IllegalArgumentException.class)
+ .hasMessageContaining(message);
+ }
+
+ @Test
+ public void testTemplateRequiresAnExternalPathOnCreateAndAlter() throws
Exception {
+ String strategy = CoreOptions.DATA_FILE_EXTERNAL_PATHS_STRATEGY.key();
+ String template =
ExternalPathStrategy.ENTROPY_INJECT_TEMPLATE.toString();
+ Identifier identifier = Identifier.create(DATABASE, "missing_path");
+ assertThatThrownBy(
+ () ->
+ catalog.createTable(
+ identifier,
+ schemaBuilder().option(strategy,
template).build(),
+ false))
+ .rootCause()
+ .hasMessageContaining("requires a non-empty template");
+ catalog.createTable(identifier, sharedSchema("missing_path"), false);
+ assertThatThrownBy(
+ () ->
+ catalog.alterTable(
+ identifier,
+ Collections.singletonList(
+ SchemaChange.removeOption(
+
CoreOptions.DATA_FILE_EXTERNAL_PATHS
+ .key())),
+ false))
+ .isInstanceOf(IllegalArgumentException.class)
+ .hasMessageContaining("requires a non-empty template");
+ }
+
+ @Test
+ public void testOnlyOneTemplateIsAllowed() {
+ assertThatThrownBy(
+ () ->
+ catalog.createTable(
+ Identifier.create(DATABASE, "bad"),
+ schemaBuilder()
+ .option(
+
CoreOptions.DATA_FILE_EXTERNAL_PATHS.key(),
+ "file://"
+ + sharedRoot
+ +
"/a/{hash}/bad,file://"
+ + sharedRoot
+ +
"/b/{hash}/bad")
+ .option(
+ CoreOptions
+
.DATA_FILE_EXTERNAL_PATHS_STRATEGY
+ .key(),
+
ExternalPathStrategy.ENTROPY_INJECT_TEMPLATE
+ .toString())
+ .build(),
+ false))
+ .rootCause()
+ .isInstanceOf(IllegalArgumentException.class)
+ .hasMessageContaining("only accepts a single path");
+ }
+
+ private Schema.Builder schemaBuilder() {
+ return Schema.newBuilder()
+ .column("id", DataTypes.INT())
+ .column("name", DataTypes.STRING())
+ .option(CoreOptions.BUCKET.key(), "-1");
+ }
+
+ private Schema sharedSchema(String tableName) {
+ return schemaBuilder()
+ .option(
+ CoreOptions.DATA_FILE_EXTERNAL_PATHS.key(),
+ "file://" + sharedRoot + "/{hash}/" + DATABASE +
".db/" + tableName)
+ .option(
+ CoreOptions.DATA_FILE_EXTERNAL_PATHS_STRATEGY.key(),
+
ExternalPathStrategy.ENTROPY_INJECT_TEMPLATE.toString())
+ .build();
+ }
+
+ private FileStoreTable createTable(String tableName) throws Exception {
+ Identifier identifier = Identifier.create(DATABASE, tableName);
+ catalog.createTable(identifier, sharedSchema(tableName), false);
+ return (FileStoreTable) catalog.getTable(identifier);
+ }
+
+ private void write(FileStoreTable table, int id, String name) throws
Exception {
+ write(table.newBatchWriteBuilder(), id, name);
+ }
+
+ private void overwrite(FileStoreTable table, int id, String name) throws
Exception {
+ write(table.newBatchWriteBuilder().withOverwrite(), id, name);
+ }
+
+ private void write(BatchWriteBuilder builder, int id, String name) throws
Exception {
+ try (BatchTableWrite write = builder.newWrite();
+ BatchTableCommit commit = builder.newCommit()) {
+ write.write(GenericRow.of(id, BinaryString.fromString(name)));
+ commit.commit(write.prepareCommit());
+ }
+ }
+
+ /** All data file paths under the shared root, relative to it. */
+ private Set<String> listSharedFiles() throws IOException {
+ if (!Files.exists(sharedRoot)) {
+ return Collections.emptySet();
+ }
+ try (Stream<java.nio.file.Path> files = Files.walk(sharedRoot)) {
+ return files.filter(Files::isRegularFile)
+ .map(sharedRoot::relativize)
+ .map(java.nio.file.Path::toString)
+ .collect(Collectors.toSet());
+ }
+ }
+
+ private Set<String> sharedTableFiles(String tableName) throws IOException {
+ return listSharedFiles().stream()
+ .filter(file -> file.contains("/" + DATABASE + ".db/" +
tableName + "/"))
+ .collect(Collectors.toSet());
+ }
+}