Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Spark 3.5: Remove use of File.Separator in RewriteTablePath #12066

Merged
merged 2 commits into from
Feb 1, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 10 additions & 12 deletions core/src/main/java/org/apache/iceberg/RewriteTablePathUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
*/
package org.apache.iceberg;

import java.io.File;
import java.io.IOException;
import java.io.Serializable;
import java.io.UncheckedIOException;
Expand Down Expand Up @@ -51,6 +50,9 @@
public class RewriteTablePathUtil {

private static final Logger LOG = LoggerFactory.getLogger(RewriteTablePathUtil.class);
// Use the POSIX separator instead of File.separator because File.separator is dependent on
// the client environment and not the target filesystem. POSIX is compatible with S3, GCS, etc
public static final String FILE_SEPARATOR = "/";

private RewriteTablePathUtil() {}

Expand Down Expand Up @@ -534,18 +536,13 @@ public static String newPath(String path, String sourcePrefix, String targetPref

/** Combine a base and relative path. */
public static String combinePaths(String absolutePath, String relativePath) {
String combined = absolutePath;
if (!combined.endsWith("/")) {
combined += "/";
}
combined += relativePath;
return combined;
return maybeAppendFileSeparator(absolutePath) + relativePath;
}

/** Returns the file name of a path. */
public static String fileName(String path) {
String filename = path;
int lastIndex = path.lastIndexOf(File.separator);
int lastIndex = path.lastIndexOf(FILE_SEPARATOR);
if (lastIndex != -1) {
filename = path.substring(lastIndex + 1);
}
Expand All @@ -554,17 +551,18 @@ public static String fileName(String path) {

/** Relativize a path. */
public static String relativize(String path, String prefix) {
String toRemove = prefix;
if (!toRemove.endsWith("/")) {
toRemove += "/";
}
String toRemove = maybeAppendFileSeparator(prefix);
if (!path.startsWith(toRemove)) {
throw new IllegalArgumentException(
String.format("Path %s does not start with %s", path, toRemove));
}
return path.substring(toRemove.length());
}

public static String maybeAppendFileSeparator(String path) {
return path.endsWith(FILE_SEPARATOR) ? path : path + FILE_SEPARATOR;
}

/**
* Construct a staging path under a given staging directory
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
*/
package org.apache.iceberg.spark.actions;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -180,9 +179,13 @@ private void validateInputs() {
validateAndSetStartVersion();

if (stagingDir == null) {
stagingDir = getMetadataLocation(table) + "copy-table-staging-" + UUID.randomUUID() + "/";
} else if (!stagingDir.endsWith("/")) {
stagingDir = stagingDir + "/";
stagingDir =
getMetadataLocation(table)
+ "copy-table-staging-"
+ UUID.randomUUID()
+ RewriteTablePathUtil.FILE_SEPARATOR;
} else {
stagingDir = RewriteTablePathUtil.maybeAppendFileSeparator(stagingDir);
}
}

Expand Down Expand Up @@ -361,7 +364,8 @@ private Pair<String, String> rewriteVersionFile(TableMetadata metadata, String v
TableMetadata newTableMetadata =
RewriteTablePathUtil.replacePaths(metadata, sourcePrefix, targetPrefix);
TableMetadataParser.overwrite(newTableMetadata, table.io().newOutputFile(stagingPath));
return Pair.of(stagingPath, newPath(versionFilePath, sourcePrefix, targetPrefix));
return Pair.of(
stagingPath, RewriteTablePathUtil.newPath(versionFilePath, sourcePrefix, targetPrefix));
}

/**
Expand Down Expand Up @@ -392,7 +396,9 @@ private RewriteResult<ManifestFile> rewriteManifestList(

result.append(rewriteResult);
// add the manifest list copy plan itself to the result
result.copyPlan().add(Pair.of(outputPath, newPath(path, sourcePrefix, targetPrefix)));
result
.copyPlan()
.add(Pair.of(outputPath, RewriteTablePathUtil.newPath(path, sourcePrefix, targetPrefix)));
return result;
}

Expand Down Expand Up @@ -710,15 +716,10 @@ private boolean fileExist(String path) {
return table.io().newInputFile(path).exists();
}

private static String newPath(String path, String sourcePrefix, String targetPrefix) {
return RewriteTablePathUtil.combinePaths(
targetPrefix, RewriteTablePathUtil.relativize(path, sourcePrefix));
}

private String getMetadataLocation(Table tbl) {
String currentMetadataPath =
((HasTableOperations) tbl).operations().current().metadataFileLocation();
int lastIndex = currentMetadataPath.lastIndexOf(File.separator);
int lastIndex = currentMetadataPath.lastIndexOf(RewriteTablePathUtil.FILE_SEPARATOR);
String metadataDir = "";
if (lastIndex != -1) {
metadataDir = currentMetadataPath.substring(0, lastIndex + 1);
Expand Down