Skip to content

Commit

Permalink
add constraint to fromJson, summary map must have operation key
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinjqliu committed Oct 19, 2024
1 parent 86be2da commit 27beb98
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
3 changes: 3 additions & 0 deletions core/src/main/java/org/apache/iceberg/SnapshotParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,9 @@ static Snapshot fromJson(JsonNode node) {
}
}
summary = builder.build();
Preconditions.checkArgument(
operation != null,
"Operation must be present in summary if summary exists");
}

Integer schemaId = JsonUtil.getIntOrNull(SCHEMA_ID, node);
Expand Down
27 changes: 27 additions & 0 deletions core/src/test/java/org/apache/iceberg/TestSnapshotJson.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@

import static org.apache.iceberg.Files.localInput;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

import java.io.File;
import java.io.IOException;
Expand Down Expand Up @@ -212,6 +214,31 @@ public void testJsonConversionWithV1Manifests() {
assertThat(snapshot.schemaId()).isEqualTo(expected.schemaId());
}

@Test
public void testJsonConversionSummaryWithoutOperationFails() {
// summary field without operation key should fail
String json =
String.format(
"{\n"
+ " \"snapshot-id\" : 2,\n"
+ " \"parent-snapshot-id\" : 1,\n"
+ " \"timestamp-ms\" : %s,\n"
+ " \"summary\" : {\n"
+ " \"files-added\" : \"4\",\n"
+ " \"files-deleted\" : \"100\"\n"
+ " },\n"
+ " \"manifests\" : [ \"/tmp/manifest1.avro\", \"/tmp/manifest2.avro\" ],\n"
+ " \"schema-id\" : 3\n"
+ "}",
System.currentTimeMillis());

IllegalArgumentException exception = assertThrows(
IllegalArgumentException.class,
() -> SnapshotParser.fromJson(json)
);
assertEquals("Operation must be present in summary if summary exists", exception.getMessage());
}

private String createManifestListWithManifestFiles(long snapshotId, Long parentSnapshotId)
throws IOException {
File manifestList = File.createTempFile("manifests", null, temp.toFile());
Expand Down

0 comments on commit 27beb98

Please sign in to comment.