This is an automated email from the ASF dual-hosted git repository.

github-merge-queue[bot] pushed a commit to branch 
gh-readonly-queue/master/pr-20487-e36f88dfaacf30919dafd1278365dfcb8ca05fd2
in repository https://gitbox.apache.org/repos/asf/hudi.git

commit 5b7404916d31cffd12c4fa52f59ed628b677d185
Author: wangxianghu <[email protected]>
AuthorDate: Mon Nov 23 17:11:19 2026 +0000

    fix(utilities): Allow initializing hudi table on an existing empty path 
(#20487)
    
    * fix(utilities): Allow initializing hudi table on an existing empty path
    
    * fix(utilities): Allow initializing hudi table on an existing empty path
    
    * fix(utilities): Allow initializing hudi table on an existing empty path
    
    * BootstrapExecutor.initializeTable throw exception when path is not empty
    
    * fix checkstyle
---
 .../apache/hudi/storage/hadoop/HoodieHadoopStorage.java  | 10 ++++++++++
 .../main/java/org/apache/hudi/storage/HoodieStorage.java | 16 ++++++++++++++++
 .../plugin/hudi/storage/HudiTrinoInlineStorage.java      |  7 +++++++
 .../io/trino/plugin/hudi/storage/HudiTrinoStorage.java   |  8 ++++++++
 .../hudi/utilities/streamer/BootstrapExecutor.java       |  6 ++++--
 .../apache/hudi/utilities/streamer/HoodieStreamer.java   | 10 ++++++++--
 .../org/apache/hudi/utilities/streamer/StreamSync.java   |  8 +++++++-
 .../utilities/deltastreamer/TestHoodieDeltaStreamer.java | 14 +++++++++++---
 8 files changed, 71 insertions(+), 8 deletions(-)

diff --git 
a/hudi-hadoop-common/src/main/java/org/apache/hudi/storage/hadoop/HoodieHadoopStorage.java
 
b/hudi-hadoop-common/src/main/java/org/apache/hudi/storage/hadoop/HoodieHadoopStorage.java
index 067f77d491bd..25d91c39f66f 100644
--- 
a/hudi-hadoop-common/src/main/java/org/apache/hudi/storage/hadoop/HoodieHadoopStorage.java
+++ 
b/hudi-hadoop-common/src/main/java/org/apache/hudi/storage/hadoop/HoodieHadoopStorage.java
@@ -249,6 +249,16 @@ public class HoodieHadoopStorage extends HoodieStorage {
         .collect(Collectors.toList());
   }
 
+  @Override
+  public boolean isEmpty(StoragePath path) throws IOException {
+    Path hadoopPath = convertToHadoopPath(path);
+    if (!fs.exists(hadoopPath)) {
+      return true;
+    }
+    RemoteIterator<LocatedFileStatus> iterator = 
fs.listLocatedStatus(hadoopPath);
+    return !iterator.hasNext();
+  }
+
   @Override
   public List<StoragePathInfo> globEntries(StoragePath pathPattern, 
StoragePathFilter filter)
       throws IOException {
diff --git a/hudi-io/src/main/java/org/apache/hudi/storage/HoodieStorage.java 
b/hudi-io/src/main/java/org/apache/hudi/storage/HoodieStorage.java
index aab82bce03b8..7950992a26c3 100644
--- a/hudi-io/src/main/java/org/apache/hudi/storage/HoodieStorage.java
+++ b/hudi-io/src/main/java/org/apache/hudi/storage/HoodieStorage.java
@@ -484,4 +484,20 @@ public abstract class HoodieStorage implements Closeable {
   public List<StoragePathInfo> globEntries(StoragePath pathPattern) throws 
IOException {
     return globEntries(pathPattern, e -> true);
   }
+
+  /**
+   * Checks whether the directory at the given storage path is empty.
+   * <p>
+   * Implementation should lean towards memory-efficient approaches (such as 
lazy-loading
+   * iterators) to avoid loading all file metadata into memory at once, which 
could cause
+   * Out-of-Memory (OOM) errors in large directories.
+   * </p>
+   *
+   * @param path The {@link StoragePath} to check.
+   * @return {@code true} if the path does not exist or contains no 
files/directories;
+   *         {@code false} otherwise.
+   * @throws IOException If any storage system I/O error occurs.
+   */
+  @PublicAPIMethod(maturity = ApiMaturityLevel.EVOLVING)
+  public abstract boolean isEmpty(StoragePath path) throws IOException;
 }
diff --git 
a/hudi-trino/src/main/java/io/trino/plugin/hudi/storage/HudiTrinoInlineStorage.java
 
b/hudi-trino/src/main/java/io/trino/plugin/hudi/storage/HudiTrinoInlineStorage.java
index 26e1eae088a0..ce9ac6952348 100644
--- 
a/hudi-trino/src/main/java/io/trino/plugin/hudi/storage/HudiTrinoInlineStorage.java
+++ 
b/hudi-trino/src/main/java/io/trino/plugin/hudi/storage/HudiTrinoInlineStorage.java
@@ -163,6 +163,13 @@ public class HudiTrinoInlineStorage
         throw new HoodieNotSupportedException("This API should not be called 
by Hudi inline storage.");
     }
 
+    @Override
+    public boolean isEmpty(StoragePath path)
+            throws IOException
+    {
+        throw new HoodieNotSupportedException("This API should not be called 
by Hudi inline storage.");
+    }
+
     @Override
     public List<StoragePathInfo> listDirectEntries(StoragePath path, 
StoragePathFilter storagePathFilter)
             throws IOException
diff --git 
a/hudi-trino/src/main/java/io/trino/plugin/hudi/storage/HudiTrinoStorage.java 
b/hudi-trino/src/main/java/io/trino/plugin/hudi/storage/HudiTrinoStorage.java
index cc2906cb5ad6..a7d1b9b382d2 100644
--- 
a/hudi-trino/src/main/java/io/trino/plugin/hudi/storage/HudiTrinoStorage.java
+++ 
b/hudi-trino/src/main/java/io/trino/plugin/hudi/storage/HudiTrinoStorage.java
@@ -222,6 +222,14 @@ public class HudiTrinoStorage
         return fileList;
     }
 
+    @Override
+    public boolean isEmpty(StoragePath path)
+            throws IOException
+    {
+        FileIterator fileIterator = 
fileSystem.listFiles(convertToLocation(path));
+        return !fileIterator.hasNext();
+    }
+
     @Override
     public List<StoragePathInfo> listDirectEntries(StoragePath path, 
StoragePathFilter filter)
             throws IOException
diff --git 
a/hudi-utilities/src/main/java/org/apache/hudi/utilities/streamer/BootstrapExecutor.java
 
b/hudi-utilities/src/main/java/org/apache/hudi/utilities/streamer/BootstrapExecutor.java
index df57a7a45fa7..2b05b909eb0f 100644
--- 
a/hudi-utilities/src/main/java/org/apache/hudi/utilities/streamer/BootstrapExecutor.java
+++ 
b/hudi-utilities/src/main/java/org/apache/hudi/utilities/streamer/BootstrapExecutor.java
@@ -192,8 +192,10 @@ public class BootstrapExecutor implements Serializable {
         log.info("Target base path already exists, overwrite it");
         fs.delete(basePath, true);
       } else {
-        throw new HoodieException("target base path already exists at " + 
cfg.targetBasePath
-            + ". Cannot bootstrap data on top of an existing table");
+        if (fs.listLocatedStatus(basePath).hasNext()) {
+          throw new HoodieException("target base path already exists at " + 
cfg.targetBasePath
+              + ". Cannot bootstrap data on top of an existing table");
+        }
       }
     }
 
diff --git 
a/hudi-utilities/src/main/java/org/apache/hudi/utilities/streamer/HoodieStreamer.java
 
b/hudi-utilities/src/main/java/org/apache/hudi/utilities/streamer/HoodieStreamer.java
index 8bc744876168..577bd2d958ec 100644
--- 
a/hudi-utilities/src/main/java/org/apache/hudi/utilities/streamer/HoodieStreamer.java
+++ 
b/hudi-utilities/src/main/java/org/apache/hudi/utilities/streamer/HoodieStreamer.java
@@ -256,7 +256,7 @@ public class HoodieStreamer implements Serializable {
 
     @Parameter(names = {"--target-base-path"},
         description = "base path for the target hoodie table. "
-            + "(Will be created if did not exist first time around. If exists, 
expected to be a hoodie table)",
+            + "(Will be created if did not exist first time around. If exists, 
expected to be a hoodie table or an empty directory.)",
         required = true)
     public String targetBasePath;
 
