roryqi commented on code in PR #11809:
URL: https://github.com/apache/gravitino/pull/11809#discussion_r3498492857


##########
iceberg/iceberg-common/src/main/java/org/apache/iceberg/hive/HiveCatalogWithMetadataLocationSupport.java:
##########
@@ -41,24 +54,129 @@ public void initialize(String name, Map<String, String> 
properties) {
     loadFields();
   }
 
+  /**
+   * Registers a table from an existing metadata file, optionally overwriting 
an existing
+   * registration.
+   *
+   * @param identifier table identifier to register
+   * @param metadataFileLocation location of the metadata file to register
+   * @param overwrite whether to overwrite an existing table registration
+   * @return the registered table
+   */
+  @Override
+  public Table registerTable(
+      TableIdentifier identifier, String metadataFileLocation, boolean 
overwrite) {
+    return MetastoreRegisterTableUtils.registerTable(
+        this, identifier, metadataFileLocation, overwrite, 
this::overwriteMetadataLocation);
+  }
+
   @Override
   public String metadataLocation(TableIdentifier tableIdentifier) {
     String dbName = tableIdentifier.namespace().level(0);
     String tableName = tableIdentifier.name();
 
     try {
-      Table table = metaClients.run(client -> client.getTable(dbName, 
tableName));
-      String tableType = 
table.getParameters().get(BaseMetastoreTableOperations.TABLE_TYPE_PROP);
+      org.apache.hadoop.hive.metastore.api.Table hiveTable =
+          metaClients.run(client -> client.getTable(dbName, tableName));
+      String tableType =
+          
hiveTable.getParameters().get(BaseMetastoreTableOperations.TABLE_TYPE_PROP);
       if (tableType == null
           || 
!tableType.equalsIgnoreCase(BaseMetastoreTableOperations.ICEBERG_TABLE_TYPE_VALUE))
 {
         return null;
       }
-      return table.getParameters().get(METADATA_LOCATION_PROP);
+      return 
hiveTable.getParameters().get(BaseMetastoreTableOperations.METADATA_LOCATION_PROP);
     } catch (Exception e) {
       return null;
     }
   }
 
+  private void overwriteMetadataLocation(
+      TableIdentifier tableIdentifier, String oldMetadataLocation, String 
newMetadataLocation) {
+    HiveTableOperations ops = (HiveTableOperations) 
newTableOps(tableIdentifier);
+    HiveOperationsBase hiveOps = ops;
+    Configuration conf = getConf();
+
+    TableMetadata base = ops.current();
+    TableMetadata targetMetadata =
+        TableMetadataParser.read(ops.io().newInputFile(newMetadataLocation));
+
+    boolean hiveEngineEnabled = hiveEngineEnabled(targetMetadata, conf);
+    boolean keepHiveStats = conf.getBoolean(ConfigProperties.KEEP_HIVE_STATS, 
false);
+
+    Set<String> removedProps =
+        base.properties().keySet().stream()
+            .filter(key -> !targetMetadata.properties().containsKey(key))
+            .collect(Collectors.toSet());
+
+    Preconditions.checkArgument(
+        !removedProps.contains(TableProperties.ENCRYPTION_TABLE_KEY),
+        "Cannot remove key ID from an encrypted table");
+    Preconditions.checkArgument(
+        Objects.equals(
+            base.properties().get(TableProperties.ENCRYPTION_TABLE_KEY),
+            
targetMetadata.properties().get(TableProperties.ENCRYPTION_TABLE_KEY)),
+        "Cannot modify key ID of an encrypted table");
+
+    HiveLock lock = ops.lockObject(base);
+    try {
+      lock.lock();
+
+      org.apache.hadoop.hive.metastore.api.Table tbl = hiveOps.loadHmsTable();
+      if (tbl == null) {
+        throw new NoSuchTableException("Table does not exist: %s", 
tableIdentifier);
+      }
+
+      HiveOperationsBase.validateTableIsIceberg(tbl, 
tableIdentifier.toString());
+
+      tbl.setSd(
+          HiveOperationsBase.storageDescriptor(
+              targetMetadata.schema(), targetMetadata.location(), 
hiveEngineEnabled));
+
+      String hmsMetadataLocation =
+          
tbl.getParameters().get(BaseMetastoreTableOperations.METADATA_LOCATION_PROP);
+      if (!Objects.equals(oldMetadataLocation, hmsMetadataLocation)) {
+        throw new CommitFailedException(
+            "Cannot overwrite table %s: metadata location %s has changed from 
%s",
+            tableIdentifier, hmsMetadataLocation, oldMetadataLocation);
+      }
+
+      HMSTablePropertyHelper.updateHmsTableForIcebergTable(
+          newMetadataLocation,
+          tbl,
+          targetMetadata,
+          removedProps,
+          hiveEngineEnabled,
+          hiveOps.maxHiveTablePropertySize(),
+          oldMetadataLocation);
+
+      if (!keepHiveStats) {
+        tbl.getParameters().remove(StatsSetupConst.COLUMN_STATS_ACCURATE);
+        tbl.getParameters().put(StatsSetupConst.DO_NOT_UPDATE_STATS, 
StatsSetupConst.TRUE);
+      }
+
+      lock.ensureActive();
+      hiveOps.persistTable(tbl, true, hiveLockEnabled(base, conf) ? null : 
oldMetadataLocation);

Review Comment:
   Why do we use `hiveLockEnabled(base, conf) ? null : oldMetadataLocation`?



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