xiarixiaoyao commented on a change in pull request #3808:
URL: https://github.com/apache/hudi/pull/3808#discussion_r742468523



##########
File path: 
hudi-client/hudi-client-common/src/main/java/org/apache/hudi/client/AbstractHoodieWriteClient.java
##########
@@ -1078,4 +1088,138 @@ public void close() {
     this.heartbeatClient.stop();
     this.txnManager.close();
   }
+
+  /**
+   * add columns to table.
+   *
+   * @param colName col name to be added. if we want to add col to a nested 
filed, the fullName should be specify
+   * @param schema col type to be added.
+   * @param doc col doc to be added.
+   * @param position col position to be added
+   * @param positionType col position change type. now support three change 
types: first/after/before
+   */
+  public void addCol(String colName, Schema schema, String doc, String 
position, TableChange.ColumnPositionChange.ColumnPositionType positionType) {

Review comment:
       add  repeat delete and add  case check in test class TestTableChanges.

##########
File path: 
hudi-client/hudi-client-common/src/main/java/org/apache/hudi/client/AbstractHoodieWriteClient.java
##########
@@ -1078,4 +1088,138 @@ public void close() {
     this.heartbeatClient.stop();
     this.txnManager.close();
   }
+
+  /**
+   * add columns to table.
+   *
+   * @param colName col name to be added. if we want to add col to a nested 
filed, the fullName should be specify
+   * @param schema col type to be added.
+   * @param doc col doc to be added.
+   * @param position col position to be added
+   * @param positionType col position change type. now support three change 
types: first/after/before
+   */
+  public void addCol(String colName, Schema schema, String doc, String 
position, TableChange.ColumnPositionChange.ColumnPositionType positionType) {
+    Pair<InternalSchema, HoodieTableMetaClient> pair = 
getInternalSchemaAndMetaClient();
+    InternalSchema newSchema = SchemaChangePersistHelper
+        .applyAddChange(pair.getLeft(), colName, 
AvroInternalSchemaConverter.convertToField(schema), doc, position, 
positionType);
+    commitTableChange(newSchema, pair.getRight());
+  }
+
+  public void addCol(String colName, Schema schema) {
+    addCol(colName, schema, null, null, null);

Review comment:
       using enum value "NO_OPERATION" instead of use "NULL_COLUMN" in 
ColumnPositionType 

##########
File path: 
hudi-client/hudi-client-common/src/main/java/org/apache/hudi/client/AbstractHoodieWriteClient.java
##########
@@ -1078,4 +1088,138 @@ public void close() {
     this.heartbeatClient.stop();
     this.txnManager.close();
   }
+
+  /**
+   * add columns to table.
+   *
+   * @param colName col name to be added. if we want to add col to a nested 
filed, the fullName should be specify
+   * @param schema col type to be added.
+   * @param doc col doc to be added.
+   * @param position col position to be added
+   * @param positionType col position change type. now support three change 
types: first/after/before
+   */
+  public void addCol(String colName, Schema schema, String doc, String 
position, TableChange.ColumnPositionChange.ColumnPositionType positionType) {
+    Pair<InternalSchema, HoodieTableMetaClient> pair = 
getInternalSchemaAndMetaClient();
+    InternalSchema newSchema = SchemaChangePersistHelper
+        .applyAddChange(pair.getLeft(), colName, 
AvroInternalSchemaConverter.convertToField(schema), doc, position, 
positionType);
+    commitTableChange(newSchema, pair.getRight());
+  }
+
+  public void addCol(String colName, Schema schema) {
+    addCol(colName, schema, null, null, null);
+  }
+
+  /**
+   * delete columns to table.
+   *
+   * @param colName col name to be deleted. if we want to delete col from a 
nested filed, the fullName should be specify
+   */
+  public void deleteColumns(String colName) {
+    Pair<InternalSchema, HoodieTableMetaClient> pair = 
getInternalSchemaAndMetaClient();
+    InternalSchema newSchema = 
SchemaChangePersistHelper.applyDeleteChange(pair.getLeft(), colName);
+    commitTableChange(newSchema, pair.getRight());
+  }
+
+  /**
+   * rename col name for hudi table.
+   *
+   * @param colName col name to be renamed. if we want to rename col from a 
nested filed, the fullName should be specify
+   * @param newName new name for current col. no need to specify fullName.
+   */
+  public void renameColumn(String colName, String newName) {
+    Pair<InternalSchema, HoodieTableMetaClient> pair = 
getInternalSchemaAndMetaClient();
+    InternalSchema newSchema = 
SchemaChangePersistHelper.applyRenameChange(pair.getLeft(), colName, newName);
+    commitTableChange(newSchema, pair.getRight());
+  }
+
+  /**
+   * update col nullable attribute for hudi table.
+   *
+   * @param colName col name to be changed. if we want to change col from a 
nested filed, the fullName should be specify
+   * @param nullable .
+   */
+  public void updateColumnNullability(String colName, boolean nullable) {
+    Pair<InternalSchema, HoodieTableMetaClient> pair = 
getInternalSchemaAndMetaClient();
+    InternalSchema newSchema = 
SchemaChangePersistHelper.applyColumnNullabilityChange(pair.getLeft(), colName, 
nullable);
+    commitTableChange(newSchema, pair.getRight());
+  }
+
+  /**
+   * update col Type for hudi table.
+   * only support update primitive type to primitive type.
+   * cannot update nest type to nest type or primitive type eg: RecordType -> 
MapType, MapType -> LongType.
+   *
+   * @param colName col name to be changed. if we want to change col from a 
nested filed, the fullName should be specify
+   * @param newType .
+   */
+  public void updateColumnType(String colName, Type newType) {
+    Pair<InternalSchema, HoodieTableMetaClient> pair = 
getInternalSchemaAndMetaClient();
+    InternalSchema newSchema = 
SchemaChangePersistHelper.applyColumnTypeChange(pair.getLeft(), colName, 
newType);
+    commitTableChange(newSchema, pair.getRight());
+  }
+
+  /**
+   * update col comment for hudi table.
+   *
+   * @param colName col name to be changed. if we want to change col from a 
nested filed, the fullName should be specify
+   * @param doc .
+   */
+  public void updateColumnComment(String colName, String doc) {
+    Pair<InternalSchema, HoodieTableMetaClient> pair = 
getInternalSchemaAndMetaClient();
+    InternalSchema newSchema = 
SchemaChangePersistHelper.applyColumnCommentChange(pair.getLeft(), colName, 
doc);
+    commitTableChange(newSchema, pair.getRight());
+  }
+
+  /**
+   * reorder the position of col.
+   *
+   * @param colName column which need to be reordered. if we want to change 
col from a nested filed, the fullName should be specify.
+   * @param referColName reference position.
+   * @param orderType col position change type. now support three change 
types: first/after/before
+   */
+  public void reOrderColPosition(String colName, String referColName, 
TableChange.ColumnPositionChange.ColumnPositionType orderType) {
+    if (colName == null || orderType == null || referColName == null) {
+      return;
+    }
+    //get internalSchema
+    Pair<InternalSchema, HoodieTableMetaClient> pair = 
getInternalSchemaAndMetaClient();
+    InternalSchema newSchema = SchemaChangePersistHelper
+        .applyReOrderColPositionChange(pair.getLeft(), colName, referColName, 
orderType);
+    commitTableChange(newSchema, pair.getRight());
+  }
+
+  private Pair<InternalSchema, HoodieTableMetaClient> 
getInternalSchemaAndMetaClient() {
+    HoodieTableMetaClient metaClient = createMetaClient(true);
+    TableSchemaResolver schemaUtil = new TableSchemaResolver(metaClient);
+    Option<InternalSchema> internalSchemaOption = 
schemaUtil.getTableInternalSchemaFromCommitMetadata();
+    if (!internalSchemaOption.isPresent()) {
+      throw new HoodieException(String.format("cannot find schema for current 
table: %s", config.getBasePath()));
+    }
+    return Pair.of(internalSchemaOption.get(), metaClient);
+  }
+
+  private void commitTableChange(InternalSchema newSchema, 
HoodieTableMetaClient metaClient) {
+    TableSchemaResolver schemaUtil = new TableSchemaResolver(metaClient);
+    String historySchemaStr = 
schemaUtil.getTableHistorySchemaStrFromCommitMetadata().orElse("");
+    Schema schema = AvroInternalSchemaConverter.convert(newSchema, 
config.getTableName());
+    String commitActionType = 
CommitUtils.getCommitActionType(WriteOperationType.INSERT, 
metaClient.getTableType());

Review comment:
       fixed




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to