[ https://issues.apache.org/jira/browse/HIVE-25256?focusedWorklogId=622945&page=com.atlassian.jira.plugin.system.issuetabpanels:worklog-tabpanel#worklog-622945 ]
ASF GitHub Bot logged work on HIVE-25256: ----------------------------------------- Author: ASF GitHub Bot Created on: 15/Jul/21 09:50 Start Date: 15/Jul/21 09:50 Worklog Time Spent: 10m Work Description: 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-unsubscr...@hive.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org Issue Time Tracking ------------------- Worklog Id: (was: 622945) Time Spent: 2h 10m (was: 2h) > Support ALTER TABLE CHANGE COLUMN for Iceberg > --------------------------------------------- > > Key: HIVE-25256 > URL: https://issues.apache.org/jira/browse/HIVE-25256 > Project: Hive > Issue Type: New Feature > Reporter: Marton Bod > Assignee: Marton Bod > Priority: Major > Labels: pull-request-available > Time Spent: 2h 10m > Remaining Estimate: 0h > > In order to provide support for renaming/changing the data type of a single > column, we should add alter table change column support for Iceberg tables. -- This message was sent by Atlassian Jira (v8.3.4#803005)