timothyw553 commented on code in PR #18876:
URL: https://github.com/apache/iceberg/pull/18876#discussion_r4723229380
##########
spark/v4.1/spark/src/test/java/org/apache/iceberg/spark/actions/TestRewriteTablePathsAction.java:
##########
@@ -1588,6 +1588,49 @@ public void testDeleteFrom() throws Exception {
assertEquals("Rows must match", originalData, copiedData);
}
+ @TestTemplate
+ public void testRewriteWithEquivalentLocalFileUriPrefix() {
+ String sourcePrefix = tableLocation;
+ assertThat(sourcePrefix).startsWith("file:/");
+
+ String alternateSourcePrefix =
+ sourcePrefix.startsWith("file:///")
+ ? "file:/" + sourcePrefix.substring("file:///".length())
+ : "file:///" + sourcePrefix.substring("file:/".length());
+
+ assertThat(alternateSourcePrefix).isNotEqualTo(sourcePrefix);
+
+ String targetPrefix = targetTableDir.toUri().toString();
+
+ RewriteTablePath.Result result =
+ actions()
+ .rewriteTablePath(table)
+ .rewriteLocationPrefix(alternateSourcePrefix, targetPrefix)
+ .execute();
+
+ assertThat(result.fileListLocation()).isNotBlank();
+ assertThat(result.latestVersion()).isNotBlank();
+
+ List<String> filesToMove =
+ spark
+ .read()
+ .format("text")
+ .load(result.fileListLocation())
+ .as(Encoders.STRING())
+ .collectAsList();
+
+ assertThat(filesToMove).isNotEmpty();
+
+ for (String entry : filesToMove) {
Review Comment:
this only checks the generated `filesToMove` list, not whether copying the
files and reading the target table actually works. could we extend the
copy-and-read tests (and add this to spark 4.2 as well) to cover mixed `file:/`
and `file:///` paths, including position deletes and deletion vectors?
##########
core/src/main/java/org/apache/iceberg/RewriteTablePathUtil.java:
##########
@@ -783,7 +783,7 @@ private static PositionDelete newPositionDeleteRecord(
Record record, String sourcePrefix, String targetPrefix) {
PositionDelete delete = PositionDelete.create();
String oldPath = (String) record.get(0);
- if (!oldPath.startsWith(sourcePrefix)) {
+ if (!isUnderPrefix(oldPath, sourcePrefix)) {
Review Comment:
now that a single delete file can mix `file:///` and `file:/` paths, could
rewriting change their sort order? for example
`file:///warehouse/data/z.parquet` followed by `file:/warehouse/data/a.parquet`
is sorted on input, but rewriting both to `target/` makes the output unsorted.
should we check `(file_path, pos)` ordering as we stream records and fail if it
decreases?
##########
core/src/main/java/org/apache/iceberg/RewriteTablePathUtil.java:
##########
@@ -856,13 +856,36 @@ public static String fileName(String path) {
* @throws IllegalArgumentException if path is not under or equal to prefix
*/
public static String relativize(String path, String prefix) {
- String toRemove = maybeAppendFileSeparator(prefix);
- String normalizedPath = maybeAppendFileSeparator(path);
- if (!normalizedPath.startsWith(toRemove)) {
+ String normalizedPath = normalizeLocalFileUri(path);
+ String normalizedPrefix = normalizeLocalFileUri(prefix);
+ String toRemove = maybeAppendFileSeparator(normalizedPrefix);
+ String pathWithSeparator = maybeAppendFileSeparator(normalizedPath);
+
+ if (!pathWithSeparator.startsWith(toRemove)) {
throw new IllegalArgumentException(
- String.format("Path %s does not start with %s", normalizedPath,
toRemove));
+ String.format("Path %s does not start with %s", pathWithSeparator,
toRemove));
+ }
+
+ return pathWithSeparator.equals(toRemove) ? "" :
normalizedPath.substring(toRemove.length());
+ }
+
+ private static boolean isUnderPrefix(String path, String prefix) {
+ if (!path.startsWith("file:/") || !prefix.startsWith("file:/")) {
+ return path.startsWith(prefix);
}
- return normalizedPath.equals(toRemove) ? "" :
path.substring(toRemove.length());
+
+ String normalizedPath = normalizeLocalFileUri(path);
+ String normalizedPrefix = normalizeLocalFileUri(prefix);
+ return maybeAppendFileSeparator(normalizedPath)
+ .startsWith(maybeAppendFileSeparator(normalizedPrefix));
+ }
+
+ private static String normalizeLocalFileUri(String path) {
+ if (path.startsWith("file:///")) {
Review Comment:
stripping two slashes from `file:///` changes some edge cases like
`file:////tmp/w/x` matching `file://tmp/w` or `file://server/tmp/d.parquet`
matching `file:///`. what if we normalize single-slash `file:/` to `file:///`
instead and add tests for those cases?
```diff
- if (path.startsWith("file:///")) {
- return "file:/" + path.substring("file:///".length());
+ if (path.startsWith("file:/") && !path.startsWith("file://")) {
+ return "file:///" + path.substring("file:/".length());
}
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]