rdblue commented on code in PR #16174:
URL: https://github.com/apache/iceberg/pull/16174#discussion_r3285012148


##########
core/src/test/java/org/apache/iceberg/util/TestLocationUtil.java:
##########
@@ -84,4 +84,170 @@ void testStripTrailingSlashForRootPathWithTrailingSlashes() 
{
         .as("Should be root path")
         .isEqualTo(rootPath);
   }
+
+  @Test
+  public void testResolveRelativeLocations() {
+    String tableLocation = "s3://bucket/table";
+
+    assertThat(LocationUtil.resolveLocation(tableLocation, 
"metadata/file.parquet"))
+        .isEqualTo("s3://bucket/table/metadata/file.parquet");
+
+    assertThat(LocationUtil.resolveLocation(tableLocation, 
"data/00000-0.parquet"))
+        .isEqualTo("s3://bucket/table/data/00000-0.parquet");
+  }
+
+  @Test
+  public void testResolveLocationsWithColonsInSegments() {
+    String tableLocation = "s3://bucket/table";
+
+    assertThat(LocationUtil.resolveLocation(tableLocation, 
"data/partition=key:value/file.parquet"))
+        .isEqualTo("s3://bucket/table/data/partition=key:value/file.parquet");
+
+    assertThat(LocationUtil.resolveLocation(tableLocation, 
"metadata/snap-123:456.avro"))
+        .isEqualTo("s3://bucket/table/metadata/snap-123:456.avro");
+  }
+
+  @Test
+  public void testResolveAbsoluteLocationsUnchanged() {
+    String tableLocation = "s3://bucket/table";
+
+    assertThat(LocationUtil.resolveLocation(tableLocation, 
"s3://other-bucket/path/file.parquet"))
+        .isEqualTo("s3://other-bucket/path/file.parquet");
+
+    assertThat(LocationUtil.resolveLocation(tableLocation, 
"hdfs://namenode/path/file.parquet"))
+        .isEqualTo("hdfs://namenode/path/file.parquet");
+  }
+
+  @Test
+  public void testRelativize() {
+    String tableLocation = "s3://bucket/table";
+
+    assertThat(
+            LocationUtil.relativizeLocation(
+                tableLocation, "s3://bucket/table/metadata/file.parquet"))
+        .isEqualTo("metadata/file.parquet");
+
+    assertThat(
+            LocationUtil.relativizeLocation(
+                tableLocation, "s3://bucket/table/data/00000-0.parquet"))
+        .isEqualTo("data/00000-0.parquet");
+  }
+
+  @Test
+  public void testRelativizeLocationNotUnderTableLocation() {
+    String tableLocation = "s3://bucket/table";
+
+    // different bucket
+    assertThat(
+            LocationUtil.relativizeLocation(tableLocation, 
"s3://other-bucket/path/file.parquet"))
+        .isEqualTo("s3://other-bucket/path/file.parquet");
+
+    // same bucket, different path
+    assertThat(
+            LocationUtil.relativizeLocation(
+                tableLocation, "s3://bucket/other-table/data/file.parquet"))
+        .isEqualTo("s3://bucket/other-table/data/file.parquet");
+  }
+
+  @Test
+  public void testRelativizeLocationWithSharedPrefix() {
+    // sibling locations that share a byte prefix with the table location but 
are not
+    // children of it must not be relativized (e.g. "table" vs "table_v2")
+    String tableLocation = "s3://bucket/table";
+
+    assertThat(
+            LocationUtil.relativizeLocation(
+                tableLocation, "s3://bucket/table_v2/data/00000-0.parquet"))
+        .isEqualTo("s3://bucket/table_v2/data/00000-0.parquet");
+  }
+
+  @Test
+  public void testRelativizeLocationEqualToTableLocation() {
+    // a location equal to the table location is not followed by a separator,
+    // so it is not a child of the table location and is returned as-is
+    String tableLocation = "s3://bucket/table";
+
+    assertThat(LocationUtil.relativizeLocation(tableLocation, tableLocation))
+        .isEqualTo(tableLocation);
+  }
+
+  @Test
+  public void testRelativizeMismatchedFileSchemeNotRelativized() {
+    // mixed file: variants are NOT relativized. Consistent URI forms are the 
caller's
+    // responsibility
+    assertThat(
+            LocationUtil.relativizeLocation(
+                "file:/tmp/table", "file:///tmp/table/metadata/file.parquet"))
+        .isEqualTo("file:///tmp/table/metadata/file.parquet");
+
+    assertThat(
+            LocationUtil.relativizeLocation(
+                "file:///tmp/table", "file:/tmp/table/metadata/file.parquet"))
+        .isEqualTo("file:/tmp/table/metadata/file.parquet");
+  }
+
+  @Test
+  public void testResolveAbsoluteLocationWithNonAlphanumericScheme() {
+    String tableLocation = "s3://bucket/table";
+
+    assertThat(LocationUtil.resolveLocation(tableLocation, 
"git+ssh://host/repo"))
+        .isEqualTo("git+ssh://host/repo");
+  }
+
+  @Test
+  public void testRelativizeResolveRoundTrip() {
+    String tableLocation = "s3://bucket/table";
+    String absoluteLocation = 
"s3://bucket/table/metadata/root-manifest.parquet";
+
+    String relativized = LocationUtil.relativizeLocation(tableLocation, 
absoluteLocation);
+    assertThat(relativized).isEqualTo("metadata/root-manifest.parquet");
+
+    String resolved = LocationUtil.resolveLocation(tableLocation, relativized);
+    assertThat(resolved).isEqualTo(absoluteLocation);
+  }
+
+  @Test
+  public void testRelativizeResolveRoundTripWithFileScheme() {
+    String tableLocation = "file:///tmp/warehouse/table";
+    String absoluteLocation = 
"file:///tmp/warehouse/table/metadata/root-manifest.parquet";
+
+    String relativized = LocationUtil.relativizeLocation(tableLocation, 
absoluteLocation);
+    assertThat(relativized).isEqualTo("metadata/root-manifest.parquet");
+
+    String resolved = LocationUtil.resolveLocation(tableLocation, relativized);
+    assertThat(resolved).isEqualTo(absoluteLocation);
+  }
+
+  @Test
+  public void 
testResolveWithTrailingOrLeadingSlashProducesDuplicateSeparator() {

Review Comment:
   I'm glad to see this. I would also add the examples from the spec here.



-- 
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]

Reply via email to