yuzelin commented on code in PR #965:
URL: https://github.com/apache/incubator-paimon/pull/965#discussion_r1176396142
##########
paimon-flink/paimon-flink-common/src/main/java/org/apache/paimon/flink/FlinkCatalog.java:
##########
@@ -222,52 +234,106 @@ public void createTable(ObjectPath tablePath,
CatalogBaseTable table, boolean ig
}
}
+ private SchemaChange toSchemaChange(Map<String, String> oldProperties,
TableChange change) {
+ if (change instanceof AddColumn) {
+ AddColumn add = (AddColumn) change;
+ String comment = add.getColumn().getComment().orElse(null);
+ SchemaChange.Move move = getMove(add.getPosition(),
add.getColumn().getName());
+ return SchemaChange.addColumn(
+ add.getColumn().getName(),
+ LogicalTypeConversion.toDataType(
+ add.getColumn().getDataType().getLogicalType()),
+ comment,
+ move);
+ } else if (change instanceof DropColumn) {
+ DropColumn drop = (DropColumn) change;
+ return SchemaChange.dropColumn(drop.getColumnName());
+ } else if (change instanceof ModifyColumnName) {
+ ModifyColumnName modify = (ModifyColumnName) change;
+ return SchemaChange.renameColumn(modify.getOldColumnName(),
modify.getNewColumnName());
+ } else if (change instanceof ModifyPhysicalColumnType) {
+ ModifyPhysicalColumnType modify = (ModifyPhysicalColumnType)
change;
+ return SchemaChange.updateColumnType(
+ modify.getOldColumn().getName(),
+
LogicalTypeConversion.toDataType(modify.getNewType().getLogicalType()));
+ } else if (change instanceof ModifyColumnPosition) {
+ ModifyColumnPosition modify = (ModifyColumnPosition) change;
+ SchemaChange.Move move =
+ getMove(modify.getNewPosition(),
modify.getNewColumn().getName());
+ return SchemaChange.updateColumnPosition(move);
+ } else if (change instanceof SetOption) {
+ SetOption setOption = (SetOption) change;
+ String key = setOption.getKey();
+ String value = setOption.getValue();
+
+ if (Objects.equals(oldProperties.get(key), value)) {
+ return null;
+ }
+
+ if (PATH.key().equalsIgnoreCase(key)) {
+ throw new IllegalArgumentException("Illegal table path in
table options: " + value);
+ }
+
+ return SchemaChange.setOption(key, value);
+ } else if (change instanceof ResetOption) {
+ ResetOption resetOption = (ResetOption) change;
+ return SchemaChange.removeOption(resetOption.getKey());
+ } else {
+ throw new UnsupportedOperationException(
+ "Change is not supported: " + change.getClass());
+ }
+ }
+
@Override
public void alterTable(
ObjectPath tablePath, CatalogBaseTable newTable, boolean
ignoreIfNotExists)
throws TableNotExistException, CatalogException {
+ this.alterTable(tablePath, newTable, null, ignoreIfNotExists);
+ }
+
+ @Override
+ public void alterTable(
+ ObjectPath tablePath,
+ CatalogBaseTable newTable,
+ List<TableChange> tableChanges,
+ boolean ignoreIfNotExists)
+ throws TableNotExistException, CatalogException {
if (ignoreIfNotExists && !tableExists(tablePath)) {
return;
}
CatalogTable table = getTable(tablePath);
- // Currently, Flink SQL only support altering table properties.
validateAlterTable(table, (CatalogTable) newTable);
- List<SchemaChange> changes = new ArrayList<>();
Map<String, String> oldProperties = table.getOptions();
- for (Map.Entry<String, String> entry :
newTable.getOptions().entrySet()) {
- String key = entry.getKey();
- String value = entry.getValue();
-
- if (Objects.equals(value, oldProperties.get(key))) {
- continue;
- }
-
- if (PATH.key().equalsIgnoreCase(key)) {
- throw new IllegalArgumentException("Illegal table path in
table options: " + value);
- }
-
- changes.add(SchemaChange.setOption(key, value));
+ List<SchemaChange> changes = new ArrayList<>();
+ if (null != tableChanges) {
+ List<SchemaChange> schemaChanges =
+ tableChanges.stream()
+ .map(m -> toSchemaChange(oldProperties, m))
+ .filter(Objects::nonNull)
+ .collect(Collectors.toList());
+ changes.addAll(schemaChanges);
}
- oldProperties
- .keySet()
- .forEach(
- k -> {
- if (!newTable.getOptions().containsKey(k)) {
- changes.add(SchemaChange.removeOption(k));
- }
- });
-
Review Comment:
I left a comment at the bottom about this.
--
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]