laskoviymishka commented on code in PR #2722:
URL: https://github.com/apache/iceberg-go/pull/2722#discussion_r4763909261
##########
table/maintenance/orphan_cleanup.go:
##########
@@ -764,6 +771,80 @@ 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" or ".x=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 i < len(segments)-1 {
+ if name, _, found := strings.Cut(segment, "="); found {
+ if _, ok := partitionNames[name]; ok {
+ continue
+ }
+ }
+ }
+ if strings.HasPrefix(segment, ".") ||
strings.HasPrefix(segment, "_") {
+ 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+"/") {
Review Comment:
Could we clean and unescape the root the way the walker does, like
[`localPath`](https://github.com/apache/iceberg-go/blob/b7f949ca12bf03c2c52435205d4dfd326de05b8d/io/local.go#L57-L68)?
`file:/` and `//` work now, but `./_wh/tbl` and `file:///…/_wh/my%20tbl` still
plan nothing, with no error. The walked paths are cleaned and decoded, so [this
prefix
check](https://github.com/apache/iceberg-go/blob/b7f949ca12bf03c2c52435205d4dfd326de05b8d/table/maintenance/orphan_cleanup.go#L814-L846)
misses and `_wh` hides every file. A bare-key blobfs root (`bucket/_wh/tbl`)
misses the same way. Two more rows in the LocalFS test would pin it.
##########
table/maintenance/orphan_cleanup_test.go:
##########
@@ -2917,3 +2917,89 @@ 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": {}, ".x": {}, "region": {}}
+
+ tests := []struct {
+ name string
+ root string
+ path string
+ hidden bool
+ }{
+ {"data file", root, root + "/data/00000.parquet", false},
+ {"metadata file", root, root + "/metadata/v1.metadata.json",
false},
+ {"delta log", root, root + "/_delta_log/00000.json", true},
+ {"hoodie dir", root, root + "/.hoodie/hoodie.properties", true},
+ {"hidden file", root, root + "/data/.hidden", true},
+ {"underscore file", root, root + "/data/_SUCCESS", true},
+ {"nested hidden dir", root, root + "/data/_tmp/part.parquet",
true},
+ {"partition dir with underscore", root, root +
"/data/_part=1/00000.parquet", false},
+ {"partition dir with dot", root, root +
"/data/.x=1/00000.parquet", false},
+ {"unknown underscore partition dir", root, root +
"/data/_other=1/00000.parquet", true},
+ {"unknown dot partition dir", root, root +
"/data/.other=1/00000.parquet", true},
+ {"regular partition dir", root, root +
"/data/region=us/00000.parquet", false},
+ {"root with trailing slash", root + "/", root +
"/data/_SUCCESS", true},
+ {"root with trailing slash and data file", root + "/", root +
"/data/00000.parquet", false},
+ }
+
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ assert.Equal(t, tt.hidden, isHiddenPath(tt.path,
tt.root, partitions))
+ })
+ }
+
+ // A hidden segment in the root itself must not hide every file.
+ assert.False(t, isHiddenPath("s3://bucket/_wh/tbl/data/a.parquet",
"s3://bucket/_wh/tbl/", nil))
+ // A different scheme for the same location still resolves relative
paths.
+ assert.True(t,
isHiddenPath("s3a://bucket/warehouse/db/tbl/_delta_log/0.json", root, nil))
+ // Hadoop-style file:/ roots and repeated slashes match the bare paths
+ // that LocalFS yields while walking.
+ assert.False(t, isHiddenPath("/tmp/_wh/tbl/data/a.parquet",
"file:/tmp/_wh/tbl", nil))
+ assert.False(t, isHiddenPath("/tmp/_wh/tbl/data/a.parquet",
"/tmp/_wh//tbl", nil))
+ assert.True(t, isHiddenPath("/tmp/_wh/tbl/_delta_log/0.json",
"file:/tmp/_wh/tbl", nil))
+}
+
+func TestPlanOrphanFilesSkipsHiddenPathsOnLocalFS(t *testing.T) {
+ ctx := context.Background()
+ base := filepath.Join(t.TempDir(), "_wh", "tbl")
+ old := time.Now().Add(-48 * time.Hour)
+
+ files := []string{
+ "metadata/v1.metadata.json",
+ "data/orphan.parquet",
+ "_delta_log/00000.json",
+ ".hoodie/hoodie.properties",
+ }
+ for _, name := range files {
+ full := filepath.Join(base, filepath.FromSlash(name))
+ require.NoError(t, os.MkdirAll(filepath.Dir(full), 0o755))
+ require.NoError(t, os.WriteFile(full, []byte("x"), 0o644))
+ require.NoError(t, os.Chtimes(full, old, old))
+ }
+
+ for _, location := range []string{
+ "file:" + base,
+ "file://" + base,
+ base,
+ } {
+ t.Run(location, func(t *testing.T) {
+ metadataLocation := location +
"/metadata/v1.metadata.json"
+ meta, err := table.NewMetadata(iceberg.NewSchema(0),
nil, table.UnsortedSortOrder, location, nil)
Review Comment:
Thanks, the `_delta_log` / `.hoodie` part is in. Could this test also cover
a partitioned table with an orphan under `_part=1/`? The table here is
[unpartitioned](https://github.com/apache/iceberg-go/blob/b7f949ca12bf03c2c52435205d4dfd326de05b8d/table/maintenance/orphan_cleanup_test.go#L2964-L3005),
so nothing runs `partitionFieldNames` through `PlanOrphanFiles`.
--
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]