pvary commented on a change in pull request #2382:
URL: https://github.com/apache/hive/pull/2382#discussion_r656844765



##########
File path: 
iceberg/iceberg-handler/src/main/java/org/apache/iceberg/mr/hive/IcebergTableUtil.java
##########
@@ -106,4 +111,61 @@ public static PartitionSpec spec(Configuration 
configuration, Schema schema) {
     });
     return builder.build();
   }
+
+  public static void updateSpec(Configuration configuration, Table table) {
+    // get the new partition transform spec
+    PartitionSpec newPartitionSpec = spec(configuration, table.schema());
+    if (newPartitionSpec == null) {
+      LOG.debug("Iceberg Partition spec is not updated due to empty partition 
spec definition.");
+      return;
+    }
+
+    List<String> newPartitionNames =
+        
newPartitionSpec.fields().stream().map(PartitionField::name).collect(Collectors.toList());
+    List<String> currentPartitionNames = 
table.spec().fields().stream().map(PartitionField::name)
+        .collect(Collectors.toList());
+    List<String> intersectingPartitionNames =
+        
currentPartitionNames.stream().filter(newPartitionNames::contains).collect(Collectors.toList());
+
+    // delete those partitions which are not present among the new partion spec
+    UpdatePartitionSpec updatePartitionSpec = table.updateSpec();
+    currentPartitionNames.stream().filter(p -> 
!intersectingPartitionNames.contains(p))
+        .forEach(updatePartitionSpec::removeField);
+    updatePartitionSpec.apply();
+
+    // add new partitions which are not yet present
+    List<PartitionTransform.PartitionTransformSpec> partitionTransformSpecList 
= SessionStateUtil
+        .getResource(configuration, 
hive_metastoreConstants.PARTITION_TRANSFORM_SPEC)
+        .map(o -> (List<PartitionTransform.PartitionTransformSpec>) 
o).orElseGet(() -> null);
+    IntStream.range(0, partitionTransformSpecList.size())
+        .filter(i -> 
!intersectingPartitionNames.contains(newPartitionSpec.fields().get(i).name()))
+        .forEach(i -> {
+          PartitionTransform.PartitionTransformSpec spec = 
partitionTransformSpecList.get(i);
+          switch (spec.transformType) {

Review comment:
       PartitionSpecVisitor would be the class which we should implement in 
this case (to be honest in both cases)




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

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