Skip to content

Commit

Permalink
Core: Make view metadata properties optional in JSON parser
Browse files Browse the repository at this point in the history
  • Loading branch information
nastra committed Oct 19, 2023
1 parent 62662db commit de9469c
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import org.apache.iceberg.io.OutputFile;
import org.apache.iceberg.relocated.com.google.common.base.Preconditions;
import org.apache.iceberg.relocated.com.google.common.collect.ImmutableList;
import org.apache.iceberg.relocated.com.google.common.collect.ImmutableMap;
import org.apache.iceberg.relocated.com.google.common.collect.Lists;
import org.apache.iceberg.util.JsonUtil;

Expand Down Expand Up @@ -69,7 +70,9 @@ static void toJson(ViewMetadata metadata, JsonGenerator gen) throws IOException
gen.writeStringField(VIEW_UUID, metadata.uuid());
gen.writeNumberField(FORMAT_VERSION, metadata.formatVersion());
gen.writeStringField(LOCATION, metadata.location());
JsonUtil.writeStringMap(PROPERTIES, metadata.properties(), gen);
if (!metadata.properties().isEmpty()) {
JsonUtil.writeStringMap(PROPERTIES, metadata.properties(), gen);
}

gen.writeArrayFieldStart(SCHEMAS);
for (Schema schema : metadata.schemas()) {
Expand Down Expand Up @@ -114,7 +117,8 @@ public static ViewMetadata fromJson(String metadataLocation, JsonNode json) {
String uuid = JsonUtil.getString(VIEW_UUID, json);
int formatVersion = JsonUtil.getInt(FORMAT_VERSION, json);
String location = JsonUtil.getString(LOCATION, json);
Map<String, String> properties = JsonUtil.getStringMap(PROPERTIES, json);
Map<String, String> properties =
json.has(PROPERTIES) ? JsonUtil.getStringMap(PROPERTIES, json) : ImmutableMap.of();

JsonNode schemasNode = JsonUtil.get(SCHEMAS, json);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -375,4 +375,65 @@ private boolean isCompressed(String path) throws IOException {
}
}
}

@Test
public void roundTripSerdeWithoutProperties() {
String uuid = "386b9f01-002b-4d8c-b77f-42c3fd3b7c9b";
ViewMetadata viewMetadata =
ViewMetadata.builder()
.assignUUID(uuid)
.setLocation("location")
.addSchema(new Schema(Types.NestedField.required(1, "x", Types.LongType.get())))
.addVersion(
ImmutableViewVersion.builder()
.schemaId(0)
.versionId(1)
.timestampMillis(23L)
.putSummary("operation", "create")
.defaultNamespace(Namespace.of("ns1"))
.build())
.setCurrentVersionId(1)
.build();

String expectedJson =
"{\n"
+ " \"view-uuid\" : \"386b9f01-002b-4d8c-b77f-42c3fd3b7c9b\",\n"
+ " \"format-version\" : 1,\n"
+ " \"location\" : \"location\",\n"
+ " \"schemas\" : [ {\n"
+ " \"type\" : \"struct\",\n"
+ " \"schema-id\" : 0,\n"
+ " \"fields\" : [ {\n"
+ " \"id\" : 1,\n"
+ " \"name\" : \"x\",\n"
+ " \"required\" : true,\n"
+ " \"type\" : \"long\"\n"
+ " } ]\n"
+ " } ],\n"
+ " \"current-version-id\" : 1,\n"
+ " \"versions\" : [ {\n"
+ " \"version-id\" : 1,\n"
+ " \"timestamp-ms\" : 23,\n"
+ " \"schema-id\" : 0,\n"
+ " \"summary\" : {\n"
+ " \"operation\" : \"create\"\n"
+ " },\n"
+ " \"default-namespace\" : [ \"ns1\" ],\n"
+ " \"representations\" : [ ]\n"
+ " } ],\n"
+ " \"version-log\" : [ {\n"
+ " \"timestamp-ms\" : 23,\n"
+ " \"version-id\" : 1\n"
+ " } ]\n"
+ "}";

String json = ViewMetadataParser.toJson(viewMetadata, true);
assertThat(json).isEqualTo(expectedJson);

assertThat(ViewMetadataParser.fromJson(json))
.usingRecursiveComparison()
.ignoringFieldsOfTypes(Schema.class)
.ignoringFields("changes")
.isEqualTo(viewMetadata);
}
}

0 comments on commit de9469c

Please sign in to comment.