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/07/09 13:28:30 UTC

[GitHub] [hive] marton-bod opened a new pull request #2463: HIVE-25256: Support ALTER TABLE CHANGE COLUMN for Iceberg

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


   ### What changes were proposed in this pull request?
   Introduce support for `ALTER TABLE tbl CHANGE COLUMN`
   
   
   ### Why are the changes needed?
   Allows us to rename a column, change its type (only float->double, int->bigint, decimal changes), change its comment or change its order.
   
   
   ### Does this PR introduce _any_ user-facing change?
   yes, new query type for iceberg
   
   
   ### How was this patch tested?
   Unit tests
   


-- 
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 #2463: HIVE-25256: Support ALTER TABLE CHANGE COLUMN for Iceberg

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



##########
File path: iceberg/iceberg-handler/src/main/java/org/apache/iceberg/mr/hive/HiveIcebergMetaHook.java
##########
@@ -505,19 +512,82 @@ private void handleReplaceColumns(org.apache.hadoop.hive.metastore.api.Table hms
     }
 
     for (FieldSchema updatedCol : schemaDifference.getTypeChanged()) {
-      Type newType = HiveSchemaUtil.convert(TypeInfoUtils.getTypeInfoFromTypeString(updatedCol.getType()));
-      if (!(newType instanceof Type.PrimitiveType)) {
-        throw new MetaException(String.format("Cannot promote type of column: '%s' to a non-primitive type: %s.",
-            updatedCol.getName(), newType));
-      }
-      updateSchema.updateColumn(updatedCol.getName(), (Type.PrimitiveType) newType, updatedCol.getComment());
+      updateSchema.updateColumn(updatedCol.getName(), getPrimitiveTypeOrThrow(updatedCol), updatedCol.getComment());
     }
 
     for (FieldSchema updatedCol : schemaDifference.getCommentChanged()) {
       updateSchema.updateColumnDoc(updatedCol.getName(), updatedCol.getComment());
     }
   }
 
+  private void handleChangeColumn(org.apache.hadoop.hive.metastore.api.Table hmsTable) throws MetaException {
+    List<FieldSchema> hmsCols = hmsTable.getSd().getCols();
+    List<FieldSchema> icebergCols = HiveSchemaUtil.convert(icebergTable.schema());
+    // compute schema difference for renames, type/comment changes
+    HiveSchemaUtil.SchemaDifference schemaDifference = HiveSchemaUtil.getSchemaDiff(hmsCols, icebergCols, true);
+    // check column reorder (which could happen even in the absence of any rename, type or comment change)
+    Map<String, String> renameMapping = ImmutableMap.of();
+    if (!schemaDifference.getMissingFromSecond().isEmpty()) {
+      renameMapping = ImmutableMap.of(
+          schemaDifference.getMissingFromSecond().get(0).getName(),
+          schemaDifference.getMissingFromFirst().get(0).getName());
+    }
+    Pair<String, Optional<String>> outOfOrder = HiveSchemaUtil.getFirstOutOfOrderColumn(hmsCols, icebergCols,
+        renameMapping);
+
+    if (!schemaDifference.isEmpty() || outOfOrder != null) {
+      updateSchema = icebergTable.updateSchema();
+    } else {
+      // we should get here if the user restated the exactly the existing column in the CHANGE COLUMN command

Review comment:
       If the comment is not clear to you, it needs to be fixed :) Will do it




-- 
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 #2463: HIVE-25256: Support ALTER TABLE CHANGE COLUMN for Iceberg

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



##########
File path: iceberg/iceberg-handler/src/main/java/org/apache/iceberg/mr/hive/HiveIcebergMetaHook.java
##########
@@ -505,19 +512,83 @@ private void handleReplaceColumns(org.apache.hadoop.hive.metastore.api.Table hms
     }
 
     for (FieldSchema updatedCol : schemaDifference.getTypeChanged()) {
-      Type newType = HiveSchemaUtil.convert(TypeInfoUtils.getTypeInfoFromTypeString(updatedCol.getType()));
-      if (!(newType instanceof Type.PrimitiveType)) {
-        throw new MetaException(String.format("Cannot promote type of column: '%s' to a non-primitive type: %s.",
-            updatedCol.getName(), newType));
-      }
-      updateSchema.updateColumn(updatedCol.getName(), (Type.PrimitiveType) newType, updatedCol.getComment());
+      updateSchema.updateColumn(updatedCol.getName(), getPrimitiveTypeOrThrow(updatedCol), updatedCol.getComment());
     }
 
     for (FieldSchema updatedCol : schemaDifference.getCommentChanged()) {
       updateSchema.updateColumnDoc(updatedCol.getName(), updatedCol.getComment());
     }
   }
 
