This is an automated email from the ASF dual-hosted git repository.

yuzelin pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/paimon.git


The following commit(s) were added to refs/heads/master by this push:
     new 7f6e5747f2 [refactor] Minor refactor TagManager for validating 
auto-tag (#5853)
7f6e5747f2 is described below

commit 7f6e5747f2a2bdcb10d58f84684875b8129169f9
Author: yuzelin <[email protected]>
AuthorDate: Wed Jul 9 09:51:57 2025 +0800

    [refactor] Minor refactor TagManager for validating auto-tag (#5853)
---
 .../org/apache/paimon/table/system/TagsTable.java  |  5 +-
 .../java/org/apache/paimon/utils/TagManager.java   | 80 ++++++++++------------
 2 files changed, 36 insertions(+), 49 deletions(-)

diff --git 
a/paimon-core/src/main/java/org/apache/paimon/table/system/TagsTable.java 
b/paimon-core/src/main/java/org/apache/paimon/table/system/TagsTable.java
index 220f012336..236fbe3110 100644
--- a/paimon-core/src/main/java/org/apache/paimon/table/system/TagsTable.java
+++ b/paimon-core/src/main/java/org/apache/paimon/table/system/TagsTable.java
@@ -223,10 +223,7 @@ public class TagsTable implements ReadonlyTable {
             }
             Path location = ((TagsSplit) split).location;
             Predicate predicate = ((TagsSplit) split).tagPredicate;
-
-            // There should not be any tag creation related options here for 
tags table.
-            TagManager tagManager =
-                    new TagManager(fileIO, location, branch, 
CoreOptions.fromMap(options()));
+            TagManager tagManager = new TagManager(fileIO, location, branch);
 
             Map<String, Tag> nameToSnapshot = new TreeMap<>();
             Map<String, Tag> predicateMap = new TreeMap<>();
diff --git a/paimon-core/src/main/java/org/apache/paimon/utils/TagManager.java 
b/paimon-core/src/main/java/org/apache/paimon/utils/TagManager.java
index 89bc4d64d6..6a0ab615bc 100644
--- a/paimon-core/src/main/java/org/apache/paimon/utils/TagManager.java
+++ b/paimon-core/src/main/java/org/apache/paimon/utils/TagManager.java
@@ -43,7 +43,6 @@ import java.time.Duration;
 import java.time.LocalDateTime;
 import java.util.ArrayList;
 import java.util.Collection;
-import java.util.Collections;
 import java.util.Comparator;
 import java.util.List;
 import java.util.Optional;
@@ -68,26 +67,44 @@ public class TagManager {
     private final FileIO fileIO;
     private final Path tablePath;
     private final String branch;
-    private final CoreOptions coreOptions;
+    @Nullable private final TagPeriodHandler tagPeriodHandler;
 
     public TagManager(FileIO fileIO, Path tablePath) {
-        this(fileIO, tablePath, DEFAULT_MAIN_BRANCH, 
CoreOptions.fromMap(Collections.emptyMap()));
+        this(fileIO, tablePath, DEFAULT_MAIN_BRANCH, (TagPeriodHandler) null);
     }
 
     public TagManager(FileIO fileIO, Path tablePath, String branch) {
-        this(fileIO, tablePath, branch, 
CoreOptions.fromMap(Collections.emptyMap()));
+        this(fileIO, tablePath, branch, (TagPeriodHandler) null);
     }
 
     /** Specify the default branch for data writing. */
-    public TagManager(FileIO fileIO, Path tablePath, String branch, 
CoreOptions coreOptions) {
+    public TagManager(FileIO fileIO, Path tablePath, String branch, 
CoreOptions options) {
+        this(fileIO, tablePath, branch, createIfNecessary(options));
+    }
+
+    @Nullable
+    private static TagPeriodHandler createIfNecessary(CoreOptions options) {
+        return TagTimeExtractor.createForAutoTag(options) == null
+                ? null
+                : TagPeriodHandler.create(options);
+    }
+
+    private TagManager(
+            FileIO fileIO,
+            Path tablePath,
+            String branch,
+            @Nullable TagPeriodHandler tagPeriodHandler) {
         this.fileIO = fileIO;
         this.tablePath = tablePath;
-        this.branch = BranchManager.normalizeBranch(branch);
-        this.coreOptions = coreOptions;
+        this.branch = branch;
+        this.tagPeriodHandler = tagPeriodHandler;
     }
 
+    // TODO: Current usage of this method only use the tag directory of new 
branch.
+    // If we will use the new branch TagManager to create tag, we need to pass 
TagPeriodHandler
+    // according to branch options.
     public TagManager copyWithBranch(String branchName) {
-        return new TagManager(fileIO, tablePath, branchName, coreOptions);
+        return new TagManager(fileIO, tablePath, branchName, 
(TagPeriodHandler) null);
     }
 
     /** Return the root Directory of tags. */
@@ -141,16 +158,7 @@ public class TagManager {
             String tagName,
             @Nullable Duration timeRetained,
             @Nullable List<TagCallback> callbacks) {
-
-        if (isAutoTag(tagName)) {
-            List<String> autoTags = getSnapshotAutoTags(snapshot);
-            if (!autoTags.isEmpty()) {
-                throw new RuntimeException(
-                        String.format(
-                                "Snapshot %s is already auto-tagged with %s.",
-                                snapshot.id(), autoTags));
-            }
-        }
+        validateNoAutoTag(tagName, snapshot);
 
         // When timeRetained is not defined, please do not write the 
tagCreatorTime field, as this
         // will cause older versions (<= 0.7) of readers to be unable to read 
this tag.
@@ -459,35 +467,17 @@ public class TagManager {
                         taggedSnapshot.id()));
     }
 
-    /**
-     * @param tagName
-     * @return true only if auto-tag enabled and the name is in right format
-     */
-    private boolean isAutoTag(String tagName) {
-        TagTimeExtractor extractor = 
TagTimeExtractor.createForAutoTag(coreOptions);
-        if (extractor == null) {
-            return false;
+    private void validateNoAutoTag(String tagName, Snapshot snapshot) {
+        if (tagPeriodHandler == null || !tagPeriodHandler.isAutoTag(tagName)) {
+            return;
         }
-        TagPeriodHandler periodHandler = TagPeriodHandler.create(coreOptions);
-        return periodHandler.isAutoTag(tagName);
-    }
 
-    /**
-     * @param snapshot
-     * @return the auto-tag names of the snapshot, empty if auto-tag is not 
enabled
-     */
-    private List<String> getSnapshotAutoTags(Snapshot snapshot) {
-        TagTimeExtractor extractor = 
TagTimeExtractor.createForAutoTag(coreOptions);
-        // no auto-tagging, no auto-tags
-        if (extractor == null) {
-            return Collections.emptyList();
+        List<String> autoTags = 
tags(tagPeriodHandler::isAutoTag).get(snapshot);
+        if (autoTags != null) {
+            throw new RuntimeException(
+                    String.format(
+                            "Snapshot %s is already auto-tagged with %s.",
+                            snapshot.id(), autoTags));
         }
-
-        TagPeriodHandler periodHandler = TagPeriodHandler.create(coreOptions);
-
-        // auto-tagging, tags in auto-tag format are auto-tags
-        return tags().getOrDefault(snapshot, new ArrayList<>()).stream()
-                .filter(tag -> periodHandler.isAutoTag(tag))
-                .collect(Collectors.toList());
     }
 }

Reply via email to