InvisibleProgrammer commented on code in PR #6393:
URL: https://github.com/apache/hive/pull/6393#discussion_r3492570070


##########
iceberg/iceberg-handler/src/main/java/org/apache/iceberg/mr/hive/HiveIcebergMetaHook.java:
##########
@@ -390,6 +435,48 @@ private void 
doPreAlterTable(org.apache.hadoop.hive.metastore.api.Table hmsTable
    * @param tblParams hms table properties, must be non-null
    */
   private void assertNotCrossTableMetadataLocationChange(Map<String, String> 
tblParams, EnvironmentContext context) {
+    if (icebergMaterializedView != null) {
+      assertNotCrossTableMetadataLocationChangeForMaterializedView(tblParams, 
context);
+    } else {
+      assertNotCrossTableMetadataLocationChangeForTable(tblParams, context);
+    }
+
+  }
+
+  private void assertNotCrossTableMetadataLocationChangeForMaterializedView(
+      Map<String, String> tblParams, EnvironmentContext context) {
+    if 
(tblParams.containsKey(BaseMetastoreTableOperations.METADATA_LOCATION_PROP)) {
+      Preconditions.checkArgument(icebergMaterializedView != null,
+          "Cannot perform table migration to Iceberg and setting the snapshot 
location in one step. " +
+              "Please migrate the table first");
+      String newMetadataLocation = 
tblParams.get(BaseMetastoreTableOperations.METADATA_LOCATION_PROP);
+      FileIO io = ((BaseTable) 
icebergMaterializedView.getStorageTable()).operations().io();
+      ViewMetadata newMetadata = ViewMetadataParser.read(io, 
newMetadataLocation);
+      ViewMetadata currentMetadata = ((BaseView) 
icebergMaterializedView.getView()).operations().current();
+      if (!currentMetadata.uuid().equals(newMetadata.uuid())) {
+        throw new UnsupportedOperationException(
+            String.format("Cannot change iceberg table %s metadata location 
pointing to another table's metadata %s",
+                icebergTable.name(), newMetadataLocation)
+        );
+      }
+
+      String currentMetadataLocation = currentMetadata.metadataFileLocation();
+      if (!currentMetadataLocation.equals(newMetadataLocation) &&
+          
!context.getProperties().containsKey(MANUAL_ICEBERG_METADATA_LOCATION_CHANGE)) {
+        // If we got here then this is an alter table operation where the 
table to be changed had an Iceberg commit
+        // meanwhile. The base metadata locations differ, while we know that 
this wasn't an intentional, manual
+        // metadata_location set by a user. To protect the interim commit we 
need to refresh the metadata file location.
+        tblParams.put(BaseMetastoreTableOperations.METADATA_LOCATION_PROP, 
currentMetadataLocation);
+        LOG.warn("Detected an alter table operation attempting to do 
alterations on an Iceberg table with a stale " +
+              "metadata_location. Considered base metadata_location: {}, 
Actual metadata_location: {}. Will override " +
+              "this request with the refreshed metadata_location in order to 
preserve the concurrent commit.",
+            newMetadataLocation, currentMetadataLocation);
+      }
+    }

Review Comment:
   Sure. Extracted.



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