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: Use File.separator instead of hardcode in RewriteTablePath #12066

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
16 changes: 6 additions & 10 deletions core/src/main/java/org/apache/iceberg/RewriteTablePathUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -534,12 +534,7 @@ 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 appendFileSeparatorIfNotExists(absolutePath) + relativePath;
}

/** Returns the file name of a path. */
Expand All @@ -554,17 +549,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 = appendFileSeparatorIfNotExists(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 appendFileSeparatorIfNotExists(String path) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe maybeAppendFileSeparator?

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 @@ -180,9 +180,10 @@ 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() + File.separator;
} else {
stagingDir = RewriteTablePathUtil.appendFileSeparatorIfNotExists(stagingDir);
}
}

Expand Down Expand Up @@ -361,7 +362,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 +394,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,11 +714,6 @@ 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();
Expand Down