public static Path createTemporaryPath(ConnectorSession session, HdfsContext context, HdfsEnvironment hdfsEnvironment, Path targetPath)
    {
        // use a per-user temporary directory to avoid permission problems
        String temporaryPrefix = getTemporaryStagingDirectoryPath(session)
                .replace("${USER}", context.getIdentity().getUser());

        // use relative temporary directory on ViewFS
        if (isViewFileSystem(context, hdfsEnvironment, targetPath)) {
            if (pathExists(context, hdfsEnvironment, targetPath)) {
                temporaryPrefix = ".hive-staging";
            }
            else {
                //use the temporary folder in parent when target path does not exist
                temporaryPrefix = "../.hive-staging";
            }
        }

        // create a temporary directory on the same filesystem
        Path temporaryRoot = new Path(targetPath, temporaryPrefix);
        if (!pathExists(context, hdfsEnvironment, temporaryRoot)) {
            createDirectory(context, hdfsEnvironment, temporaryRoot);
        }
        Path temporaryPath = new Path(temporaryRoot, randomUUID().toString());

        createDirectory(context, hdfsEnvironment, temporaryPath);

        return temporaryPath;
    }
@Test void testCreateTemporaryPathOnViewFS()
    {
        HdfsEnvironment hdfsEnvironment = createTestHdfsEnvironment(new HiveClientConfig(), new MetastoreClientConfig());
        Path viewfsPath = new Path("viewfs://ns-default/test-dir");

        File storageDir = Files.createTempDir();
        // ViewFS check requires the mount point config, using system temporary folder as the storage
        hdfsEnvironment.getConfiguration(CONTEXT, viewfsPath).set("fs.viewfs.mounttable.ns-default.link./test-dir", "file://" + storageDir);

        //Make temporary folder under an existing data folder without staging folder ".hive-staging"
        Path temporaryPath = createTemporaryPath(SESSION, CONTEXT, hdfsEnvironment, viewfsPath);

        assertEquals(temporaryPath.getParent().toString(), "viewfs://ns-default/test-dir/.hive-staging");
        try {
            UUID.fromString(temporaryPath.getName());
        }
        catch (IllegalArgumentException e) {
            fail("Expected a UUID folder name ");
        }

        //Make temporary folder under an existing data folder with an existing staging folder ".hive-staging"
        temporaryPath = createTemporaryPath(SESSION, CONTEXT, hdfsEnvironment, viewfsPath);

        assertEquals(temporaryPath.getParent().toString(), "viewfs://ns-default/test-dir/.hive-staging");
        try {
            UUID.fromString(temporaryPath.getName());
        }
        catch (IllegalArgumentException e) {
            fail("Expected a UUID folder name ");
        }

        //Make temporary folder under a non-existing data folder (for new tables), it would use the temporary folder of the parent
        temporaryPath = createTemporaryPath(SESSION, CONTEXT, hdfsEnvironment, new Path(viewfsPath, "non-existing"));
        assertEquals(temporaryPath.getParent().toString(), "viewfs://ns-default/test-dir/.hive-staging");
        try {
            UUID.fromString(temporaryPath.getName());
        }
        catch (IllegalArgumentException e) {
            fail("Expected a UUID folder name ");
        }
    }