You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@iceberg.apache.org by GitBox <gi...@apache.org> on 2022/03/13 22:58:52 UTC

[GitHub] [iceberg] rdblue opened a new pull request #4319: API: Implement equals and hashCode in CharSequenceSet

rdblue opened a new pull request #4319:
URL: https://github.com/apache/iceberg/pull/4319


   This implements `equals`, `hashCode`, and `toString` in `CharSequenceSet` to make testing easier.
   
   This is a small change in support of #4241.


-- 
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: issues-unsubscribe@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org


[GitHub] [iceberg] kbendick commented on a change in pull request #4319: Core: Add transaction catalog tests

Posted by GitBox <gi...@apache.org>.
kbendick commented on a change in pull request #4319:
URL: https://github.com/apache/iceberg/pull/4319#discussion_r826314325



##########
File path: core/src/main/java/org/apache/iceberg/BaseMetastoreTableOperations.java
##########
@@ -115,7 +116,11 @@ protected void doRefresh() {
   public void commit(TableMetadata base, TableMetadata metadata) {
     // if the metadata is already out of date, reject it
     if (base != current()) {
-      throw new CommitFailedException("Cannot commit: stale table metadata");
+      if (base != null) {
+        throw new CommitFailedException("Cannot commit: stale table metadata");
+      } else {
+        throw new AlreadyExistsException("Table already exists: %s", tableName());

Review comment:
       This exception message reads a little funny to me, but I think in context it will make sense.
   
   For clarification, what's happening here is that we entered some sort of create table transaction, and then at the end of the transaction, the table was created by some other process. Is that correct?
   
   A comment might help make that more clear but I can see how the stack trace will make sense in context.

##########
File path: core/src/test/java/org/apache/iceberg/catalog/CatalogTests.java
##########
@@ -319,6 +402,1345 @@ public void testNamespaceWithDot() {
     Assert.assertFalse("Namespace should not exist", catalog.namespaceExists(withDot));
   }
 
+  @Test
+  public void testBasicCreateTable() {
+    C catalog = catalog();
+
+    TableIdentifier ident = TableIdentifier.of("ns", "table");
+
+    Assert.assertFalse("Table should not exist", catalog.tableExists(ident));
+
+    Table table = catalog.buildTable(ident, SCHEMA).create();
+    Assert.assertTrue("Table should exist", catalog.tableExists(ident));
+
+    // validate table settings
+    Assert.assertEquals("Table name should report its full name", catalog.name() + "." + ident, table.name());
+    Assert.assertEquals("Schema should match expected ID assignment",
+        TABLE_SCHEMA.asStruct(), table.schema().asStruct());
+    Assert.assertNotNull("Should have a location", table.location());
+    Assert.assertTrue("Should be unpartitioned", table.spec().isUnpartitioned());
+    Assert.assertTrue("Should be unsorted", table.sortOrder().isUnsorted());
+    Assert.assertNotNull("Should have table properties", table.properties());
+  }

Review comment:
       Do we need to drop the tables that are being created?

##########
File path: core/src/test/java/org/apache/iceberg/catalog/CatalogTests.java
##########
@@ -319,6 +402,1345 @@ public void testNamespaceWithDot() {
     Assert.assertFalse("Namespace should not exist", catalog.namespaceExists(withDot));
   }
 
+  @Test
+  public void testBasicCreateTable() {
+    C catalog = catalog();
+
+    TableIdentifier ident = TableIdentifier.of("ns", "table");
+
+    Assert.assertFalse("Table should not exist", catalog.tableExists(ident));
+
+    Table table = catalog.buildTable(ident, SCHEMA).create();
+    Assert.assertTrue("Table should exist", catalog.tableExists(ident));
+
+    // validate table settings
+    Assert.assertEquals("Table name should report its full name", catalog.name() + "." + ident, table.name());
+    Assert.assertEquals("Schema should match expected ID assignment",
+        TABLE_SCHEMA.asStruct(), table.schema().asStruct());
+    Assert.assertNotNull("Should have a location", table.location());
+    Assert.assertTrue("Should be unpartitioned", table.spec().isUnpartitioned());
+    Assert.assertTrue("Should be unsorted", table.sortOrder().isUnsorted());
+    Assert.assertNotNull("Should have table properties", table.properties());
+  }
+
+  @Test
+  public void testBasicCreateTableThatAlreadyExists() {
+    C catalog = catalog();
+
+    TableIdentifier ident = TableIdentifier.of("ns", "table");
+
+    Assert.assertFalse("Table should not exist", catalog.tableExists(ident));
+
+    catalog.buildTable(ident, SCHEMA).create();
+    Assert.assertTrue("Table should exist", catalog.tableExists(ident));
+
+    AssertHelpers.assertThrows("Should fail to create a table that already exists",
+        AlreadyExistsException.class, "ns.table",
+        () -> catalog.buildTable(ident, OTHER_SCHEMA).create());
+
+    Table table = catalog.loadTable(ident);
+    Assert.assertEquals("Schema should match original table schema",
+        TABLE_SCHEMA.asStruct(), table.schema().asStruct());
+  }
+
+  @Test
+  public void testCompleteCreateTable() {
+    C catalog = catalog();
+
+    TableIdentifier ident = TableIdentifier.of("ns", "table");
+
+    Assert.assertFalse("Table should not exist", catalog.tableExists(ident));

Review comment:
       I think the only thing that could be extracted out would be the create table step. However, some of these tests only create a namespace and not the table. Others create a table in a different namespace, using the added `TABLE` constant.
   
   We could make a class level constant out of `ident`, but it might confuse things with `TABLE`.
   
   Is it possible to use that instead? I'm assuming there's a reason for the change in namespace.




-- 
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: issues-unsubscribe@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org


[GitHub] [iceberg] rdblue commented on a change in pull request #4319: Core: Add transaction catalog tests

Posted by GitBox <gi...@apache.org>.
rdblue commented on a change in pull request #4319:
URL: https://github.com/apache/iceberg/pull/4319#discussion_r826335278



##########
File path: core/src/test/java/org/apache/iceberg/catalog/CatalogTests.java
##########
@@ -319,6 +402,1345 @@ public void testNamespaceWithDot() {
     Assert.assertFalse("Namespace should not exist", catalog.namespaceExists(withDot));
   }
 
+  @Test
+  public void testBasicCreateTable() {
+    C catalog = catalog();
+
+    TableIdentifier ident = TableIdentifier.of("ns", "table");
+
+    Assert.assertFalse("Table should not exist", catalog.tableExists(ident));
+
+    Table table = catalog.buildTable(ident, SCHEMA).create();
+    Assert.assertTrue("Table should exist", catalog.tableExists(ident));
+
+    // validate table settings
+    Assert.assertEquals("Table name should report its full name", catalog.name() + "." + ident, table.name());
+    Assert.assertEquals("Schema should match expected ID assignment",
+        TABLE_SCHEMA.asStruct(), table.schema().asStruct());
+    Assert.assertNotNull("Should have a location", table.location());
+    Assert.assertTrue("Should be unpartitioned", table.spec().isUnpartitioned());
+    Assert.assertTrue("Should be unsorted", table.sortOrder().isUnsorted());
+    Assert.assertNotNull("Should have table properties", table.properties());
+  }

Review comment:
       No, the catalog is re-initialized every time and data on disk is in a temporary folder.




-- 
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: issues-unsubscribe@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org


[GitHub] [iceberg] rdblue merged pull request #4319: Core: Add transaction catalog tests

Posted by GitBox <gi...@apache.org>.
rdblue merged pull request #4319:
URL: https://github.com/apache/iceberg/pull/4319


   


-- 
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: issues-unsubscribe@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org


[GitHub] [iceberg] szehon-ho commented on a change in pull request #4319: Core: Add transaction catalog tests

Posted by GitBox <gi...@apache.org>.
szehon-ho commented on a change in pull request #4319:
URL: https://github.com/apache/iceberg/pull/4319#discussion_r826341160



##########
File path: core/src/main/java/org/apache/iceberg/BaseMetastoreTableOperations.java
##########
@@ -115,7 +116,11 @@ protected void doRefresh() {
   public void commit(TableMetadata base, TableMetadata metadata) {
     // if the metadata is already out of date, reject it
     if (base != current()) {
-      throw new CommitFailedException("Cannot commit: stale table metadata");
+      if (base != null) {
+        throw new CommitFailedException("Cannot commit: stale table metadata");
+      } else {
+        throw new AlreadyExistsException("Table already exists: %s", tableName());

Review comment:
       yea makes sense now, thanks.  I guess it may be the same concern as @kbendick :)  Maybe we should add this to @base argument javadoc in the TableOperations could clarify it, but itd be unrelated.




-- 
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: issues-unsubscribe@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org


[GitHub] [iceberg] rdblue commented on a change in pull request #4319: Core: Add transaction catalog tests

Posted by GitBox <gi...@apache.org>.
rdblue commented on a change in pull request #4319:
URL: https://github.com/apache/iceberg/pull/4319#discussion_r826319550



##########
File path: core/src/main/java/org/apache/iceberg/BaseMetastoreTableOperations.java
##########
@@ -115,7 +116,11 @@ protected void doRefresh() {
   public void commit(TableMetadata base, TableMetadata metadata) {
     // if the metadata is already out of date, reject it
     if (base != current()) {
-      throw new CommitFailedException("Cannot commit: stale table metadata");
+      if (base != null) {
+        throw new CommitFailedException("Cannot commit: stale table metadata");
+      } else {
+        throw new AlreadyExistsException("Table already exists: %s", tableName());

Review comment:
       When `base` is null, that signals that the commit is trying to create the table and it should not exist. When `current` is not null, that signals that the table already exists. So in order to throw `AlreadyExistsException` from the create transaction, this needs to catch that case.




-- 
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: issues-unsubscribe@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org


[GitHub] [iceberg] rdblue commented on a change in pull request #4319: Core: Add transaction catalog tests

Posted by GitBox <gi...@apache.org>.
rdblue commented on a change in pull request #4319:
URL: https://github.com/apache/iceberg/pull/4319#discussion_r826526152



##########
File path: core/src/main/java/org/apache/iceberg/BaseMetastoreTableOperations.java
##########
@@ -115,7 +116,11 @@ protected void doRefresh() {
   public void commit(TableMetadata base, TableMetadata metadata) {
     // if the metadata is already out of date, reject it
     if (base != current()) {
-      throw new CommitFailedException("Cannot commit: stale table metadata");
+      if (base != null) {
+        throw new CommitFailedException("Cannot commit: stale table metadata");
+      } else {
+        throw new AlreadyExistsException("Table already exists: %s", tableName());

Review comment:
       I added a comment to make this clear.




-- 
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: issues-unsubscribe@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org


[GitHub] [iceberg] rdblue commented on pull request #4319: Core: Add transaction catalog tests

Posted by GitBox <gi...@apache.org>.
rdblue commented on pull request #4319:
URL: https://github.com/apache/iceberg/pull/4319#issuecomment-1067520223


   Thanks for reviewing, @nastra, @szehon-ho, @kbendick, and @danielcweeks!


-- 
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: issues-unsubscribe@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org


[GitHub] [iceberg] rdblue commented on a change in pull request #4319: Core: Add transaction catalog tests

Posted by GitBox <gi...@apache.org>.
rdblue commented on a change in pull request #4319:
URL: https://github.com/apache/iceberg/pull/4319#discussion_r826524811



##########
File path: core/src/main/java/org/apache/iceberg/jdbc/JdbcTableOperations.java
##########
@@ -123,6 +123,9 @@ public void doCommit(TableMetadata base, TableMetadata metadata) {
     } catch (SQLWarning e) {
       throw new UncheckedSQLException(e, "Database warning");
     } catch (SQLException e) {
+      if (e.getMessage().contains("constraint failed")) {

Review comment:
       `SQLIntegrityConstraintViolationException` is handled above. The problem is that SQLite doesn't set anything correctly. It doesn't throw `SQLIntegrityConstraintViolationException`, or set `SQLState=23`, or include other exceptions. It uses its own exception class with this error message:
   
   > [SQLITE_CONSTRAINT_PRIMARYKEY]  A PRIMARY KEY constraint failed (UNIQUE constraint failed: iceberg_tables.catalog_name, iceberg_tables.table_namespace, iceberg_tables.table_name)
   
   It does set `e.vendorCode=19`, but that would require checking that the exception class contains `SQLite` so we're doing string matching either way.
   
   I'll add a comment to make it clear what's happening here.




-- 
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: issues-unsubscribe@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org


[GitHub] [iceberg] szehon-ho commented on a change in pull request #4319: Core: Add transaction catalog tests

Posted by GitBox <gi...@apache.org>.
szehon-ho commented on a change in pull request #4319:
URL: https://github.com/apache/iceberg/pull/4319#discussion_r826341160



##########
File path: core/src/main/java/org/apache/iceberg/BaseMetastoreTableOperations.java
##########
@@ -115,7 +116,11 @@ protected void doRefresh() {
   public void commit(TableMetadata base, TableMetadata metadata) {
     // if the metadata is already out of date, reject it
     if (base != current()) {
-      throw new CommitFailedException("Cannot commit: stale table metadata");
+      if (base != null) {
+        throw new CommitFailedException("Cannot commit: stale table metadata");
+      } else {
+        throw new AlreadyExistsException("Table already exists: %s", tableName());

Review comment:
       yea makes sense now, thanks.  I guess its same concern as @kbendick :)  Maybe we should add this to @base argument javadoc in the TableOperations could clarify it, but itd be unrelated.




-- 
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: issues-unsubscribe@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org


[GitHub] [iceberg] rdblue commented on a change in pull request #4319: API: Implement equals and hashCode in CharSequenceSet

Posted by GitBox <gi...@apache.org>.
rdblue commented on a change in pull request #4319:
URL: https://github.com/apache/iceberg/pull/4319#discussion_r825516869



##########
File path: api/src/main/java/org/apache/iceberg/util/CharSequenceSet.java
##########
@@ -152,4 +156,28 @@ public boolean removeAll(Collection<?> objects) {
   public void clear() {
     wrapperSet.clear();
   }
+
+  @Override
+  public boolean equals(Object o) {
+    if (this == o) {
+      return true;
+    }
+
+    if (o == null || getClass() != o.getClass()) {
+      return false;
+    }
+
+    CharSequenceSet that = (CharSequenceSet) o;
+    return wrapperSet.equals(that.wrapperSet);
+  }
+
+  @Override
+  public int hashCode() {
+    return Objects.hash(wrapperSet);
+  }
+
+  @Override
+  public String toString() {
+    return Streams.stream(iterator()).collect(Collectors.joining("CharSequenceSet({", ", ", "})"));

Review comment:
       @szehon-ho, I addressed [your comment](https://github.com/apache/iceberg/pull/4241/files#r816330118).




-- 
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: issues-unsubscribe@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org


[GitHub] [iceberg] szehon-ho commented on a change in pull request #4319: Core: Add transaction catalog tests

Posted by GitBox <gi...@apache.org>.
szehon-ho commented on a change in pull request #4319:
URL: https://github.com/apache/iceberg/pull/4319#discussion_r826341160



##########
File path: core/src/main/java/org/apache/iceberg/BaseMetastoreTableOperations.java
##########
@@ -115,7 +116,11 @@ protected void doRefresh() {
   public void commit(TableMetadata base, TableMetadata metadata) {
     // if the metadata is already out of date, reject it
     if (base != current()) {
-      throw new CommitFailedException("Cannot commit: stale table metadata");
+      if (base != null) {
+        throw new CommitFailedException("Cannot commit: stale table metadata");
+      } else {
+        throw new AlreadyExistsException("Table already exists: %s", tableName());

Review comment:
       yea makes sense now, thanks.  I guess it may be the same concern as @kbendick :)  Maybe we should add this to @base argument javadoc in the TableOperations to clarify it, but itd be unrelated.




-- 
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: issues-unsubscribe@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org


[GitHub] [iceberg] szehon-ho commented on a change in pull request #4319: Core: Add transaction catalog tests

Posted by GitBox <gi...@apache.org>.
szehon-ho commented on a change in pull request #4319:
URL: https://github.com/apache/iceberg/pull/4319#discussion_r826317706



##########
File path: core/src/main/java/org/apache/iceberg/BaseMetastoreTableOperations.java
##########
@@ -115,7 +116,11 @@ protected void doRefresh() {
   public void commit(TableMetadata base, TableMetadata metadata) {
     // if the metadata is already out of date, reject it
     if (base != current()) {
-      throw new CommitFailedException("Cannot commit: stale table metadata");
+      if (base != null) {
+        throw new CommitFailedException("Cannot commit: stale table metadata");
+      } else {
+        throw new AlreadyExistsException("Table already exists: %s", tableName());

Review comment:
       I didnt look deeply, but wondering why if caller passes base=null and current is not null , we can tell the table already exists?   It seems it would be a new assumption to all the calling code?




-- 
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: issues-unsubscribe@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org


[GitHub] [iceberg] rdblue commented on a change in pull request #4319: Core: Add transaction catalog tests

Posted by GitBox <gi...@apache.org>.
rdblue commented on a change in pull request #4319:
URL: https://github.com/apache/iceberg/pull/4319#discussion_r826526066



##########
File path: core/src/main/java/org/apache/iceberg/BaseMetastoreTableOperations.java
##########
@@ -115,7 +116,11 @@ protected void doRefresh() {
   public void commit(TableMetadata base, TableMetadata metadata) {
     // if the metadata is already out of date, reject it
     if (base != current()) {
-      throw new CommitFailedException("Cannot commit: stale table metadata");
+      if (base != null) {
+        throw new CommitFailedException("Cannot commit: stale table metadata");
+      } else {
+        throw new AlreadyExistsException("Table already exists: %s", tableName());

Review comment:
       I added a comment to make this clear.




-- 
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: issues-unsubscribe@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org


[GitHub] [iceberg] nastra commented on a change in pull request #4319: Core: Add transaction catalog tests

Posted by GitBox <gi...@apache.org>.
nastra commented on a change in pull request #4319:
URL: https://github.com/apache/iceberg/pull/4319#discussion_r825667994



##########
File path: core/src/test/java/org/apache/iceberg/catalog/CatalogTests.java
##########
@@ -319,6 +402,1345 @@ public void testNamespaceWithDot() {
     Assert.assertFalse("Namespace should not exist", catalog.namespaceExists(withDot));
   }
 
+  @Test
+  public void testBasicCreateTable() {
+    C catalog = catalog();
+
+    TableIdentifier ident = TableIdentifier.of("ns", "table");
+
+    Assert.assertFalse("Table should not exist", catalog.tableExists(ident));
+
+    Table table = catalog.buildTable(ident, SCHEMA).create();
+    Assert.assertTrue("Table should exist", catalog.tableExists(ident));
+
+    // validate table settings
+    Assert.assertEquals("Table name should report its full name", catalog.name() + "." + ident, table.name());
+    Assert.assertEquals("Schema should match expected ID assignment",
+        TABLE_SCHEMA.asStruct(), table.schema().asStruct());
+    Assert.assertNotNull("Should have a location", table.location());
+    Assert.assertTrue("Should be unpartitioned", table.spec().isUnpartitioned());
+    Assert.assertTrue("Should be unsorted", table.sortOrder().isUnsorted());
+    Assert.assertNotNull("Should have table properties", table.properties());
+  }
+
+  @Test
+  public void testBasicCreateTableThatAlreadyExists() {
+    C catalog = catalog();
+
+    TableIdentifier ident = TableIdentifier.of("ns", "table");
+
+    Assert.assertFalse("Table should not exist", catalog.tableExists(ident));
+
+    catalog.buildTable(ident, SCHEMA).create();
+    Assert.assertTrue("Table should exist", catalog.tableExists(ident));
+
+    AssertHelpers.assertThrows("Should fail to create a table that already exists",
+        AlreadyExistsException.class, "ns.table",
+        () -> catalog.buildTable(ident, OTHER_SCHEMA).create());
+
+    Table table = catalog.loadTable(ident);
+    Assert.assertEquals("Schema should match original table schema",
+        TABLE_SCHEMA.asStruct(), table.schema().asStruct());
+  }
+
+  @Test
+  public void testCompleteCreateTable() {
+    C catalog = catalog();
+
+    TableIdentifier ident = TableIdentifier.of("ns", "table");
+
+    Assert.assertFalse("Table should not exist", catalog.tableExists(ident));

Review comment:
       it looks like the first 3 lines are repeated in a lot of tests. would it make sense to extract this into some method?

##########
File path: core/src/test/java/org/apache/iceberg/catalog/CatalogTests.java
##########
@@ -319,6 +402,1345 @@ public void testNamespaceWithDot() {
     Assert.assertFalse("Namespace should not exist", catalog.namespaceExists(withDot));
   }
 
+  @Test
+  public void testBasicCreateTable() {
+    C catalog = catalog();
+
+    TableIdentifier ident = TableIdentifier.of("ns", "table");
+
+    Assert.assertFalse("Table should not exist", catalog.tableExists(ident));
+
+    Table table = catalog.buildTable(ident, SCHEMA).create();
+    Assert.assertTrue("Table should exist", catalog.tableExists(ident));
+
+    // validate table settings
+    Assert.assertEquals("Table name should report its full name", catalog.name() + "." + ident, table.name());
+    Assert.assertEquals("Schema should match expected ID assignment",
+        TABLE_SCHEMA.asStruct(), table.schema().asStruct());
+    Assert.assertNotNull("Should have a location", table.location());
+    Assert.assertTrue("Should be unpartitioned", table.spec().isUnpartitioned());
+    Assert.assertTrue("Should be unsorted", table.sortOrder().isUnsorted());
+    Assert.assertNotNull("Should have table properties", table.properties());
+  }
+
+  @Test
+  public void testBasicCreateTableThatAlreadyExists() {
+    C catalog = catalog();
+
+    TableIdentifier ident = TableIdentifier.of("ns", "table");
+
+    Assert.assertFalse("Table should not exist", catalog.tableExists(ident));
+
+    catalog.buildTable(ident, SCHEMA).create();
+    Assert.assertTrue("Table should exist", catalog.tableExists(ident));
+
+    AssertHelpers.assertThrows("Should fail to create a table that already exists",
+        AlreadyExistsException.class, "ns.table",
+        () -> catalog.buildTable(ident, OTHER_SCHEMA).create());
+
+    Table table = catalog.loadTable(ident);
+    Assert.assertEquals("Schema should match original table schema",
+        TABLE_SCHEMA.asStruct(), table.schema().asStruct());
+  }
+
+  @Test
+  public void testCompleteCreateTable() {
+    C catalog = catalog();
+
+    TableIdentifier ident = TableIdentifier.of("ns", "table");
+
+    Assert.assertFalse("Table should not exist", catalog.tableExists(ident));
+
+    Map<String, String> properties = ImmutableMap.of("user", "someone", "created-at", "2022-02-25T00:38:19");
+    Table table = catalog.buildTable(ident, SCHEMA)
+        .withLocation("file:/tmp/ns/table")
+        .withPartitionSpec(SPEC)
+        .withSortOrder(WRITE_ORDER)
+        .withProperties(properties)
+        .create();
+
+    // validate table settings
+    Assert.assertEquals("Table name should report its full name", catalog.name() + "." + ident, table.name());
+    Assert.assertTrue("Table should exist", catalog.tableExists(ident));
+    Assert.assertEquals("Schema should match expected ID assignment",
+        TABLE_SCHEMA.asStruct(), table.schema().asStruct());
+    Assert.assertNotNull("Should have a location", table.location());
+    Assert.assertEquals("Should use requested partition spec", TABLE_SPEC, table.spec());
+    Assert.assertEquals("Should use requested write order", TABLE_WRITE_ORDER, table.sortOrder());
+    Assert.assertEquals("Table properties should be a superset of the requested properties",
+        properties.entrySet(),
+        Sets.intersection(properties.entrySet(), table.properties().entrySet()));
+  }
+
+  @Test
+  public void testLoadTable() {
+    C catalog = catalog();
+
+    TableIdentifier ident = TableIdentifier.of("ns", "table");
+
+    Assert.assertFalse("Table should not exist", catalog.tableExists(ident));
+
+    Map<String, String> properties = ImmutableMap.of("user", "someone", "created-at", "2022-02-25T00:38:19");
+    catalog.buildTable(ident, SCHEMA)
+        .withLocation("file:/tmp/ns/table")
+        .withPartitionSpec(SPEC)
+        .withSortOrder(WRITE_ORDER)
+        .withProperties(properties)
+        .create();
+    Assert.assertTrue("Table should exist", catalog.tableExists(ident));
+
+    Table table = catalog.loadTable(ident);
+    // validate table settings
+    Assert.assertEquals("Table name should report its full name", catalog.name() + "." + ident, table.name());
+    Assert.assertTrue("Table should exist", catalog.tableExists(ident));
+    Assert.assertEquals("Schema should match expected ID assignment",
+        TABLE_SCHEMA.asStruct(), table.schema().asStruct());
+    Assert.assertNotNull("Should have a location", table.location());
+    Assert.assertEquals("Should use requested partition spec", TABLE_SPEC, table.spec());
+    Assert.assertEquals("Should use requested write order", TABLE_WRITE_ORDER, table.sortOrder());
+    Assert.assertEquals("Table properties should be a superset of the requested properties",
+        properties.entrySet(),
+        Sets.intersection(properties.entrySet(), table.properties().entrySet()));
+  }
+
+  @Test
+  public void testLoadMissingTable() {
+    C catalog = catalog();
+
+    TableIdentifier ident = TableIdentifier.of("ns", "table");
+
+    Assert.assertFalse("Table should not exist", catalog.tableExists(ident));
+    AssertHelpers.assertThrows("Should fail to load a nonexistent table",
+        NoSuchTableException.class, ident.toString(),
+        () -> catalog.loadTable(ident));
+  }
+
+  @Test
+  public void testDropTable() {
+    C catalog = catalog();
+
+    if (requiresNamespaceCreate()) {
+      catalog.createNamespace(NS);
+    }
+
+    Assert.assertFalse("Table should not exist before create", catalog.tableExists(TABLE));
+
+    catalog.buildTable(TABLE, SCHEMA).create();
+    Assert.assertTrue("Table should exist after create", catalog.tableExists(TABLE));
+
+    boolean dropped = catalog.dropTable(TABLE);
+    Assert.assertTrue("Should drop a table that does exist", dropped);
+    Assert.assertFalse("Table should not exist after drop", catalog.tableExists(TABLE));
+  }
+
+  @Test
+  public void testDropMissingTable() {
+    C catalog = catalog();
+
+    if (requiresNamespaceCreate()) {
+      catalog.createNamespace(NS);
+    }
+
+    TableIdentifier noSuchTableIdent = TableIdentifier.of(NS, "notable");
+    Assert.assertFalse("Table should not exist", catalog.tableExists(noSuchTableIdent));
+    Assert.assertFalse("Should not drop a table that does not exist", catalog.dropTable(noSuchTableIdent));
+  }
+
+  @Test
+  public void testListTables() {
+    C catalog = catalog();
+
+    Namespace ns1 = Namespace.of("ns_1");
+    Namespace ns2 = Namespace.of("ns_2");
+
+    TableIdentifier ns1Table1 = TableIdentifier.of(ns1, "table_1");
+    TableIdentifier ns1Table2 = TableIdentifier.of(ns1, "table_2");
+    TableIdentifier ns2Table1 = TableIdentifier.of(ns2, "table_1");
+
+    if (requiresNamespaceCreate()) {
+      catalog.createNamespace(ns1);
+      catalog.createNamespace(ns2);
+    }
+
+    assertEmpty("Should not have tables in a new namespace, ns_1", catalog, ns1);
+    assertEmpty("Should not have tables in a new namespace, ns_2", catalog, ns2);
+
+    catalog.buildTable(ns1Table1, SCHEMA).create();
+
+    Assert.assertEquals("Should contain ns_1.table_1 after create",
+        ImmutableSet.of(ns1Table1), Sets.newHashSet(catalog.listTables(ns1)));
+
+    catalog.buildTable(ns2Table1, SCHEMA).create();
+
+    Assert.assertEquals("Should contain ns_2.table_1 after create",
+        ImmutableSet.of(ns2Table1), Sets.newHashSet(catalog.listTables(ns2)));
+    Assert.assertEquals("Should not show changes to ns_2 in ns_1",
+        ImmutableSet.of(ns1Table1), Sets.newHashSet(catalog.listTables(ns1)));
+
+    catalog.buildTable(ns1Table2, SCHEMA).create();
+
+    Assert.assertEquals("Should not show changes to ns_1 in ns_2",
+        ImmutableSet.of(ns2Table1), Sets.newHashSet(catalog.listTables(ns2)));
+    Assert.assertEquals("Should contain ns_1.table_2 after create",
+        ImmutableSet.of(ns1Table1, ns1Table2), Sets.newHashSet(catalog.listTables(ns1)));
+
+    catalog.dropTable(ns1Table1);
+
+    Assert.assertEquals("Should not show changes to ns_1 in ns_2",
+        ImmutableSet.of(ns2Table1), Sets.newHashSet(catalog.listTables(ns2)));
+    Assert.assertEquals("Should not contain ns_1.table_1 after drop",
+        ImmutableSet.of(ns1Table2), Sets.newHashSet(catalog.listTables(ns1)));
+
+    catalog.dropTable(ns1Table2);
+
+    Assert.assertEquals("Should not show changes to ns_1 in ns_2",
+        ImmutableSet.of(ns2Table1), Sets.newHashSet(catalog.listTables(ns2)));
+    assertEmpty("Should not contain ns_1.table_2 after drop", catalog, ns1);
+
+    catalog.dropTable(ns2Table1);
+    assertEmpty("Should not contain ns_2.table_1 after drop", catalog, ns2);
+  }
+
+  @Test
+  public void testUpdateTableSchema() {
+    C catalog = catalog();
+
+    Table table = catalog.buildTable(TABLE, SCHEMA).create();
+    UpdateSchema update = table.updateSchema()
+        .addColumn("new_col", Types.LongType.get());
+
+    Schema expected = update.apply();
+
+    update.commit();
+
+    Table loaded = catalog.loadTable(TABLE);
+
+    Assert.assertEquals("Loaded table should have expected schema", expected.asStruct(), loaded.schema().asStruct());
+  }
+
+  @Test
+  public void testUUIDValidation() {
+    C catalog = catalog();
+
+    Table table = catalog.buildTable(TABLE, SCHEMA).create();
+    UpdateSchema update = table.updateSchema()
+        .addColumn("new_col", Types.LongType.get());
+
+    Assert.assertTrue("Should successfully drop table", catalog.dropTable(TABLE));
+    catalog.buildTable(TABLE, OTHER_SCHEMA).create();
+
+    String expectedMessage = supportsServerSideRetry() ? "Requirement failed: UUID does not match" : "Cannot commit";
+    AssertHelpers.assertThrows("Should reject changes to tables that have been dropped and recreated",
+        CommitFailedException.class, expectedMessage, update::commit);
+
+    Table loaded = catalog.loadTable(TABLE);
+    Assert.assertEquals("Loaded table should have expected schema",
+        OTHER_SCHEMA.asStruct(), loaded.schema().asStruct());
+  }
+
+  @Test
+  public void testUpdateTableSchemaServerSideRetry() {
+    Assume.assumeTrue("Schema update recovery is only supported with server-side retry", supportsServerSideRetry());
+    C catalog = catalog();
+
+    Table table = catalog.buildTable(TABLE, SCHEMA).create();
+
+    UpdateSchema update = table.updateSchema()
+        .addColumn("new_col", Types.LongType.get());
+    Schema expected = update.apply();
+
+    // update the spec concurrently so that the first update fails, but can succeed on retry
+    catalog.loadTable(TABLE).updateSpec()
+        .addField("shard", Expressions.bucket("id", 16))
+        .commit();
+
+    // commit the original update
+    update.commit();
+
+    Table loaded = catalog.loadTable(TABLE);
+    Assert.assertEquals("Loaded table should have expected schema", expected.asStruct(), loaded.schema().asStruct());
+  }
+
+  @Test
+  public void testUpdateTableSchemaConflict() {
+    C catalog = catalog();
+
+    Table table = catalog.buildTable(TABLE, SCHEMA).create();
+
+    UpdateSchema update = table.updateSchema()
+        .addColumn("new_col", Types.LongType.get());
+
+    // update the schema concurrently so that the original update fails
+    UpdateSchema concurrent = catalog.loadTable(TABLE).updateSchema()
+        .deleteColumn("data");
+    Schema expected = concurrent.apply();
+    concurrent.commit();
+
+    // attempt to commit the original update
+    String expectedMessage = supportsServerSideRetry() ?
+        "Requirement failed: current schema changed" : "Cannot commit";
+    AssertHelpers.assertThrows("Second schema update commit should fail because of a conflict",
+        CommitFailedException.class, expectedMessage, update::commit);
+
+    Table loaded = catalog.loadTable(TABLE);
+    Assert.assertEquals("Loaded table should have expected schema", expected.asStruct(), loaded.schema().asStruct());
+  }
+
+  @Test
+  public void testUpdateTableSchemaAssignmentConflict() {
+    C catalog = catalog();
+
+    Table table = catalog.buildTable(TABLE, SCHEMA).create();
+
+    UpdateSchema update = table.updateSchema()
+        .addColumn("new_col", Types.LongType.get());
+
+    // update the schema concurrently so that the original update fails
+    UpdateSchema concurrent = catalog.loadTable(TABLE).updateSchema()
+        .addColumn("another_col", Types.StringType.get());
+    Schema expected = concurrent.apply();
+    concurrent.commit();
+
+    // attempt to commit the original update
+    String expectedMessage = supportsServerSideRetry() ?
+        "Requirement failed: last assigned field id changed" : "Cannot commit";
+    AssertHelpers.assertThrows("Second schema update commit should fail because of a conflict",
+        CommitFailedException.class, expectedMessage, update::commit);
+
+    Table loaded = catalog.loadTable(TABLE);
+    Assert.assertEquals("Loaded table should have expected schema", expected.asStruct(), loaded.schema().asStruct());
+  }
+
+  @Test
+  public void testUpdateTableSpec() {
+    C catalog = catalog();
+
+    Table table = catalog.buildTable(TABLE, SCHEMA).create();
+    UpdatePartitionSpec update = table.updateSpec()
+        .addField("shard", Expressions.bucket("id", 16));
+
+    PartitionSpec expected = update.apply();
+
+    update.commit();
+
+    Table loaded = catalog.loadTable(TABLE);
+
+    // the spec ID may not match, so check equality of the fields
+    Assert.assertEquals("Loaded table should have expected spec", expected.fields(), loaded.spec().fields());
+  }
+
+  @Test
+  public void testUpdateTableSpecServerSideRetry() {
+    Assume.assumeTrue("Spec update recovery is only supported with server-side retry", supportsServerSideRetry());
+    C catalog = catalog();
+
+    Table table = catalog.buildTable(TABLE, SCHEMA).create();
+
+    UpdatePartitionSpec update = table.updateSpec()
+        .addField("shard", Expressions.bucket("id", 16));
+    PartitionSpec expected = update.apply();
+
+    // update the schema concurrently so that the first update fails, but can succeed on retry
+    catalog.loadTable(TABLE).updateSchema()
+        .addColumn("another_col", Types.StringType.get())
+        .commit();
+
+    // commit the original update
+    update.commit();
+
+    Table loaded = catalog.loadTable(TABLE);
+
+    // the spec ID may not match, so check equality of the fields
+    Assert.assertEquals("Loaded table should have expected spec", expected.fields(), loaded.spec().fields());
+  }
+
+  @Test
+  public void testUpdateTableSpecConflict() {
+    C catalog = catalog();
+
+    Table table = catalog.buildTable(TABLE, SCHEMA).withPartitionSpec(SPEC).create();
+
+    UpdatePartitionSpec update = table.updateSpec()
+        .addField("shard", Expressions.bucket("data", 16));
+
+    // update the spec concurrently so that the original update fails
+    UpdatePartitionSpec concurrent = catalog.loadTable(TABLE).updateSpec()
+        .removeField(Expressions.bucket("id", 16));
+    PartitionSpec expected = concurrent.apply();
+    concurrent.commit();
+
+    // attempt to commit the original update
+    String expectedMessage = supportsServerSideRetry() ?
+        "Requirement failed: default partition spec changed" : "Cannot commit";
+    AssertHelpers.assertThrows("Second partition spec update commit should fail because of a conflict",
+        CommitFailedException.class, expectedMessage, update::commit);
+
+    Table loaded = catalog.loadTable(TABLE);
+
+    // the spec ID may not match, so check equality of the fields
+    Assert.assertEquals("Loaded table should have expected spec", expected.fields(), loaded.spec().fields());
+  }
+
+  @Test
+  public void testUpdateTableAssignmentSpecConflict() {
+    C catalog = catalog();
+
+    Table table = catalog.buildTable(TABLE, SCHEMA).create();
+
+    UpdatePartitionSpec update = table.updateSpec()
+        .addField("shard", Expressions.bucket("id", 16));
+
+    // update the spec concurrently so that the original update fails
+    UpdatePartitionSpec concurrent = catalog.loadTable(TABLE).updateSpec()
+        .addField("shard", Expressions.truncate("id", 100));
+    PartitionSpec expected = concurrent.apply();
+    concurrent.commit();
+
+    // attempt to commit the original update
+    String expectedMessage = supportsServerSideRetry() ?
+        "Requirement failed: last assigned partition id changed" : "Cannot commit";
+    AssertHelpers.assertThrows("Second partition spec update commit should fail because of a conflict",
+        CommitFailedException.class, expectedMessage, update::commit);
+
+    Table loaded = catalog.loadTable(TABLE);
+
+    // the spec ID may not match, so check equality of the fields
+    Assert.assertEquals("Loaded table should have expected spec", expected.fields(), loaded.spec().fields());
+  }
+
+  @Test
+  public void testUpdateTableSortOrder() {

Review comment:
       `testUpdateTableSortOrder` and `testUpdateTableSortOrderServerSideRetry` for example only differ in 1-2 lines, so I woder if we can improve the tests a bit and try and reduce code duplication (in a follow up PR)




-- 
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: issues-unsubscribe@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org


[GitHub] [iceberg] danielcweeks commented on a change in pull request #4319: Core: Add transaction catalog tests

Posted by GitBox <gi...@apache.org>.
danielcweeks commented on a change in pull request #4319:
URL: https://github.com/apache/iceberg/pull/4319#discussion_r826458947



##########
File path: core/src/main/java/org/apache/iceberg/jdbc/JdbcTableOperations.java
##########
@@ -123,6 +123,9 @@ public void doCommit(TableMetadata base, TableMetadata metadata) {
     } catch (SQLWarning e) {
       throw new UncheckedSQLException(e, "Database warning");
     } catch (SQLException e) {
+      if (e.getMessage().contains("constraint failed")) {

Review comment:
       This feels like a pretty non-portable way of handling constraint failed.  There may be a better way to identify this either using the `SQLState` string or with the nested exceptions in the provided `iterator()`.  




-- 
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: issues-unsubscribe@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org