rdblue commented on code in PR #5199:
URL: https://github.com/apache/iceberg/pull/5199#discussion_r917054416


##########
core/src/main/java/org/apache/iceberg/rest/CatalogHandlers.java:
##########
@@ -282,20 +283,77 @@ private static boolean isCreate(UpdateTableRequest 
request) {
     return isCreate;
   }
 
-  private static TableMetadata create(TableOperations ops, TableMetadata 
start, UpdateTableRequest request) {
+  private static TableMetadata commitUpdate(Catalog catalog, TableIdentifier 
ident, UpdateTableRequest request) {
+    Table table = catalog.loadTable(ident);
+    if (table instanceof BaseTable) {
+      TableOperations ops = ((BaseTable) table).operations();
+      return commit(ops, request.requirements(), request.updates());
+    } else {
+      throw new IllegalStateException("Cannot wrap catalog that does not 
produce BaseTable");
+    }
+  }
+
+  private static TableMetadata commitCreate(Catalog catalog, TableIdentifier 
ident, UpdateTableRequest request) {
+    // this is a hacky way to get TableOperations for an uncommitted table
+    Transaction transaction = catalog.buildTable(ident, 
EMPTY_SCHEMA).createOrReplaceTransaction();
+    if (!(transaction instanceof BaseTransaction)) {
+      throw new IllegalStateException("Cannot wrap catalog that does not 
produce BaseTransaction");
+    }
+    TableOperations ops = ((BaseTransaction) transaction).underlyingOps();
+
     // the only valid requirement is that the table will be created
     request.requirements().forEach(requirement -> 
requirement.validate(ops.current()));
 
+    CreateCommitInfo info = extractCreateCommitInfo(request);
+    PartitionSpec partitionSpec =
+        info.unboundPartitionSpec == null ? PartitionSpec.unpartitioned() : 
info.unboundPartitionSpec.bind(info.schema);
+    SortOrder sortOrder =
+        info.unboundSortOrder == null ? SortOrder.unsorted() : 
info.unboundSortOrder.bind(info.schema);
+    if (info.props == null) {
+      info.props = ImmutableMap.of();
+    }
+
+    TableMetadata start =
+            TableMetadata.newTableMetadata(info.schema, partitionSpec, 
sortOrder, info.location, info.props);
     TableMetadata.Builder builder = TableMetadata.buildFrom(start);
-    request.updates().forEach(update -> update.applyTo(builder));
+    info.otherUpdates.forEach(update -> update.applyTo(builder));
 
     // create transactions do not retry. if the table exists, retrying is not 
a solution
     ops.commit(null, builder.build());
 
     return ops.current();
   }
 
-  private static TableMetadata commit(TableOperations ops, UpdateTableRequest 
request) {
+  private static CreateCommitInfo extractCreateCommitInfo(UpdateTableRequest 
request) {
+    CreateCommitInfo result = new CreateCommitInfo();
+    for (MetadataUpdate update : request.updates()) {
+      if (update instanceof MetadataUpdate.AddSchema) {
+        result.schema = ((MetadataUpdate.AddSchema) update).schema();

Review Comment:
   This still isn't quite right. I think it needs to break if it sees a second 
`AddSchema` or another change that must come after the create, because we know 
that the create won't contain two.
   
   For example, if we see `AddSchema`, `AddSchema`, `AddPartitionSpec`, 
(leaving out set changes) then we know the table was created as an 
unpartitioned table. Pulling the partition spec out to the table create isn't 
correct. I think that would cause a problem in the final table because creating 
it with a partition spec would give that spec ID 0, rather than the 
unpartitioned spec. As a result, the spec IDs would be off if there were data 
written in the transaction.



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