You are viewing a plain text version of this content. The canonical link for it is here.
Posted to gitbox@hive.apache.org by GitBox <gi...@apache.org> on 2021/06/22 11:52:17 UTC

[GitHub] [hive] marton-bod opened a new pull request #2418: HIVE-25255: Add support for replace columns

marton-bod opened a new pull request #2418:
URL: https://github.com/apache/hive/pull/2418


   


-- 
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:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: gitbox-unsubscribe@hive.apache.org
For additional commands, e-mail: gitbox-help@hive.apache.org


[GitHub] [hive] marton-bod commented on a change in pull request #2418: HIVE-25255: Add support for replace columns

Posted by GitBox <gi...@apache.org>.
marton-bod commented on a change in pull request #2418:
URL: https://github.com/apache/hive/pull/2418#discussion_r663975836



##########
File path: iceberg/iceberg-catalog/src/main/java/org/apache/iceberg/hive/HiveSchemaUtil.java
##########
@@ -138,22 +137,88 @@ public static Type convert(TypeInfo typeInfo) {
   }
 
   /**
-   * Produces the difference of two FieldSchema lists by only taking into account the field name and type.
+   * Returns a SchemaDifference containing those fields which are present in only one of the collections, as well as
+   * those fields which are present in both (in terms of the name) but their type or comment has changed.
    * @param minuendCollection Collection of fields to subtract from
    * @param subtrahendCollection Collection of fields to subtract
-   * @return the result list of difference
+   * @param bothDirections Whether or not to compute the missing fields from the minuendCollection as well
+   * @return the difference between the two schemas
    */
