laskoviymishka commented on code in PR #2722:
URL: https://github.com/apache/iceberg-go/pull/2722#discussion_r4718485853


##########
table/maintenance/orphan_cleanup.go:
##########
@@ -764,6 +768,75 @@ func (b *referencePathBatch) flush() {
        b.paths = b.paths[:0]
 }
 
+// partitionFieldNames returns the names of all partition fields across every
+// partition spec of the table.
+func partitionFieldNames(tbl *table.Table) map[string]struct{} {
+       names := make(map[string]struct{})
+       for _, spec := range tbl.Metadata().PartitionSpecs() {
+               for _, field := range spec.Fields() {
+                       names[field.Name] = struct{}{}
+               }
+       }
+
+       return names
+}
+
+// isHiddenPath reports whether path, relative to root, has a segment that
+// starts with "_" or ".". Such files belong to other tools (for example
+// _delta_log or .hoodie) and are never treated as orphans. A directory
+// named after a partition field, such as "_part=1", is not hidden. This
+// matches Java's PartitionAwareHiddenPathFilter.
+func isHiddenPath(path, root string, partitionNames map[string]struct{}) bool {
+       rel := relativeToRoot(path, root)
+       segments := strings.Split(rel, "/")
+       for i, segment := range segments {
+               if segment == "" {
+                       continue
+               }
+               if strings.HasPrefix(segment, ".") {

Review Comment:
   Should the partition-name lookup run before this `.` check? Java's 
`PartitionAwareHiddenPathFilter` exempts partition fields starting with `_` or 
`.` 
([FileSystemWalker.java](https://github.com/apache/iceberg/blob/cfc80db65bff096bc52e580d1fee9f3a78febb34/core/src/main/java/org/apache/iceberg/util/FileSystemWalker.java#L211-L212)),
 so "matches Java" only holds for `_` here. With `.x` as a partition field, 
`data/.x=1/b.parquet` comes back hidden, and orphans under `.x=1/` are never 
cleaned.
   
   A `.x=1` test row would pin it. Java also exempts the file name itself 
([L162-L178](https://github.com/apache/iceberg/blob/cfc80db65bff096bc52e580d1fee9f3a78febb34/core/src/main/java/org/apache/iceberg/util/FileSystemWalker.java#L162-L178));
 since Iceberg doesn't write `_p=…` file names, saying "directory" in the doc 
comment covers that.



##########
table/maintenance/orphan_cleanup_test.go:
##########
@@ -2917,3 +2917,37 @@ func TestGetReferencedFilesAcrossBatchBoundary(t 
*testing.T) {
                require.True(t, refs[fmt.Sprintf("s3://bucket/data/%d.parquet", 
i)])
        }
 }
+
+func TestIsHiddenPath(t *testing.T) {

Review Comment:
   Could we add one `PlanOrphanFiles` test on `LocalFS` with `_delta_log/` and 
`.hoodie/` files under the table? The filter is only tested here as a pure 
function on `s3://` strings, so nothing checks real walker output, 
`partitionFieldNames` across specs, or a `WithCleanupLocation` root. A table at 
a `file:/…/_wh/tbl` location would also catch the `relativeToRoot` fallback.



##########
table/maintenance/orphan_cleanup.go:
##########
@@ -764,6 +768,75 @@ func (b *referencePathBatch) flush() {
        b.paths = b.paths[:0]
 }
 
+// partitionFieldNames returns the names of all partition fields across every
+// partition spec of the table.
+func partitionFieldNames(tbl *table.Table) map[string]struct{} {
+       names := make(map[string]struct{})
+       for _, spec := range tbl.Metadata().PartitionSpecs() {
+               for _, field := range spec.Fields() {
+                       names[field.Name] = struct{}{}
+               }
+       }
+
+       return names
+}
+
+// isHiddenPath reports whether path, relative to root, has a segment that
+// starts with "_" or ".". Such files belong to other tools (for example
+// _delta_log or .hoodie) and are never treated as orphans. A directory
+// named after a partition field, such as "_part=1", is not hidden. This
+// matches Java's PartitionAwareHiddenPathFilter.
+func isHiddenPath(path, root string, partitionNames map[string]struct{}) bool {
+       rel := relativeToRoot(path, root)
+       segments := strings.Split(rel, "/")
+       for i, segment := range segments {
+               if segment == "" {
+                       continue
+               }
+               if strings.HasPrefix(segment, ".") {
+                       return true
+               }
+               if !strings.HasPrefix(segment, "_") {
+                       continue
+               }
+               if i < len(segments)-1 {
+                       if name, _, found := strings.Cut(segment, "="); found {
+                               if _, ok := partitionNames[name]; ok {
+                                       continue
+                               }
+                       }
+               }
+
+               return true
+       }
+
+       return false
+}
+
+// relativeToRoot returns path relative to root, comparing only the URL path
+// so that differing schemes or authorities for the same location still match.
+func relativeToRoot(path, root string) string {
+       pathPart := urlPath(path)
+       rootPart := strings.TrimSuffix(urlPath(root), "/")
+       if rootPart != "" && strings.HasPrefix(pathPart, rootPart+"/") {
+               return strings.TrimPrefix(pathPart, rootPart+"/")
+       }
+
+       return strings.TrimPrefix(pathPart, "/")

Review Comment:
   Could we normalize the root the same way the walker does, or treat a prefix 
miss as not hidden? When the root doesn't prefix-match, this checks the full 
path, so a `_` or `.` ancestor above the table hides every file and cleanup 
returns an empty plan with no error.
   
   On `LocalFS` a Hadoop-style `file:/tmp/.../_wh/tbl` root hits this: 
`urlPath` only strips `://`, and 
[`LocalFS.WalkDir`](https://github.com/apache/iceberg-go/blob/efc625d30d78b23cb2b4927713c6824fdcab8902/io/local.go#L144-L160)
 yields bare OS paths, so `data/a.parquet` comes back hidden. `file:///` and 
bare roots are fine; `.../_wh//tbl` hits it too.



##########
table/maintenance/orphan_cleanup.go:
##########
@@ -511,6 +512,9 @@ func planOrphanFiles(ctx context.Context, tbl *table.Table, 
cfg *orphanCleanupCo
                        if info.IsDir() || !info.ModTime().Before(cutoff) {
                                return nil
                        }
+                       if isHiddenPath(path, scanLocation, partitionNames) {

Review Comment:
   Could the `DeleteOrphanFiles` godoc mention this skip? [The exported 
doc](https://github.com/apache/iceberg-go/blob/efc625d30d78b23cb2b4927713c6824fdcab8902/table/maintenance/orphan_cleanup.go#L329-L344)
 still reads as covering every unreferenced file under the location, and only 
the unexported `isHiddenPath` explains it. One sentence would do:
   
   ```go
   // Files under a path segment starting with "_" or "." (for example
   // _delta_log or .hoodie) are never treated as orphans, except
   // directories named after a partition field.
   ```



##########
table/maintenance/orphan_cleanup_test.go:
##########
@@ -2917,3 +2917,37 @@ func TestGetReferencedFilesAcrossBatchBoundary(t 
*testing.T) {
                require.True(t, refs[fmt.Sprintf("s3://bucket/data/%d.parquet", 
i)])
        }
 }
+
+func TestIsHiddenPath(t *testing.T) {
+       root := "s3://bucket/warehouse/db/tbl"
+       partitions := map[string]struct{}{"_part": {}, "region": {}}
+
+       tests := []struct {
+               name   string
+               path   string
+               hidden bool
+       }{
+               {"data file", root + "/data/00000.parquet", false},
+               {"metadata file", root + "/metadata/v1.metadata.json", false},
+               {"delta log", root + "/_delta_log/00000.json", true},
+               {"hoodie dir", root + "/.hoodie/hoodie.properties", true},
+               {"hidden file", root + "/data/.hidden", true},
+               {"underscore file", root + "/data/_SUCCESS", true},
+               {"nested hidden dir", root + "/data/_tmp/part.parquet", true},
+               {"partition dir with underscore", root + 
"/data/_part=1/00000.parquet", false},
+               {"unknown underscore partition dir", root + 
"/data/_other=1/00000.parquet", true},
+               {"regular partition dir", root + 
"/data/region=us/00000.parquet", false},
+               {"root with trailing slash", root + "/data/_SUCCESS", true},

Review Comment:
   This row passes the same `root` and path as `underscore file`, so no 
trailing-slash root goes through the table. Could we either drop it, or give 
the struct a `root` field so this row can pass `root + "/"`?



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