bbiiaaoo commented on code in PR #12383:
URL: https://github.com/apache/gravitino/pull/12383#discussion_r3763311749


##########
catalogs/catalog-lakehouse-generic/src/main/java/org/apache/gravitino/catalog/lakehouse/lance/LanceTableOperations.java:
##########
@@ -301,15 +304,24 @@ public Table createTable(
   @Override
   public Table alterTable(NameIdentifier ident, TableChange... changes)
       throws NoSuchSchemaException, TableAlreadyExistsException {
+    List<Field> fieldsToAdd = prepareFieldsToAdd(changes);
+    // A declared or registered table can have an empty stored schema while 
its Lance dataset
+    // already contains columns. Hydrate the metadata before applying an 
incremental AddColumn.
+    Table loadedTable = fieldsToAdd.isEmpty() ? super.loadTable(ident) : 
loadTable(ident);
+    validateFieldsToAdd(loadedTable, fieldsToAdd);
+    long version = handleLanceTableChange(loadedTable, changes, fieldsToAdd);
 
-    Table loadedTable = super.loadTable(ident);
-    long version = handleLanceTableChange(loadedTable, changes);
     // After making changes to the Lance dataset, we need to update the table 
metadata in
     // Gravitino. If there's any failure during this process, the code will 
throw an exception
     // and the update won't be applied in Gravitino.
-    TableChange[] metadataChanges = Arrays.copyOf(changes, changes.length + 1);
+    int internalChangeCount = fieldsToAdd.isEmpty() ? 1 : 2;
+    TableChange[] metadataChanges = Arrays.copyOf(changes, changes.length + 
internalChangeCount);
     metadataChanges[changes.length] =
         TableChange.setProperty(LanceConstants.LANCE_TABLE_VERSION, 
String.valueOf(version));
+    if (!fieldsToAdd.isEmpty()) {
+      metadataChanges[changes.length + 1] =

Review Comment:
   Done. I replaced the manual array copying and indexing with sequential 
ArrayUtils.add calls.



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

Reply via email to