diff --git a/paimon-core/src/main/java/org/apache/paimon/rest/RESTCatalog.java b/paimon-core/src/main/java/org/apache/paimon/rest/RESTCatalog.java index 33adc1b0a09e..f436254f1dcd 100644 --- a/paimon-core/src/main/java/org/apache/paimon/rest/RESTCatalog.java +++ b/paimon-core/src/main/java/org/apache/paimon/rest/RESTCatalog.java @@ -41,6 +41,7 @@ import org.apache.paimon.rest.requests.AlterDatabaseRequest; import org.apache.paimon.rest.requests.CreateDatabaseRequest; import org.apache.paimon.rest.requests.CreateTableRequest; +import org.apache.paimon.rest.requests.SchemaChanges; import org.apache.paimon.rest.requests.UpdateTableRequest; import org.apache.paimon.rest.responses.AlterDatabaseResponse; import org.apache.paimon.rest.responses.ConfigResponse; @@ -414,7 +415,8 @@ protected TableSchema getDataTableSchema(Identifier identifier) throws TableNotE // todo: how know which exception to throw private void updateTable(Identifier fromTable, Identifier toTable, List changes) { - UpdateTableRequest request = new UpdateTableRequest(fromTable, toTable, changes); + UpdateTableRequest request = + new UpdateTableRequest(fromTable, toTable, new SchemaChanges(changes)); client.post( resourcePaths.table(fromTable.getDatabaseName(), fromTable.getTableName()), request, diff --git a/paimon-core/src/main/java/org/apache/paimon/rest/RESTObjectMapper.java b/paimon-core/src/main/java/org/apache/paimon/rest/RESTObjectMapper.java index fa66e754ac85..b0f9ba7b83e2 100644 --- a/paimon-core/src/main/java/org/apache/paimon/rest/RESTObjectMapper.java +++ b/paimon-core/src/main/java/org/apache/paimon/rest/RESTObjectMapper.java @@ -18,6 +18,10 @@ package org.apache.paimon.rest; +import org.apache.paimon.catalog.Identifier; +import org.apache.paimon.rest.serializer.IdentifierSerializer; +import org.apache.paimon.rest.serializer.SchemaSerializer; +import org.apache.paimon.schema.Schema; import org.apache.paimon.types.DataField; import org.apache.paimon.types.DataType; import org.apache.paimon.types.DataTypeJsonParser; @@ -49,8 +53,15 @@ public static Module createPaimonRestJacksonModule() { DataField.class, DataField::serializeJson, DataTypeJsonParser::parseDataField); + registerJsonObjects( + module, Schema.class, SchemaSerializer.INSTANCE, SchemaSerializer.INSTANCE); registerJsonObjects( module, DataType.class, DataType::serializeJson, DataTypeJsonParser::parseDataType); + registerJsonObjects( + module, + Identifier.class, + IdentifierSerializer.INSTANCE, + IdentifierSerializer.INSTANCE); return module; } } diff --git a/paimon-core/src/main/java/org/apache/paimon/rest/requests/CreateTableRequest.java b/paimon-core/src/main/java/org/apache/paimon/rest/requests/CreateTableRequest.java index 3a3262932522..3f80e0fee5f9 100644 --- a/paimon-core/src/main/java/org/apache/paimon/rest/requests/CreateTableRequest.java +++ b/paimon-core/src/main/java/org/apache/paimon/rest/requests/CreateTableRequest.java @@ -29,60 +29,29 @@ /** Request for creating table. */ public class CreateTableRequest implements RESTRequest { - private static final String FIELD_DATABASE_NAME = "database"; - private static final String FIELD_TABLE_NAME = "table"; - private static final String FIELD_BRANCH_NAME = "branch"; + private static final String FIELD_IDENTIFIER_NAME = "identifier"; private static final String FIELD_SCHEMA = "schema"; - @JsonProperty(FIELD_DATABASE_NAME) - private String databaseName; - - @JsonProperty(FIELD_TABLE_NAME) - private String tableName; - - @JsonProperty(FIELD_BRANCH_NAME) - private String branchName; + @JsonProperty(FIELD_IDENTIFIER_NAME) + private Identifier identifier; @JsonProperty(FIELD_SCHEMA) - private TableSchema schema; + private Schema schema; @JsonCreator public CreateTableRequest( - @JsonProperty(FIELD_DATABASE_NAME) String databaseName, - @JsonProperty(FIELD_TABLE_NAME) String tableName, - @JsonProperty(FIELD_BRANCH_NAME) String branchName, - @JsonProperty(FIELD_SCHEMA) TableSchema schema) { - this.databaseName = databaseName; - this.tableName = tableName; - this.branchName = branchName; + @JsonProperty(FIELD_IDENTIFIER_NAME) Identifier identifier, + @JsonProperty(FIELD_SCHEMA) Schema schema) { this.schema = schema; } - public CreateTableRequest(Identifier identifier, Schema schema) { - this( - identifier.getDatabaseName(), - identifier.getTableName(), - identifier.getBranchName(), - new TableSchema(schema)); - } - - @JsonGetter(FIELD_DATABASE_NAME) - public String getDatabaseName() { - return databaseName; - } - - @JsonGetter(FIELD_TABLE_NAME) - public String getTableName() { - return tableName; - } - - @JsonGetter(FIELD_BRANCH_NAME) - public String getBranchName() { - return branchName; + @JsonGetter(FIELD_IDENTIFIER_NAME) + public Identifier getIdentifier() { + return identifier; } @JsonGetter(FIELD_SCHEMA) - public TableSchema getSchema() { + public Schema getSchema() { return schema; } } diff --git a/paimon-core/src/main/java/org/apache/paimon/rest/requests/SchemaChanges.java b/paimon-core/src/main/java/org/apache/paimon/rest/requests/SchemaChanges.java new file mode 100644 index 000000000000..698525f604b9 --- /dev/null +++ b/paimon-core/src/main/java/org/apache/paimon/rest/requests/SchemaChanges.java @@ -0,0 +1,131 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.paimon.rest.requests; + +import org.apache.paimon.schema.SchemaChange; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; + +public class SchemaChanges { + private static final String FIELD_SET_OPTIONS_NAME = "set-options"; + private static final String FIELD_REMOVE_OPTIONS_NAME = "remove-options"; + private static final String FIELD_COMMENT_NAME = "comment"; + private static final String FIELD_ADD_COLUMNS_NAME = "add-columns"; + private static final String FIELD_RENAME_COLUMNS_NAME = "rename-columns"; + + private Map setOptions; + private List removeOptions; + private String comment; + private List addColumns; + private List renameColumns; + private List dropColumns; + private List updateColumnTypes; + private List updateColumnNullabilities; + private List updateColumnComments; + private List updateColumnPositions; + + public SchemaChanges( + Map setOptions, + List removeOptions, + String comment, + List addColumns, + List renameColumns, + List dropColumns, + List updateColumnTypes, + List updateColumnNullabilities, + List updateColumnComments, + List updateColumnPositions) { + this.setOptions = setOptions; + this.removeOptions = removeOptions; + this.comment = comment; + this.addColumns = addColumns; + this.renameColumns = renameColumns; + this.dropColumns = dropColumns; + this.updateColumnTypes = updateColumnTypes; + this.updateColumnNullabilities = updateColumnNullabilities; + this.updateColumnComments = updateColumnComments; + this.updateColumnPositions = updateColumnPositions; + } + + public SchemaChanges(List changes) { + Map setOptions = null; + List removeOptions = new ArrayList<>(); + String comment = null; + List addColumns = new ArrayList<>(); + for (SchemaChange change : changes) { + if (change instanceof SchemaChange.SetOption) { + setOptions.put( + ((SchemaChange.SetOption) change).key(), + ((SchemaChange.SetOption) change).value()); + } else if (change instanceof SchemaChange.RemoveOption) { + removeOptions.add(((SchemaChange.RemoveOption) change).key()); + } else if (change instanceof SchemaChange.UpdateComment) { + comment = ((SchemaChange.UpdateComment) change).comment(); + } else if (change instanceof SchemaChange.AddColumn) { + addColumns.add((SchemaChange.AddColumn) change); + } + } + this.setOptions = setOptions; + this.removeOptions = removeOptions; + this.comment = comment; + this.addColumns = addColumns; + } + + public Map getSetOptions() { + return setOptions; + } + + public List getRemoveOptions() { + return removeOptions; + } + + public String getComment() { + return comment; + } + + public List getAddColumns() { + return addColumns; + } + + public List getRenameColumns() { + return renameColumns; + } + + public List getDropColumns() { + return dropColumns; + } + + public List getUpdateColumnTypes() { + return updateColumnTypes; + } + + public List getUpdateColumnNullabilities() { + return updateColumnNullabilities; + } + + public List getUpdateColumnComments() { + return updateColumnComments; + } + + public List getUpdateColumnPositions() { + return updateColumnPositions; + } +} diff --git a/paimon-core/src/main/java/org/apache/paimon/rest/requests/TableSchema.java b/paimon-core/src/main/java/org/apache/paimon/rest/requests/TableSchema.java deleted file mode 100644 index d49e4062eea5..000000000000 --- a/paimon-core/src/main/java/org/apache/paimon/rest/requests/TableSchema.java +++ /dev/null @@ -1,123 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.apache.paimon.rest.requests; - -import org.apache.paimon.schema.Schema; -import org.apache.paimon.types.DataField; - -import org.apache.paimon.shade.jackson2.com.fasterxml.jackson.annotation.JsonCreator; -import org.apache.paimon.shade.jackson2.com.fasterxml.jackson.annotation.JsonGetter; -import org.apache.paimon.shade.jackson2.com.fasterxml.jackson.annotation.JsonProperty; - -import java.util.List; -import java.util.Map; -import java.util.Objects; - -/** - * Wrap the {@link Schema} class to support RESTCatalog. Define a class as: 1. This class in rest - * catalog is easy to maintain. 2. It's easy to manage rest API fields. - */ -public class TableSchema { - private static final String FIELD_FILED_NAME = "fields"; - private static final String FIELD_PARTITION_KEYS_NAME = "partitionKeys"; - private static final String FIELD_PRIMARY_KEYS_NAME = "primaryKeys"; - private static final String FIELD_OPTIONS_NAME = "options"; - private static final String FIELD_COMMENT_NAME = "comment"; - - @JsonProperty(FIELD_FILED_NAME) - private final List fields; - - @JsonProperty(FIELD_PARTITION_KEYS_NAME) - private final List partitionKeys; - - @JsonProperty(FIELD_PRIMARY_KEYS_NAME) - private final List primaryKeys; - - @JsonProperty(FIELD_OPTIONS_NAME) - private final Map options; - - @JsonProperty(FIELD_COMMENT_NAME) - private final String comment; - - @JsonCreator - public TableSchema( - @JsonProperty(FIELD_FILED_NAME) List fields, - @JsonProperty(FIELD_PARTITION_KEYS_NAME) List partitionKeys, - @JsonProperty(FIELD_PRIMARY_KEYS_NAME) List primaryKeys, - @JsonProperty(FIELD_OPTIONS_NAME) Map options, - @JsonProperty(FIELD_COMMENT_NAME) String comment) { - this.fields = fields; - this.partitionKeys = partitionKeys; - this.primaryKeys = primaryKeys; - this.options = options; - this.comment = comment; - } - - public TableSchema(Schema schema) { - this.fields = schema.fields(); - this.partitionKeys = schema.partitionKeys(); - this.primaryKeys = schema.primaryKeys(); - this.options = schema.options(); - this.comment = schema.comment(); - } - - @JsonGetter(FIELD_FILED_NAME) - public List getFields() { - return fields; - } - - @JsonGetter(FIELD_PARTITION_KEYS_NAME) - public List getPartitionKeys() { - return partitionKeys; - } - - @JsonGetter(FIELD_PRIMARY_KEYS_NAME) - public List getPrimaryKeys() { - return primaryKeys; - } - - @JsonGetter(FIELD_OPTIONS_NAME) - public Map getOptions() { - return options; - } - - @JsonGetter(FIELD_COMMENT_NAME) - public String getComment() { - return comment; - } - - @Override - public boolean equals(Object o) { - if (o == null || getClass() != o.getClass()) { - return false; - } else { - TableSchema that = (TableSchema) o; - return Objects.equals(fields, that.fields) - && Objects.equals(partitionKeys, that.partitionKeys) - && Objects.equals(primaryKeys, that.primaryKeys) - && Objects.equals(options, that.options) - && Objects.equals(comment, that.comment); - } - } - - @Override - public int hashCode() { - return Objects.hash(fields, partitionKeys, primaryKeys, options, comment); - } -} diff --git a/paimon-core/src/main/java/org/apache/paimon/rest/requests/UpdateTableRequest.java b/paimon-core/src/main/java/org/apache/paimon/rest/requests/UpdateTableRequest.java index ca12a1152455..ab1c7b18a59e 100644 --- a/paimon-core/src/main/java/org/apache/paimon/rest/requests/UpdateTableRequest.java +++ b/paimon-core/src/main/java/org/apache/paimon/rest/requests/UpdateTableRequest.java @@ -20,52 +20,49 @@ import org.apache.paimon.catalog.Identifier; import org.apache.paimon.rest.RESTRequest; -import org.apache.paimon.schema.SchemaChange; import org.apache.paimon.shade.jackson2.com.fasterxml.jackson.annotation.JsonCreator; import org.apache.paimon.shade.jackson2.com.fasterxml.jackson.annotation.JsonGetter; import org.apache.paimon.shade.jackson2.com.fasterxml.jackson.annotation.JsonProperty; -import java.util.List; - /** Request for updating table. */ public class UpdateTableRequest implements RESTRequest { - private static final String FIELD_FROM_IDENTIFIER = "from"; - private static final String FIELD_TO_IDENTIFIER = "to"; - private static final String FIELD_SCHEMA_CHANGES = "changes"; + private static final String FIELD_FROM_IDENTIFIER_NAME = "from"; + private static final String FIELD_TO_IDENTIFIER_NAME = "to"; + private static final String FIELD_SCHEMA_CHANGES_NAME = "changes"; - @JsonProperty(FIELD_FROM_IDENTIFIER) + @JsonProperty(FIELD_FROM_IDENTIFIER_NAME) private Identifier fromIdentifier; - @JsonProperty(FIELD_TO_IDENTIFIER) + @JsonProperty(FIELD_TO_IDENTIFIER_NAME) private Identifier toIdentifier; - @JsonProperty(FIELD_SCHEMA_CHANGES) - private List changes; + @JsonProperty(FIELD_SCHEMA_CHANGES_NAME) + private SchemaChanges changes; @JsonCreator public UpdateTableRequest( - @JsonProperty(FIELD_FROM_IDENTIFIER) Identifier fromIdentifier, - @JsonProperty(FIELD_TO_IDENTIFIER) Identifier toIdentifier, - @JsonProperty(FIELD_SCHEMA_CHANGES) List changes) { + @JsonProperty(FIELD_FROM_IDENTIFIER_NAME) Identifier fromIdentifier, + @JsonProperty(FIELD_TO_IDENTIFIER_NAME) Identifier toIdentifier, + @JsonProperty(FIELD_SCHEMA_CHANGES_NAME) SchemaChanges changes) { this.fromIdentifier = fromIdentifier; this.toIdentifier = toIdentifier; this.changes = changes; } - @JsonGetter(FIELD_FROM_IDENTIFIER) + @JsonGetter(FIELD_FROM_IDENTIFIER_NAME) public Identifier getFromIdentifier() { return fromIdentifier; } - @JsonGetter(FIELD_TO_IDENTIFIER) + @JsonGetter(FIELD_TO_IDENTIFIER_NAME) public Identifier getToIdentifier() { return toIdentifier; } - @JsonGetter(FIELD_SCHEMA_CHANGES) - public List getChanges() { + @JsonGetter(FIELD_SCHEMA_CHANGES_NAME) + public SchemaChanges getChanges() { return changes; } } diff --git a/paimon-core/src/main/java/org/apache/paimon/rest/serializer/IdentifierSerializer.java b/paimon-core/src/main/java/org/apache/paimon/rest/serializer/IdentifierSerializer.java new file mode 100644 index 000000000000..b58583ee2a13 --- /dev/null +++ b/paimon-core/src/main/java/org/apache/paimon/rest/serializer/IdentifierSerializer.java @@ -0,0 +1,72 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.paimon.rest.serializer; + +import org.apache.paimon.catalog.Identifier; +import org.apache.paimon.utils.JsonDeserializer; +import org.apache.paimon.utils.JsonSerializer; + +import org.apache.paimon.shade.jackson2.com.fasterxml.jackson.core.JsonGenerator; +import org.apache.paimon.shade.jackson2.com.fasterxml.jackson.databind.JsonNode; + +import java.io.IOException; + +/** Serializer for {@link Identifier}. */ +public class IdentifierSerializer + implements JsonSerializer, JsonDeserializer { + + public static final IdentifierSerializer INSTANCE = new IdentifierSerializer(); + + private static final String FIELD_DATABASE_NAME = "database"; + private static final String FIELD_TABLE_NAME = "table"; + private static final String FIELD_BRANCH_NAME = "branch"; + + @Override + public void serialize(Identifier identifier, JsonGenerator generator) throws IOException { + generator.writeStartObject(); + generator.writeStringField(FIELD_DATABASE_NAME, identifier.getDatabaseName()); + if (identifier.getTableName() != null) { + generator.writeStringField(FIELD_TABLE_NAME, identifier.getTableName()); + } + if (identifier.getBranchName() != null) { + generator.writeStringField(FIELD_BRANCH_NAME, identifier.getBranchName()); + } + generator.writeEndObject(); + } + + @Override + public Identifier deserialize(JsonNode node) { + JsonNode databaseNode = node.get(FIELD_DATABASE_NAME); + String databaseName = null; + if (databaseNode != null) { + databaseName = databaseNode.asText(); + } + JsonNode tableNode = node.get(FIELD_TABLE_NAME); + String tableName = null; + if (tableNode != null) { + tableName = tableNode.asText(); + } + JsonNode branchNode = node.get(FIELD_BRANCH_NAME); + String branchName = null; + if (branchNode != null) { + branchName = branchNode.asText(); + } + return new Identifier(databaseName, tableName, branchName); + } +} diff --git a/paimon-core/src/main/java/org/apache/paimon/rest/serializer/SchemaSerializer.java b/paimon-core/src/main/java/org/apache/paimon/rest/serializer/SchemaSerializer.java new file mode 100644 index 000000000000..ed4bde4c9d48 --- /dev/null +++ b/paimon-core/src/main/java/org/apache/paimon/rest/serializer/SchemaSerializer.java @@ -0,0 +1,109 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.paimon.rest.serializer; + +import org.apache.paimon.schema.Schema; +import org.apache.paimon.types.DataField; +import org.apache.paimon.types.DataTypeJsonParser; +import org.apache.paimon.utils.JsonDeserializer; +import org.apache.paimon.utils.JsonSerializer; + +import org.apache.paimon.shade.jackson2.com.fasterxml.jackson.core.JsonGenerator; +import org.apache.paimon.shade.jackson2.com.fasterxml.jackson.databind.JsonNode; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; +import java.util.Map; + +/** Serializer for the {@link Schema} class to support RESTCatalog. */ +public class SchemaSerializer implements JsonSerializer, JsonDeserializer { + + public static final SchemaSerializer INSTANCE = new SchemaSerializer(); + + private static final String FIELD_FILED_NAME = "fields"; + private static final String FIELD_PARTITION_KEYS_NAME = "partitionKeys"; + private static final String FIELD_PRIMARY_KEYS_NAME = "primaryKeys"; + private static final String FIELD_OPTIONS_NAME = "options"; + private static final String FIELD_COMMENT_NAME = "comment"; + + @Override + public void serialize(Schema schema, JsonGenerator generator) throws IOException { + generator.writeStartObject(); + generator.writeArrayFieldStart(FIELD_FILED_NAME); + for (DataField field : schema.fields()) { + field.serializeJson(generator); + } + generator.writeEndArray(); + generator.writeArrayFieldStart(FIELD_PARTITION_KEYS_NAME); + for (String partitionKey : schema.partitionKeys()) { + generator.writeString(partitionKey); + } + generator.writeEndArray(); + generator.writeArrayFieldStart(FIELD_PRIMARY_KEYS_NAME); + for (String partitionKey : schema.primaryKeys()) { + generator.writeString(partitionKey); + } + generator.writeEndArray(); + generator.writeObjectFieldStart(FIELD_OPTIONS_NAME); + for (Map.Entry entry : schema.options().entrySet()) { + generator.writeStringField(entry.getKey(), entry.getValue()); + } + generator.writeEndObject(); + if (schema.comment() != null) { + generator.writeStringField(FIELD_COMMENT_NAME, schema.comment()); + } + generator.writeEndObject(); + } + + @Override + public Schema deserialize(JsonNode node) { + Iterator fieldJsons = node.get(FIELD_FILED_NAME).elements(); + List fields = new ArrayList<>(); + while (fieldJsons.hasNext()) { + fields.add(DataTypeJsonParser.parseDataField(fieldJsons.next())); + } + Iterator partitionJsons = node.get(FIELD_PARTITION_KEYS_NAME).elements(); + List partitionKeys = new ArrayList<>(); + while (partitionJsons.hasNext()) { + partitionKeys.add(partitionJsons.next().asText()); + } + + Iterator primaryJsons = node.get(FIELD_PRIMARY_KEYS_NAME).elements(); + List primaryKeys = new ArrayList<>(); + while (primaryJsons.hasNext()) { + primaryKeys.add(primaryJsons.next().asText()); + } + JsonNode optionsJson = node.get(FIELD_OPTIONS_NAME); + Map options = new HashMap<>(); + Iterator optionsKeys = optionsJson.fieldNames(); + while (optionsKeys.hasNext()) { + String key = optionsKeys.next(); + options.put(key, optionsJson.get(key).asText()); + } + JsonNode commentNode = node.get(FIELD_COMMENT_NAME); + String comment = null; + if (commentNode != null) { + comment = commentNode.asText(); + } + return new Schema(fields, partitionKeys, primaryKeys, options, comment); + } +} diff --git a/paimon-core/src/main/java/org/apache/paimon/schema/SchemaSerializer.java b/paimon-core/src/main/java/org/apache/paimon/schema/TableSchemaSerializer.java similarity index 97% rename from paimon-core/src/main/java/org/apache/paimon/schema/SchemaSerializer.java rename to paimon-core/src/main/java/org/apache/paimon/schema/TableSchemaSerializer.java index 4fb28359c92f..f6b6c7302f2c 100644 --- a/paimon-core/src/main/java/org/apache/paimon/schema/SchemaSerializer.java +++ b/paimon-core/src/main/java/org/apache/paimon/schema/TableSchemaSerializer.java @@ -39,10 +39,10 @@ import static org.apache.paimon.schema.TableSchema.PAIMON_08_VERSION; /** A {@link JsonSerializer} for {@link TableSchema}. */ -public class SchemaSerializer +public class TableSchemaSerializer implements JsonSerializer, JsonDeserializer { - public static final SchemaSerializer INSTANCE = new SchemaSerializer(); + public static final TableSchemaSerializer INSTANCE = new TableSchemaSerializer(); @Override public void serialize(TableSchema tableSchema, JsonGenerator generator) throws IOException { diff --git a/paimon-core/src/main/java/org/apache/paimon/utils/JsonSerdeUtil.java b/paimon-core/src/main/java/org/apache/paimon/utils/JsonSerdeUtil.java index edc6dac5f992..7c36f4261347 100644 --- a/paimon-core/src/main/java/org/apache/paimon/utils/JsonSerdeUtil.java +++ b/paimon-core/src/main/java/org/apache/paimon/utils/JsonSerdeUtil.java @@ -18,8 +18,8 @@ package org.apache.paimon.utils; -import org.apache.paimon.schema.SchemaSerializer; import org.apache.paimon.schema.TableSchema; +import org.apache.paimon.schema.TableSchemaSerializer; import org.apache.paimon.types.DataField; import org.apache.paimon.types.DataType; import org.apache.paimon.types.DataTypeJsonParser; @@ -181,7 +181,10 @@ public T deserialize(JsonParser parser, DeserializationContext context) private static Module createPaimonJacksonModule() { SimpleModule module = new SimpleModule("Paimon"); registerJsonObjects( - module, TableSchema.class, SchemaSerializer.INSTANCE, SchemaSerializer.INSTANCE); + module, + TableSchema.class, + TableSchemaSerializer.INSTANCE, + TableSchemaSerializer.INSTANCE); registerJsonObjects( module, DataField.class, diff --git a/paimon-core/src/test/java/org/apache/paimon/rest/MockRESTMessage.java b/paimon-core/src/test/java/org/apache/paimon/rest/MockRESTMessage.java index a701b54c9dbd..ddf78d59c8ae 100644 --- a/paimon-core/src/test/java/org/apache/paimon/rest/MockRESTMessage.java +++ b/paimon-core/src/test/java/org/apache/paimon/rest/MockRESTMessage.java @@ -22,6 +22,7 @@ import org.apache.paimon.rest.requests.AlterDatabaseRequest; import org.apache.paimon.rest.requests.CreateDatabaseRequest; import org.apache.paimon.rest.requests.CreateTableRequest; +import org.apache.paimon.rest.requests.UpdateTableRequest; import org.apache.paimon.rest.responses.AlterDatabaseResponse; import org.apache.paimon.rest.responses.CreateDatabaseResponse; import org.apache.paimon.rest.responses.ErrorResponse; @@ -111,4 +112,10 @@ public static CreateTableRequest createTableRequest(String name) { .build(); return new CreateTableRequest(identifier, schema); } + + public static UpdateTableRequest updateTableRequest(String fromTableName, String toTableName) { + Identifier fromIdentifier = Identifier.create(databaseName(), fromTableName); + Identifier toIdentifier = Identifier.create(databaseName(), toTableName); + return new UpdateTableRequest(fromIdentifier, toIdentifier, null); + } } diff --git a/paimon-core/src/test/java/org/apache/paimon/rest/RESTObjectMapperTest.java b/paimon-core/src/test/java/org/apache/paimon/rest/RESTObjectMapperTest.java index 6da61932a6e7..987bb1cd4c5f 100644 --- a/paimon-core/src/test/java/org/apache/paimon/rest/RESTObjectMapperTest.java +++ b/paimon-core/src/test/java/org/apache/paimon/rest/RESTObjectMapperTest.java @@ -21,6 +21,7 @@ import org.apache.paimon.rest.requests.AlterDatabaseRequest; import org.apache.paimon.rest.requests.CreateDatabaseRequest; import org.apache.paimon.rest.requests.CreateTableRequest; +import org.apache.paimon.rest.requests.UpdateTableRequest; import org.apache.paimon.rest.responses.AlterDatabaseResponse; import org.apache.paimon.rest.responses.ConfigResponse; import org.apache.paimon.rest.responses.CreateDatabaseResponse; @@ -135,9 +136,7 @@ public void createTableRequestParseTest() throws Exception { CreateTableRequest request = MockRESTMessage.createTableRequest("t1"); String requestStr = mapper.writeValueAsString(request); CreateTableRequest parseData = mapper.readValue(requestStr, CreateTableRequest.class); - assertEquals(request.getDatabaseName(), parseData.getDatabaseName()); - assertEquals(request.getTableName(), parseData.getTableName()); - assertEquals(request.getBranchName(), parseData.getBranchName()); + assertEquals(request.getIdentifier(), parseData.getIdentifier()); assertEquals(request.getSchema(), parseData.getSchema()); } @@ -158,4 +157,13 @@ public void dataFieldParseTest() throws Exception { assertEquals(type, parseData.type()); assertEquals(descStr, parseData.description()); } + + @Test + public void updateTableRequestParseTest() throws Exception { + UpdateTableRequest request = MockRESTMessage.updateTableRequest("t1", "t2"); + String requestStr = mapper.writeValueAsString(request); + UpdateTableRequest parseData = mapper.readValue(requestStr, UpdateTableRequest.class); + assertEquals(request.getFromIdentifier(), parseData.getFromIdentifier()); + assertEquals(request.getToIdentifier(), parseData.getToIdentifier()); + } }