+  private void handleChangeColumn(org.apache.hadoop.hive.metastore.api.Table hmsTable) throws MetaException {
+    List<FieldSchema> hmsCols = hmsTable.getSd().getCols();
+    List<FieldSchema> icebergCols = HiveSchemaUtil.convert(icebergTable.schema());
+    // compute schema difference for renames, type/comment changes
+    HiveSchemaUtil.SchemaDifference schemaDifference = HiveSchemaUtil.getSchemaDiff(hmsCols, icebergCols, true);
+    // check column reorder (which could happen even in the absence of any rename, type or comment change)
+    Map<String, String> renameMapping = ImmutableMap.of();
+    if (!schemaDifference.getMissingFromSecond().isEmpty()) {
+      renameMapping = ImmutableMap.of(
+          schemaDifference.getMissingFromSecond().get(0).getName(),
+          schemaDifference.getMissingFromFirst().get(0).getName());
+    }
+    Pair<String, Optional<String>> outOfOrder = HiveSchemaUtil.getFirstOutOfOrderColumn(hmsCols, icebergCols,
+        renameMapping);
+
+    if (!schemaDifference.isEmpty() || outOfOrder != null) {
+      updateSchema = icebergTable.updateSchema();
+    } else {
+      // we should get here if the user didn't change anything about the column
+      // i.e. no changes to the name, type, comment or order
+      LOG.info("Found no difference between new and old schema for ALTER TABLE CHANGE COLUMN for" +
+          " table: {}. There will be no Iceberg commit.", hmsTable.getTableName());
+      return;
+    }
+
+    // case 1: column name has been renamed
+    if (!schemaDifference.getMissingFromSecond().isEmpty()) {
+      FieldSchema updatedField = schemaDifference.getMissingFromSecond().get(0);
+      FieldSchema oldField = schemaDifference.getMissingFromFirst().get(0);
+      updateSchema.renameColumn(oldField.getName(), updatedField.getName());
+
+      // check if type/comment changed too
+      if (!Objects.equals(oldField.getType(), updatedField.getType())) {
+        updateSchema.updateColumn(oldField.getName(), getPrimitiveTypeOrThrow(updatedField), updatedField.getComment());
+      } else if (!Objects.equals(oldField.getComment(), updatedField.getComment())) {
+        updateSchema.updateColumnDoc(oldField.getName(), updatedField.getComment());
+      }
+
+    // case 2: only column type and/or comment changed
+    } else if (!schemaDifference.getTypeChanged().isEmpty()) {
+      FieldSchema updatedField = schemaDifference.getTypeChanged().get(0);
+      updateSchema.updateColumn(updatedField.getName(), getPrimitiveTypeOrThrow(updatedField),
+          updatedField.getComment());
+
+    // case 3: only comment changed
+    } else if (!schemaDifference.getCommentChanged().isEmpty()) {
+      FieldSchema updatedField = schemaDifference.getCommentChanged().get(0);
+      updateSchema.updateColumnDoc(updatedField.getName(), updatedField.getComment());
+    }

Review comment:
       Yes, it should. In that case, we'd have an entry in both the `commentChanged` and the `typeChanged` lists in the `schemaDifference`. There's a unit test covering this called `testAlterTableChangeColumnTypeAndComment`




-- 
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] szlta merged pull request #2463: HIVE-25256: Support ALTER TABLE CHANGE COLUMN for Iceberg

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


   


