Index: ql/src/java/org/apache/hadoop/hive/ql/exec/MoveTask.java =================================================================== --- ql/src/java/org/apache/hadoop/hive/ql/exec/MoveTask.java (revision 1186875) +++ ql/src/java/org/apache/hadoop/hive/ql/exec/MoveTask.java (working copy) @@ -77,7 +77,30 @@ fs.delete(targetPath, true); // if source exists, rename. Otherwise, create a empty directory if (fs.exists(sourcePath)) { + // If it multiple level of folder are there fs.rename is failing so first + // create the targetpath.getParent() if it not exist + Path deletePath = null; + Path mkDirPath = targetPath.getParent(); + if (mkDirPath != null & !fs.exists(mkDirPath)) { + Path actualPath = mkDirPath; + // targetPath path is /x/y/z/1/2/3 here /x/y/z is present in the file system + // create the structure till /x/y/z/1/2 to work rename for multilevel directory + // and if rename fails delete the path /x/y/z/1 + while (actualPath != null && !fs.exists(actualPath)) { + deletePath = actualPath; + actualPath = actualPath.getParent(); + } + fs.mkdirs(mkDirPath); + } if (!fs.rename(sourcePath, targetPath)) { + try { + if (deletePath != null) { + fs.delete(deletePath, true); + } + } catch (IOException e) { + LOG.info("Unable to delete the path created for facilitating rename" + + deletePath); + } throw new HiveException("Unable to rename: " + sourcePath + " to: " + targetPath); }