@@ -757,7 +757,9 @@ public class HoodieStreamer implements Serializable {
       this.configurationHotUpdateStrategyOpt = 
StringUtils.isNullOrEmpty(cfg.configHotUpdateStrategyClass) ? Option.empty() :
           
ConfigurationHotUpdateStrategyUtils.createConfigurationHotUpdateStrategy(cfg.configHotUpdateStrategyClass,
 cfg, properties.get());
 
-      if (this.storage.exists(new StoragePath(cfg.targetBasePath))) {
+      final StoragePath basePath = new StoragePath(cfg.targetBasePath);
+      final StoragePath metaPath = new StoragePath(basePath, 
HoodieTableMetaClient.METAFOLDER_NAME);
+      if (this.storage.exists(metaPath)) {
         try {
           HoodieTableMetaClient meta = HoodieTableMetaClient.builder()
               .setConf(this.storage.getConf().newInstance())
@@ -781,6 +783,10 @@ public class HoodieStreamer implements Serializable {
           }
         }
       } else {
+        if (!this.storage.isEmpty(basePath)) {
+          throw new HoodieException("Target base path " + cfg.targetBasePath + 
" is not empty and not a hoodie table. "
+              + "Please either delete the existing data or use a different 
path.");
+        }
         initializeTableTypeAndBaseFileFormat();
       }
 
diff --git 
a/hudi-utilities/src/main/java/org/apache/hudi/utilities/streamer/StreamSync.java
 
b/hudi-utilities/src/main/java/org/apache/hudi/utilities/streamer/StreamSync.java
index 262a58bd8908..0b9a467dc23c 100644
--- 
a/hudi-utilities/src/main/java/org/apache/hudi/utilities/streamer/StreamSync.java
+++ 
b/hudi-utilities/src/main/java/org/apache/hudi/utilities/streamer/StreamSync.java
@@ -381,7 +381,9 @@ public class StreamSync implements Serializable, Closeable {
    * @throws IOException in case of any IOException
    */
   private HoodieTableMetaClient initializeMetaClient(boolean refreshTimeline) 
throws IOException {
-    if (storage.exists(new StoragePath(cfg.targetBasePath))) {
+    final StoragePath basePath = new StoragePath(cfg.targetBasePath);
+    final StoragePath metaPath = new StoragePath(basePath, 
HoodieTableMetaClient.METAFOLDER_NAME);
+    if (this.storage.exists(metaPath)) {
       try {
         HoodieTableMetaClient metaClient = HoodieTableMetaClient.builder()
             .setConf(HadoopFSUtils.getStorageConfWithCopy(conf))
@@ -435,6 +437,10 @@ public class StreamSync implements Serializable, Closeable 
{
         throw e;
       }
     } else {
+      if (!this.storage.isEmpty(basePath)) {
+        throw new HoodieException("Target base path " + cfg.targetBasePath + " 
is not empty and not a hoodie table. "
+            + "Please either delete the existing data or use a different 
path.");
+      }
       return initializeEmptyTable();
     }
   }
diff --git 
a/hudi-utilities/src/test/java/org/apache/hudi/utilities/deltastreamer/TestHoodieDeltaStreamer.java
 
b/hudi-utilities/src/test/java/org/apache/hudi/utilities/deltastreamer/TestHoodieDeltaStreamer.java
index e5302c29dd18..52901611a35c 100644
--- 
a/hudi-utilities/src/test/java/org/apache/hudi/utilities/deltastreamer/TestHoodieDeltaStreamer.java
+++ 
b/hudi-utilities/src/test/java/org/apache/hudi/utilities/deltastreamer/TestHoodieDeltaStreamer.java
@@ -41,7 +41,6 @@ import org.apache.hudi.common.util.Option;
 import org.apache.hudi.config.HoodieWriteConfig;
 import org.apache.hudi.exception.HoodieException;
 import org.apache.hudi.exception.HoodieIOException;
-import org.apache.hudi.exception.TableNotFoundException;
 import org.apache.hudi.hadoop.fs.HadoopFSUtils;
 import org.apache.hudi.hive.HiveSyncConfig;
 import org.apache.hudi.hive.HoodieHiveSyncClient;
@@ -419,14 +418,23 @@ public class TestHoodieDeltaStreamer extends 
HoodieDeltaStreamerTestBase {
 
   @Test
   public void testTableCreation() throws Exception {
-    Exception e = assertThrows(TableNotFoundException.class, () -> {
+    Exception e = assertThrows(HoodieException.class, () -> {
       fs.mkdirs(new Path(basePath + "/not_a_table"));
+      fs.createNewFile(new Path(basePath + "/not_a_table/test.txt"));
       syncOnce(TestHelpers.makeConfig(basePath + "/not_a_table", 
WriteOperationType.BULK_INSERT));
-    }, "Should error out when pointed out at a dir thats not a table");
+    }, "Should error out when pointed out at a dir that's not a table");
     // expected
     log.debug("Expected error during table creation", e);
   }
 
+  @Test
+  public void testTableCreationOnEmptyPath() throws Exception {
+    assertDoesNotThrow(() -> {
+      fs.mkdirs(new Path(basePath + "/empty_path"));
+      syncOnce(TestHelpers.makeConfig(basePath + "/empty_path", 
WriteOperationType.BULK_INSERT));
+    });
+  }
+
   @ParameterizedTest
   @ValueSource(booleans = {true, false})
   public void testTableCreationContainsHiveStylePartitioningEnable(boolean 
configFlag) throws Exception {

Reply via email to