-- 
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] szlta commented on a change in pull request #2463: HIVE-25256: Support ALTER TABLE CHANGE COLUMN for Iceberg

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



##########
File path: iceberg/iceberg-handler/src/main/java/org/apache/iceberg/mr/hive/HiveIcebergStorageHandler.java
##########
@@ -86,6 +88,10 @@
   private static final Splitter TABLE_NAME_SPLITTER = Splitter.on("..");
   private static final String TABLE_NAME_SEPARATOR = "..";
 
+  private static final List<AlterTableType> ALLOWED_ALTER_OPS = ImmutableList.of(
+      AlterTableType.ADDPROPS, AlterTableType.DROPPROPS, AlterTableType.ADDCOLS,
+      AlterTableType.REPLACE_COLUMNS, AlterTableType.RENAME_COLUMN, AlterTableType.SETPARTITIONSPEC);
+

Review comment:
       Shouldn't we rely on EnumSet<AlterTableType> SUPPORTED_ALTER_OPS found in meta hook class here too?

##########
File path: iceberg/iceberg-handler/src/main/java/org/apache/iceberg/mr/hive/HiveIcebergMetaHook.java
##########
@@ -505,19 +512,83 @@ private void handleReplaceColumns(org.apache.hadoop.hive.metastore.api.Table hms
     }
 
     for (FieldSchema updatedCol : schemaDifference.getTypeChanged()) {
-      Type newType = HiveSchemaUtil.convert(TypeInfoUtils.getTypeInfoFromTypeString(updatedCol.getType()));
-      if (!(newType instanceof Type.PrimitiveType)) {
-        throw new MetaException(String.format("Cannot promote type of column: '%s' to a non-primitive type: %s.",
-            updatedCol.getName(), newType));
-      }
-      updateSchema.updateColumn(updatedCol.getName(), (Type.PrimitiveType) newType, updatedCol.getComment());
+      updateSchema.updateColumn(updatedCol.getName(), getPrimitiveTypeOrThrow(updatedCol), updatedCol.getComment());
     }
 
     for (FieldSchema updatedCol : schemaDifference.getCommentChanged()) {
       updateSchema.updateColumnDoc(updatedCol.getName(), updatedCol.getComment());
     }
   }
 
