deniskuzZ commented on code in PR #6256:
URL: https://github.com/apache/hive/pull/6256#discussion_r2660880843


##########
iceberg/iceberg-handler/src/main/java/org/apache/iceberg/mr/hive/HiveIcebergMetaHook.java:
##########
@@ -624,12 +631,55 @@ private void 
alterTableProperties(org.apache.hadoop.hive.metastore.api.Table hms
     Map<String, String> hmsTableParameters = hmsTable.getParameters();
     Splitter splitter = Splitter.on(PROPERTIES_SEPARATOR);
     UpdateProperties icebergUpdateProperties = icebergTable.updateProperties();
+
     if (contextProperties.containsKey(SET_PROPERTIES)) {
-      splitter.splitToList(contextProperties.get(SET_PROPERTIES))
-          .forEach(k -> icebergUpdateProperties.set(k, 
hmsTableParameters.get(k)));
+      List<String> propertiesToSet = 
splitter.splitToList(contextProperties.get(SET_PROPERTIES));
+
+      // Check if we are setting regular sort order as it needs conversion 
from Hive JSON to Iceberg SortOrder
+      if (propertiesToSet.contains(TableProperties.DEFAULT_SORT_ORDER)) {
+        // If the HMS table has Hive SortFields JSON in default-sort-order
+        // We need to convert it to Iceberg SortOrder and use 
replaceSortOrder() API
+        String sortOrderJSONString = 
hmsTableParameters.get(TableProperties.DEFAULT_SORT_ORDER);
+
+        List<SortFieldDesc> sortFieldDescList = 
parseSortFieldsJSON(sortOrderJSONString);
+        if (sortFieldDescList != null) {
+          try {
+            ReplaceSortOrder replaceSortOrder = 
icebergTable.replaceSortOrder();
+
+            // Chain all the sort field additions
+            for (SortFieldDesc fieldDesc : sortFieldDescList) {
+              NullOrder nullOrder = 
convertNullOrder(fieldDesc.getNullOrdering());
+
+              if (fieldDesc.getDirection() == SortFieldDesc.SortDirection.ASC) 
{
+                replaceSortOrder.asc(fieldDesc.getColumnName(), nullOrder);
+              } else {
+                replaceSortOrder.desc(fieldDesc.getColumnName(), nullOrder);
+              }
+            }
+
+            replaceSortOrder.commit();
+
+            // Update HMS table parameters with the Iceberg SortOrder JSON
+            SortOrder newSortOrder = icebergTable.sortOrder();
+            hmsTableParameters.put(TableProperties.DEFAULT_SORT_ORDER, 
SortOrderParser.toJson(newSortOrder));
+
+            LOG.info("Successfully set sort order for table {}: {}", 
hmsTable.getTableName(), newSortOrder);
+          } catch (Exception e) {
+            LOG.warn("Failed to apply sort order for table {}: {}", 
hmsTable.getTableName(), sortOrderJSONString, e);
+          }
+        }
+
+        // Set other properties excluding default-sort-order which is already 
processed)
+        propertiesToSet.stream()

Review Comment:
   why add filter here and not handle `DEFAULT_SORT_ORDER` during 
`propertiesToSet` iteration?
   ````
   Map<String, Consumer<String>> handlers = new HashMap<>();
   handlers.put(
       TableProperties.DEFAULT_SORT_ORDER,
       k -> handleDefaultSortOrder(hmsTable, hmsTableParameters)
   );
   
   splitter.splitToList(contextProperties.get(SET_PROPERTIES)).forEach(key ->
       handlers.getOrDefault(
           key,
           k -> update.set(k, hmsTableParameters.get(k))
       ).accept(key)
   );
   ````



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