pvary commented on code in PR #18424:
URL: https://github.com/apache/iceberg/pull/18424#discussion_r4616145613


##########
hive-metastore/src/test/java/org/apache/iceberg/hive/TestHiveCommits.java:
##########
@@ -321,6 +321,149 @@ public void testAlreadyExistsException() {
         .hasMessage("Table already exists: %s.%s", DB_NAME, TABLE_NAME);
   }
 
+  @Test
+  void alreadyExistsSuccessOnCreate() throws TException, InterruptedException {
+    TableIdentifier identifier = TableIdentifier.of(DB_NAME, "created_tbl");
+    HiveTableOperations ops = (HiveTableOperations) 
catalog.newTableOps(identifier);
+    TableMetadata metadata =
+        TableMetadata.newTableMetadata(
+            SCHEMA,
+            PartitionSpec.unpartitioned(),
+            catalog.defaultWarehouseLocation(identifier),
+            ImmutableMap.of());
+    HiveTableOperations spyOps = spy(ops);
+    createAndThrowAlreadyExists(ops, spyOps);
+
+    try {
+      spyOps.commit(null, metadata);
+
+      assertThat(metadataFileExists(ops.refresh()))
+          .as("Current metadata file should still exist")
+          .isTrue();
+    } finally {
+      catalog.dropTable(identifier, true);
+    }
+  }
+
+  @Test
+  void alreadyExistsConcurrentCreate() throws TException, InterruptedException 
{
+    TableIdentifier identifier = TableIdentifier.of(DB_NAME, "created_tbl");
+    HiveTableOperations ops = (HiveTableOperations) 
catalog.newTableOps(identifier);
+    TableMetadata metadata =
+        TableMetadata.newTableMetadata(
+            SCHEMA,
+            PartitionSpec.unpartitioned(),
+            catalog.defaultWarehouseLocation(identifier),
+            ImmutableMap.of(HIVE_LOCK_ENABLED, "false"));
+    HiveTableOperations spyOps = spy(ops);
+    doAnswer(
+            i -> {
+              catalog.createTable(identifier, SCHEMA);
+              return i.callRealMethod();
+            })
+        .when(spyOps)
+        .persistTable(any(), anyBoolean(), any());
+
+    try {
+      assertThatThrownBy(() -> spyOps.commit(null, metadata))
+          .isInstanceOf(AlreadyExistsException.class)
+          .hasMessage("Table already exists: %s.%s", DB_NAME, "created_tbl");
+      assertThat(metadataFileCount(ops.refresh()))
+          .as("Only the metadata file of the concurrently created table should 
exist")
+          .isEqualTo(1);
+    } finally {
+      catalog.dropTable(identifier, true);
+    }
+  }
+
+  @Test
+  void alreadyExistsConcurrentViewCreate() throws TException, 
InterruptedException {
+    TableIdentifier identifier = TableIdentifier.of(DB_NAME, "created_tbl");
+    HiveTableOperations ops = (HiveTableOperations) 
catalog.newTableOps(identifier);
+    TableMetadata metadata =
+        TableMetadata.newTableMetadata(
+            SCHEMA,
+            PartitionSpec.unpartitioned(),
+            catalog.defaultWarehouseLocation(identifier),
+            ImmutableMap.of(HIVE_LOCK_ENABLED, "false"));
+    HiveTableOperations spyOps = spy(ops);
+    doAnswer(
+            i -> {
+              catalog
+                  .buildView(identifier)
+                  .withSchema(SCHEMA)
+                  .withDefaultNamespace(identifier.namespace())
+                  .withQuery("hive", "select * from tbl")
+                  .create();
+              return i.callRealMethod();
+            })
+        .when(spyOps)
+        .persistTable(any(), anyBoolean(), any());
+
+    try {
+      assertThatThrownBy(() -> spyOps.commit(null, metadata))
+          .isInstanceOf(AlreadyExistsException.class)
+          .hasMessage("Table already exists: %s.%s", DB_NAME, "created_tbl");
+      assertThat(metadataVersionFiles("created_tbl"))
+          .as("Only the metadata file of the concurrently created view should 
exist")
+          .hasSize(1);
+    } finally {
+      catalog.dropView(identifier);
+    }
+  }
+
+  @Test
+  void alreadyExistsUnknownOnCreate() throws TException, InterruptedException {
+    TableIdentifier identifier = TableIdentifier.of(DB_NAME, "created_tbl");
+    HiveTableOperations ops = (HiveTableOperations) 
catalog.newTableOps(identifier);
+    TableMetadata metadata =
+        TableMetadata.newTableMetadata(
+            SCHEMA,
+            PartitionSpec.unpartitioned(),
+            catalog.defaultWarehouseLocation(identifier),
+            ImmutableMap.of());
+    HiveTableOperations spyOps = spy(ops);
+    createAndThrowAlreadyExists(ops, spyOps);
+    breakFallbackCatalogCommitCheck(spyOps);
+
+    try {
+      assertThatThrownBy(() -> spyOps.commit(null, metadata))
+          .isInstanceOf(CommitStateUnknownException.class)
+          .hasMessageStartingWith("Table already exists");
+      assertThat(metadataFileExists(ops.refresh()))
+          .as("Current metadata file should still exist")
+          .isTrue();
+    } finally {
+      catalog.dropTable(identifier, true);
+    }
+  }
+
+  @Test
+  void alreadyExistsCheckOOMOnCreate() throws TException, InterruptedException 
{
+    TableIdentifier identifier = TableIdentifier.of(DB_NAME, "created_tbl");
+    HiveTableOperations ops = (HiveTableOperations) 
catalog.newTableOps(identifier);
+    TableMetadata metadata =
+        TableMetadata.newTableMetadata(
+            SCHEMA,
+            PartitionSpec.unpartitioned(),
+            catalog.defaultWarehouseLocation(identifier),
+            ImmutableMap.of());
+    HiveTableOperations spyOps = spy(ops);
+    createAndThrowAlreadyExists(ops, spyOps);
+    when(spyOps.refresh()).thenThrow(new OutOfMemoryError());
+
+    try {
+      assertThatThrownBy(() -> spyOps.commit(null, metadata))
+          .isInstanceOf(OutOfMemoryError.class)
+          .hasMessage(null);

Review Comment:
   What is this check for?



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