Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 11 additions & 8 deletions core/src/main/java/org/apache/iceberg/SchemaParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -183,13 +183,16 @@ private static Type typeFromJson(JsonNode json) {
return Types.fromPrimitiveString(json.asText());

} else if (json.isObject()) {
String type = json.get(TYPE).asText();
if (STRUCT.equals(type)) {
return structFromJson(json);
} else if (LIST.equals(type)) {
return listFromJson(json);
} else if (MAP.equals(type)) {
return mapFromJson(json);
JsonNode typeObj = json.get(TYPE);
if (typeObj != null) {
String type = typeObj.asText();
if (STRUCT.equals(type)) {
return structFromJson(json);
} else if (LIST.equals(type)) {
return listFromJson(json);
} else if (MAP.equals(type)) {
return mapFromJson(json);
}
}
}

Expand Down Expand Up @@ -254,7 +257,7 @@ private static Types.MapType mapFromJson(JsonNode json) {
}

public static Schema fromJson(JsonNode json) {
Type type = typeFromJson(json);
Type type = typeFromJson(json);
Preconditions.checkArgument(type.isNestedType() && type.asNestedType().isStructType(),
"Cannot create schema, not a struct type: %s", type);
Integer schemaId = JsonUtil.getIntOrNull(SCHEMA_ID, json);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,18 @@ public void testRoundTripSerdeWithV1TableMetadata() throws Exception {
assertRoundTripSerializesEquallyFrom(json, resp);
}

@Test
public void testMissingSchemaType() throws Exception {
// When the schema type (struct) is missing
String tableMetadataJson = readTableMetadataInputFile("TableMetadataV1MissingSchemaType.json");
AssertHelpers.assertThrows(
"Cannot parse type from json when there is no type",
IllegalArgumentException.class,
"Cannot parse type from json:",
() -> TableMetadataParser.fromJson(null, TEST_METADATA_LOCATION, tableMetadataJson)
);
}

@Test
public void testRoundTripSerdeWithV2TableMetadata() throws Exception {
String tableMetadataJson = readTableMetadataInputFile("TableMetadataV2Valid.json");
Expand Down
41 changes: 41 additions & 0 deletions core/src/test/resources/TableMetadataV1MissingSchemaType.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{
"format-version": 1,
"table-uuid": "d20125c8-7284-442c-9aea-15fee620737c",
"location": "s3://bucket/test/location",
"last-updated-ms": 1602638573874,
"last-column-id": 3,
"schema": {
"fields": [
{
"id": 1,
"name": "x",
"required": true,
"type": "long"
},
{
"id": 2,
"name": "y",
"required": true,
"type": "long",
"doc": "comment"
},
{
"id": 3,
"name": "z",
"required": true,
"type": "long"
}
]
},
"partition-spec": [
{
"name": "x",
"transform": "identity",
"source-id": 1,
"field-id": 1000
}
],
"properties": {},
"current-snapshot-id": -1,
"snapshots": []
}