public Table alterTable()

in core/src/main/java/org/apache/gravitino/catalog/TableOperationDispatcher.java [198:289]


  public Table alterTable(NameIdentifier ident, TableChange... changes)
      throws NoSuchTableException, IllegalArgumentException {
    validateAlterProperties(ident, HasPropertyMetadata::tablePropertiesMetadata, changes);

    // Check if there exist TableChange.RenameTable in the changes, if so, we need to TreeLock of
    // write on the new table name, or use the read lock on the table instead.
    boolean containsRenameTable =
        Arrays.stream(changes).anyMatch(c -> c instanceof TableChange.RenameTable);
    NameIdentifier nameIdentifierForLock =
        containsRenameTable ? NameIdentifier.of(ident.namespace().levels()) : ident;

    return TreeLockUtils.doWithTreeLock(
        nameIdentifierForLock,
        LockType.WRITE,
        () -> {
          NameIdentifier catalogIdent = getCatalogIdentifier(ident);
          Table alteredTable =
              doWithCatalog(
                  catalogIdent,
                  c ->
                      c.doWithTableOps(
                          t -> t.alterTable(ident, applyCapabilities(c.capabilities(), changes))),
                  NoSuchTableException.class,
                  IllegalArgumentException.class);

          StringIdentifier stringId = getStringIdFromProperties(alteredTable.properties());
          // Case 1: The table is not created by Gravitino and this table is never imported.
          TableEntity te = null;
          if (stringId == null) {
            te = getEntity(ident, TABLE, TableEntity.class);
            if (te == null) {
              return EntityCombinedTable.of(alteredTable)
                  .withHiddenProperties(
                      getHiddenPropertyNames(
                          getCatalogIdentifier(ident),
                          HasPropertyMetadata::tablePropertiesMetadata,
                          alteredTable.properties()));
            }
          }

          long tableId;
          if (stringId != null) {
            tableId = stringId.id();
          } else {
            tableId = te.id();
          }

          TableEntity updatedTableEntity =
              operateOnEntity(
                  ident,
                  id ->
                      store.update(
                          id,
                          TableEntity.class,
                          TABLE,
                          tableEntity -> {
                            String newName =
                                Arrays.stream(changes)
                                    .filter(c -> c instanceof TableChange.RenameTable)
                                    .map(c -> ((TableChange.RenameTable) c).getNewName())
                                    .reduce((c1, c2) -> c2)
                                    .orElse(tableEntity.name());
                            // Update the columns
                            Pair<Boolean, List<ColumnEntity>> columnsUpdateResult =
                                updateColumnsIfNecessary(alteredTable, tableEntity);

                            return TableEntity.builder()
                                .withId(tableEntity.id())
                                .withName(newName)
                                .withNamespace(ident.namespace())
                                .withColumns(columnsUpdateResult.getRight())
                                .withAuditInfo(
                                    AuditInfo.builder()
                                        .withCreator(tableEntity.auditInfo().creator())
                                        .withCreateTime(tableEntity.auditInfo().createTime())
                                        .withLastModifier(
                                            PrincipalUtils.getCurrentPrincipal().getName())
                                        .withLastModifiedTime(Instant.now())
                                        .build())
                                .build();
                          }),
                  "UPDATE",
                  tableId);

          return EntityCombinedTable.of(alteredTable, updatedTableEntity)
              .withHiddenProperties(
                  getHiddenPropertyNames(
                      getCatalogIdentifier(ident),
                      HasPropertyMetadata::tablePropertiesMetadata,
                      alteredTable.properties()));
        });
  }