-  public static Collection<FieldSchema> schemaDifference(
-      Collection<FieldSchema> minuendCollection, Collection<FieldSchema> subtrahendCollection) {
+  public static SchemaDifference getSchemaDiff(Collection<FieldSchema> minuendCollection,
+                                               Collection<FieldSchema> subtrahendCollection, boolean bothDirections) {
+    SchemaDifference difference = new SchemaDifference();
 
-    Function<FieldSchema, FieldSchema> unsetCommentFunc = fs -> new FieldSchema(fs.getName(), fs.getType(), null);
-    Set<FieldSchema> subtrahendWithoutComment =
-        subtrahendCollection.stream().map(unsetCommentFunc).collect(Collectors.toSet());
+    for (FieldSchema first : minuendCollection) {
+      boolean found = false;
+      for (FieldSchema second : subtrahendCollection) {
+        if (first.getName().equals(second.getName())) {

Review comment:
       I didn't use it because `name` should never be null, unlike comment (and maybe type?). Anyways, I've changed it to use Objects.equals to be safe and for symmetry.




-- 
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: gitbox-unsubscribe@hive.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: gitbox-unsubscribe@hive.apache.org
For additional commands, e-mail: gitbox-help@hive.apache.org


[GitHub] [hive] marton-bod commented on a change in pull request #2418: HIVE-25255: Add support for replace columns

Posted by GitBox <gi...@apache.org>.
marton-bod commented on a change in pull request #2418:
URL: https://github.com/apache/hive/pull/2418#discussion_r663975175



##########
File path: iceberg/iceberg-handler/src/main/java/org/apache/iceberg/mr/hive/HiveIcebergMetaHook.java
##########
@@ -474,6 +480,43 @@ private static PartitionSpec spec(Configuration configuration, Schema schema, Pr
     }
   }
 
+  private void handleReplaceColumns(org.apache.hadoop.hive.metastore.api.Table hmsTable) throws MetaException {
+    HiveSchemaUtil.SchemaDifference schemaDifference = HiveSchemaUtil.getSchemaDiff(hmsTable.getSd().getCols(),
+        HiveSchemaUtil.convert(icebergTable.schema()), true);
+    if (!schemaDifference.isEmpty()) {
+      updateSchema = icebergTable.updateSchema();
+    } else {
+      // we should get here if the user restated the exactly the existing columns in the REPLACE COLUMNS command
+      LOG.info("Found no difference between new and old schema for ALTER TABLE REPLACE COLUMNS for" +
+          " table: {}. There will be no Iceberg commit.", hmsTable.getTableName());

Review comment:
       Done

##########
File path: iceberg/iceberg-catalog/src/main/java/org/apache/iceberg/hive/HiveSchemaUtil.java
##########
@@ -138,22 +137,88 @@ public static Type convert(TypeInfo typeInfo) {
   }
 
   /**
-   * Produces the difference of two FieldSchema lists by only taking into account the field name and type.
+   * Returns a SchemaDifference containing those fields which are present in only one of the collections, as well as
+   * those fields which are present in both (in terms of the name) but their type or comment has changed.
    * @param minuendCollection Collection of fields to subtract from
    * @param subtrahendCollection Collection of fields to subtract
-   * @return the result list of difference
+   * @param bothDirections Whether or not to compute the missing fields from the minuendCollection as well
+   * @return the difference between the two schemas
    */
-  public static Collection<FieldSchema> schemaDifference(
-      Collection<FieldSchema> minuendCollection, Collection<FieldSchema> subtrahendCollection) {
+  public static SchemaDifference getSchemaDiff(Collection<FieldSchema> minuendCollection,
+                                               Collection<FieldSchema> subtrahendCollection, boolean bothDirections) {
+    SchemaDifference difference = new SchemaDifference();
 
-    Function<FieldSchema, FieldSchema> unsetCommentFunc = fs -> new FieldSchema(fs.getName(), fs.getType(), null);
-    Set<FieldSchema> subtrahendWithoutComment =
-        subtrahendCollection.stream().map(unsetCommentFunc).collect(Collectors.toSet());
+    for (FieldSchema first : minuendCollection) {
+      boolean found = false;
+      for (FieldSchema second : subtrahendCollection) {
+        if (first.getName().equals(second.getName())) {
+          found = true;
+          if (!Objects.equals(first.getType(), second.getType())) {
+            difference.typeChanged(first);
+          }
+          if (!Objects.equals(first.getComment(), second.getComment())) {
+            difference.commentChanged(first);
+          }
+        }
+      }
+      if (!found) {
+        difference.missingFromSecond(first);
+      }
+    }
+
+    if (bothDirections) {
+      SchemaDifference otherWay = getSchemaDiff(subtrahendCollection, minuendCollection, false);
+      otherWay.missingFromSecond().forEach(difference::missingFromFirst);
+    }
 
-    return minuendCollection.stream()
-        .filter(fs -> !subtrahendWithoutComment.contains(unsetCommentFunc.apply(fs))).collect(Collectors.toList());
+    return difference;
   }
 
+  public static class SchemaDifference {
+    private final List<FieldSchema> missingFromFirst = new ArrayList<>();
+    private final List<FieldSchema> missingFromSecond = new ArrayList<>();
+    private final List<FieldSchema> typeChanged = new ArrayList<>();
+    private final List<FieldSchema> commentChanged = new ArrayList<>();
+
+    public List<FieldSchema> missingFromFirst() {

Review comment:
       Good point, renamed the methods

##########
File path: iceberg/iceberg-catalog/src/main/java/org/apache/iceberg/hive/HiveSchemaUtil.java
##########
@@ -138,22 +137,88 @@ public static Type convert(TypeInfo typeInfo) {
   }
 
   /**
-   * Produces the difference of two FieldSchema lists by only taking into account the field name and type.
+   * Returns a SchemaDifference containing those fields which are present in only one of the collections, as well as
+   * those fields which are present in both (in terms of the name) but their type or comment has changed.
    * @param minuendCollection Collection of fields to subtract from
    * @param subtrahendCollection Collection of fields to subtract
-   * @return the result list of difference
+   * @param bothDirections Whether or not to compute the missing fields from the minuendCollection as well
+   * @return the difference between the two schemas
    */
-  public static Collection<FieldSchema> schemaDifference(
-      Collection<FieldSchema> minuendCollection, Collection<FieldSchema> subtrahendCollection) {
+  public static SchemaDifference getSchemaDiff(Collection<FieldSchema> minuendCollection,
+                                               Collection<FieldSchema> subtrahendCollection, boolean bothDirections) {
+    SchemaDifference difference = new SchemaDifference();
 
-    Function<FieldSchema, FieldSchema> unsetCommentFunc = fs -> new FieldSchema(fs.getName(), fs.getType(), null);
-    Set<FieldSchema> subtrahendWithoutComment =
-        subtrahendCollection.stream().map(unsetCommentFunc).collect(Collectors.toSet());
+    for (FieldSchema first : minuendCollection) {
+      boolean found = false;
+      for (FieldSchema second : subtrahendCollection) {
+        if (first.getName().equals(second.getName())) {

Review comment:
       I didn't use it because `name` should never be null, unlike comment (and maybe type?). Anyways, I've changed it to use Objects.equals to be safe and for symmetry.




-- 
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: gitbox-unsubscribe@hive.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: gitbox-unsubscribe@hive.apache.org
For additional commands, e-mail: gitbox-help@hive.apache.org


[GitHub] [hive] marton-bod commented on a change in pull request #2418: HIVE-25255: Add support for replace columns

Posted by GitBox <gi...@apache.org>.
marton-bod commented on a change in pull request #2418:
URL: https://github.com/apache/hive/pull/2418#discussion_r658724347



##########
File path: iceberg/iceberg-handler/src/main/java/org/apache/iceberg/mr/hive/HiveIcebergMetaHook.java
##########
@@ -262,17 +264,52 @@ public void preAlterTable(org.apache.hadoop.hive.metastore.api.Table hmsTable, E
 
     if (AlterTableType.ADDCOLS.equals(currentAlterTableOp)) {
       Collection<FieldSchema> addedCols =
-          HiveSchemaUtil.schemaDifference(hmsTable.getSd().getCols(), HiveSchemaUtil.convert(icebergTable.schema()));
+          HiveSchemaUtil.newColumns(hmsTable.getSd().getCols(), HiveSchemaUtil.convert(icebergTable.schema()));
+      if (!addedCols.isEmpty()) {
+        updateSchema = icebergTable.updateSchema();
+      }
+      for (FieldSchema addedCol : addedCols) {
+        updateSchema.addColumn(
+            addedCol.getName(),
+            HiveSchemaUtil.convert(TypeInfoUtils.getTypeInfoFromTypeString(addedCol.getType())),
+            addedCol.getComment()
+        );
+      }
+    } else if (AlterTableType.REPLACE_COLUMNS.equals(currentAlterTableOp)) {
+      List<FieldSchema> icebergSchema = HiveSchemaUtil.convert(icebergTable.schema());
+      List<FieldSchema> hmsSchema = hmsTable.getSd().getCols();
+      Collection<FieldSchema> droppedCols = HiveSchemaUtil.newColumns(icebergSchema, hmsSchema);
+      Collection<FieldSchema> addedCols = HiveSchemaUtil.newColumns(hmsSchema, icebergSchema);
+      Collection<FieldSchema> typeUpdated = HiveSchemaUtil.updatedTypeColumns(hmsSchema, icebergSchema);
+      Collection<FieldSchema> commentUpdated = HiveSchemaUtil.updatedCommentColumns(hmsSchema, icebergSchema);
+      if (!droppedCols.isEmpty() || !addedCols.isEmpty() || !typeUpdated.isEmpty() || !commentUpdated.isEmpty()) {
+        updateSchema = icebergTable.updateSchema();
+      }
+
+      for (FieldSchema droppedCol : droppedCols) {
+        updateSchema.deleteColumn(droppedCol.getName());
+      }
+
       for (FieldSchema addedCol : addedCols) {
-        if (updateSchema == null) {
-          updateSchema = icebergTable.updateSchema();
-        }
         updateSchema.addColumn(
             addedCol.getName(),
             HiveSchemaUtil.convert(TypeInfoUtils.getTypeInfoFromTypeString(addedCol.getType())),
             addedCol.getComment()
         );
       }
+
+      for (FieldSchema updatedCol : typeUpdated) {
+        Type newType = HiveSchemaUtil.convert(TypeInfoUtils.getTypeInfoFromTypeString(updatedCol.getType()));
+        if (!(newType instanceof Type.PrimitiveType)) {

Review comment:
       The Iceberg library will throw an exception in that case, saying type promotion is not supported (e.g. int -> string)




-- 
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:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: gitbox-unsubscribe@hive.apache.org
For additional commands, e-mail: gitbox-help@hive.apache.org


[GitHub] [hive] marton-bod commented on a change in pull request #2418: HIVE-25255: Add support for replace columns

Posted by GitBox <gi...@apache.org>.
marton-bod commented on a change in pull request #2418:
URL: https://github.com/apache/hive/pull/2418#discussion_r658723963



##########
File path: iceberg/iceberg-handler/src/main/java/org/apache/iceberg/mr/hive/HiveIcebergMetaHook.java
##########
@@ -262,17 +264,52 @@ public void preAlterTable(org.apache.hadoop.hive.metastore.api.Table hmsTable, E
 
     if (AlterTableType.ADDCOLS.equals(currentAlterTableOp)) {
       Collection<FieldSchema> addedCols =
-          HiveSchemaUtil.schemaDifference(hmsTable.getSd().getCols(), HiveSchemaUtil.convert(icebergTable.schema()));
+          HiveSchemaUtil.newColumns(hmsTable.getSd().getCols(), HiveSchemaUtil.convert(icebergTable.schema()));
+      if (!addedCols.isEmpty()) {
+        updateSchema = icebergTable.updateSchema();
+      }
+      for (FieldSchema addedCol : addedCols) {
+        updateSchema.addColumn(
+            addedCol.getName(),
+            HiveSchemaUtil.convert(TypeInfoUtils.getTypeInfoFromTypeString(addedCol.getType())),
+            addedCol.getComment()
+        );
+      }
+    } else if (AlterTableType.REPLACE_COLUMNS.equals(currentAlterTableOp)) {
+      List<FieldSchema> icebergSchema = HiveSchemaUtil.convert(icebergTable.schema());
+      List<FieldSchema> hmsSchema = hmsTable.getSd().getCols();
+      Collection<FieldSchema> droppedCols = HiveSchemaUtil.newColumns(icebergSchema, hmsSchema);
+      Collection<FieldSchema> addedCols = HiveSchemaUtil.newColumns(hmsSchema, icebergSchema);
+      Collection<FieldSchema> typeUpdated = HiveSchemaUtil.updatedTypeColumns(hmsSchema, icebergSchema);
+      Collection<FieldSchema> commentUpdated = HiveSchemaUtil.updatedCommentColumns(hmsSchema, icebergSchema);
+      if (!droppedCols.isEmpty() || !addedCols.isEmpty() || !typeUpdated.isEmpty() || !commentUpdated.isEmpty()) {
+        updateSchema = icebergTable.updateSchema();
+      }

Review comment:
       Added logging. We should only get here if the user uses the exact same column spec in the REPLACE COLS that the table already has, effectively changing nothing.




-- 
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:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: gitbox-unsubscribe@hive.apache.org
For additional commands, e-mail: gitbox-help@hive.apache.org


[GitHub] [hive] pvary commented on a change in pull request #2418: HIVE-25255: Add support for replace columns

Posted by GitBox <gi...@apache.org>.
pvary commented on a change in pull request #2418:
URL: https://github.com/apache/hive/pull/2418#discussion_r657003101



##########
File path: iceberg/iceberg-handler/src/main/java/org/apache/iceberg/mr/hive/HiveIcebergMetaHook.java
##########
@@ -262,17 +264,52 @@ public void preAlterTable(org.apache.hadoop.hive.metastore.api.Table hmsTable, E
 
     if (AlterTableType.ADDCOLS.equals(currentAlterTableOp)) {
       Collection<FieldSchema> addedCols =
-          HiveSchemaUtil.schemaDifference(hmsTable.getSd().getCols(), HiveSchemaUtil.convert(icebergTable.schema()));
+          HiveSchemaUtil.newColumns(hmsTable.getSd().getCols(), HiveSchemaUtil.convert(icebergTable.schema()));
+      if (!addedCols.isEmpty()) {
+        updateSchema = icebergTable.updateSchema();
+      }
+      for (FieldSchema addedCol : addedCols) {
+        updateSchema.addColumn(
+            addedCol.getName(),
+            HiveSchemaUtil.convert(TypeInfoUtils.getTypeInfoFromTypeString(addedCol.getType())),
+            addedCol.getComment()
+        );
+      }
+    } else if (AlterTableType.REPLACE_COLUMNS.equals(currentAlterTableOp)) {
+      List<FieldSchema> icebergSchema = HiveSchemaUtil.convert(icebergTable.schema());
+      List<FieldSchema> hmsSchema = hmsTable.getSd().getCols();
+      Collection<FieldSchema> droppedCols = HiveSchemaUtil.newColumns(icebergSchema, hmsSchema);

Review comment:
       I understand why `newColums` are used, but maybe a better name could be found instead of `newColumns`, like `missingColumns`?




-- 
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:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: gitbox-unsubscribe@hive.apache.org
For additional commands, e-mail: gitbox-help@hive.apache.org


[GitHub] [hive] szlta merged pull request #2418: HIVE-25255: Add support for replace columns

Posted by GitBox <gi...@apache.org>.
szlta merged pull request #2418:
URL: https://github.com/apache/hive/pull/2418


   


-- 
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: gitbox-unsubscribe@hive.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: gitbox-unsubscribe@hive.apache.org
For additional commands, e-mail: gitbox-help@hive.apache.org


[GitHub] [hive] marton-bod commented on a change in pull request #2418: HIVE-25255: Add support for replace columns

Posted by GitBox <gi...@apache.org>.
marton-bod commented on a change in pull request #2418:
URL: https://github.com/apache/hive/pull/2418#discussion_r658690088



##########
File path: iceberg/iceberg-handler/src/main/java/org/apache/iceberg/mr/hive/HiveIcebergMetaHook.java
##########
@@ -222,6 +223,7 @@ public void commitDropTable(org.apache.hadoop.hive.metastore.api.Table hmsTable,
   }
 
   @Override
+  @SuppressWarnings("checkstyle:CyclomaticComplexity")

Review comment:
       Extracted the replace columns logic to a new method to decrease the complexity of the preAlterTable method.




-- 
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:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: gitbox-unsubscribe@hive.apache.org
For additional commands, e-mail: gitbox-help@hive.apache.org


[GitHub] [hive] szlta commented on a change in pull request #2418: HIVE-25255: Add support for replace columns

Posted by GitBox <gi...@apache.org>.
szlta commented on a change in pull request #2418:
URL: https://github.com/apache/hive/pull/2418#discussion_r663922844



##########
File path: iceberg/iceberg-catalog/src/main/java/org/apache/iceberg/hive/HiveSchemaUtil.java
##########
@@ -138,22 +137,88 @@ public static Type convert(TypeInfo typeInfo) {
   }
 
   /**
-   * Produces the difference of two FieldSchema lists by only taking into account the field name and type.
+   * Returns a SchemaDifference containing those fields which are present in only one of the collections, as well as
+   * those fields which are present in both (in terms of the name) but their type or comment has changed.
    * @param minuendCollection Collection of fields to subtract from
    * @param subtrahendCollection Collection of fields to subtract
-   * @return the result list of difference
+   * @param bothDirections Whether or not to compute the missing fields from the minuendCollection as well
+   * @return the difference between the two schemas
    */
-  public static Collection<FieldSchema> schemaDifference(
-      Collection<FieldSchema> minuendCollection, Collection<FieldSchema> subtrahendCollection) {
+  public static SchemaDifference getSchemaDiff(Collection<FieldSchema> minuendCollection,
+                                               Collection<FieldSchema> subtrahendCollection, boolean bothDirections) {
+    SchemaDifference difference = new SchemaDifference();
 
-    Function<FieldSchema, FieldSchema> unsetCommentFunc = fs -> new FieldSchema(fs.getName(), fs.getType(), null);
-    Set<FieldSchema> subtrahendWithoutComment =
-        subtrahendCollection.stream().map(unsetCommentFunc).collect(Collectors.toSet());
+    for (FieldSchema first : minuendCollection) {
+      boolean found = false;
+      for (FieldSchema second : subtrahendCollection) {
+        if (first.getName().equals(second.getName())) {
+          found = true;
+          if (!Objects.equals(first.getType(), second.getType())) {
+            difference.typeChanged(first);
+          }
+          if (!Objects.equals(first.getComment(), second.getComment())) {
+            difference.commentChanged(first);
+          }
+        }
+      }
+      if (!found) {
+        difference.missingFromSecond(first);
+      }
+    }
+
+    if (bothDirections) {
+      SchemaDifference otherWay = getSchemaDiff(subtrahendCollection, minuendCollection, false);
+      otherWay.missingFromSecond().forEach(difference::missingFromFirst);
+    }
 
-    return minuendCollection.stream()
-        .filter(fs -> !subtrahendWithoutComment.contains(unsetCommentFunc.apply(fs))).collect(Collectors.toList());
+    return difference;
   }
 
+  public static class SchemaDifference {
+    private final List<FieldSchema> missingFromFirst = new ArrayList<>();
+    private final List<FieldSchema> missingFromSecond = new ArrayList<>();
+    private final List<FieldSchema> typeChanged = new ArrayList<>();
+    private final List<FieldSchema> commentChanged = new ArrayList<>();
+
+    public List<FieldSchema> missingFromFirst() {

Review comment:
       I'd rather avoid having an overload for this method that has a very different behavior (get vs add), maybe you could rename either set of these methods to be more descriptive.

##########
File path: iceberg/iceberg-catalog/src/main/java/org/apache/iceberg/hive/HiveSchemaUtil.java
##########
@@ -138,22 +137,88 @@ public static Type convert(TypeInfo typeInfo) {
   }
 
   /**
-   * Produces the difference of two FieldSchema lists by only taking into account the field name and type.
+   * Returns a SchemaDifference containing those fields which are present in only one of the collections, as well as
+   * those fields which are present in both (in terms of the name) but their type or comment has changed.
    * @param minuendCollection Collection of fields to subtract from
    * @param subtrahendCollection Collection of fields to subtract
-   * @return the result list of difference
+   * @param bothDirections Whether or not to compute the missing fields from the minuendCollection as well
+   * @return the difference between the two schemas
    */
-  public static Collection<FieldSchema> schemaDifference(
-      Collection<FieldSchema> minuendCollection, Collection<FieldSchema> subtrahendCollection) {
+  public static SchemaDifference getSchemaDiff(Collection<FieldSchema> minuendCollection,
+                                               Collection<FieldSchema> subtrahendCollection, boolean bothDirections) {
+    SchemaDifference difference = new SchemaDifference();
 
-    Function<FieldSchema, FieldSchema> unsetCommentFunc = fs -> new FieldSchema(fs.getName(), fs.getType(), null);
-    Set<FieldSchema> subtrahendWithoutComment =
-        subtrahendCollection.stream().map(unsetCommentFunc).collect(Collectors.toSet());
+    for (FieldSchema first : minuendCollection) {
+      boolean found = false;
+      for (FieldSchema second : subtrahendCollection) {
+        if (first.getName().equals(second.getName())) {

Review comment:
       Why not use Objects.equals here too?

##########
File path: iceberg/iceberg-handler/src/main/java/org/apache/iceberg/mr/hive/HiveIcebergMetaHook.java
##########
@@ -474,6 +480,43 @@ private static PartitionSpec spec(Configuration configuration, Schema schema, Pr
     }
   }
 
+  private void handleReplaceColumns(org.apache.hadoop.hive.metastore.api.Table hmsTable) throws MetaException {
+    HiveSchemaUtil.SchemaDifference schemaDifference = HiveSchemaUtil.getSchemaDiff(hmsTable.getSd().getCols(),
+        HiveSchemaUtil.convert(icebergTable.schema()), true);
+    if (!schemaDifference.isEmpty()) {
+      updateSchema = icebergTable.updateSchema();
+    } else {
+      // we should get here if the user restated the exactly the existing columns in the REPLACE COLUMNS command
+      LOG.info("Found no difference between new and old schema for ALTER TABLE REPLACE COLUMNS for" +
+          " table: {}. There will be no Iceberg commit.", hmsTable.getTableName());

Review comment:
       nit: could place an explicit return statement here. In case any future change tweak the code below, someone could miss this path having to yield to a no-op.




-- 
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: gitbox-unsubscribe@hive.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: gitbox-unsubscribe@hive.apache.org
For additional commands, e-mail: gitbox-help@hive.apache.org


[GitHub] [hive] pvary commented on a change in pull request #2418: HIVE-25255: Add support for replace columns

Posted by GitBox <gi...@apache.org>.
pvary commented on a change in pull request #2418:
URL: https://github.com/apache/hive/pull/2418#discussion_r657007337



##########
File path: iceberg/iceberg-handler/src/main/java/org/apache/iceberg/mr/hive/HiveIcebergMetaHook.java
##########
@@ -262,17 +264,52 @@ public void preAlterTable(org.apache.hadoop.hive.metastore.api.Table hmsTable, E
 
     if (AlterTableType.ADDCOLS.equals(currentAlterTableOp)) {
       Collection<FieldSchema> addedCols =
-          HiveSchemaUtil.schemaDifference(hmsTable.getSd().getCols(), HiveSchemaUtil.convert(icebergTable.schema()));
+          HiveSchemaUtil.newColumns(hmsTable.getSd().getCols(), HiveSchemaUtil.convert(icebergTable.schema()));
+      if (!addedCols.isEmpty()) {
+        updateSchema = icebergTable.updateSchema();
+      }
+      for (FieldSchema addedCol : addedCols) {
+        updateSchema.addColumn(
+            addedCol.getName(),
+            HiveSchemaUtil.convert(TypeInfoUtils.getTypeInfoFromTypeString(addedCol.getType())),
+            addedCol.getComment()
+        );
+      }
+    } else if (AlterTableType.REPLACE_COLUMNS.equals(currentAlterTableOp)) {
+      List<FieldSchema> icebergSchema = HiveSchemaUtil.convert(icebergTable.schema());
+      List<FieldSchema> hmsSchema = hmsTable.getSd().getCols();
+      Collection<FieldSchema> droppedCols = HiveSchemaUtil.newColumns(icebergSchema, hmsSchema);
+      Collection<FieldSchema> addedCols = HiveSchemaUtil.newColumns(hmsSchema, icebergSchema);
+      Collection<FieldSchema> typeUpdated = HiveSchemaUtil.updatedTypeColumns(hmsSchema, icebergSchema);
+      Collection<FieldSchema> commentUpdated = HiveSchemaUtil.updatedCommentColumns(hmsSchema, icebergSchema);
+      if (!droppedCols.isEmpty() || !addedCols.isEmpty() || !typeUpdated.isEmpty() || !commentUpdated.isEmpty()) {
+        updateSchema = icebergTable.updateSchema();
+      }
+
+      for (FieldSchema droppedCol : droppedCols) {
+        updateSchema.deleteColumn(droppedCol.getName());
+      }
+
       for (FieldSchema addedCol : addedCols) {
-        if (updateSchema == null) {
-          updateSchema = icebergTable.updateSchema();
-        }
         updateSchema.addColumn(
             addedCol.getName(),
             HiveSchemaUtil.convert(TypeInfoUtils.getTypeInfoFromTypeString(addedCol.getType())),
             addedCol.getComment()
         );
       }
+
+      for (FieldSchema updatedCol : typeUpdated) {
+        Type newType = HiveSchemaUtil.convert(TypeInfoUtils.getTypeInfoFromTypeString(updatedCol.getType()));
+        if (!(newType instanceof Type.PrimitiveType)) {

Review comment:
       What happens when we try to convert primitive types that are not compatible?




-- 
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:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: gitbox-unsubscribe@hive.apache.org
For additional commands, e-mail: gitbox-help@hive.apache.org


[GitHub] [hive] pvary commented on a change in pull request #2418: HIVE-25255: Add support for replace columns

Posted by GitBox <gi...@apache.org>.
pvary commented on a change in pull request #2418:
URL: https://github.com/apache/hive/pull/2418#discussion_r657004864



##########
File path: iceberg/iceberg-handler/src/main/java/org/apache/iceberg/mr/hive/HiveIcebergMetaHook.java
##########
@@ -262,17 +264,52 @@ public void preAlterTable(org.apache.hadoop.hive.metastore.api.Table hmsTable, E
 
     if (AlterTableType.ADDCOLS.equals(currentAlterTableOp)) {
       Collection<FieldSchema> addedCols =
-          HiveSchemaUtil.schemaDifference(hmsTable.getSd().getCols(), HiveSchemaUtil.convert(icebergTable.schema()));
+          HiveSchemaUtil.newColumns(hmsTable.getSd().getCols(), HiveSchemaUtil.convert(icebergTable.schema()));
+      if (!addedCols.isEmpty()) {
+        updateSchema = icebergTable.updateSchema();
+      }
+      for (FieldSchema addedCol : addedCols) {
+        updateSchema.addColumn(
+            addedCol.getName(),
+            HiveSchemaUtil.convert(TypeInfoUtils.getTypeInfoFromTypeString(addedCol.getType())),
+            addedCol.getComment()
+        );
+      }
+    } else if (AlterTableType.REPLACE_COLUMNS.equals(currentAlterTableOp)) {
+      List<FieldSchema> icebergSchema = HiveSchemaUtil.convert(icebergTable.schema());
+      List<FieldSchema> hmsSchema = hmsTable.getSd().getCols();
+      Collection<FieldSchema> droppedCols = HiveSchemaUtil.newColumns(icebergSchema, hmsSchema);
+      Collection<FieldSchema> addedCols = HiveSchemaUtil.newColumns(hmsSchema, icebergSchema);
+      Collection<FieldSchema> typeUpdated = HiveSchemaUtil.updatedTypeColumns(hmsSchema, icebergSchema);
+      Collection<FieldSchema> commentUpdated = HiveSchemaUtil.updatedCommentColumns(hmsSchema, icebergSchema);

Review comment:
       This is wasteful a little bit - I assume we loop through the columns every time.
   
   Are the methods used elsewhere? Is the code so much cleaner this way that we are comfortable to pay the price?




-- 
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:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: gitbox-unsubscribe@hive.apache.org
For additional commands, e-mail: gitbox-help@hive.apache.org


[GitHub] [hive] pvary commented on a change in pull request #2418: HIVE-25255: Add support for replace columns

Posted by GitBox <gi...@apache.org>.
pvary commented on a change in pull request #2418:
URL: https://github.com/apache/hive/pull/2418#discussion_r657001439



##########
File path: iceberg/iceberg-handler/src/main/java/org/apache/iceberg/mr/hive/HiveIcebergMetaHook.java
##########
@@ -222,6 +223,7 @@ public void commitDropTable(org.apache.hadoop.hive.metastore.api.Table hmsTable,
   }
 
   @Override
+  @SuppressWarnings("checkstyle:CyclomaticComplexity")

Review comment:
       Could we decrease the complexity instead of suppressing?




-- 
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:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: gitbox-unsubscribe@hive.apache.org
For additional commands, e-mail: gitbox-help@hive.apache.org


[GitHub] [hive] pvary commented on a change in pull request #2418: HIVE-25255: Add support for replace columns

Posted by GitBox <gi...@apache.org>.
pvary commented on a change in pull request #2418:
URL: https://github.com/apache/hive/pull/2418#discussion_r657005322



##########
File path: iceberg/iceberg-handler/src/main/java/org/apache/iceberg/mr/hive/HiveIcebergMetaHook.java
##########
@@ -262,17 +264,52 @@ public void preAlterTable(org.apache.hadoop.hive.metastore.api.Table hmsTable, E
 
     if (AlterTableType.ADDCOLS.equals(currentAlterTableOp)) {
       Collection<FieldSchema> addedCols =
-          HiveSchemaUtil.schemaDifference(hmsTable.getSd().getCols(), HiveSchemaUtil.convert(icebergTable.schema()));
+          HiveSchemaUtil.newColumns(hmsTable.getSd().getCols(), HiveSchemaUtil.convert(icebergTable.schema()));
+      if (!addedCols.isEmpty()) {
+        updateSchema = icebergTable.updateSchema();
+      }
+      for (FieldSchema addedCol : addedCols) {
+        updateSchema.addColumn(
+            addedCol.getName(),
+            HiveSchemaUtil.convert(TypeInfoUtils.getTypeInfoFromTypeString(addedCol.getType())),
+            addedCol.getComment()
+        );
+      }
+    } else if (AlterTableType.REPLACE_COLUMNS.equals(currentAlterTableOp)) {
+      List<FieldSchema> icebergSchema = HiveSchemaUtil.convert(icebergTable.schema());
+      List<FieldSchema> hmsSchema = hmsTable.getSd().getCols();
+      Collection<FieldSchema> droppedCols = HiveSchemaUtil.newColumns(icebergSchema, hmsSchema);
+      Collection<FieldSchema> addedCols = HiveSchemaUtil.newColumns(hmsSchema, icebergSchema);
+      Collection<FieldSchema> typeUpdated = HiveSchemaUtil.updatedTypeColumns(hmsSchema, icebergSchema);
+      Collection<FieldSchema> commentUpdated = HiveSchemaUtil.updatedCommentColumns(hmsSchema, icebergSchema);
+      if (!droppedCols.isEmpty() || !addedCols.isEmpty() || !typeUpdated.isEmpty() || !commentUpdated.isEmpty()) {
+        updateSchema = icebergTable.updateSchema();
+      }

Review comment:
       Maybe the throw an exception, or log an error in `else`?




-- 
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:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: gitbox-unsubscribe@hive.apache.org
For additional commands, e-mail: gitbox-help@hive.apache.org


[GitHub] [hive] marton-bod commented on a change in pull request #2418: HIVE-25255: Add support for replace columns

Posted by GitBox <gi...@apache.org>.
marton-bod commented on a change in pull request #2418:
URL: https://github.com/apache/hive/pull/2418#discussion_r658722931



##########
File path: iceberg/iceberg-handler/src/main/java/org/apache/iceberg/mr/hive/HiveIcebergMetaHook.java
##########
@@ -262,17 +264,52 @@ public void preAlterTable(org.apache.hadoop.hive.metastore.api.Table hmsTable, E
 
     if (AlterTableType.ADDCOLS.equals(currentAlterTableOp)) {
       Collection<FieldSchema> addedCols =
-          HiveSchemaUtil.schemaDifference(hmsTable.getSd().getCols(), HiveSchemaUtil.convert(icebergTable.schema()));
+          HiveSchemaUtil.newColumns(hmsTable.getSd().getCols(), HiveSchemaUtil.convert(icebergTable.schema()));
+      if (!addedCols.isEmpty()) {
+        updateSchema = icebergTable.updateSchema();
+      }
+      for (FieldSchema addedCol : addedCols) {
+        updateSchema.addColumn(
+            addedCol.getName(),
+            HiveSchemaUtil.convert(TypeInfoUtils.getTypeInfoFromTypeString(addedCol.getType())),
+            addedCol.getComment()
+        );
+      }
+    } else if (AlterTableType.REPLACE_COLUMNS.equals(currentAlterTableOp)) {
+      List<FieldSchema> icebergSchema = HiveSchemaUtil.convert(icebergTable.schema());
+      List<FieldSchema> hmsSchema = hmsTable.getSd().getCols();
+      Collection<FieldSchema> droppedCols = HiveSchemaUtil.newColumns(icebergSchema, hmsSchema);
+      Collection<FieldSchema> addedCols = HiveSchemaUtil.newColumns(hmsSchema, icebergSchema);
+      Collection<FieldSchema> typeUpdated = HiveSchemaUtil.updatedTypeColumns(hmsSchema, icebergSchema);
+      Collection<FieldSchema> commentUpdated = HiveSchemaUtil.updatedCommentColumns(hmsSchema, icebergSchema);

Review comment:
       I've refactored the code to loop only twice and collect the info into a schema difference object




-- 
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:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: gitbox-unsubscribe@hive.apache.org
For additional commands, e-mail: gitbox-help@hive.apache.org


[GitHub] [hive] szlta commented on a change in pull request #2418: HIVE-25255: Add support for replace columns

Posted by GitBox <gi...@apache.org>.
szlta commented on a change in pull request #2418:
URL: https://github.com/apache/hive/pull/2418#discussion_r663922844



##########
File path: iceberg/iceberg-catalog/src/main/java/org/apache/iceberg/hive/HiveSchemaUtil.java
##########
@@ -138,22 +137,88 @@ public static Type convert(TypeInfo typeInfo) {
   }
 
   /**
-   * Produces the difference of two FieldSchema lists by only taking into account the field name and type.
+   * Returns a SchemaDifference containing those fields which are present in only one of the collections, as well as
+   * those fields which are present in both (in terms of the name) but their type or comment has changed.
    * @param minuendCollection Collection of fields to subtract from
    * @param subtrahendCollection Collection of fields to subtract
-   * @return the result list of difference
+   * @param bothDirections Whether or not to compute the missing fields from the minuendCollection as well
+   * @return the difference between the two schemas
    */
-  public static Collection<FieldSchema> schemaDifference(
-      Collection<FieldSchema> minuendCollection, Collection<FieldSchema> subtrahendCollection) {
+  public static SchemaDifference getSchemaDiff(Collection<FieldSchema> minuendCollection,
+                                               Collection<FieldSchema> subtrahendCollection, boolean bothDirections) {
+    SchemaDifference difference = new SchemaDifference();
 
-    Function<FieldSchema, FieldSchema> unsetCommentFunc = fs -> new FieldSchema(fs.getName(), fs.getType(), null);
-    Set<FieldSchema> subtrahendWithoutComment =
-        subtrahendCollection.stream().map(unsetCommentFunc).collect(Collectors.toSet());
+    for (FieldSchema first : minuendCollection) {
+      boolean found = false;
+      for (FieldSchema second : subtrahendCollection) {
+        if (first.getName().equals(second.getName())) {
+          found = true;
+          if (!Objects.equals(first.getType(), second.getType())) {
+            difference.typeChanged(first);
+          }
+          if (!Objects.equals(first.getComment(), second.getComment())) {
+            difference.commentChanged(first);
+          }
+        }
+      }
+      if (!found) {
+        difference.missingFromSecond(first);
+      }
+    }
+
+    if (bothDirections) {
+      SchemaDifference otherWay = getSchemaDiff(subtrahendCollection, minuendCollection, false);
+      otherWay.missingFromSecond().forEach(difference::missingFromFirst);
+    }
 
-    return minuendCollection.stream()
-        .filter(fs -> !subtrahendWithoutComment.contains(unsetCommentFunc.apply(fs))).collect(Collectors.toList());
+    return difference;
   }
 
+  public static class SchemaDifference {
+    private final List<FieldSchema> missingFromFirst = new ArrayList<>();
+    private final List<FieldSchema> missingFromSecond = new ArrayList<>();
+    private final List<FieldSchema> typeChanged = new ArrayList<>();
+    private final List<FieldSchema> commentChanged = new ArrayList<>();
+
+    public List<FieldSchema> missingFromFirst() {

Review comment:
       I'd rather avoid having an overload for this method that has a very different behavior (get vs add), maybe you could rename either set of these methods to be more descriptive.

##########
File path: iceberg/iceberg-catalog/src/main/java/org/apache/iceberg/hive/HiveSchemaUtil.java
##########
@@ -138,22 +137,88 @@ public static Type convert(TypeInfo typeInfo) {
   }
 
   /**
-   * Produces the difference of two FieldSchema lists by only taking into account the field name and type.
+   * Returns a SchemaDifference containing those fields which are present in only one of the collections, as well as
+   * those fields which are present in both (in terms of the name) but their type or comment has changed.
    * @param minuendCollection Collection of fields to subtract from
    * @param subtrahendCollection Collection of fields to subtract
-   * @return the result list of difference
+   * @param bothDirections Whether or not to compute the missing fields from the minuendCollection as well
+   * @return the difference between the two schemas
    */
-  public static Collection<FieldSchema> schemaDifference(
-      Collection<FieldSchema> minuendCollection, Collection<FieldSchema> subtrahendCollection) {
+  public static SchemaDifference getSchemaDiff(Collection<FieldSchema> minuendCollection,
+                                               Collection<FieldSchema> subtrahendCollection, boolean bothDirections) {
+    SchemaDifference difference = new SchemaDifference();
 
-    Function<FieldSchema, FieldSchema> unsetCommentFunc = fs -> new FieldSchema(fs.getName(), fs.getType(), null);
-    Set<FieldSchema> subtrahendWithoutComment =
-        subtrahendCollection.stream().map(unsetCommentFunc).collect(Collectors.toSet());
+    for (FieldSchema first : minuendCollection) {
+      boolean found = false;
+      for (FieldSchema second : subtrahendCollection) {
+        if (first.getName().equals(second.getName())) {

Review comment:
       Why not use Objects.equals here too?

##########
File path: iceberg/iceberg-handler/src/main/java/org/apache/iceberg/mr/hive/HiveIcebergMetaHook.java
##########
@@ -474,6 +480,43 @@ private static PartitionSpec spec(Configuration configuration, Schema schema, Pr
     }
   }
 
+  private void handleReplaceColumns(org.apache.hadoop.hive.metastore.api.Table hmsTable) throws MetaException {
+    HiveSchemaUtil.SchemaDifference schemaDifference = HiveSchemaUtil.getSchemaDiff(hmsTable.getSd().getCols(),
+        HiveSchemaUtil.convert(icebergTable.schema()), true);
+    if (!schemaDifference.isEmpty()) {
+      updateSchema = icebergTable.updateSchema();
+    } else {
+      // we should get here if the user restated the exactly the existing columns in the REPLACE COLUMNS command
+      LOG.info("Found no difference between new and old schema for ALTER TABLE REPLACE COLUMNS for" +
+          " table: {}. There will be no Iceberg commit.", hmsTable.getTableName());

Review comment:
       nit: could place an explicit return statement here. In case any future change tweak the code below, someone could miss this path having to yield to a no-op.




-- 
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: gitbox-unsubscribe@hive.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: gitbox-unsubscribe@hive.apache.org
For additional commands, e-mail: gitbox-help@hive.apache.org


[GitHub] [hive] marton-bod commented on a change in pull request #2418: HIVE-25255: Add support for replace columns

Posted by GitBox <gi...@apache.org>.
marton-bod commented on a change in pull request #2418:
URL: https://github.com/apache/hive/pull/2418#discussion_r658724520



##########
File path: iceberg/iceberg-handler/src/main/java/org/apache/iceberg/mr/hive/HiveIcebergMetaHook.java
##########
@@ -262,17 +264,52 @@ public void preAlterTable(org.apache.hadoop.hive.metastore.api.Table hmsTable, E
 
     if (AlterTableType.ADDCOLS.equals(currentAlterTableOp)) {
       Collection<FieldSchema> addedCols =
-          HiveSchemaUtil.schemaDifference(hmsTable.getSd().getCols(), HiveSchemaUtil.convert(icebergTable.schema()));
+          HiveSchemaUtil.newColumns(hmsTable.getSd().getCols(), HiveSchemaUtil.convert(icebergTable.schema()));
+      if (!addedCols.isEmpty()) {
+        updateSchema = icebergTable.updateSchema();
+      }
+      for (FieldSchema addedCol : addedCols) {
+        updateSchema.addColumn(
+            addedCol.getName(),
+            HiveSchemaUtil.convert(TypeInfoUtils.getTypeInfoFromTypeString(addedCol.getType())),
+            addedCol.getComment()
+        );
+      }
+    } else if (AlterTableType.REPLACE_COLUMNS.equals(currentAlterTableOp)) {
+      List<FieldSchema> icebergSchema = HiveSchemaUtil.convert(icebergTable.schema());
+      List<FieldSchema> hmsSchema = hmsTable.getSd().getCols();
+      Collection<FieldSchema> droppedCols = HiveSchemaUtil.newColumns(icebergSchema, hmsSchema);

Review comment:
       See below. refactored the code




-- 
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:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: gitbox-unsubscribe@hive.apache.org
For additional commands, e-mail: gitbox-help@hive.apache.org


[GitHub] [hive] marton-bod commented on a change in pull request #2418: HIVE-25255: Add support for replace columns

Posted by GitBox <gi...@apache.org>.
marton-bod commented on a change in pull request #2418:
URL: https://github.com/apache/hive/pull/2418#discussion_r663975175



##########
File path: iceberg/iceberg-handler/src/main/java/org/apache/iceberg/mr/hive/HiveIcebergMetaHook.java
##########
@@ -474,6 +480,43 @@ private static PartitionSpec spec(Configuration configuration, Schema schema, Pr
     }
   }
 
+  private void handleReplaceColumns(org.apache.hadoop.hive.metastore.api.Table hmsTable) throws MetaException {
+    HiveSchemaUtil.SchemaDifference schemaDifference = HiveSchemaUtil.getSchemaDiff(hmsTable.getSd().getCols(),
+        HiveSchemaUtil.convert(icebergTable.schema()), true);
+    if (!schemaDifference.isEmpty()) {
+      updateSchema = icebergTable.updateSchema();
+    } else {
+      // we should get here if the user restated the exactly the existing columns in the REPLACE COLUMNS command
+      LOG.info("Found no difference between new and old schema for ALTER TABLE REPLACE COLUMNS for" +
+          " table: {}. There will be no Iceberg commit.", hmsTable.getTableName());

Review comment:
       Done

##########
File path: iceberg/iceberg-catalog/src/main/java/org/apache/iceberg/hive/HiveSchemaUtil.java
##########
@@ -138,22 +137,88 @@ public static Type convert(TypeInfo typeInfo) {
   }
 
   /**
-   * Produces the difference of two FieldSchema lists by only taking into account the field name and type.
+   * Returns a SchemaDifference containing those fields which are present in only one of the collections, as well as
+   * those fields which are present in both (in terms of the name) but their type or comment has changed.
    * @param minuendCollection Collection of fields to subtract from
    * @param subtrahendCollection Collection of fields to subtract
-   * @return the result list of difference
+   * @param bothDirections Whether or not to compute the missing fields from the minuendCollection as well
+   * @return the difference between the two schemas
    */
-  public static Collection<FieldSchema> schemaDifference(
-      Collection<FieldSchema> minuendCollection, Collection<FieldSchema> subtrahendCollection) {
+  public static SchemaDifference getSchemaDiff(Collection<FieldSchema> minuendCollection,
+                                               Collection<FieldSchema> subtrahendCollection, boolean bothDirections) {
+    SchemaDifference difference = new SchemaDifference();
 
-    Function<FieldSchema, FieldSchema> unsetCommentFunc = fs -> new FieldSchema(fs.getName(), fs.getType(), null);
-    Set<FieldSchema> subtrahendWithoutComment =
-        subtrahendCollection.stream().map(unsetCommentFunc).collect(Collectors.toSet());
+    for (FieldSchema first : minuendCollection) {
+      boolean found = false;
+      for (FieldSchema second : subtrahendCollection) {
+        if (first.getName().equals(second.getName())) {
+          found = true;
+          if (!Objects.equals(first.getType(), second.getType())) {
+            difference.typeChanged(first);
+          }
+          if (!Objects.equals(first.getComment(), second.getComment())) {
+            difference.commentChanged(first);
+          }
+        }
+      }
+      if (!found) {
+        difference.missingFromSecond(first);
+      }
+    }
+
+    if (bothDirections) {
+      SchemaDifference otherWay = getSchemaDiff(subtrahendCollection, minuendCollection, false);
+      otherWay.missingFromSecond().forEach(difference::missingFromFirst);
+    }
 
-    return minuendCollection.stream()
-        .filter(fs -> !subtrahendWithoutComment.contains(unsetCommentFunc.apply(fs))).collect(Collectors.toList());
+    return difference;
   }
 
+  public static class SchemaDifference {
+    private final List<FieldSchema> missingFromFirst = new ArrayList<>();
+    private final List<FieldSchema> missingFromSecond = new ArrayList<>();
+    private final List<FieldSchema> typeChanged = new ArrayList<>();
+    private final List<FieldSchema> commentChanged = new ArrayList<>();
+
+    public List<FieldSchema> missingFromFirst() {

Review comment:
       Good point, renamed the methods




-- 
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: gitbox-unsubscribe@hive.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: gitbox-unsubscribe@hive.apache.org
For additional commands, e-mail: gitbox-help@hive.apache.org