+  private void handleChangeColumn(org.apache.hadoop.hive.metastore.api.Table hmsTable) throws MetaException {
+    List<FieldSchema> hmsCols = hmsTable.getSd().getCols();
+    List<FieldSchema> icebergCols = HiveSchemaUtil.convert(icebergTable.schema());
+    // compute schema difference for renames, type/comment changes
+    HiveSchemaUtil.SchemaDifference schemaDifference = HiveSchemaUtil.getSchemaDiff(hmsCols, icebergCols, true);
+    // check column reorder (which could happen even in the absence of any rename, type or comment change)
+    Map<String, String> renameMapping = ImmutableMap.of();
+    if (!schemaDifference.getMissingFromSecond().isEmpty()) {
+      renameMapping = ImmutableMap.of(
+          schemaDifference.getMissingFromSecond().get(0).getName(),
+          schemaDifference.getMissingFromFirst().get(0).getName());
+    }
+    Pair<String, Optional<String>> outOfOrder = HiveSchemaUtil.getFirstOutOfOrderColumn(hmsCols, icebergCols,
+        renameMapping);
+
+    if (!schemaDifference.isEmpty() || outOfOrder != null) {
+      updateSchema = icebergTable.updateSchema();
+    } else {
+      // we should get here if the user didn't change anything about the column
+      // i.e. no changes to the name, type, comment or order
+      LOG.info("Found no difference between new and old schema for ALTER TABLE CHANGE COLUMN for" +
+          " table: {}. There will be no Iceberg commit.", hmsTable.getTableName());
+      return;
+    }
+
+    // case 1: column name has been renamed
+    if (!schemaDifference.getMissingFromSecond().isEmpty()) {
+      FieldSchema updatedField = schemaDifference.getMissingFromSecond().get(0);
+      FieldSchema oldField = schemaDifference.getMissingFromFirst().get(0);
+      updateSchema.renameColumn(oldField.getName(), updatedField.getName());
+
+      // check if type/comment changed too
+      if (!Objects.equals(oldField.getType(), updatedField.getType())) {
+        updateSchema.updateColumn(oldField.getName(), getPrimitiveTypeOrThrow(updatedField), updatedField.getComment());
+      } else if (!Objects.equals(oldField.getComment(), updatedField.getComment())) {
+        updateSchema.updateColumnDoc(oldField.getName(), updatedField.getComment());
+      }
+
+    // case 2: only column type and/or comment changed
+    } else if (!schemaDifference.getTypeChanged().isEmpty()) {
+      FieldSchema updatedField = schemaDifference.getTypeChanged().get(0);
+      updateSchema.updateColumn(updatedField.getName(), getPrimitiveTypeOrThrow(updatedField),
+          updatedField.getComment());
+
+    // case 3: only comment changed
+    } else if (!schemaDifference.getCommentChanged().isEmpty()) {
+      FieldSchema updatedField = schemaDifference.getCommentChanged().get(0);
+      updateSchema.updateColumnDoc(updatedField.getName(), updatedField.getComment());
+    }

Review comment:
       Will this handle the case when name is unchanged, but both the type and the comment has been altered?




-- 
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 #2463: HIVE-25256: Support ALTER TABLE CHANGE COLUMN for Iceberg

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



##########
File path: iceberg/iceberg-handler/src/main/java/org/apache/iceberg/mr/hive/HiveIcebergMetaHook.java
##########
@@ -505,19 +512,82 @@ private void handleReplaceColumns(org.apache.hadoop.hive.metastore.api.Table hms
     }
 
     for (FieldSchema updatedCol : schemaDifference.getTypeChanged()) {
-      Type newType = HiveSchemaUtil.convert(TypeInfoUtils.getTypeInfoFromTypeString(updatedCol.getType()));
-      if (!(newType instanceof Type.PrimitiveType)) {
-        throw new MetaException(String.format("Cannot promote type of column: '%s' to a non-primitive type: %s.",
-            updatedCol.getName(), newType));
-      }
-      updateSchema.updateColumn(updatedCol.getName(), (Type.PrimitiveType) newType, updatedCol.getComment());
+      updateSchema.updateColumn(updatedCol.getName(), getPrimitiveTypeOrThrow(updatedCol), updatedCol.getComment());
     }
 
     for (FieldSchema updatedCol : schemaDifference.getCommentChanged()) {
       updateSchema.updateColumnDoc(updatedCol.getName(), updatedCol.getComment());
     }
   }
 
