diff --git a/ql/src/java/org/apache/hadoop/hive/ql/metadata/Hive.java b/ql/src/java/org/apache/hadoop/hive/ql/metadata/Hive.java index 55ae535df5..27ec7dcea8 100644 --- a/ql/src/java/org/apache/hadoop/hive/ql/metadata/Hive.java +++ b/ql/src/java/org/apache/hadoop/hive/ql/metadata/Hive.java @@ -4301,6 +4301,19 @@ public void recycleDirToCmPath(Path dataPath, boolean isPurge) throws HiveExcept } } + private static void deleteAndRename(FileSystem destFs, Path destFile, FileStatus srcStatus, Path destPath) + throws IOException { + if (destFs.exists(destFile)) { + // rename cannot overwrite non empty destination directory, so deleting the destination before renaming. + destFs.delete(destFile); + LOG.info("Deleting destination file" + destFile.toUri()); + } + if(!destFs.rename(srcStatus.getPath(), destFile)) { + throw new IOException("rename for src path: " + srcStatus.getPath() + " to dest:" + + destPath + " returned false"); + } + } + //it is assumed that parent directory of the destf should already exist when this //method is called. when the replace value is true, this method works a little different //from mv command if the destf is a directory, it replaces the destf instead of moving under @@ -4386,37 +4399,14 @@ public static boolean moveFile(final HiveConf conf, Path srcf, final Path destf, "Unable to move source " + srcStatus.getPath() + " to destination " + destFile; if (null == pool) { - boolean success = false; - if (destFs instanceof DistributedFileSystem) { - ((DistributedFileSystem)destFs).rename(srcStatus.getPath(), destFile, Options.Rename.OVERWRITE); - success = true; - } else { - destFs.delete(destFile, false); - success = destFs.rename(srcStatus.getPath(), destFile); - } - if(!success) { - throw new IOException("rename for src path: " + srcStatus.getPath() + " to dest:" - + destf + " returned false"); - } + deleteAndRename(destFs, destFile, srcStatus, destf); } else { futures.add(pool.submit(new Callable() { @Override public Void call() throws HiveException { SessionState.setCurrentSessionState(parentSession); try { - boolean success = false; - if (destFs instanceof DistributedFileSystem) { - ((DistributedFileSystem)destFs).rename(srcStatus.getPath(), destFile, Options.Rename.OVERWRITE); - success = true; - } else { - destFs.delete(destFile, false); - success = destFs.rename(srcStatus.getPath(), destFile); - } - if (!success) { - throw new IOException( - "rename for src path: " + srcStatus.getPath() + " to dest path:" - + destFile + " returned false"); - } + deleteAndRename(destFs, destFile, srcStatus, destf); } catch (Exception e) { throw getHiveException(e, poolMsg); }