You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@paimon.apache.org by lz...@apache.org on 2024/08/14 15:12:19 UTC

(paimon) branch master updated: [core] Throw SnapshotNotExistException if the snapshot for creating tag does not exist (#3951)

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

lzljs3620320 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 e1c4db9dd [core] Throw SnapshotNotExistException if the snapshot for creating tag does not exist (#3951)
e1c4db9dd is described below

commit e1c4db9dd5287f541af7eab8d3cea01eed2f8083
Author: HunterXHunter <cn...@gmail.com>
AuthorDate: Wed Aug 14 23:12:14 2024 +0800

    [core] Throw SnapshotNotExistException if the snapshot for creating tag does not exist (#3951)
---
 .../paimon/table/AbstractFileStoreTable.java       | 26 ++++---
 .../paimon/utils/SnapshotNotExistException.java    | 41 +++++++++++
 .../flink/procedure/CreateTagsProcedureITCase.java | 83 ++++++++++++++++++++++
 3 files changed, 139 insertions(+), 11 deletions(-)

diff --git a/paimon-core/src/main/java/org/apache/paimon/table/AbstractFileStoreTable.java b/paimon-core/src/main/java/org/apache/paimon/table/AbstractFileStoreTable.java
index a6228ab27..95b9d8dcf 100644
--- a/paimon-core/src/main/java/org/apache/paimon/table/AbstractFileStoreTable.java
+++ b/paimon-core/src/main/java/org/apache/paimon/table/AbstractFileStoreTable.java
@@ -60,6 +60,7 @@ import org.apache.paimon.tag.TagPreview;
 import org.apache.paimon.utils.BranchManager;
 import org.apache.paimon.utils.SegmentsCache;
 import org.apache.paimon.utils.SnapshotManager;
+import org.apache.paimon.utils.SnapshotNotExistException;
 import org.apache.paimon.utils.TagManager;
 
 import javax.annotation.Nullable;
@@ -79,7 +80,6 @@ import java.util.function.BiConsumer;
 
 import static org.apache.paimon.CoreOptions.PATH;
 import static org.apache.paimon.utils.Preconditions.checkArgument;
-import static org.apache.paimon.utils.Preconditions.checkNotNull;
 
 /** Abstract {@link FileStoreTable}. */
 abstract class AbstractFileStoreTable implements FileStoreTable {
@@ -502,7 +502,7 @@ abstract class AbstractFileStoreTable implements FileStoreTable {
         rollbackHelper().cleanLargerThan(snapshotManager.snapshot(snapshotId));
     }
 
-    public Snapshot createTagInternal(long fromSnapshotId) {
+    public Snapshot findSnapshot(long fromSnapshotId) throws SnapshotNotExistException {
         SnapshotManager snapshotManager = snapshotManager();
         Snapshot snapshot = null;
         if (snapshotManager.snapshotExists(fromSnapshotId)) {
@@ -518,35 +518,39 @@ abstract class AbstractFileStoreTable implements FileStoreTable {
                 }
             }
         }
-        checkArgument(
-                snapshot != null,
-                "Cannot create tag because given snapshot #%s doesn't exist.",
-                fromSnapshotId);
+
+        SnapshotNotExistException.checkNotNull(
+                snapshot,
+                String.format(
+                        "Cannot create tag because given snapshot #%s doesn't exist.",
+                        fromSnapshotId));
+
         return snapshot;
     }
 
     @Override
     public void createTag(String tagName, long fromSnapshotId) {
-        createTag(
-                tagName, createTagInternal(fromSnapshotId), coreOptions().tagDefaultTimeRetained());
+        createTag(tagName, findSnapshot(fromSnapshotId), coreOptions().tagDefaultTimeRetained());
     }
 
     @Override
     public void createTag(String tagName, long fromSnapshotId, Duration timeRetained) {
-        createTag(tagName, createTagInternal(fromSnapshotId), timeRetained);
+        createTag(tagName, findSnapshot(fromSnapshotId), timeRetained);
     }
 
     @Override
     public void createTag(String tagName) {
         Snapshot latestSnapshot = snapshotManager().latestSnapshot();
-        checkNotNull(latestSnapshot, "Cannot create tag because latest snapshot doesn't exist.");
+        SnapshotNotExistException.checkNotNull(
+                latestSnapshot, "Cannot create tag because latest snapshot doesn't exist.");
         createTag(tagName, latestSnapshot, coreOptions().tagDefaultTimeRetained());
     }
 
     @Override
     public void createTag(String tagName, Duration timeRetained) {
         Snapshot latestSnapshot = snapshotManager().latestSnapshot();
-        checkNotNull(latestSnapshot, "Cannot create tag because latest snapshot doesn't exist.");
+        SnapshotNotExistException.checkNotNull(
+                latestSnapshot, "Cannot create tag because latest snapshot doesn't exist.");
         createTag(tagName, latestSnapshot, timeRetained);
     }
 
diff --git a/paimon-core/src/main/java/org/apache/paimon/utils/SnapshotNotExistException.java b/paimon-core/src/main/java/org/apache/paimon/utils/SnapshotNotExistException.java
new file mode 100644
index 000000000..587cbfc93
--- /dev/null
+++ b/paimon-core/src/main/java/org/apache/paimon/utils/SnapshotNotExistException.java
@@ -0,0 +1,41 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.paimon.utils;
+
+import org.apache.paimon.Snapshot;
+
+/** Thrown by TagManager or SnapshotManager when the specified snapshotId does not exist. */
+public class SnapshotNotExistException extends RuntimeException {
+
+    private static final long serialVersionUID = 1L;
+
+    public SnapshotNotExistException(long snapshotId) {
+        super("Snapshot " + snapshotId + " does not exist");
+    }
+
+    public SnapshotNotExistException(String errMsg) {
+        super(errMsg);
+    }
+
+    public static void checkNotNull(Snapshot snapshotId, String errMsg) {
+        if (snapshotId == null) {
+            throw new SnapshotNotExistException(errMsg);
+        }
+    }
+}
diff --git a/paimon-flink/paimon-flink-common/src/test/java/org/apache/paimon/flink/procedure/CreateTagsProcedureITCase.java b/paimon-flink/paimon-flink-common/src/test/java/org/apache/paimon/flink/procedure/CreateTagsProcedureITCase.java
new file mode 100644
index 000000000..aef952b6c
--- /dev/null
+++ b/paimon-flink/paimon-flink-common/src/test/java/org/apache/paimon/flink/procedure/CreateTagsProcedureITCase.java
@@ -0,0 +1,83 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.paimon.flink.procedure;
+
+import org.apache.paimon.flink.CatalogITCaseBase;
+import org.apache.paimon.utils.SnapshotNotExistException;
+
+import org.apache.flink.types.Row;
+import org.junit.jupiter.api.Test;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatException;
+
+/** IT Case for {@link CreateTagProcedure}. */
+public class CreateTagsProcedureITCase extends CatalogITCaseBase {
+
+    @Test
+    public void testCreateTags() {
+        sql(
+                "CREATE TABLE T ("
+                        + " k STRING,"
+                        + " dt STRING,"
+                        + " PRIMARY KEY (k, dt) NOT ENFORCED"
+                        + ") PARTITIONED BY (dt) WITH ("
+                        + " 'bucket' = '1'"
+                        + ")");
+        sql("insert into T values('k', '2024-01-01')");
+        sql("insert into T values('k2', '2024-01-02')");
+
+        sql("CALL sys.create_tag('default.T', 'tag1')");
+
+        assertThat(
+                        sql("select * from T /*+ OPTIONS('scan.tag-name'='tag1') */").stream()
+                                .map(Row::toString))
+                .containsExactlyInAnyOrder("+I[k2, 2024-01-02]", "+I[k, 2024-01-01]");
+
+        sql("CALL sys.create_tag('default.T', 'tag2', 1)");
+
+        assertThat(
+                        sql("select * from T /*+ OPTIONS('scan.tag-name'='tag2') */").stream()
+                                .map(Row::toString))
+                .containsExactlyInAnyOrder("+I[k, 2024-01-01]");
+    }
+
+    @Test
+    public void testThrowSnapshotNotExistException() {
+        sql(
+                "CREATE TABLE T ("
+                        + " k STRING,"
+                        + " dt STRING,"
+                        + " PRIMARY KEY (k, dt) NOT ENFORCED"
+                        + ") PARTITIONED BY (dt) WITH ("
+                        + " 'bucket' = '1'"
+                        + ")");
+
+        assertThatException()
+                .isThrownBy(() -> sql("CALL sys.create_tag('default.T', 'tag1')"))
+                .withRootCauseInstanceOf(SnapshotNotExistException.class)
+                .withMessageContaining("Cannot create tag because latest snapshot doesn't exist.");
+
+        assertThatException()
+                .isThrownBy(() -> sql("CALL sys.create_tag('default.T', 'tag1', 1)"))
+                .withRootCauseInstanceOf(SnapshotNotExistException.class)
+                .withMessageContaining(
+                        "Cannot create tag because given snapshot #1 doesn't exist.");
+    }
+}