+  private void handleChangeColumn(org.apache.hadoop.hive.metastore.api.Table hmsTable) throws MetaException {
+    List<FieldSchema> hmsCols = hmsTable.getSd().getCols();
+    List<FieldSchema> icebergCols = HiveSchemaUtil.convert(icebergTable.schema());
+    // compute schema difference for renames, type/comment changes
+    HiveSchemaUtil.SchemaDifference schemaDifference = HiveSchemaUtil.getSchemaDiff(hmsCols, icebergCols, true);
+    // check column reorder (which could happen even in the absence of any rename, type or comment change)
+    Map<String, String> renameMapping = ImmutableMap.of();
+    if (!schemaDifference.getMissingFromSecond().isEmpty()) {
+      renameMapping = ImmutableMap.of(
+          schemaDifference.getMissingFromSecond().get(0).getName(),
+          schemaDifference.getMissingFromFirst().get(0).getName());
+    }
+    Pair<String, Optional<String>> outOfOrder = HiveSchemaUtil.getFirstOutOfOrderColumn(hmsCols, icebergCols,
+        renameMapping);
+
+    if (!schemaDifference.isEmpty() || outOfOrder != null) {
+      updateSchema = icebergTable.updateSchema();
+    } else {
+      // we should get here if the user restated the exactly the existing column in the CHANGE COLUMN command

Review comment:
       Updated the comment, let me know if this clarifies it.




-- 
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 #2463: HIVE-25256: Support ALTER TABLE CHANGE COLUMN for Iceberg

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



##########
File path: iceberg/iceberg-handler/src/main/java/org/apache/iceberg/mr/hive/HiveIcebergMetaHook.java
##########
@@ -505,19 +512,82 @@ private void handleReplaceColumns(org.apache.hadoop.hive.metastore.api.Table hms
     }
 
     for (FieldSchema updatedCol : schemaDifference.getTypeChanged()) {
-      Type newType = HiveSchemaUtil.convert(TypeInfoUtils.getTypeInfoFromTypeString(updatedCol.getType()));
-      if (!(newType instanceof Type.PrimitiveType)) {
-        throw new MetaException(String.format("Cannot promote type of column: '%s' to a non-primitive type: %s.",
-            updatedCol.getName(), newType));
-      }
-      updateSchema.updateColumn(updatedCol.getName(), (Type.PrimitiveType) newType, updatedCol.getComment());
+      updateSchema.updateColumn(updatedCol.getName(), getPrimitiveTypeOrThrow(updatedCol), updatedCol.getComment());
     }
 
     for (FieldSchema updatedCol : schemaDifference.getCommentChanged()) {
       updateSchema.updateColumnDoc(updatedCol.getName(), updatedCol.getComment());
     }
   }
 
+  private void handleChangeColumn(org.apache.hadoop.hive.metastore.api.Table hmsTable) throws MetaException {
+    List<FieldSchema> hmsCols = hmsTable.getSd().getCols();
+    List<FieldSchema> icebergCols = HiveSchemaUtil.convert(icebergTable.schema());
+    // compute schema difference for renames, type/comment changes
+    HiveSchemaUtil.SchemaDifference schemaDifference = HiveSchemaUtil.getSchemaDiff(hmsCols, icebergCols, true);
+    // check column reorder (which could happen even in the absence of any rename, type or comment change)
+    Map<String, String> renameMapping = ImmutableMap.of();
+    if (!schemaDifference.getMissingFromSecond().isEmpty()) {
+      renameMapping = ImmutableMap.of(
+          schemaDifference.getMissingFromSecond().get(0).getName(),
+          schemaDifference.getMissingFromFirst().get(0).getName());
+    }
+    Pair<String, Optional<String>> outOfOrder = HiveSchemaUtil.getFirstOutOfOrderColumn(hmsCols, icebergCols,
+        renameMapping);
+
+    if (!schemaDifference.isEmpty() || outOfOrder != null) {
+      updateSchema = icebergTable.updateSchema();
+    } else {
+      // we should get here if the user restated the exactly the existing column in the CHANGE COLUMN command

Review comment:
       Please fix the comment, I do not get it 😄 




-- 
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 #2463: HIVE-25256: Support ALTER TABLE CHANGE COLUMN for Iceberg

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



##########
File path: hbase-handler/src/test/results/negative/hbase_ddl.q.out
##########
@@ -26,4 +26,4 @@ key                 	int                 	It is a column key
 value               	string              	It is the column string value
 	 	 
 #### A masked pattern was here ####
-FAILED: SemanticException [Error 10134]: ALTER TABLE can only be used for [ADDPROPS, DROPPROPS, ADDCOLS, REPLACE_COLUMNS, SETPARTITIONSPEC] to a non-native table  hbase_table_1
+FAILED: SemanticException [Error 10134]: ALTER TABLE can only be used for [ADDPROPS, DROPPROPS, ADDCOLS] to a non-native table  hbase_table_1

Review comment:
       What happens with HBase tables if we try replacing columns and setting partition spec?




-- 
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 #2463: HIVE-25256: Support ALTER TABLE CHANGE COLUMN for Iceberg

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



##########
File path: iceberg/iceberg-handler/src/main/java/org/apache/iceberg/mr/hive/HiveIcebergMetaHook.java
##########
@@ -505,19 +512,82 @@ private void handleReplaceColumns(org.apache.hadoop.hive.metastore.api.Table hms
     }
 
     for (FieldSchema updatedCol : schemaDifference.getTypeChanged()) {
-      Type newType = HiveSchemaUtil.convert(TypeInfoUtils.getTypeInfoFromTypeString(updatedCol.getType()));
-      if (!(newType instanceof Type.PrimitiveType)) {
-        throw new MetaException(String.format("Cannot promote type of column: '%s' to a non-primitive type: %s.",
-            updatedCol.getName(), newType));
-      }
-      updateSchema.updateColumn(updatedCol.getName(), (Type.PrimitiveType) newType, updatedCol.getComment());
+      updateSchema.updateColumn(updatedCol.getName(), getPrimitiveTypeOrThrow(updatedCol), updatedCol.getComment());
     }
 
     for (FieldSchema updatedCol : schemaDifference.getCommentChanged()) {
       updateSchema.updateColumnDoc(updatedCol.getName(), updatedCol.getComment());
     }
   }
 
+  private void handleChangeColumn(org.apache.hadoop.hive.metastore.api.Table hmsTable) throws MetaException {
+    List<FieldSchema> hmsCols = hmsTable.getSd().getCols();
+    List<FieldSchema> icebergCols = HiveSchemaUtil.convert(icebergTable.schema());
+    // compute schema difference for renames, type/comment changes
+    HiveSchemaUtil.SchemaDifference schemaDifference = HiveSchemaUtil.getSchemaDiff(hmsCols, icebergCols, true);
+    // check column reorder (which could happen even in the absence of any rename, type or comment change)
+    Map<String, String> renameMapping = ImmutableMap.of();
+    if (!schemaDifference.getMissingFromSecond().isEmpty()) {
+      renameMapping = ImmutableMap.of(
+          schemaDifference.getMissingFromSecond().get(0).getName(),
+          schemaDifference.getMissingFromFirst().get(0).getName());
+    }
+    Pair<String, Optional<String>> outOfOrder = HiveSchemaUtil.getFirstOutOfOrderColumn(hmsCols, icebergCols,
+        renameMapping);
+
+    if (!schemaDifference.isEmpty() || outOfOrder != null) {
+      updateSchema = icebergTable.updateSchema();
+    } else {
+      // we should get here if the user restated the exactly the existing column in the CHANGE COLUMN command

Review comment:
       Or fix me 😄 




-- 
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 #2463: HIVE-25256: Support ALTER TABLE CHANGE COLUMN for Iceberg

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



##########
File path: hbase-handler/src/test/results/negative/hbase_ddl.q.out
##########
@@ -26,4 +26,4 @@ key                 	int                 	It is a column key
 value               	string              	It is the column string value
 	 	 
 #### A masked pattern was here ####
-FAILED: SemanticException [Error 10134]: ALTER TABLE can only be used for [ADDPROPS, DROPPROPS, ADDCOLS, REPLACE_COLUMNS, SETPARTITIONSPEC] to a non-native table  hbase_table_1
+FAILED: SemanticException [Error 10134]: ALTER TABLE can only be used for [ADDPROPS, DROPPROPS, ADDCOLS] to a non-native table  hbase_table_1

Review comment:
       Hbase would get this SemanticException:
   ```
   ALTER TABLE can only be used for [ADDPROPS, DROPPROPS, ADDCOLS] to a non-native table  hbase_table_1
   ```
   HBase (and all other storage handlers except for Iceberg at the moment), should get this exception for all alter commands other than SET/UNSET TBLPROPERTIES and ADD COLUMNS.




-- 
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] szlta merged pull request #2463: HIVE-25256: Support ALTER TABLE CHANGE COLUMN for Iceberg

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


   


-- 
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] szlta commented on a change in pull request #2463: HIVE-25256: Support ALTER TABLE CHANGE COLUMN for Iceberg

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



##########
File path: iceberg/iceberg-handler/src/main/java/org/apache/iceberg/mr/hive/HiveIcebergMetaHook.java
##########
@@ -505,19 +512,83 @@ private void handleReplaceColumns(org.apache.hadoop.hive.metastore.api.Table hms
     }
 
     for (FieldSchema updatedCol : schemaDifference.getTypeChanged()) {
-      Type newType = HiveSchemaUtil.convert(TypeInfoUtils.getTypeInfoFromTypeString(updatedCol.getType()));
-      if (!(newType instanceof Type.PrimitiveType)) {
-        throw new MetaException(String.format("Cannot promote type of column: '%s' to a non-primitive type: %s.",
-            updatedCol.getName(), newType));
-      }
-      updateSchema.updateColumn(updatedCol.getName(), (Type.PrimitiveType) newType, updatedCol.getComment());
+      updateSchema.updateColumn(updatedCol.getName(), getPrimitiveTypeOrThrow(updatedCol), updatedCol.getComment());
     }
 
     for (FieldSchema updatedCol : schemaDifference.getCommentChanged()) {
       updateSchema.updateColumnDoc(updatedCol.getName(), updatedCol.getComment());
     }
   }
 
+  private void handleChangeColumn(org.apache.hadoop.hive.metastore.api.Table hmsTable) throws MetaException {
+    List<FieldSchema> hmsCols = hmsTable.getSd().getCols();
+    List<FieldSchema> icebergCols = HiveSchemaUtil.convert(icebergTable.schema());
+    // compute schema difference for renames, type/comment changes
+    HiveSchemaUtil.SchemaDifference schemaDifference = HiveSchemaUtil.getSchemaDiff(hmsCols, icebergCols, true);
+    // check column reorder (which could happen even in the absence of any rename, type or comment change)
+    Map<String, String> renameMapping = ImmutableMap.of();
+    if (!schemaDifference.getMissingFromSecond().isEmpty()) {
+      renameMapping = ImmutableMap.of(
+          schemaDifference.getMissingFromSecond().get(0).getName(),
+          schemaDifference.getMissingFromFirst().get(0).getName());
+    }
+    Pair<String, Optional<String>> outOfOrder = HiveSchemaUtil.getFirstOutOfOrderColumn(hmsCols, icebergCols,
+        renameMapping);
+
+    if (!schemaDifference.isEmpty() || outOfOrder != null) {
+      updateSchema = icebergTable.updateSchema();
+    } else {
+      // we should get here if the user didn't change anything about the column
+      // i.e. no changes to the name, type, comment or order
+      LOG.info("Found no difference between new and old schema for ALTER TABLE CHANGE COLUMN for" +
+          " table: {}. There will be no Iceberg commit.", hmsTable.getTableName());
+      return;
+    }
+
+    // case 1: column name has been renamed
+    if (!schemaDifference.getMissingFromSecond().isEmpty()) {
+      FieldSchema updatedField = schemaDifference.getMissingFromSecond().get(0);
+      FieldSchema oldField = schemaDifference.getMissingFromFirst().get(0);
+      updateSchema.renameColumn(oldField.getName(), updatedField.getName());
+
+      // check if type/comment changed too
+      if (!Objects.equals(oldField.getType(), updatedField.getType())) {
+        updateSchema.updateColumn(oldField.getName(), getPrimitiveTypeOrThrow(updatedField), updatedField.getComment());
+      } else if (!Objects.equals(oldField.getComment(), updatedField.getComment())) {
+        updateSchema.updateColumnDoc(oldField.getName(), updatedField.getComment());
+      }
+
+    // case 2: only column type and/or comment changed
+    } else if (!schemaDifference.getTypeChanged().isEmpty()) {
+      FieldSchema updatedField = schemaDifference.getTypeChanged().get(0);
+      updateSchema.updateColumn(updatedField.getName(), getPrimitiveTypeOrThrow(updatedField),
+          updatedField.getComment());
+
+    // case 3: only comment changed
+    } else if (!schemaDifference.getCommentChanged().isEmpty()) {
+      FieldSchema updatedField = schemaDifference.getCommentChanged().get(0);
+      updateSchema.updateColumnDoc(updatedField.getName(), updatedField.getComment());
+    }

Review comment:
       Thanks!




-- 
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 #2463: HIVE-25256: Support ALTER TABLE CHANGE COLUMN for Iceberg

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



##########
File path: hbase-handler/src/test/results/negative/hbase_ddl.q.out
##########
@@ -26,4 +26,4 @@ key                 	int                 	It is a column key
 value               	string              	It is the column string value
 	 	 
 #### A masked pattern was here ####
-FAILED: SemanticException [Error 10134]: ALTER TABLE can only be used for [ADDPROPS, DROPPROPS, ADDCOLS, REPLACE_COLUMNS, SETPARTITIONSPEC] to a non-native table  hbase_table_1
+FAILED: SemanticException [Error 10134]: ALTER TABLE can only be used for [ADDPROPS, DROPPROPS, ADDCOLS] to a non-native table  hbase_table_1

Review comment:
       They get this SemanticException:
   ```
   ALTER TABLE can only be used for [ADDPROPS, DROPPROPS, ADDCOLS] to a non-native table  hbase_table_1
   ```
   HBase (and all other storage handlers except for Iceberg at the moment), should get this exception for alter commands other than SET/UNSET TBLPROPERTIES and ADD COLUMNS.




-- 
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 #2463: HIVE-25256: Support ALTER TABLE CHANGE COLUMN for Iceberg

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



##########
File path: hbase-handler/src/test/results/negative/hbase_ddl.q.out
##########
@@ -26,4 +26,4 @@ key                 	int                 	It is a column key
 value               	string              	It is the column string value
 	 	 
 #### A masked pattern was here ####
-FAILED: SemanticException [Error 10134]: ALTER TABLE can only be used for [ADDPROPS, DROPPROPS, ADDCOLS, REPLACE_COLUMNS, SETPARTITIONSPEC] to a non-native table  hbase_table_1
+FAILED: SemanticException [Error 10134]: ALTER TABLE can only be used for [ADDPROPS, DROPPROPS, ADDCOLS] to a non-native table  hbase_table_1

Review comment:
       Previously, when we were working on adding new alter commands for Iceberg, we kept adding these new operation types (rename columns, etc.) to the allowed list. However, there was only one global allowed list for all storage handler types. Now, the allowed list has been moved into the storage handler, so I've reverted the global list to its original form (before all our Iceberg changes started flowing in)




-- 
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 #2463: HIVE-25256: Support ALTER TABLE CHANGE COLUMN for Iceberg

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



##########
File path: iceberg/iceberg-handler/src/main/java/org/apache/iceberg/mr/hive/HiveIcebergStorageHandler.java
##########
@@ -86,6 +88,10 @@
   private static final Splitter TABLE_NAME_SPLITTER = Splitter.on("..");
   private static final String TABLE_NAME_SEPARATOR = "..";
 
+  private static final List<AlterTableType> ALLOWED_ALTER_OPS = ImmutableList.of(
+      AlterTableType.ADDPROPS, AlterTableType.DROPPROPS, AlterTableType.ADDCOLS,
+      AlterTableType.REPLACE_COLUMNS, AlterTableType.RENAME_COLUMN, AlterTableType.SETPARTITIONSPEC);
+

Review comment:
       Yes, good idea!




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